Developer Technical Services
Developer Technical Services
NET
API
Developer Technical Services
Americas Team
US (CA, AZ, WA), Canada, Brazil
European Team
Switzerland, United Kingdom, France, Czech Republic, Russia
APac Team
China, Japan, India
Getting Support
http://www.autodesk.com/adn-devhelp
Provides access to
On-line knowledge base
Call submission
Newsgroups
Answers to frequently asked questions are posted in our online knowledge base
Course Objective
It is to understand:
the fundamentals of the AutoCAD .NET API
how to teach yourself the AutoCAD .NET API
where to get help afterwards
What it is not:
Teach you .NET framework or C# , VB programming language
Give you complete coverage of all API functions
Class Agenda
Lectures with Labs
Slides give an abstract overview
Labs and discussion give a practical perspective
Lectures
Overview of .NET.
AutoCAD .NET Visual Studio project settings Hello World!
User Interaction - User Input and Entity Selection
Database Fundamentals Symbol tables, Transactions
User Interface design Win Form Dialogs and Palettes
Event handling Reacting to AutoCAD Events in .NET. Database
Dictionaries, XRecords, Table Traversal
Point Monitor
Jigs
Additional User Interface Elements (Non CUI related)
Class Schedule
Time 9:30 - 5:30
Lunch 12:00 - 1:00
Day 1
Overview of .NET
Visual Studio Project Settings Lab 1
User Input Lab 2
Creating Entity, Block definition and Block References Lab 3
UI Design Win form Dialogs, Palettes and Event Handling Lab 4
Day 2
Dictionaries Lab 5
.NET Overview
What is .NET?
Important Concepts
.NET Overview
What is .NET?
Components of .NET
The .NET Framework used for building and running all kinds of
software, including Web-based applications, smart client applications,
and XML Web Services
.NET Framework
VB
C++
C#
JScript
ASP.NET
Windows Forms
.NET Overview
What is .NET?
NET Framework
Common Language Runtime (CLR)
Object-Oriented programming environment
Common execution environment for .NET applications
-Similar to Java VM but with much stronger interoperability
Framework Class Library (FCL)
Object Oriented Collection of re-usable types
(Source: MSDN)
.NET Overview
CLR Execution Model
Source code
Compiler
Managed
Code
(dll or exe)
VB
C#
C++
vbc.exe
csc.exe
cc.exe
Assembly
IL Code
Assembly
IL Code
Assembly
IL Code
Programming style
Multiple supported languages Choose your weapons
.NET Overview
What is .NET?
Important Concepts
.NET Overview
Benefits of programming in .NET
Consistent Object Oriented Development platform
Automatic memory management Garbage collection
Support for multiple languages
(Source: MSDN)
.NET Overview
Consistent Object Oriented Development platform
Everything you see can be treated as an Object!
Dim myLine As New Line()
myLine.StartPoint = New Point3d(0, 0, 0)
myLine.EndPoint = New Point3d(10, 10, 0)
myLine.GetClosestPointTo(New Point3d(5, 5.1, 0), False)
Dim x as Interger = 7
Dim s as String = x.ToString()
.NET Overview
Consistent Object Oriented Development
platform
Mature API Constructs
Whats wrong with this function?
int acedSSGet(const char * str,
const void * pt1,
const void * pt2,
const struct resbuf * filter,
ads_name ss);
.NET Overview
Consistent Object Oriented Development platform
Mature API Constructs
Some 6 new classes defined to encapsulate acedSSGet()
Dim values(2) As TypedValue
Define the selection criteria
values(0) = New TypedValue(DxfCode.Start, Circle)
values(1) = New TypedValue(DxfCode.Color, 1)
Dim selFilter As New SelectionFilter(values)
Dim selOpts As New PromptSelectionOptions()
selOpts.AllowDuplicates = True
Run the selection
Dim res As PromptSelectionResult = Editor.GetSelection(selOpts, selFilter)
.NET Overview
Benefits of programming in .NET
Consistent Object Oriented Development platform
Automatic memory management (Garbage
collection)
and consistent exception handling
Support for multiple languages
(Source: MSDN)
.NET Overview
Benefits of programming in .NET
Automatic memory management
char *pName=(char*)malloc(128);
strcpy(pName,"Hello");
//...
free(pName);
.NET Overview
Benefits of programming in .NET
Consistent exception handling
Old Way VB: Can be very confusing and problematic!
On Error GoTo UnexpectedError
Dim x As Double=10/0 error!
UnexpectedError:
MsgBox Str$(Err.Number)
New VB .NET
Try
Dim x As Double=10/0 error which throws exception
Catch
what happened? Division by Zero!
Finally
cleanup - do this either way
End Try
.NET Overview
Benefits of programming in .NET
Consistent Object Oriented Development platform
Automatic memory management (Garbage collection)
and consistent exception handling
Support for multiple languages
(Source: MSDN)
.NET Overview
Benefits of programming in .NET
Support for multiple languages
C#, VB most commonly used
Can interop between code written in different languages. For
example, a class written in C# can be inherited from a class
written in VB! In fact, AutoCADs managed assemblies are
written using managed C++ which you will access from VB.NET.
No significant difference in performance as all languages
compile to IL (Intermediate Language) executed by the CLR
.NET Overview
What is .NET?
Important Concepts
.NET Overview
Important Concepts
Assemblies
Fundamental unit of deployment and
execution in .NET
Contains a manifest that describes the
assembly
Boundary for code execution and access
permission
Assembly
Manifest
Name?
What files make up the assembly?
Dependent assemblies?
Version and Culture?
netmodule
(Source: MSDN)
Dlls
Bmp
Class Agenda
Lectures and Labs
Overview of .NET.
AutoCAD .NET Visual Studio project settings Hello World!
User Interaction - Simple User Input and Entity Selection
Database Fundamentals Symbol tables, Transactions
Database Fundamentals Dictionaries, XRecords, Table
Traversal
More User Interaction Advanced Prompts
User Interface design - WinForm Dialogs and Palettes
Event handling Reacting to AutoCAD Events in .NET.
ADN website
DevNotes
DevHelp Online
Development Environment
Microsoft Visual Studio 2010 (SP1) or
Microsoft Visual Studio 2012
AutoCAD 2014
Microsoft Windows 8
Microsoft Windows 7
Microsoft Windows XP
ObjectARX SDK
ADN
Code witten in
Visual Basic .NET
Reference to AutoCAD
DLLs. Use it from
ObjectARX INC folder
Project VB.NET
Compile
Assembly
(.dll)
On proxy detection
[HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\AutoCAD\R19.1\ACAD-D001:409\Applications\AcLayer]
"DESCRIPTION"="AutoCAD Layer Manager"
"LOADER"="C:\\Program Files\\AutoCAD 2014\\aclayer.dll"
"LOADCTRLS"=dword:0000000e
"MANAGED"=dword:00000001
[HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\AutoCAD\R19.1\ACADD001:409\Applications\AcLayer\Commands]
"LAYER"="LAYER
[HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\AutoCAD\R19.1\ACADD001:409\Applications\AcLayer\Groups]
"ACLAYER_CMDS"="ACLAYER_CMDS
AutoLoader
AutoCAD loads bundles from
%appdata%\Autodesk\ApplicationPlugins
Each bundle has PackageContents.xml
HKEY_LOCAL_MACHINE
SOFTWARE
R18.0: 2010
.1: 2011
.2:2012
Autodesk
R19.0: 2013
.1: 2014
AutoCAD
R19.0
ACADB001:409
409: English
416: Portuguese
X000: Civil3D
X001: AutoCAD 040A: Spanish
Applications
Class Agenda
Lectures and Labs
Overview of .NET.
AutoCAD .NET Visual Studio project settings Hello World!
User Interaction - User Input and Entity Selection
Database Fundamentals Symbol tables, Transactions
User Interface design Win Form Dialogs and Palettes
Event handling Reacting to AutoCAD Events in .NET. Database
Dictionaries, XRecords, Table Traversal
Point Monitor
Jigs
Additional User Interface Elements
Ask the user to select the point and store the selection
result
Dim pointResult As PromptPointResult =
ed.GetPoint(pointOptions)
Create
a Point3d variable to store the selected point
Requires an additional imports for Point3d: Autodesk.AutoCAD.Geometry
ed.WriteMessage(selectedPoint.ToString())
Set Keywords
Enter Number of Sides [Triangle/Square/Pentagon] :
Set Defaults
Enter Number of Sides [Triangle/Square/Pentagon] <3>:
Additional prompts
Types:
PromptPointOptions
PromptStringOptions
PromptDoubleOptions
PromptAngleOptions
PromptCornerOptions
PromptDistanceOptions
PromptEntityOptions
PromptIntegerOptions
PromptKeywordOptions
PromptNestedEntityOptions
PromptSelectionOptions
Etc.
Help file
for the
rescue!
Class Agenda
Lectures and Labs
Overview of .NET.
AutoCAD .NET Visual Studio project settings Hello World!
User Interaction - User Input and Entity Selection
Database Fundamentals Symbol tables, Transactions
User Interface design Win Form Dialogs and Palettes
Event handling Reacting to AutoCAD Events in .NET. Database
Dictionaries, XRecords, Table Traversal
Point Monitor
Jigs
Additional User Interface Elements
DWG
BlockTable
BlockTableRecord
LayerTable
LayerTableRecord
Other tabels
DBDictionary
TextStyleTable
Materials
DimStyleTable
Visual Styles
UcsTable
Others
(Custom)
Model Space
LineTypeTable
Paper Space 0
Other layers
ViewTable
Paper Space 1
ViewportTable
Other blocks
RegAppTable
Database Components
Symbol Tables
Examples Layer Table, Linetype Table, Textstyle Table etc.
Containers to store Symbol Table Records
Example LayerTableRecord, LinetypeTableRecord etc
All Symbol Tables have common methods of a container
such as
Add to add a record
Item to lookup an entry with a search string
Has To know if an entry exists
Is enumerable
Each symbol table can hold only records of a specific type
For example, a LayerTable can hold only
LayerTableRecords
Use SnoopDb to see where the Layers reside
Transactions
Transactions
Sets the boundary for database operations
Handles exception cleanly
Operates with a single Undo filer
Can be
committed All database operations are saved
rolled back All database operations are aborted
Can be nested
Nesting Transactions
1
Transaction 2
Transaction 1
obj1
3
obj2
obj3
obj2
obj1
Database
obj3
obj2
Transactions
Standard db access with Transactions
Public Function MyFunc()
Get the database current in AutoCAD
Dim db As Database = HostApplicationServices.WorkingDatabase()
Start a transaction using the database transaction manager
Dim trans As Transaction = db.TransactionManager.StartTransaction()
Try
Do all database operations here
Lets get the block table from the database
Drill into the database and obtain a reference to the BlockTable
Dim bt As BlockTable = trans.GetObject(db.BlockTableId,
OpenMode.ForWrite)
Everything successful, so commit the transaction
trans.commit()
Catch
trans.Abort()
Finally
All ok. Call Dispose explicitly before exiting
trans.dispose()
End Try
End Function
Use Add method for adding Symbol Table Records to add to Symbol
Table
Use AppendXXX to add add other kinds of objects to its owners For
example
AppendEntity to add to BlockTableRecord
Database
BlockTable
BlockTableRecord
Model Space
Entity
Line
Polyline
MText
Circle
Many others...
Lab 3
Create Entity, Block and Block Reference
Class Agenda
Lectures and Labs
Overview of .NET.
AutoCAD .NET Visual Studio project settings Hello World!
User Interaction - User Input and Entity Selection
Database Fundamentals Symbol tables, Transactions
User Interface design Win Form Dialogs and Palettes
Event handling Reacting to AutoCAD Events in .NET. Database
Dictionaries, XRecords, Table Traversal
Point Monitor
Jigs
Additional User Interface Elements
Properties
Select Events
Select a button,
mouse right-click,
Properties
Or just double-click
on the button, same
result
TIP
Windows Defined
Windows Forms (Winform)
Host of other controls defined in CLR
Palette in AutoCAD
Create a user control
Create a PaletteSet
Add the user control to the palette using Add method.
Set the Visible property of the paletteset
Handling Events
Event
message sent by an object to notify something has happened
message is received in a function call by one or more listeners
event sender only requires function pointer to dispatch a message
any interested party can implement the function and receive the
event
function must have a specific signature that the sender requires
Use .NET delegates to wire sender and receiver
Delegates
Using Delegates
Delegates in AutoCADs .NET API usually have EventHandler suffix
Lookup the signature of the delegate in the object browser
Lab 4
PaletteSet and DB Events
Class Agenda
Lectures and Labs
Overview of .NET.
AutoCAD .NET Visual Studio project settings Hello World!
User Interaction - User Input and Entity Selection
Database Fundamentals Symbol tables, Transactions
User Interface design Win Form Dialogs and Palettes
Event handling Reacting to AutoCAD Events in .NET. Database
Dictionaries, XRecords, Table Traversal
Point Monitor
Jigs
Additional User Interface Elements
Get NOD
To get the NOD for the database
Dim db = HostApplicationServices.WorkingDatabase
Dim NOD As DBDictionary =
trans.GetObject(db.NamedObjectsDictionaryId,
OpenMode.ForWrite, False)
Dictionary Hierarchy
Named Object Dictionary
Extension Dictionary
DBDictionary
SetAt
GetAt
Xrecord
Data
ResultBuffer
New
AsArray
Array of
TypeValues
Lab 5
Adding Custom Data
Class Agenda
Lectures and Labs
Overview of .NET.
AutoCAD .NET Visual Studio project settings Hello World!
User Interaction - User Input and Entity Selection
Database Fundamentals Symbol tables, Transactions
User Interface design Win Form Dialogs and Palettes
Event handling Reacting to AutoCAD Events in .NET. Database
Dictionaries, XRecords, Table Traversal
Point Monitor
Jigs
Additional User Interface Elements
DrawContext
Geometry.Draw
Lab 6
PointMonitor
Class Agenda
Lectures and Labs
Overview of .NET.
AutoCAD .NET Visual Studio project settings Hello World!
User Interaction - User Input and Entity Selection
Database Fundamentals Symbol tables, Transactions
User Interface design Win Form Dialogs and Palettes
Event handling Reacting to AutoCAD Events in .NET. Database
Dictionaries, XRecords, Table Traversal
Point Monitor
Jigs
Additional User Interface Elements
Jigs
Allows you to graphically manipulate and form an
Entity in real time.
Two types of Jig available
EntityJig Controls only one entity
DrawJig Controls one or more.
Need to use a Class that inherits from EntityJig or DrawJig
Jigs
The constructor for this class takes
the entity being jigged.
Use the Editor Drag function to start
the Jig
Pass in the Jig
Jig Functions
Two functions that must be overridden
Sampler
Used to get input from the user
Update
Used to update the entity that is being jigged.
Returns SamplerStatus
NoChange
OK
Lab 7
Jigs
Class Agenda
Lectures and Labs
Overview of .NET.
AutoCAD .NET Visual Studio project settings Hello World!
User Interaction - User Input and Entity Selection
Database Fundamentals Symbol tables, Transactions
User Interface design Win Form Dialogs and Palettes
Event handling Reacting to AutoCAD Events in .NET. Database
Dictionaries, XRecords, Table Traversal
Point Monitor
Jigs
Additional User Interface Elements
Context menu
Application Level
Application.AddDefaultContextMenuExtension
Object Level
Application. AddObjectContextMenuExtension per RXClass
Lab 8
Additional User Interface Elements
(Non CUI based)
Thank You !