.NET

.NET

Reply
Contributor
matthewvclearspaninc
Posts: 11
Registered: ‎05-13-2014
Message 1 of 4 (108 Views)
Accepted Solution

Strange behavior when trying to edit block reference

108 Views, 3 Replies
06-11-2014 07:24 AM

My DrawMember method currently draws a rectangle, appends it to a block definition, and inserts a block reference to it.  When I try to use the Block Editor to edit the block inside of AutoCAD, however, the screen blinks and then all block references on the screen vanish.  The blocks do still exist in the Block Table (I confirmed this using the ARXDBG app), but they are gone from model space.

 

Why is this happening? My best guess is that there is a problem with the way I'm using Transactions.  I have attached my code if you would like to take a look.  The method is called from within a separate transaction.

 

-AutoCAD 2015

-.NET/C#

Valued Mentor
cadMeUp
Posts: 331
Registered: ‎05-11-2006
Message 2 of 4 (101 Views)

Re: Strange behavior when trying to edit block reference

06-11-2014 07:40 AM in reply to: matthewvclearspaninc

A couple of the attributes you create are being added to the block but you also need to add them to the transaction:

trans.AddNewlyCreatedDBObject(AttributeDefinition)

 

Any new entity you create need to be added to something, and also needs to be added to a transaction, if using transactions.

 

Also, typically when you add a blockref to model/paper space you need to additionally add the attributes as

AttributeReference objects from the AttributeDefinition's in the block record.

Distinguished Contributor
mcicognani
Posts: 125
Registered: ‎12-01-2004
Message 3 of 4 (90 Views)

Re: Strange behavior when trying to edit block reference

06-11-2014 07:55 AM in reply to: cadMeUp
Distinguished Contributor
mcicognani
Posts: 125
Registered: ‎12-01-2004
Message 4 of 4 (85 Views)

Re: Strange behavior when trying to edit block reference

06-11-2014 08:05 AM in reply to: mcicognani

This is a little code snippet that insert a BlockReference with attributes:

 


/// generic InsertBlock()
/// pOrg: Insertion point
/// BlockDef: Objectid of block definition in BlockTable
/// cSpace: target space (ModelSpace or PaperSpace)
/// tr: current active transaction (must be provided by caller)

public
 static ObjectId InsertBlock(Point3d pOrg, ObjectId BlockDef, BlockTableRecord cSpace, Transaction tr) {     // create a new block-reference     BlockReference br = new BlockReference(pOrg, BlockDef);     br.SetDatabaseDefaults(cSpace.Database);     cSpace.AppendEntity(br);     tr.AddNewlyCreatedDBObject(br, true);     // Copy annotative info, if present     BlockTableRecord bd = (BlockTableRecord)tr.GetObject(BlockDef, OpenMode.ForRead);     if (bd.Annotative == AnnotativeStates.True)     {         ObjectContextManager ocm = cSpace.Database.ObjectContextManager;         ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");         ObjectContexts.AddContext(br, occ.CurrentContext);     }     // copy attributes     foreach (ObjectId attId in bd)     {         Entity ent = (Entity)tr.GetObject(attId, OpenMode.ForRead);         if (ent is AttributeDefinition)         {             AttributeDefinition ad = (AttributeDefinition)ent;             AttributeReference ar = new AttributeReference();             ar.SetAttributeFromBlock(ad, br.BlockTransform);             br.AttributeCollection.AppendAttribute(ar);             tr.AddNewlyCreatedDBObject(ar, true);         }     }     return br.Id; }
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.