ObjectARX
ObjectARX
and
AutoCAD .NET
Autodesk Developer Technical Services
Getting Acquainted
© 2008 Autodesk
Autodesk Developer Network (ADN)
www.autodesk.com/joinadn
© 2008 Autodesk
Developer Technical Services
Worldwide Workgroup
Over 25 Specialists World Wide
Virtually 24 hour support, 5 days a week
Americas Team
CA, WA, São Paulo
European Team
Switzerland, United Kingdom, France, Czech Republic, Russia
APac Team
China, Japan, India
© 2008 Autodesk
Getting Support
http://www.autodesk.com/adn-devhelp
Provides access to
On-line knowledgebase
Request submission
Newsgroups
Requests are logged automatically
1-3 day turnaround
Callbacks as needed
Answers to frequently asked questions are posted in our on-
line knowledge base
© 2008 Autodesk
Course Objective
It is to understand:
The fundamentals of ObjectARX and AutoCAD .NET
How to teach yourself AutoCAD APIs
Where to get help with afterwards
What it is not:
Teach you C++, C#, VB, .NET
Give you complete of coverage of all API functions
© 2008 Autodesk
Class Agenda
Lectures:
Overview of APIs
AutoCAD: Hello.arx – Step 1 + 2
ObjectDBX: Structure – Step 3 + 4
ObjectDBX: Extend it! – Step 5 + 6
AutoCAD: Multi-Document Environment
Notification System – Step 7
AutoCAD .NET
© 2008 Autodesk
Class Schedule
Day 3, Day 4
AutoCAD .NET
© 2008 Autodesk
Training Material
© 2008 Autodesk
Agenda
Overview of APIs
AutoCAD: Hello.arx
ObjectDBX: Structure
ObjectDBX: Extend it!
AutoCAD: Multi-Document Environment
Notification System
© 2008 Autodesk
API Overview
© 2008 Autodesk
What is an API?
© 2008 Autodesk
API Packaging
© 2008 Autodesk
Client/Server Interaction
In process
Client/server is in the same process
Local
Client/server is on the same machine
Remote
Client/server is on the same network
© 2008 Autodesk
The Plug-In Architecture
.DLL
.ARX .DLL
.ARX
API APIs
ObjectARX
.EXE
AutoCAD
© 2008 Autodesk
Forms of Client Code
© 2008 Autodesk
AutoCAD APIs and IDEs
© 2008 Autodesk
API Implementation I
driver
Input
Input acquisition
AutoCAD
PlotHDI
2D graphics
Plotter
driver
cache
(WHIP!)
Display
HEIDI
driver
3D graphics
ObjectDBX
cache
© 2008 Autodesk
API Implementation II
axdb.dll
AutoCAD
© 2008 Autodesk
Old API Performance Comparison
S.0.58 API Performance
100.00
90.00
80.00
70.00
60.00
Seconds
50.00
40.00
Total
30.00
Create 1000 2dPolylines
20.00
Add XData to 1000 Entities
10.00 Update 1000 Circles
0.00 Create 1000 lines
COM (VB)
COM (VBA)
ObjectARX
Cre
Cre
Upd
400
350
Time in seconds
300
250
ObjectARX
200
.NET
150
LISP
100
50
0
Xrecord Custom Object Xdata
© 2008 Autodesk
How to Store Your Data in AutoCAD
Different Programming Techniques
140000
120000
Time in seconds
100000 ObjectARX
80000 .NET
60000 LISP
40000 VBA
VB
20000
0
Xrecord Xdata
© 2008 Autodesk
How to Store Your Data in AutoCAD
Different Programming Techniques
4000
3000 ObjectARX
.NET
2000
VBA
1000 LISP
0
Xrecord Custom
Object
© 2008 Autodesk
API Comparison Summary
Speed
ObjectARX, .NET is close second, VBA also though obsolete
Coverage
ObjectARX, .NET everything except custom objects
Ease of use
.NET, VBA
Learning curve
.NET, VBA
© 2008 Autodesk
Agenda
Overview of APIs
AutoCAD: Hello.arx
ObjectDBX: Structure
ObjectDBX: Extend it!
AutoCAD: Multi-Document Environment
Notification System
© 2008 Autodesk
AutoCAD: Hello.arx
ObjectARX
What it is
Where to get it
ObjectARX applications
Structure
Loading
Memory management
Creating commands
Command mechanism
Prompting for user input
ObjectARX Wizards
© 2008 Autodesk
What is ObjectARX?
A framework?
More than just a toolkit
Specialized objects can be plugged back into the system
Custom entities
Reactors
...
© 2008 Autodesk
What can I do with ObjectARX?
© 2008 Autodesk
Getting the ObjectARX SDK
Download - Free
On Autodesk public web site
Developer Center - ObjectARX
(http://usa.autodesk.com/adsk/servlet/index?id=773204&siteID=123112)
© 2008 Autodesk
Binary compatibility
ObjectARX 2010
AutoCAD 2010
Binary incompatible with earlier releases
ObjectARX 2009
AutoCAD 2009
ObjectARX 2008
AutoCAD 2008 and 2009
ObjectARX 2007
AutoCAD 2007, 2008 and 2009
Binary incompatible with earlier releases
© 2008 Autodesk
Development Environment
© 2008 Autodesk
ObjectARX SDK Contents
© 2008 Autodesk
Main Libraries - I
© 2008 Autodesk
Main Libraries - II
© 2008 Autodesk
Utilities
ObjectARX Wizard
(<your path>\ObjectARX 2010\utils\ObjARXWiz\ArxWizards.msi)
© 2008 Autodesk
On-line Documentation
Found in ObjectARX\docs
ObjectARX Documentation (arxdoc.chm)
ObjectARX Developers Guide (arxdev.chm)
Reference Manual (arxref.chm)
Managed Class Reference Guide (arxmgd.chm)
ObjectARX 2010 Migration Guide (arxxmg.chm)
64-bit Migration Guide (arxmgr.chm)
Interoperability Guide (arxiop.chm)
ObjectARX Readme (readarx.chm)
© 2008 Autodesk
Samples
© 2008 Autodesk
.ARX = .DLL + 2 exported functions
acrxEntryPoint
acrxGetApiVersion
.DLL
.ARX .ARX
.DLL
API APIs
ObjectARX
.EXE
AutoCAD
© 2008 Autodesk
Loading ObjectARX Applications I
Demand loaded
Startup
acad.rx file
Registry
(arxload) in acad.lsp
On command invocation
Registry
On request
From another application
On proxy detection
Registry
© 2008 Autodesk
Loading ObjectARX Applications II
3.
Rxapi.lib
AutoCAD 5. 4.
.ARX
1. 6.
2.
Operating System
1. LoadLibrary
2. _DllMainCRTStartup (see MSDN for info)
3. CRT_INIT (constructs global class vars)
4. DllMain (see MSDN for info)
5. acrxGetApiVersion
6. acrxEntryPoint
© 2008 Autodesk
Wrong ARX Memory Management
AutoCAD/ObjectDBX 3.
MSVCRT.DLL
2. Piece of
CRASH! Memory
.ARX 1. MSVCRTD.DLL
© 2008 Autodesk
Correct ARX Memory Management I
3.
AutoCAD/ObjectDBX MSVCRT.DLL
Piece of
Memory
2.
1.
.ARX
© 2008 Autodesk
Correct ARX Memory Management II
2.
AutoCAD/ObjectDBX 4. MSVCRT.DLL
Piece of
1. 3. Memory
Rxheap.lib
Libcmt.lib
.ARX
© 2008 Autodesk
Getting Started
© 2008 Autodesk
Command Mechanism I
.ARX
1.
AcEdCommandStack
4.
5. Callback
2, 3. 6. function
AcEdCommand
AutoCAD
1. Add command
2. Create command object
3. Parameterize it
4. User types command name
5. Lookup command object
6. Call callback function
© 2008 Autodesk
Command Mechanism II
© 2008 Autodesk
Lab - Step 1
© 2008 Autodesk
Prompting for User Input
AcEd functions
String, number acquisition acedGetString(), acedGetInt()
Point, angle acquisition acedGetPoint(), acedGetAngle()
Entity selection acedEntSel()
© 2008 Autodesk
ObjectARX Wizards
Application Wizard
ObjectARX\utils\ObjARXWiz\ArxWizards.msi
COM Wrapper Wizard
Custom Object Wizard
Reactors Class Wizard
etc.
© 2008 Autodesk
Lab - Step 2
© 2008 Autodesk
Agenda
Overview of APIs
AutoCAD: Hello.arx
ObjectDBX: Structure
ObjectDBX: Extend it!
AutoCAD: Multi-Document Environment
Notification System
© 2008 Autodesk
Terminology
AutoCAD Session
Document
Current Document
ObjectARX works with this in background
Active Document
User sees this in UI
Drawing
Database
Working Database
Current Drawing
acdbHostApplicationServices()->workingDatabase()
© 2008 Autodesk
ObjectDBX: Introduction
© 2008 Autodesk
What is ObjectDBX and RealDWG?
Set of DLLs
© 2008 Autodesk
DWG Viewers
© 2008 Autodesk
Database Framework
DWG Client Apps
ObjectARX
AutoCAD
Application
User Interface
Partners: Autodesk: ObjectDBX
App 1 Inventor acdb18.dll achapi18.lib
Object Enablers
acismobj18.lib acge18.lib
App 2 Revit ObjEnabler1.dbx
axdb.lib acis.dll(s)
ObjEnabler2.dbx
Corp. Dev. Acgiapi.lib others...
Max
ObjEnabler3.dbx
Other ObjEnablerN.dbx
DWG DXF
© 2008 Autodesk
Enabling Custom Objects w/o ACAD
Partners: Autodesk:
DWG DXF
© 2008 Autodesk
Plug-In Model & ObjectDBX
AutoCAD process
.ARX .DBX
Acad.exe &
other AutoCAD ObjectDBX DLLs
specific DLLs
© 2008 Autodesk
ObjectARX & ObjectDBX
acdb18.lib acad.lib
acge18lib acedapi.lib
rcexelib.obj acgiapi.lib acui18.lib
axdb.lib adui18.lib
achapi18.lib oleaprot.lib
rxapi.lib AcTc.lib
acismobj18.lib AcTcUI.lib
rxheap.lib …
…
RealDWG
Shared
© 2008 Autodesk
What can I do with ObjectARX?
© 2008 Autodesk
AutoCAD Drawing Database
© 2008 Autodesk
Object Identity
Handle (AcDbHandle)
Unique identifier of an object for the life of the drawing
Object ID (AcDbObjectId)
Unique identifier of an object for a session of ObjectDBX
Multiple drawings allowed per session
IDs unique across ALL files would require too much storage space
(GUIDs)
Pointer
Unique identifier of an object for the duration of a transaction
© 2008 Autodesk
Handles, Object IDs, Pointers
ObjectDBX session
1.
2.
Object Open New
.dwg Closed AcDbObject AcDbObject
4. 3.
5.
© 2008 Autodesk
Object ID and Entity Name
© 2008 Autodesk
Open Modes
Read
255 simultaneous readers
Write
Open/Close model
1 writer at a time
read/write are mutually exclusive
Transaction model
multiple writers are allowed
read/write are NOT exclusive
Notify
Used internally
© 2008 Autodesk
Transactions
Two models
Open/Close
acdbOpenObject
AcDbObject::close, AcDbObject::cancel
Transaction
AcDbTransactionManager::startTransaction
AcTransaction::getObject
AcDbTransactionManager::end/abortTransaction
© 2008 Autodesk
Nesting AcTransactions
1 2 3 4
Transaction 2 obj2 obj3
obj1 obj3
obj2
AcDbDatabase
© 2008 Autodesk
Getting an AcDbDatabase Object
Construct one
In memory
© 2008 Autodesk
AcDbDatabase Ownership Hierarchy
© 2008 Autodesk
Object Model Overview
classmap.dwg
in ObjectARX distribution
© 2008 Autodesk
Important Classes
AcRxObject
AcGiDrawable
AcDbObject
AcDbEntity
AcDbCurve
AcDbDictionary AcDbLine
AcDbSymbolTable
AcDbBlockTable
AcDbSymbolTableRecord
© 2008 Autodesk
Snoop Tools for the AutoCAD Database
© 2008 Autodesk
Important Objects
Block Table
Three default records
*MODEL_SPACE
*PAPER_SPACE, *PAPER_SPACE0
Only entities added to one of these is visible in AutoCAD editor
Block table records only own entities
© 2008 Autodesk
Iterating Through Containers
© 2008 Autodesk
Iterator Pattern
Client1 Client2
iterator1 iterator1
Aggregate
© 2008 Autodesk
RTTI – Runtime Type Identification
AcDbEntity * ent;
if( ent->isKindOf( AcDbLine::desc() ) )
{
AcDbLine * line = AcDbLine::cast( ent );
// do something with line->startPoint() ...
}
© 2008 Autodesk
AcRxClass I (Overview)
Three roles:
Runtime type identification
Class factory
Protocol extension
samples\database\dataxtsn\peinvent
samples\entity\tempapp_dg
© 2008 Autodesk
AcRxClass II (Cont.)
© 2008 Autodesk
AcRxClass III (Object Diagram)
Runtime class
An AcDbLine hierarchy An AcRxClass
for
AcDbCurve
Another AcDbLine
An AcRxClass
for
AcDbLine
An AsdkCircle An AcRxClass
for
AcDbCircle
A protocol
extension An AcRxClass
object for
Pseudo AsdkCircle
.DBX/.ARX App constructor
© 2008 Autodesk
AcRxClass IV (Roles)
Class Factory
During filing we look up the class identifier in the runtime class
hierarchy
AcRxClass provides a „constructor‟ function
Protocol Extension
AcRxClasses hold a list of „extension objects‟
© 2008 Autodesk
Storing Data in the Database
© 2008 Autodesk
Lab - Step 3
© 2008 Autodesk
AcDbDatabase Ownership Hierarchy
© 2008 Autodesk
Inter-Object References
Ownership
Dictates objects written to disk (DWG/DXF)
A database object has exactly one owner
Database is the ultimate owner
Bi-directional
Pointer reference
Arbitrary references between object
Multiple objects can point to the same object
Uni-directional
© 2008 Autodesk
Inter-Object References
AcDbDatabase
AcDbDictionary
AcDbBlockTable AcDbLayerTable
Named Object Dict.
AcDbDictionary
AcDbBlockTableRecord AcDbLayerTableRecord
Company Dictionary *
AcDbLine AcDbObject'
AcDbHardOwnershipId AcDbHardPointerId
AcDbSoftOwnershipId AcDbSoftPointerId
© 2008 Autodesk
Inter-Object References
© 2008 Autodesk
Inter-Object References
© 2008 Autodesk
Soft/Hard References
Save
Follows both types of ownership links
Wblock
Follows hard references
DeepClone
COPY, MIRROR, EXPLODE commands
Follows only ownership links
Purge
Soft references do not protect the object from purge
© 2008 Autodesk
Changing an Object’s Identity I
AcDbObject::handOverTo
Replaces an objects in the db with a new one
Objects cannot be removed from the db
You can only flag them as erased
removed
Open New
AcDbObject AcDbObject
© 2008 Autodesk
Changing an Object’s Identity II
AcDbObject::swapIdWith
id id
AcDbObject AcDbObject
ObjectDBX Session
© 2008 Autodesk
Lab - Step 4
© 2008 Autodesk
Agenda
Overview of APIs
AutoCAD: Hello.arx
ObjectDBX: Structure
ObjectDBX: Extend it!
AutoCAD: Multi-Document Environment
Notification System
© 2008 Autodesk
ObjectDBX: Extend It!
© 2008 Autodesk
Create a New Class
Derive from
AcRxObject RTTI protocol
AcGiDrawable + Graphics protocol
AcDbObject + Filing protocol
AcDbEntity Persistent graphics
AcDbCurve Curve entities
…
© 2008 Autodesk
Filing
Read/write object state to another object (filer)
Visitor Pattern
© 2008 Autodesk
Filing Out
An AcDbDwgFiler 3.
An AcDbObject
1.
2.
3.
Another
AcDbObject
ObjectDBX
2.
© 2008 Autodesk
Filing In
An AcDbDwgFiler 4.
An AcDbObject
2.
1.
4.
2.
Another
3.
AcDbObject
ObjectDBX
3.
© 2008 Autodesk
Filing Member Functions - I
© 2008 Autodesk
Filing Member Functions - II
© 2008 Autodesk
Filing Member Functions - III
© 2008 Autodesk
AcDbDxfFiler
© 2008 Autodesk
Check You Have Access Rights
assertWriteEnabled
Every member function that modifies data
assertNotifyEnabled
Used internally
© 2008 Autodesk
Proxy
Class ID
Class ID
© 2008 Autodesk
Lab - Step 5
© 2008 Autodesk
Graphics Display
© 2008 Autodesk
AcGi Class Diagram
AcGiContext
AcGiCommonDraw
AcGiSubentityTraits
AcGiGeometry
AcGiWorldDraw AcGiWorldGeometry
AcGiViewportDraw AcGiViewportGeometry
AcGiViewport
Legend:
Inheritance
Containment
© 2008 Autodesk
Graphics Acquisition
4.
An AcGiWorldDraw An AcGiDrawable
6.
1.
3.
5.
Host Application An AcGiViewportDraw
2.
© 2008 Autodesk
AcGiSubEntityTraits
© 2008 Autodesk
Special Uses of AcGi
© 2008 Autodesk
Host-Defined Mechanisms
© 2008 Autodesk
AcDbEntity Protocol
© 2008 Autodesk
AcDbEntity Protocol
subIntersectWith - no default
subGetGeomExtents - no default
subList - print specified data, DXF name, layer,
space, and handle by default
subExplode - no default, must be implemented for
hatch to work
If exploding to non-“native” objects, return eExplodeAgain
Hatch calls explode recursively until down to “native”
entities (until eOk returned)
© 2008 Autodesk
Lab - Step 6
© 2008 Autodesk
Agenda
Overview of APIs
AutoCAD: Hello.arx
ObjectDBX: Structure
ObjectDBX: Extend it!
AutoCAD: Multi-Document Environment
Notification System
© 2008 Autodesk
AutoCAD: Multi-Document Environment (MDE)
Execution context
Document locking
© 2008 Autodesk
Documents
© 2008 Autodesk
Per-Document Data
© 2008 Autodesk
Execution Contexts
© 2008 Autodesk
Fiber scheduling in AutoCAD
App Fiber Doc1 Fiber Doc2 Fiber
Msg loop
Cmd started
in Doc1
Time
Doc1 prompts for
user input
Msg loop
User switches to
Doc2, starts cmd
Cmd completes
in Doc2
Msg loop
© 2008 Autodesk
Document Locking
Document-level transaction
Non-database resident data cannot rely on the ObjectDBX
transaction model
e.g. system variables
© 2008 Autodesk
Lock Types
Read
Write
XWrite
AutoWrite
© 2008 Autodesk
Document vs. Application
Document context
Automatic locking (simpler)
e.g. for exclusive write
addCommand( …, ACRX_CMD_DOCEXCLUSIVELOCK, … );
Application context
Manual locking (more control)
acDocManager->lockDocument( pDoc, kXWrite );
Required for multiple docs
© 2008 Autodesk
Current vs. Active Documents
Current…
for the user = „Active‟
for the API = „Current‟
© 2008 Autodesk
Agenda
Overview of APIs
AutoCAD: Hello.arx
ObjectDBX: Structure
ObjectDBX: Extend it!
AutoCAD: Multi-Document Environment
Notification System
© 2008 Autodesk
Notification System
© 2008 Autodesk
Notification
Observer pattern class diagram
Observer
Subject observers
Attach(Observer) Update()
Detach(Observer)
For all o in observers { SendNotification()
o->Update()
}
ConcreteSubject ConcreteObserver
Update()
subjectState observerState
© 2008 Autodesk
Notification
3.
A ConcreteSubject 4. A ConcreteObserver
2. 1.
Client
© 2008 Autodesk
Notification Example
Interaction diagram
3.
An AcDbLine 4.
Your custom object
2. 1.
Client
© 2008 Autodesk
ObjectDBX
Observers (reactors)
An AcDbDatabase
An AcDbObject An AcDbObjectReactor
Subjects
© 2008 Autodesk
AutoCAD
Observers (reactors)
An AcEdInputPointFilter
The AcEdInputPointManager An AcEdInputPointMonitor
An AcEdInputContextReactor
© 2008 Autodesk
Acting as an Observer
© 2008 Autodesk
Acting as a Subject
© 2008 Autodesk
Lab - Step 7
© 2008 Autodesk
Thank you!
© 2008 Autodesk