43
Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Embed Size (px)

Citation preview

Page 1: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Advanced deepclone API in AutoCAD®

Cyrille FauvelAutodesk Developer NetworkADN Sparks Manager

Page 2: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Class Objective

You learn AutoCAD deepClone API and cloning techniques Benchmarks, performance and implementation considerations Aspects of controlling cloning operations in AutoCAD

We do not discuss and assume knowledge of How to program in .NET or ObjectARX The basics of the AutoCAD .Net or ObjectARX API AutoCAD product usage

Page 3: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

About the Presenter

Cyrille FauvelAutodesk Developer Network

Cyrille is a member of the M&E workgroup of the Autodesk Developer Network (ADN) team, providing developer support, training, and conference presentations on AutoCAD, RealDWG, Inventor, Maya, 3ds Max, FBX API.

He joined Autodesk in 1994 as an AutoCAD developer based in Switzerland and California, USA. Later moved to the Developer Support group, and Consulting division.

Cyrille graduated in Mechanics and Electronics in France, worked as a teacher and C++ developer for a 3rd party developer on AutoCAD. He is fluent in six programming languages, tries to learn Japanese which turns out to be more difficult than learning English, has two kids, plays the guitar and piano, likes reading, love sailing on oceans, sports, and especially hand-ball.

Page 4: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Autodesk Developer Network

Access to almost all Autodesk software and SDK’s Includes early access to beta software

Members-only website with thousands of technical articles Unlimited technical support Product direction through conferences Marketing benefits

Exposure on autodesk.com Promotional opportunities

One to three free API training classes Based on user level

www.autodesk.com/joinadn

Page 5: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Agenda

1Overview

Cloning! What is it?Why should I care?

2Basics

Shallow Clone, deep Clone & wblock CloneWho is using cloning?

3API

Cloning and ID TranslationsObjectARX or .Net?

4A concrete example

Modify the default AutoCAD behavior

5Additional information

Inspect, spy, learning more

Page 6: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Overview

Cloning! What is it?Why should I care?

Page 7: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Cloning

Cloning in biology is the process of producing similar populations of genetically identical individuals that occurs in nature when organisms such as bacteria, insects or plants reproduce asexually. Cloning in biotechnology refers to processes used to create copies of DNA fragments (molecular cloning), cells (cell cloning), or organisms. The term also refers to the production of multiple copies of a product such as digital media or software.

The term clone is derived from κλῶνος, the Greek word for "trunk, branch", referring to the process whereby a new plant can be created from a twig. In horticulture, the spelling clon was used until the twentieth century; the final e came into use to indicate the vowel is a "long o" instead of a "short o". Since the term entered the popular lexicon in a more general context, the spelling clone has been used exclusively.

Page 8: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Cloning (disambiguation)

Cloning is the process of making an identical copy of something.

Cloning may also refer to:

Cloning (programming), the copying of a programming object Disk cloning, the copying of the contents of a computer hard disk to a

storage medium or file Phone cloning, the transfer of identity between one mobile telephone

and another Reduplication, aka "cloning", in linguistics refers to a process by which

the root or stem of a word is repeated Molecular cloning, the process of identifying and isolating a specific

gene

Page 9: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Cloning

Clone is not Copy

For this presentation

Clone = make a new instance of an object which has the same property’ values of the original object

Copy = copy the value of an object property

Page 10: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

First user is AutoCAD

AutoCAD uses deepClone and/or wblock Copy, Mirror, Array operations Wblock operations Insert operations Explode operation Copy & Paste Entity drag operation Reference editing …

Any other plug-ins which want to control cloning operations to: Prevent clones Make sure objects required for a clone will be present

Maintain the integrity of a user defined structure across AutoCAD commands …

Page 11: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Basics

Shallow Clone, Deep Clone & Wblock CloneWho is using cloning?

Page 12: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Cloning methods

Shallow Cloning Copy of a singleton ObjectARX C++ object AcRxObject::clone()

RTTI and C++ object copy copyFrom() to copy properties

Deep Cloning clones an object and its hard/soft ownership references but do not clone hard/soft pointer referenced objects however, copies the pointer reference values

Wblock Cloning clones an object and its hard owner and hard pointer references but ignores any soft references and reset non cloned references to null

Page 13: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

First user is AutoCAD

AutoCAD uses deepClone and/or wblock Copy operations

deepClone Wblock operations

wblock Insert operations

deepClone (insert) Copy & Paste

Wblock & deepClone (insert) Entity drag operation

deepClone …

Page 14: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

'Ownership' and 'Pointer References'

4 types Combination of [ Hard or Soft ] and [ Ownership or Pointer ]

Hard Reference required for the object to survive in a database

Soft Reference not required (can be ignored depending of the context)

Ownership Parent – Child relation ship

Pointer Cooperative object the object is referencing for its definition

Page 15: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

References

ObjectARX AcDbHardOwnershipId AcDbSoftOwnershipId AcDbHardPointerId AcDbSoftPointerId

.Net ObjectId

No custom objects in .Net Use overrules instead

Cloning reference logic follows reference found in the filing protocol dwgInFields() / dwgOutFields()

Page 16: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Object ownership

Dictionary

Ext. Dict.

ObjectObjectObjectObject

Object

ObjectDictionary

Object’

Hard OwnerSoft Owner

Soft PointerHard Pointer

Page 17: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Entity ownership

Hard OwnerSoft Owner

Soft PointerHard Pointer

Entity

Sub EntitySub EntitySub EntitySub Entity

Page 18: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Can we get things more complicated?Entities are objects

Hard OwnerSoft Owner

Soft PointerHard Pointer

Entityor BTR

Sub EntitySub EntitySub EntitySub Entityor Entity

Object’

Object’

Dictionary

Ext. Dict.

ObjectObjectObjectObject

ObjectDictionary

Dictionary

Ext. Dict.

ObjectObjectObjectObject

ObjectDictionary

Object’Object’Object’

Dictionary

Ext. Dict.

ObjectObjectObjectObject

ObjectDictionary

Dictionary

Ext. Dict.

ObjectObjectObjectObject

ObjectDictionary

Ext. Dict.

Page 19: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

DWGBlock Tbl

BTR

Layer Tbl

LTR

EntityEntityEntityEntity

… NOD

Dictionary

Object

BTR LTR

ObjectObjectObject

ObjectDictionary

Hard OwnerSoft Owner

Soft PointerHard Pointer

……

A DWG database

Page 20: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Extension dictionary

Using extension dictionary can simplify a lot cloning operations, because: The object hard ‘owns’ it extension dictionary versus having to

reference an object in the NOD deepClone will find & clone it Wblock will find & clone it as well

The extension dictionary hard ‘owns’ its elements deepClone will clone its elements Wblock will clone them as well

Sounds a good solution to avoid to think about cloning. Well consider the following: Each DWG object is about ~300b footprint An object with an extension dictionary and an 1 object in it is ~ 1k Make a DWG 3 times bigger with no useful data in yet

Page 21: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Cloning phases

Default cloning phase AutoCAD clones objects by following the ID references

Application cloning phase AutoCAD may give control to a plug-in to do additional cloning and will

follow the ID references as required ID translation phase

AutoCAD translate clone IDs to new ID & handles for object in ‘Flux’

Page 22: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

In Flux

Clones in ‘Flux’ mode, means object being cloned, versus created and mapped to an original object Requires ID translation

You cannot intervene during this ID translation process process

Use the AcRxEventReactor class for monitoring activities and steps

Page 23: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Primary object

If the key object is part of the primary set of objects cloned

An object can tell if it's being cloned by referencing or not That's what the isPrimary flag is all about

Page 24: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Cloning contexts

kDcCopy / _COPY, _MIRROR, _ARRAY, Drag’n Drop kDcExplode / _EXPLODE kDcBlock / _BLOCK kDcXrefBind / _XREF, _-BIND kDcSymTableMerge / _IGESIN, _DXFIN, _XREF, _-ATTACH kDcInsert / _INSERT, _PASTECLIP, Drag’n Drop kDcWblock / _WBLOCK, _COPYCLIP, _DXFOUT kDcObjects / _BLOCK, _REFEDIT, _REFCLOSE kDcXrefInsert / _XREF _-INSERT kDcInsertCopy kDcWblkObjects / _REFEDIT, _REFCLOSE

Page 25: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

AcDbObject::deepClone()AcDbObject::wblockClone()

Never use AcDbObject::deepClone() or AcDbObject::wblockClone() directly You are missing the context But you can call them in custom object implementation or reactors

Instead use Within the same database

AcDbDatabase::deepCloneObjects() Across databases

AcDbDatabase::wblockCloneObjects() AcDbDatabase::wblock() AcDbDatabase::insert()

Page 26: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

API

Cloning and ID TranslationsObjectARX or .Net?

Page 27: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

AcDbDatabaseAutodesk.AutoCAD.DatabaseServices.Database

ObjectARX and .Net Use these to start a cloning operation

void abortDeepClone(AcDbIdMapping&); Acad::ErrorStatus deepCloneObjects(AcDbObjectIdArray&, AcDbObjectId&, AcDbIdMapping&, bool); void forceWblockDatabaseCopy();

Acad::ErrorStatus insert(AcDbObjectId&, const ACHAR*, AcDbDatabase*, bool); Acad::ErrorStatus insert(AcDbObjectId&, const ACHAR*, const ACHAR*, AcDbDatabase*, bool); Acad::ErrorStatus insert(const AcGeMatrix3d&, AcDbDatabase*, bool);

Acad::ErrorStatus wblock(AcDbDatabase*&); Acad::ErrorStatus wblock(AcDbDatabase*&, AcDbObjectId); Acad::ErrorStatus wblock(AcDbDatabase*&, const AcDbObjectIdArray&, const AcGePoint3d&); Acad::ErrorStatus wblock AcDbDatabase*, const AcDbObjectIdArray&, const AcGePoint3d&,

AcDb::DuplicateRecordCloning); Acad::ErrorStatus wblockCloneObjects(AcDbObjectIdArray&, AcDbObjectId&, AcDbIdMapping&,

AcDb::DuplicateRecordCloning, bool);

Page 28: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

AcRxEventReactor / AcEditorReactorAutodesk.AutoCAD.DatabaseServices.Database

ObjectARX and .Net void beginDeepClone(AcDbDatabase*, AcDbIdMapping&); void beginWblockObjects(AcDbDatabase*, AcDbIdMapping&); void beginInsert(AcDbDatabase*, const AcGeMatrix3d&, AcDbDatabase*); void beginInsert(AcDbDatabase*, const ACHAR *, AcDbDatabase*); void beginWblock(AcDbDatabase*, AcDbDatabase*); void beginWblock(AcDbDatabase*, AcDbDatabase*, AcDbObjectId); void beginWblock(AcDbDatabase*, AcDbDatabase*, const AcGePoint3d*&); void wblockNotice(AcDbDatabase*); void otherInsert(AcDbDatabase*, AcDbIdMapping&, AcDbDatabase*); void otherWblock(AcDbDatabase*, AcDbIdMapping&, AcDbDatabase*); void beginDeepCloneXlation(AcDbIdMapping&, Acad::ErrorStatus*); void endDeepClone(AcDbIdMapping&); void endWblock(AcDbDatabase*); void abortDeepClone(AcDbIdMapping&); void abortWblock(AcDbDatabase*); void abortInsert(AcDbDatabase*); Acad::ErrorStatus veto();

Page 29: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

RefEdit

RefEdit uses the wblock() mechanism Checkout Checkin

Only modified objects will be clones in checkin

See the AcApLongTransactionReactor class void beginCheckOut(AcDbLongTransaction&, AcDbObjectIdArray&); void endCheckOut(AcDbLongTransaction&); void beginCheckIn(AcDbLongTransaction&); void endCheckIn(AcDbLongTransaction&); void abortLongTransaction(AcDbLongTransaction&); Acad::ErrorStatus veto();

Page 30: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Custom objects

ObjectARX Only AcDbObject Autodesk.AutoCAD.DatabaseServices.DBObject

.Net implements these method, but you cannot override them You can call them in a cloning context/operation

API Sealed in ARX now Acad::ErrorStatus deepClone(AcDbObject*, AcDbObject*&, AcDbIdMapping&,

Adesk::Boolean) const; Acad::ErrorStatus wblockClone(AcRxObject*, AcDbObject*&, AcDbIdMapping&,

Adesk::Boolean) const;

Use the subXXX() version instead Acad::ErrorStatus subDeepClone(AcDbObject*, AcDbObject*&, AcDbIdMapping&,

Adesk::Boolean) const; Acad::ErrorStatus subWblockClone(AcRxObject*, AcDbObject*&, AcDbIdMapping&,

Adesk::Boolean) const;

Page 31: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Overrule API

Overrules registered on per class basis PEX protocol

See ‘CP230-1V - .Net Overrule API’ for details

Can ‘modify’ Draw aspect, Grips,

Avoid the need of a custom object

Page 32: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Available overrules

PropertiesOverrule DrawableOverrule OsnapOverrule TransformOverrule GripOverrule SubentityOverrule HighlightOverrule GeometryOverrule

ObjectOverrule

Page 33: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Overrule API

ObjectARX and .Net

AcDbObjectOverrule Acad::ErrorStatus deepClone(const AcDbObject*, AcDbObject*, AcDbObject*&,

AcDbIdMapping&, Adesk::Boolean); Acad::ErrorStatus wblockClone(const AcDbObject*, AcRxObject*, AcDbObject*&,

AcDbIdMapping&, Adesk::Boolean);

Autodesk.AutoCAD.DatabaseServices.ObjectOverrule DBObject DeepClone(DBObject, DBObject, IdMapping, [MarshalAs(UnmanagedType.U1)] bool); DBObject WblockClone(DBObject, RXObject, IdMapping, [MarshalAs(UnmanagedType.U1)] bool);

Page 34: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

A concrete example

Modify the default AutoCAD behavior

Page 35: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

ARXCloning sample

CLONE1 command Performs a shallow clone of a non-database resident object.

CLONE2 command Performs a shallow clone of a database resident object into the same

database CLONE3 command

Performs a shallow clone of a database resident object to another DWG database

Very basic code one simple object such as a line or circle.

Page 36: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

OPLine project

OPLine1 Custom entity derived from AcDbPolyline And a companion custom object derived from AcDdBObject stored in

the NOD

OPLine2 Try to replicate the above structure without custom object but using

reactors instead

OPLine3 Same but using the new overrule API

Page 37: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Additional information

Inspect, spy, learning more

Page 38: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Learning more

CP230-1V - .Net Overrule API CP240-2V

AutoCAD .NET: Practical Examples of Customizing AutoCAD Entity Behavior

Stephen Preston – Autodesk, Inc.

Page 39: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Learning more AutoCAD API

Online Help, Developer's Guide and SDK Samples

AutoCAD Developer Centerhttp://www.autodesk.com/developautocadFree DevTV for .NET API

Webcasts and Trainings on Programming and Newshttp://www.adskconsulting.com/adn/cs/api_course_sched.php > AutoCAD API

Through the Interfacehttp://through-the-interface.typepad.com

Page 40: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Materials

Handout AU10_CP220-3C_ARX_Cloning_2011_API.docx

Presentation AU10_CP220-3C_ARX_Cloning_2011_API.pptx

Sample code and models AU10_CP220-3C_ARX_Cloning_2011_API.zip

Page 41: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Thank you!

[email protected]

Page 42: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

Autodesk [and other] are registered trademarks or trademarks of Autodesk, Inc., and/or its subsidiaries and/or affiliates in the USA and/or other countries. All other brand names, product names, or trademarks belong to their respective holders. Autodesk reserves the right to alter product and services offerings, and specifications and pricing at any time without notice, and is not responsible for typographical or graphical errors that may appear in this document. © 2010 Autodesk, Inc. All rights reserved.

Page 43: Advanced deepclone API in AutoCAD ® Cyrille Fauvel Autodesk Developer Network ADN Sparks Manager

DWGBlock Tbl

BTR

Layer Tbl

LTR

EntityEntityEntityEntity

… NOD

Dictionary

Object

BTR LTR

ObjectObjectObject

Dictionary

Ext. Dict.

ObjectObjectObjectObject

Object

Object Entity

Dictionary

ObjectDictionary

Sub EntitySub EntitySub EntitySub Entity

Hard OwnerSoft Owner

Soft PointerHard Pointer

Object’

……