Vous êtes sur la page 1sur 39

AutoCAD.

NET
DevelopersGuide
StephenPretonAutodeskDeveloperTechnicalServicesTeam(DevTech)
ViewDesignar

Properties||ViewClassDiagram
lutin

AutoCAD.NETDevelopersGuide
Author:StephenPreton
AutodeskDeveloperTechnicalServicesTeam(DevTech)
Contents
1Introduction..................................................................................................................22Topicsin
thissection..............................................................................................................22Guide
Organization.......................................................................................................................22
OverviewoftheAutoCAD.NETAPI..............................................................................................22
ComponentsoftheAutoCAD.NETAPI.........................................................................................
23OverviewofMicrosoftVisualStudio
..............................................................................................25Topicsinthis
section..............................................................................................................25WhichEditionof
MicrosoftVisualStudiotoUse.........................................................................25UseCOM
Interoperabilitywith.NET..........................................................................................26
DependenciesandRestrictions.................................................................................................27
ForMoreInformation.....................................................................................................................27
SampleCode................................................................................................................................
27TransitionfromActiveXAutomationto.NET
.................................................................................28
2GettingStartedwithMicrosoftVisualStudio............................................................28Topicsin
thissection..............................................................................................................28Understand
MicrosoftVisualStudioProjects.................................................................................29Definethe
ComponentsinaProject..............................................................................................29Class
Modules....................................................................................................................30Forms
.................................................................................................................................30References
.........................................................................................................................30ViewProject
Information...............................................................................................................30Workwith
MicrosoftVisualStudioProjects....................................................................................31
Procedures.........................................................................................................................31
Topicsinthissection..............................................................................................................32
CreateaNewProject.................................................................................................................32
Procedures.........................................................................................................................32Open
anExistingProjectorSolution..........................................................................................35
Procedures.........................................................................................................................35Save
aProjectorSolution.........................................................................................................36
Procedures.........................................................................................................................36Work
withMultipleProjectsinaSolution...................................................................................37Adda
projecttoasolution..................................................................................................37Unloada
projectfromasolution.........................................................................................37Procedures

.........................................................................................................................37EditanExisting
ProjectorSolution................................................................................................37Topicsinthis
section..............................................................................................................37AddNewItems
..........................................................................................................................38Procedures
.........................................................................................................................38ImportExisting
Items.................................................................................................................39Procedures
.........................................................................................................................39EditItems
..................................................................................................................................40Procedures
.........................................................................................................................40Topicsinthis
section..............................................................................................................40UsetheCode
Window............................................................................................................40


UsetheWindowsFormDesigner...........................................................................................42
Procedures.........................................................................................................................42Use
thePropertiesWindow....................................................................................................43
Procedures.........................................................................................................................44
RenameaProject......................................................................................................................44
Procedures.........................................................................................................................45Add
andReferenceOtherProjects............................................................................................45
Procedures.........................................................................................................................46Set
theOptionsforMicrosoftVisualStudio................................................................................47
Procedures.........................................................................................................................47Load
anAssemblyintoAutoCAD..................................................................................................47
Procedures.........................................................................................................................48
AccessandSearchReferencedLibraries(ObjectBrowser)..........................................................
48Procedures.........................................................................................................................49
Exercises:CreateYourFirstProject..............................................................................................
50Topicsinthissection..............................................................................................................50
Exercise:CreateaNewProject.................................................................................................50
Exercise:ReferencetheAutoCAD.NETAPIFiles.....................................................................51
Exercise:CreateaNewCommand............................................................................................51
Exercise:SettheTargetFrameworkforaProject......................................................................54
Exercise:BuildandLoada.NETAssemblyinAutoCAD............................................................54
RelatedAutoCADCommandsandTerminology............................................................................
55Commands.........................................................................................................................55
Terminology........................................................................................................................55More
Information...........................................................................................................................56
3BasicsoftheAutoCAD.NETAPI................................................................................57Topicsin
thissection..............................................................................................................57Understand
theAutoCADObjectHierarchy...................................................................................57Topicsin
thissection..............................................................................................................58The
ApplicationObject...............................................................................................................58The
DocumentObject................................................................................................................60The
DatabaseObject.................................................................................................................61
SymbolTablesandDictionaries..........................................................................................61
VBA/ActiveXCrossReference........................................................................................62The
GraphicalandNongraphicalObjects..................................................................................62The
CollectionObjects...............................................................................................................63
NonNativeGraphicalandNongraphicalObjects.......................................................................64
AccesstheObjectHierarchy.........................................................................................................
65VB.NET..............................................................................................................................65
C#.......................................................................................................................................65
Topicsinthissection..............................................................................................................65

ReferenceObjectsintheObjectHierarchy................................................................................65
VB.NET..............................................................................................................................65
C#.......................................................................................................................................65
VB.NET..............................................................................................................................66
C#.......................................................................................................................................66
VB.NET..............................................................................................................................66
C#.......................................................................................................................................66
VB.NET..............................................................................................................................66
C#.......................................................................................................................................67
VBA/ActiveXCodeReference.........................................................................................
68CollectionObjects
.........................................................................................................................68
VBA/ActiveXCodeReference.........................................................................................68Access
theApplicationObject....................................................................................................68VB.NET
..............................................................................................................................68
C#.......................................................................................................................................68


VBA/ActiveXCodeReference.........................................................................................81Define
CommandsandAutoLISPFunctions.................................................................................81
Topicsinthissection..............................................................................................................81
CommandDefinition..................................................................................................................82
SyntaxtoDefineaCommand.............................................................................................82
VB.NET..............................................................................................................................82
C#.......................................................................................................................................83
VB.NET..............................................................................................................................83
C#.......................................................................................................................................83
AutoLISPFunctionDefinition.....................................................................................................83
SyntaxtoDefineanAutoLISPFunction..............................................................................83
VB.NET..............................................................................................................................83
C#.......................................................................................................................................84
RetrieveValuesPassedintoanAutoLISPFunction............................................................84To
defineanAutoLISPFunction.........................................................................................84VB.NET
..............................................................................................................................84
C#.......................................................................................................................................85
4ControltheAutoCADEnvironment............................................................................86Topicsin
thissection..............................................................................................................86Controlthe
ApplicationWindow.....................................................................................................86Position
andsizetheApplicationwindow............................................................................86VB.NET
..............................................................................................................................86
C#.......................................................................................................................................87
VBA/ActiveXCodeReference.........................................................................................77
UnderstandPropertiesandMethods.............................................................................................
78OutofProcessversus
InProcess.................................................................................................78VB.NET
..............................................................................................................................79
C#.......................................................................................................................................80
Topicsinthissection..............................................................................................................70
AccessaCollection...................................................................................................................70
VB.NET..............................................................................................................................70
C#.......................................................................................................................................70
VBA/ActiveXCodeReference.........................................................................................71Adda
NewMembertoaCollectionObject.................................................................................71
VB.NET..............................................................................................................................71
C#.......................................................................................................................................71
VBA/ActiveXCodeReference.........................................................................................72Iterate
throughaCollectionObject.............................................................................................72VB.NET
..............................................................................................................................73

C#.......................................................................................................................................73
VBA/ActiveXCodeReference.........................................................................................75Erasea
MemberofaCollectionObject......................................................................................76VB.NET
..............................................................................................................................76
C#.......................................................................................................................................76
VBA/ActiveXCodeReference.........................................................................................74Findthe
layertablerecordnamedMyLayerintheLayerTableobject.....................................74VB.NET
..............................................................................................................................74
C#.......................................................................................................................................75
VBA/ActiveX Code Reference......................................................................................... 73
Iterate through the LayerTable object ................................................................................. 73
VB.NET .............................................................................................................................. 73
C#.......................................................................................................................................73
VBA/ActiveX Code Reference......................................................................................... 87
Minimize and maximize the Application window.................................................................. 87
VB.NET..............................................................................................................................87


VBA/ActiveXCodeReference.........................................................................................90Control
theDrawingWindows.......................................................................................................90
Topicsinthissection..............................................................................................................90
PositionandSizetheDocumentWindow..................................................................................90
SizetheactiveDocumentwindow......................................................................................90
VB.NET..............................................................................................................................90
C#.......................................................................................................................................91
VBA/ActiveXCodeReference.........................................................................................93Zoom
andPantheCurrentView................................................................................................93Topics
inthissection..............................................................................................................93Manipulate
theCurrentView..................................................................................................93
VBA/ActiveXCodeReference.......................................................................................104Use
NamedViews...................................................................................................................104Adda
namedviewandsetitcurrent.................................................................................105VB.NET
............................................................................................................................105
C#.....................................................................................................................................105
VBACodeCrossReference...........................................................................................94
Functionusedtomanipulatethecurrentview.....................................................................94
VB.NET..............................................................................................................................94
C#.......................................................................................................................................96
DefinetoWindow...................................................................................................................99
Zoomtoanareadefinedbytwopoints...............................................................................99
VB.NET..............................................................................................................................99
C#.......................................................................................................................................99
VBA/ActiveXCodeReference.......................................................................................100Scalea
View........................................................................................................................100Zoominon
theactivedrawingusingaspecifiedscale......................................................100VB.NET
............................................................................................................................100
C#.....................................................................................................................................101
VBA/ActiveXCodeReference.......................................................................................101Center
Objects.....................................................................................................................101VB.NET
............................................................................................................................102
C#.....................................................................................................................................102
VBA/ActiveXCodeReference.......................................................................................102Display
DrawingExtentsandLimits.....................................................................................102Calculate
theextentsofthecurrentspace........................................................................102Calculatethe
limitsofthecurrentspace...........................................................................103Zoomintothe
extentsandlimitsofthecurrentspace......................................................103VB.NET
............................................................................................................................103

C#.....................................................................................................................................103
C#.......................................................................................................................................
VB.NET ..............................................................................................................................
C#.......................................................................................................................................88

88
88

VBA/ActiveX Code Reference......................................................................................... 89Make


the Application window invisible and visible .............................................................. 89 VB.NET
..............................................................................................................................
89
C#.......................................................................................................................................89
VBA/ActiveX Code Reference......................................................................................... 91
Minimize and maximize the active Document window......................................................... 91
VB.NET .............................................................................................................................. 91
C#.......................................................................................................................................92
VBA/ActiveX Code Reference......................................................................................... 92 Find
the current state ofthe activeDocument window........................................................92VB.NET
..............................................................................................................................
92
C#.......................................................................................................................................92
VBA/ActiveXCodeReference.......................................................................................106
Eraseanamedview.........................................................................................................106


VBA/ActiveXCodeReference.......................................................................................
117Create,Open,Save,andCloseDrawings
...................................................................................117
VBA/ActiveXCodeReference.......................................................................................122Work
withNoDocumentsOpen...............................................................................................122
Customizetheapplicationmenu.......................................................................................123
VB.NET............................................................................................................................123
C#.....................................................................................................................................124Lock
andUnlockaDocument......................................................................................................126
Lockadatabasebeforemodifyinganobject.....................................................................126
VB.NET............................................................................................................................126
C#.....................................................................................................................................127Set
AutoCADPreferences...........................................................................................................128
AccessthePreferencesobject.........................................................................................128
VB.NET............................................................................................................................128
C#.....................................................................................................................................128
VBA/ActiveXCodeReference.......................................................................................108Use
TiledViewports.................................................................................................................108
Topicsinthissection............................................................................................................108
IdentifyandManipulatetheActiveViewport.........................................................................109
Createanewtiledviewportconfigurationwithtwohorizontalwindows.............................109
VB.NET............................................................................................................................109
C#.....................................................................................................................................111
VBA/ActiveXCodeReference.......................................................................................116Update
theGeometryintheDocumentWindow......................................................................116VB.NET
............................................................................................................................117
C#.....................................................................................................................................117
VBA/ActiveXCodeReference.......................................................................................117Topics
inthissection............................................................................................................117Create
andOpenaDrawing....................................................................................................118Create
anewdrawing.......................................................................................................118VB.NET
............................................................................................................................118
C#.....................................................................................................................................118
VBA/ActiveXCodeReference.......................................................................................120Save
andCloseaDrawing......................................................................................................120Close
aDrawing...............................................................................................................120Savethe
activedrawing....................................................................................................120VB.NET
............................................................................................................................120
C#.....................................................................................................................................121
VBA/ActiveXCodeReference.......................................................................................113MakeA

TiledViewportCurrent.............................................................................................113Splita
viewport,theniteratethroughthewindows............................................................113VB.NET
............................................................................................................................113
C#.....................................................................................................................................114
VB.NET............................................................................................................................106
C#.....................................................................................................................................107
VBA/ActiveX CodeReference.......................................................................................118Open
an existing drawing ................................................................................................. 119 VB.NET
............................................................................................................................
119
C#.....................................................................................................................................119
VBA/ActiveX Code Reference....................................................................................... 121
Determine if a drawing has unsaved changes................................................................... 121
VB.NET ............................................................................................................................ 121
C#.....................................................................................................................................122
VBA/ActiveX Code Reference....................................................................................... 129 Set
the crosshairs to full screen........................................................................................ 129 VB.NET
............................................................................................................................
129
C#.....................................................................................................................................129


VBA/ActiveXCodeReference.......................................................................................142Prompt
forUserInput..................................................................................................................143Topics
inthissection............................................................................................................144GetString
Method.....................................................................................................................144Getastring
valuefromtheuserattheAutoCADcommandline.......................................144VB.NET
............................................................................................................................144
C#.....................................................................................................................................144
VBA/ActiveXCodeReference.......................................................................................130Topics
inthissection............................................................................................................131Database
Preferences..........................................................................................................131Setand
ReturnSystemVariables............................................................................................131VB.NET
............................................................................................................................131
C#.....................................................................................................................................131
VBA/ActiveXCodeReference.......................................................................................131Draw
withPrecision.................................................................................................................132Topics
inthissection............................................................................................................132Adjust
SnapandGridAlignment..............................................................................................132Change
thegridandsnapsettings...................................................................................132VB.NET
............................................................................................................................132
C#.....................................................................................................................................133
VBA/ActiveXCodeReference.......................................................................................134Use
OrthoMode......................................................................................................................135
VB.NET............................................................................................................................135
C#.....................................................................................................................................135
VBA/ActiveXCodeReference.......................................................................................135
CalculatePointsandValues....................................................................................................135
GetanglefromXaxis.......................................................................................................136
VB.NET............................................................................................................................136
C#.....................................................................................................................................136
VBA/ActiveXCodeReference.......................................................................................139
CalculateAreas.......................................................................................................................139
Topicsinthissection............................................................................................................139
CalculateaDefinedArea.....................................................................................................140
Calculatetheareadefinedbypointsenteredfromtheuser..............................................140
VB.NET............................................................................................................................140
C#.....................................................................................................................................141
VBA/ActiveXCodeReference.......................................................................................145
GetPointMethod......................................................................................................................145
Getapointselectedbytheuser.......................................................................................145
VB.NET............................................................................................................................145

C#.....................................................................................................................................146
VBA/ActiveXCodeReference.......................................................................................147
GetKeywordsMethod..............................................................................................................148
GetakeywordfromtheuserattheAutoCADcommandline............................................148
VB.NET............................................................................................................................148
VBA/ActiveX Code Reference....................................................................................... 129
Display the screen menu and scroll bars .......................................................................... 130
VB.NET ............................................................................................................................ 130
C#.....................................................................................................................................130
VBA/ActiveX Code Reference....................................................................................... 136
Calculate Polar Point ........................................................................................................ 137
VB.NET ............................................................................................................................ 137
C#.....................................................................................................................................137
VBA/ActiveX Code Reference....................................................................................... 138 Find
the distance between two points with the GetDistance method.................................138VB.NET
............................................................................................................................
138
C#.....................................................................................................................................139


VBA/ActiveXCodeReference.......................................................................................153
5CreateandEditAutoCADEntities............................................................................154Topicsin
thissection............................................................................................................154Openand
CloseObjects.............................................................................................................154Topicsin
thissection............................................................................................................154Workwith
ObjectIds.................................................................................................................154Obtainan
ObjectId...........................................................................................................155OpenanObject
................................................................................................................155VB.NET
............................................................................................................................155
C#.....................................................................................................................................156Use
TransactionswiththeTransactionManager.....................................................................156
Topicsinthissection............................................................................................................156
StartaNewTransactionandOpenanObject......................................................................157
Queryobjects...................................................................................................................157
VB.NET............................................................................................................................157
C#.....................................................................................................................................158Adda
newobjecttothedatabase.....................................................................................158VB.NET
............................................................................................................................159
C#.....................................................................................................................................159
CommitandRollbackChanges............................................................................................160
VB.NET............................................................................................................................160
C#.....................................................................................................................................160Nest
Transactions................................................................................................................161Use
nestedtransactionstocreateandmodifyobjects......................................................161VB.NET
............................................................................................................................161
C#.....................................................................................................................................163Open
andCloseObjectswithouttheTransactionManager.....................................................165Query
objects...................................................................................................................166VB.NET
............................................................................................................................166
C#.....................................................................................................................................166Adda
newobjecttothedatabase.....................................................................................167VB.NET
............................................................................................................................167
C#.....................................................................................................................................168
UpgradeandDowngradeOpenObjects..................................................................................168
OpenNotifications............................................................................................................169
VB.NET............................................................................................................................169
C#.....................................................................................................................................170
CreateObjects............................................................................................................................
171Topicsinthissection............................................................................................................
171DeterminetheParentObject...................................................................................................
171AccessModelspace,Paperspaceorthecurrentspace...................................................172

VBA/ActiveXCodeReference.......................................................................................151Access
theAutoCADCommandLine..........................................................................................152Send
acommandtotheAutoCADcommandline.............................................................153VB.NET
............................................................................................................................153
C#.....................................................................................................................................153
VBA/ActiveXCodeReference.......................................................................................150Control
UserInput....................................................................................................................150Getan
integervalueorakeyword....................................................................................150VB.NET
............................................................................................................................150
C#.....................................................................................................................................151
C#.....................................................................................................................................148
VBA/ActiveX Code Reference....................................................................................... 149
VB.NET ............................................................................................................................ 149
C#.....................................................................................................................................149


VBA/ActiveXCodeReference.......................................................................................174Create
Lines............................................................................................................................175Topics
inthissection............................................................................................................175Createa
LineObject............................................................................................................175VB.NET
............................................................................................................................175
C#.....................................................................................................................................176
VBA/ActiveXCodeReference.......................................................................................185Create
PointObjects................................................................................................................185Createa
Pointobjectandchangeitsappearance.............................................................186VB.NET
............................................................................................................................186
C#.....................................................................................................................................187
VBA/ActiveXCodeReference.......................................................................................187Create
SolidFilledAreas.........................................................................................................188VB.NET
............................................................................................................................188
C#.....................................................................................................................................189
VBA/ActiveXCodeReference.......................................................................................190Work
withRegions...................................................................................................................191Topics
inthissection............................................................................................................191Create
Regions....................................................................................................................191Createa
simpleregion......................................................................................................191VB.NET
............................................................................................................................191
C#.....................................................................................................................................192
VBA/ActiveXCodeReference.......................................................................................197Create
Hatches........................................................................................................................197Topics
inthissection............................................................................................................198Createa
HatchObject..........................................................................................................198Associatea
Hatch................................................................................................................198Assignthe
HatchPatternTypeandName............................................................................198Definethe
HatchBoundaries................................................................................................199Createa
Hatchobject.......................................................................................................200
VBA/ActiveXCodeReference.......................................................................................177Create
aPolylineobject.......................................................................................................177VB.NET
............................................................................................................................177
C#.....................................................................................................................................178
VBA/ActiveXCodeReference.......................................................................................179Topics
inthissection............................................................................................................179Createa
Circleobject...........................................................................................................180VB.NET
............................................................................................................................180
C#.....................................................................................................................................180

VBA/ActiveXCodeReference.......................................................................................181Create
anArcobject............................................................................................................181VB.NET
............................................................................................................................181
C#.....................................................................................................................................182
VBA/ActiveXCodeReference.......................................................................................183Create
aSplineobject..........................................................................................................183VB.NET
............................................................................................................................183
C#.....................................................................................................................................184
VBA/ActiveXCodeReference.......................................................................................193Create
CompositeRegions..................................................................................................194Subtract
regions...............................................................................................................194Uniteregions
....................................................................................................................194Findthe
intersectionoftworegions..................................................................................194Createa
compositeregion................................................................................................194VB.NET
............................................................................................................................194
C#.....................................................................................................................................195
VB.NET............................................................................................................................172
C#.....................................................................................................................................173


VBA/ActiveXCodeReference.......................................................................................202Work
withSelectionSets.............................................................................................................203
Topicsinthissection............................................................................................................203
ObtainthePickFirstSelectionSet............................................................................................203
GetthePickfirstselectionset............................................................................................203
VB.NET............................................................................................................................203
C#.....................................................................................................................................204
VBA/ActiveXCodeReference.......................................................................................225
RemoveObjectsFromaSelectionSet....................................................................................225
EditNamedand2DObjects........................................................................................................
227Topicsinthissection............................................................................................................
227
VBA/ActiveXCodeReference.......................................................................................205Select
ObjectsintheDrawingArea.........................................................................................205Prompt
forobjectsonscreenanditeratetheselectionset................................................206VB.NET
............................................................................................................................207
C#.....................................................................................................................................207
VBA/ActiveXCodeReference.......................................................................................210AddTo
orMergeMultipleSelectionSets.................................................................................210Add
selectedobjectstoaselectionset.............................................................................210VB.NET
............................................................................................................................210
C#.....................................................................................................................................211
VBA/ActiveXCodeReference.......................................................................................212Define
RulesforSelectionFilters.............................................................................................213Topics
inthissection............................................................................................................213Use
SelectionFilterstoDefineSelectionSetRules.............................................................213
Specifyasingleselectioncriterionforaselectionset.......................................................214
VB.NET............................................................................................................................214
C#.....................................................................................................................................214
VBA/ActiveXCodeReference.......................................................................................215Specify
MultipleCriteriainaSelectionFilter.........................................................................215Select
objectsthatmeettwocriterion...............................................................................216VB.NET
............................................................................................................................216
C#.....................................................................................................................................216
VBA/ActiveXCodeReference.......................................................................................217Add
ComplexitytoYourFilterListConditions.......................................................................217Select
acirclewhoseradiusisgreaterthanorequalto5.0..............................................218VB.NET
............................................................................................................................218
C#.....................................................................................................................................219

VBA/ActiveXCodeReference.......................................................................................221Use
WildCardPatternsinSelectionSetFilterCriteria.........................................................221Select
MTextwhereaspecificwordappearsinthetext....................................................222VB.NET
............................................................................................................................222
C#.....................................................................................................................................223
VBA/ActiveXCodeReference.......................................................................................223Filterfor
ExtendedData.......................................................................................................224Select
circlesthatcontainxdata........................................................................................224VB.NET
............................................................................................................................224
C#.....................................................................................................................................224
VB.NET............................................................................................................................200
C#.....................................................................................................................................201
VBA/ActiveX Code Reference....................................................................................... 208
VB.NET ............................................................................................................................ 209
C#.....................................................................................................................................209
VBA/ActiveX Code Reference....................................................................................... 219
Select either Text or MText............................................................................................... 220
VB.NET ............................................................................................................................ 220
C#.....................................................................................................................................220

Vous aimerez peut-être aussi