- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Let's say I have a block called "Car" and a block called "Caravan". The car is a standard block and the caravan is dynamic with options to change the length.
I want my users to be able to insert the car/caravan combination in one go so I combine both blocks into one "Car+Caravan" block but then I explode that block when they insert it. This then means that they have access to the dynamics of the caravan as well as being abe to move the car around independantly of the caravan.
BUT
I want to put the car and caravan in a group so that they will move together if required.
So, I've tried the two explode options so far - BlockReference.Explode(idCollection) and BlockReference.ExplodeToOwnerSpace.
Method 1: BlockReference.Explode
public static ObjectIdCollection ExplodeBlockInsertComponentsAndReturnIdsToGroup(BlockReference blkRef, ref Transaction trans)
{
Database db = HostApplicationServices.WorkingDatabase;
BlockTableRecord btr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
DBObjectCollection objColl = new DBObjectCollection();
blkRef.Explode(objColl);
ObjectIdCollection idsToGroup = new ObjectIdCollection();
foreach (DBObject obj in objColl) {
if ((obj) is Entity) {
ObjectId objId = btr.AppendEntity(obj);
trans.AddNewlyCreatedDBObject(obj, true);
idsToGroup.Add(objId);
}
}
return idsToGroup;
}I then create a group using the returned ids.
This almost works but the dynaimcs of the Caravan are lost. It doesn't even seem to realise that it's still a block even though all of the entities of the caravan highlight when I select a bit of it.
Method 2: Using ExplodeToOwnerSpace with a Database ObjectAppended handler (like in Kean's post here:http://through-the-interface.typepad.com/through_the_interface/2014/09/exploding-nested-autocad-bloc...)
public static ObjectIdCollection idsToGroup;
public static void ExplodeBlockInsertComponentsAndReturnIdsToGroup(BlockReference blkRef)
{
Database db = HostApplicationServices.WorkingDatabase;
idsToGroup = new ObjectIdCollection();
db.ObjectAppended += ObjectAppended;
blkRef.ExplodeToOwnerSpace();
db.ObjectAppended -= ObjectAppended;
}
public static void ObjectAppended(Object sender, ObjectEventArgs e)
{
idsToGroup.Add(e.DBObject.ObjectId);
}
Again, the returned ids are used to create a group.
I thought I'd cracked it with this as the blocks are grouped and the caravan has the dynamic properties - BUT - as soon as I try to move the group AutoCAD crashes.
So, in method 1 I think I'm missing something when I loop through the ObjectIdCollection adding them to the transaction - maybe something to do with the DynamicBlockTableRecord?
Method 2 is much closer but the group appears to be corrupt.
I'm not doing anything fancy in the grouping code (it works for the ExplodeToOwnerSpace fine) but here's the code for that anyway...
public static bool CreateGroupFromIds(ObjectIdCollection objCol, string blockName, ref Transaction trans)
{
try {
Database db = HostApplicationServices.WorkingDatabase;
string groupName = GroupHelper.GetNextGroupNameForBlock(blockName); // This just gets the next group name for the block
Group grp = new Group(groupName, true);
DBDictionary gd = trans.GetObject(db.GroupDictionaryId, OpenMode.ForWrite);
gd.SetAt(groupName, grp);
trans.AddNewlyCreatedDBObject(grp, true);
grp.InsertAt(0, objCol);
return true;
} catch (Exception ex) {
throw ex;
}
}I also tried doing the grouping inside a different transaction after the ExplodeToOwnerSpace function, just to make sure that the objects were actually properly in the database but no joy.
Thanks in advance for any assistance.
P
Solved! Go to Solution.