ObjectARX 2010 - Marat 2009-06-01 FINAL
ObjectARX 2010 - Marat 2009-06-01 FINAL
ObjectARX 2010 - Marat 2009-06-01 FINAL
and
AutoCAD .NET
Autodesk Developer Technical Services
Getting Acquainted
Instructor: Marat Mirgaleev (
Developer Consultant ADN Mechanics Engineering + Computer Science 20 years in software development (CAD, 3D, Databases) C++, C#, Delphi, Assembler; Object-Oriented Analysis and Design )
Your work, your software AutoCAD customization experience Your company What are you expecting from this training?
2008 Autodesk
Members-only website with 1000s of technical articles Unlimited technical support Product direction through conferences Marketing benefits
www.autodesk.com/joinadn
2008 Autodesk
Americas Team
European Team
APac Team
2008 Autodesk
Getting Support
http://www.autodesk.com/adn-devhelp
Provides access to
On-line knowledgebase Request submission Newsgroups 1-3 day turnaround Callbacks as needed
Answers to frequently asked questions are posted in our online 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 with Labs Slides give an abstract overview Labs and my comments give the practical experience 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
Time 10:00 - 5:00 Lunch 12:00 - 1:00 Day 1 Overview of APIs AutoCAD: Hello.arx Day 2 ObjectDBX: Structure ObjectDBX: Extend it! Day 3 AutoCAD: MDE Environment Notification System Day 3, Day 4 AutoCAD .NET
2008 Autodesk
Training Material
C:\AutoCAD API Training
ObjectARX*.pptx AutoCAD.NET*.pptx
2008 Autodesk
Agenda Overview of APIs AutoCAD: Hello.arx ObjectDBX: Structure ObjectDBX: Extend it! AutoCAD: Multi-Document Environment Notification System
2008 Autodesk
API Overview
A general look at APIs
Implementation Comparison
2008 Autodesk
What is an API?
A contract, it is about responsibilities Both sides are (or may be) required to
Code implementing API can be packaged as Source code modules (.h/.cpp in C++) Binary
2008 Autodesk
API Packaging
Client EXE, DLL, script, etc.
4
ATL templates
API
API
SomeApp.exe
3
API
API
Win 32 API DLLs
Client/Server Interaction
In process
Local
Remote
2008 Autodesk
.DLL .ARX
API ObjectARX APIs
.DLL .ARX
.EXE AutoCAD
2008 Autodesk
Source script AutoLISP in the past Pre-processed, semi interpreted code Compiled Visual LISP Compiled into IL Processor instructions ObjectARX
AutoCAD.NET
Compiled code
FAST
2008 Autodesk
ObjectARX .NET Managed API COM Automation (VB, Java, Delphi, etc.) AutoLISP
2008 Autodesk
API Implementation I
Input driver Input acquisition AutoCAD PlotHDI 2D graphics cache (WHIP!) Plotter driver Display driver
HEIDI
3D graphics cache
ObjectDBX
ObjectARX SDK (free, http://www.autodesk.com/objectarx) RealDWG (licensed, http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=770257) Plot DDK (licensed, http://www.autodesk.com/adn-devhelp) HEIDI DDK (licensed, http://www.autodesk.com/heidi) Wintab API (free)
2008 Autodesk
dwg
API Implementation II
.NET Lisp script VBA script COM Client
(VB, Java, Delphi)
Acdbmgd.dll Acmgd.dll
vl.arx
acvba.arx
axdb.dll
ObjectARX APIs COM
AutoCAD
2008 Autodesk
ObjectARX
0.29
0.26
0.13
1.12
2.81
Seconds
M CPU
Total Create 1000 2dPolylines Add XData to 1000 Entities Update 1000 Circles Create 1000 lines COM (VB) COM (VBA) Create 1000 Circles LI SP ObjectARX
Create 1
Create 1
Update 1
AutoCAD's APIs
2008 Autodesk
Create 1
Time in seconds
2008 Autodesk
120000 100000 80000 60000 40000 20000 0 Xrecord Xdata ObjectARX .NET LISP VBA VB
2008 Autodesk
4000 3000 2000 1000 0 Xrecord Custom Object ObjectARX .NET VBA LISP
2008 Autodesk
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 Structure Loading Memory management Command mechanism Prompting for user input
ObjectARX applications
Creating commands
ObjectARX Wizards
2008 Autodesk
What is ObjectARX?
AutoCAD Runtime eXtension
A framework?
More than just a toolkit Specialized objects can be plugged back into the system
2008 Autodesk
2008 Autodesk
2008 Autodesk
Binary compatibility
ObjectARX 2010
ObjectARX 2009
AutoCAD 2009
ObjectARX 2008
ObjectARX 2007
AutoCAD 2007, 2008 and 2009 Binary incompatible with earlier releases
2008 Autodesk
Development Environment
For ObjectARX development
Microsoft Visual Studio 2008 SP1 only for AutoCAD 2010 VS 2005 SP1 only for AutoCAD 2009 VS 2005 RTM for AutoCAD 2007, 2008, 2009 VS 2008 VS 2005 RTM or SP1
ObjectARX 2007, 2008, 2009 or 2010 Microsoft Windows 2000 SP2 or better
2008 Autodesk
2008 Autodesk
Main Libraries - I
AcRx Object and Class Management (rxapi.lib) AcEd AutoCAD Editor Facilities (acedapi.lib) AcDb AutoCAD Database (acdb18.lib) AcGi AutoCAD Graphics Interface (acgiapi.lib) AcGe AutoCAD Geometry Library (acge18.lib) 32- and 64-bit versions:
\ObjectARX 2010\lib-win32 and \ObjectARX 2010\lib-x64
2008 Autodesk
Main Libraries - II
AcUi/AdUi MFC Extension Library (adui18.lib acui18.lib) Automation (axdb.lib oleaprot.lib) ads_ Function Set (acad.lib) Static Linking Memory Management (rxheap.lib)
2008 Autodesk
Utilities
AutoCAD Facet Modeler (aecmodeler60.lib) AutoCAD Boundary Representation (acbr18.lib, acgex19.lib) ObjectARX Wizard
(<your path>\ObjectARX 2010\utils\ObjARXWiz\ArxWizards.msi)
ATIL - Raster graphics Teefy - Converting existing applications to Unicode string handling
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
The ObjectARX SDK comes with a number of code samples organized into sub-categories:
ObjectARX\Samples
COM Database DotNet Editor Entity Graphics Misc Reactors PolySamp and ArxDbg
2008 Autodesk
.DLL .ARX
API ObjectARX APIs
.ARX .DLL
.EXE AutoCAD
2008 Autodesk
Demand loaded
acad.rx file Registry (arxload) in acad.lsp Registry From another application Registry
On command invocation
On request
On proxy detection
5. 6.
Rxapi.lib
.ARX 2.
4.
Operating System
3.
MSVCRT.DLL
CRASH! .ARX
Piece of Memory
1.
MSVCRTD.DLL
1. .ARX allocates memory from debug heap 2. .ARX passes allocated memory to AutoCAD 3. AutoCAD de-allocates it with release delete CRASH
2008 Autodesk
MSVCRT.DLL
Piece of Memory
1.
.ARX
1. .ARX allocates memory from release heap 2. .ARX passes allocated memory to AutoCAD 3. AutoCAD de-allocates it with release delete
OK
2008 Autodesk
AutoCAD/ObjectDBX
1.
Rxheap.lib
4.
Piece of Memory
MSVCRT.DLL
3.
Libcmt.lib
.ARX
1. .ARX allocates memory via rxheap.lib 2. AutoCAD allocates memory on release heap 3. .ARX passes memory to AutoCAD 4. AutoCAD de-allocates it with release delete
OK
2008 Autodesk
Getting Started
Visual C++ 9.0 (.NET) AutoCAD 2010 ObjectARX 2010
www.sysinternals.com
2008 Autodesk
Command Mechanism I
1. 4. AcEdCommandStack 2, 3. 5. AcEdCommand AutoCAD Callback function .ARX
6.
1. Add command
2. Create command object 3. Parameterize it
Command Mechanism II
Removing commands is as important as adding them Command names can clash
Registered Developer Symbol (RDS) Search for Registered Developer Symbol takes you to
http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=1075006
2008 Autodesk
Lab - Step 1
Setup your first .ARX
2008 Autodesk
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
Play around with user input functions
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
Active Document
Drawing Database
Working Database
Current Drawing
acdbHostApplicationServices()->workingDatabase()
2008 Autodesk
ObjectDBX: Introduction
What is ObjectDBX and RealDWG? AutoCAD drawing database
Object identity Transactions Inter-object references Important classes Important objects Storing data
2008 Autodesk
2008 Autodesk
DWG Viewers
No viewing available in RealDWG! AutoCAD OEM DWF
www.dwfit.com
Autodesk Design Review DWG TrueView conversion of dwgs from old formats
2008 Autodesk
Database Framework
DWG Client Apps AutoCAD
Partners:
App 1 App 2 Corp. Dev. Other
ObjectARX Application
User Interface
Autodesk:
Inventor Revit Max
ObjectDBX
acdb18.dll acismobj18.lib axdb.lib Acgiapi.lib achapi18.lib acge18.lib
ObjEnabler1.dbx
Object Enablers
acis.dll(s) others...
ObjEnabler2.dbx ObjEnabler3.dbx ObjEnablerN.dbx
2008 Autodesk
Autodesk:
Inventor Revit
acISMobj.lib acge18.lib acis.dll(s) others...
ObjectDBX
Object Enablers
acdb18.lib achapi18.lib
ObjEnabler1.dbx ObjEnabler2.dbx ObjEnabler3.dbx
Max
axdb.lib acgiapi.lib
2008 Autodesk
ObjectDBX DLLs
2008 Autodesk
rcexelib.obj
RealDWG
2008 Autodesk
Shared
Create/Modify DWG/DXF files (.ARX) Create object enabler DLLs (.DBX) Cannot create standalone applications this requires RealDWG
2008 Autodesk
Objects have identity (primary key) Objects are only accessed in a transaction Objects can have references to other objects Objects can encapsulate data
2008 Autodesk
Object Identity
Handle (AcDbHandle)
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
2008 Autodesk
.dwg
Object Closed
4. 5.
Open AcDbObject
3.
New AcDbObject
4. Object closed
Pointer becomes invalid
An AcDbObjectId
Exchange one for another acdbGetAdsName( adsName, objId ); acdbGetObjectId( objId, adsName );
2008 Autodesk
Open Modes
Read
Write
1 writer at a time read/write are mutually exclusive multiple writers are allowed read/write are NOT exclusive
Transaction model
Notify
2008 Autodesk
Used internally
Transactions
Two models
Open/Close
Transaction
2008 Autodesk
Nesting AcTransactions
1
Transaction 2 Transaction 1
obj1 obj2
2
obj2 obj3
obj3
1. Client starts Trans1 and gets Obj1 & Obj2 2. Client starts Trans2 and gets Obj2 & Obj3 3. Client commits Trans2
Trans2 changes are committed Trans1 changes are committed Trans1 (and Trans2) changes are rolled back
In memory
2008 Autodesk
2008 Autodesk
in ObjectARX distribution
2008 Autodesk
Important Classes
AcRxObject AcGiDrawable AcDbObject AcDbEntity AcDbCurve AcDbDictionary AcDbSymbolTable AcDbBlockTable AcDbSymbolTableRecord Database resident objects Drawable objects AcDbLine
2008 Autodesk
C++ C# C++
2008 Autodesk
Important Objects
Block Table Three default records
Only entities added to one of these is visible in AutoCAD editor Block table records only own entities
Named Objects Dictionary AutoCADs symbol tables Dictionaries own any objects (but not entities)
2008 Autodesk
Symbol Tables Block Table Records Dictionaries Polylines PolyFaceMesh & PolygonMesh ACIS Solids
Called traversers
BlockReferences (Inserts)
2008 Autodesk
Iterator Pattern
Abstract form of i in the following construct: for( int i=0; i<numElems; i++ ) a = array[i]; Needs initialization, done condition and next operation
Client1
Client2
iterator1
iterator1 Aggregate
2008 Autodesk
AcDbObjects AcDbEntities RTTI allows us to do this cast isA isKindOf down-cast pointer safely get class descriptor is object derived from?
AcDbEntity * ent; if( ent->isKindOf( AcDbLine::desc() ) ) { AcDbLine * line = AcDbLine::cast( ent ); // do something with line->startPoint() ... }
2008 Autodesk
AcRxClass I (Overview)
Three roles:
samples\database\dataxtsn\peinvent samples\entity\tempapp_dg
2008 Autodesk
AcRxClass II (Cont.)
AcRxObject-derived objects hold a pointer to an AcRxClass object (class descriptor object)
2008 Autodesk
Another AcDbLine
An AsdkCircle
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
Each AcRxObject provides a virtual member to look up the static member pointing to its AcRxClass object
Protocol Extension
2008 Autodesk
Extension dictionaries on individual objects Dictionaries e.g. under the Named Objects
Data stored with these methods can be also accessed by LISP and COM
2008 Autodesk
Lab - Step 3
Accessing object containers
Symbol tables
2008 Autodesk
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
AcDbBlockTable
AcDbLayerTable
AcDbBlockTableRecord
AcDbLayerTableRecord
AcDbLine
AcDbObject'
AcDbHardOwnershipId AcDbSoftOwnershipId
AcDbHardPointerId AcDbSoftPointerId
2008 Autodesk
Inter-Object References
Hard ownership examples
Extension Dictionary
Extension Dictionaries
Entity Text
Linetype Style
2008 Autodesk
Inter-Object References
Soft owner examples
Dictionary
Entries Entities
Symbol Tables
Entity Object
2008 Autodesk
Soft/Hard References
Save
Wblock
DeepClone
Purge
2008 Autodesk
Replaces an objects in the db with a new one Objects cannot be removed from the db
Open AcDbObject
New AcDbObject
ObjectDBX Session
added
2008 Autodesk
id
id
AcDbObject
ObjectDBX Session
2008 Autodesk
AcDbObject
Lab - Step 4
Accessing object containers
Dictionaries
2008 Autodesk
Agenda Overview of APIs AutoCAD: Hello.arx ObjectDBX: Structure ObjectDBX: Extend it! AutoCAD: Multi-Document Environment Notification System
2008 Autodesk
2008 Autodesk
RTTI protocol + Graphics protocol + Filing protocol Persistent graphics Curve entities
ObjectDBX and host applications interact with your object through these interfaces
2008 Autodesk
Filing
Read/write object state to another object (filer)
Visitor Pattern
Filers implement AcDbDwgFiler or AcDbDxfFiler interface Used for DWG/DXF save/load, and also:
2008 Autodesk
Filing Out
An AcDbDwgFiler
3.
An AcDbObject
1. 2. 3.
ObjectDBX
Another AcDbObject
2.
1. Object implementing filer protocol created 2. Filer is passed to the database object 3. Database object calls back into the filer
2008 Autodesk
Filing In
An AcDbDwgFiler
4.
An AcDbObject
2.
1. 2. 3.
4.
ObjectDBX
Another AcDbObject
3.
1. Class identifier is read from filer 2. Class is instantiated by AcRxClass class factory 3. Filer is passed to the newly created object 4. Database object calls back into the filer
2008 Autodesk
2008 Autodesk
Copy same as Insert (files out objects state and files it into another object) Purge Object paging Any Object modification for undo recording kPurgeFiler kPageFiler kUndoFiler
2008 Autodesk
kFileFiler kFileFiler
kBagFiler
2008 Autodesk
AcDbDxfFiler
Data sent to a DXF filer is tagged with DXF group codes
Group code ranges for each data type AcDb enum has first in each range use +1, +2, etc to get others AcDb::DxfCode
2008 Autodesk
assertReadEnabled Every member function that reads data assertWriteEnabled Every member function that modifies data assertNotifyEnabled Used internally
2008 Autodesk
Proxy
A proxy object is created in memory when a defining application is not available
Class ID
2008 Autodesk
Class ID
Lab - Step 5
Creating a Custom Object
2008 Autodesk
Graphics Display
ObjectDBX does not display any graphics ObjectDBX defines a callback API that host applications may implement
AcGi Library defines the interface between database objects and a display system
2008 Autodesk
AcGiWorldGeometry AcGiViewportGeometry
2008 Autodesk
Graphics Acquisition
4.
An AcGiWorldDraw
An AcGiDrawable
6.
1.
3. 5.
Host Application
2.
An AcGiViewportDraw
1. Host creates object implementing AcGiWorldDraw 2. Host creates object implementing AcGiViewportDraw 3. Host passes world-draw object to your drawable 4. Drawable draws viewport independent graphics 5. Host passes viewport-draw object to your drawable 6. Drawable draws viewport dependent graphics
2008 Autodesk
AcGiGeometry
Responsible for inserting geometry into the graphics cache for later display Primitive geometry used:
circle, circularArc, polyline, mesh, pline, polygon, shell, text, xline and ray
2008 Autodesk
AcGiSubEntityTraits
Traits control current attributes for primitives:
color, layer, linetype - Current for editor filltype - kAcGiFillNever if standard mode kAcGiFillAlways if hide, shade, render gsmarker - invalid, must be set by app
2008 Autodesk
2008 Autodesk
Host-Defined Mechanisms
ObjectDBX defines protocols for
2008 Autodesk
AcDbEntity Protocol
subGetOsnapPoints subGetGripPoints subMoveGripPointsAt subGetStretchPoints - define osnap points - define grip points - default is to call AcDbEntity::subTransformBy - provide stretch points other than the grippoints, default is to call subGetGripPoints
subMoveStretchPointsAt - used by stretch, default is subTransformBy subTransformBy - needed for moving entity
2008 Autodesk
AcDbEntity Protocol
subIntersectWith - no default
- print specified data, DXF name, layer, space, and handle by default - 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
Creating a Custom Entity
2008 Autodesk
Agenda Overview of APIs AutoCAD: Hello.arx ObjectDBX: Structure ObjectDBX: Extend it! AutoCAD: Multi-Document Environment Notification System
2008 Autodesk
2008 Autodesk
Documents
State of the edit session AcApDocument class encapsulates
2008 Autodesk
Per-Document Data
Applications may have per-doc data
2008 Autodesk
Execution Contexts
AutoCAD has a fiber-based architecture
see MSDN for more info on fibers fibers are scheduled by AutoCAD they have their own call stack
runs windows message loop kicks off a fiber for each document created
2008 Autodesk
Msg loop Cmd started in Doc1 Time Msg loop User switches to Doc2, starts cmd Cmd completes in Doc2 Msg loop Doc1 prompts for user input
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
New Lock Request
(from another context)
XWrite Write Read AWrite Current Lock None Read Write XWrite AutoWrite
v v v
v v
v v v v
2008 Autodesk
addCommand( , ACRX_CMD_DOCEXCLUSIVELOCK, );
Application context
2008 Autodesk
2008 Autodesk
Agenda Overview of APIs AutoCAD: Hello.arx ObjectDBX: Structure ObjectDBX: Extend it! AutoCAD: Multi-Document Environment Notification System
2008 Autodesk
Notification System
Observer Pattern Implementation Subjects & Observers
ObjectDBX AutoCAD
2008 Autodesk
Notification
Observer pattern class diagram
1. Knows its observers 2. Provides interface to attach/detach observers observers Defines updating interface
Observer Update()
ConcreteSubject subjectState
2008 Autodesk
Notification
Observer pattern interaction diagram
3.
A ConcreteSubject
4.
A ConcreteObserver
2. Client
1.
1. Client creates observer 2. Client attaches observer to subject 3. Subject changes state and fires notification to observer 4. Observer learns details of state change
2008 Autodesk
Notification Example
Interaction diagram
3.
An AcDbLine
4.
2. Client
1.
1. Client creates your custom object 2. Client calls addPersistentReactor on the AcDbLine 3. Changing AcDbLine sends objectModified notification 4. Your custom object looks at the line to find out what changed
2008 Autodesk
ObjectDBX
Another AcDbObject An AcDbDatabase
Observers (reactors)
An AcDbDatabaseReactor
Subjects
An AcDbObject
An AcDbObjectReactor
2008 Autodesk
AutoCAD
The AcDbSummaryInfoManager The AcGsManager The AcApProfileManager
Observers (reactors)
An AcDbSummaryInfoReactor An AcGsReactor An AcApProfileManagerReactor An AcEdInputPointFilter
Subjects
The AcEdInputPointManager
An AcEdInputPointMonitor An AcEdInputContextReactor
2008 Autodesk
Acting as an Observer
Derive a concrete observer class
2008 Autodesk
Acting as a Subject
Define your own observer (notification) interface Provide methods to attach/detach observers Send notifications when state changes
2008 Autodesk
Lab - Step 7
Observing various subjects
2008 Autodesk
Thank you!
2008 Autodesk