How create a version of a block, so it can be edited without affecting original.

How create a version of a block, so it can be edited without affecting original.

oneMSN
Advocate Advocate
541 Views
2 Replies
Message 1 of 3

How create a version of a block, so it can be edited without affecting original.

oneMSN
Advocate
Advocate

I am trying to create a block version of a block in my drawing so that I can edit it.  
I thought I could do this with anonymous blocks and I learned that I could make a block anonymous by the method shown below.
The unexpected result of doing this was that it change all blocks in the drawing to anonymous too.  I did not want that, I want a version of the original block that I can edit independently without redefining the original.

 

What's the correct way to create a version of an existing block.

Here is how I created the Anonymous block.

using (BlockReference blockRef = new BlockReference(original.Position, blockDef.ObjectId))
                        {
                            //SET THE LAYER
                            blockRef.Layer = original.Layer;
                            BlockTableRecord blkTblRcd = trans.GetObject(blockRef.AnonymousBlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;
                            blkTblRcd.Name = "*U";
                            //Add the block reference to modelspace
                            TypedValue tv = new TypedValue((int)DxfCode.ExtendedDataAsciiString);
                            blockRef.XData = new ResultBuffer
                            {
                                new TypedValue((int)DxfCode.ExtendedDataRegAppName, TestAppName),   //1001
                                new TypedValue((int)DxfCode.ExtendedDataAsciiString, "Test")
                            };

                            modelSpaceBtr.AppendEntity(blockRef);
                            trans.AddNewlyCreatedDBObject(blockRef, true);
0 Likes
Accepted solutions (1)
542 Views
2 Replies
Replies (2)
Message 2 of 3

oneMSN
Advocate
Advocate
Accepted solution

Ok I am this far.  Just need to figure out the correct way to get the attribute:

 

[CommandMethod("CommandTests", "makeAnonymous2", CommandFlags.Modal)]
        public void MakeAnonymous2()
        {            
            if (ActiveDocument.Editor.GetPickFirstSelection("Pick block", out SelectionSet selectionSet) == PromptStatus.OK)
            {
                using (Transaction trans = ActiveDocument.Database.TransactionManager.StartTransaction())
                {
                    BlockReference original = ActiveDocument.Database.BlockReferencesInSelectionSet(selectionSet).FirstOrDefault();
                    BlockTable dbBt = (BlockTable)ActiveDocument.Database.BlockTableId.GetObject(OpenMode.ForWrite);
                    BlockTableRecord modelSpaceBtr = dbBt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;

                    TypedValue tv = new TypedValue(0, "INSERT");
                    BlockTableRecord originalBtr = (BlockTableRecord)original.BlockTableRecord.GetObject(OpenMode.ForRead);
                    var map = new IdMapping();

                    BlockTableRecord newBtr = (BlockTableRecord)originalBtr.DeepClone(dbBt, map, false);
                    newBtr.Name = "*U";
                    dbBt.Add(newBtr);
                    trans.AddNewlyCreatedDBObject(newBtr, true);

                    BlockReference newBlock = new BlockReference(original.Position, newBtr.Id);

                    modelSpaceBtr.AppendEntity(newBlock);
                    trans.AddNewlyCreatedDBObject(newBlock, true);

                    ActiveDocument.Database.EraseEntity(original);
                    trans.Commit();
                }
            }
        }
0 Likes
Message 3 of 3

oneMSN
Advocate
Advocate

For the attributes I used the original block to get the block definition from the database and used that to add the attribute defs to the new anonymous block.

0 Likes