Redefining Dynamic blocks from another file

nshupeFMPE3
Advocate
Advocate

Redefining Dynamic blocks from another file

nshupeFMPE3
Advocate
Advocate

I am trying to redefine dynamic blocks in working project files from a library file with updated definitions for the dynamic blocks. 

This entry in the forum has one method for redefining dynamic blocks. This post is rather old though and I thought I would ask if there has been any change or update to how this should be done. 

There is also this from the online developer guide. But I dont see any mention in there about dynamic blocks. 

The intended behavior I am trying to mimic is this. 

We have blocks on our Tool Palettes that reference the library file. We right click and redefine the required block, which updates the block in model space. Then we AttSync the block as some attributes have been added over time. 

0 Likes
Reply
321 Views
1 Reply
Reply (1)

nshupeFMPE3
Advocate
Advocate

So far this seems to work pretty well for what I need. 

public static ObjectIdCollection LoadBlockFromDwg(string[] BlockName, string filepath)
        {
            Editor ed = Active.Editor;
            Database destDb = Active.Database;
            Database sourceDb = new Database(false, true);

            // Create a variable to store the list of block identifiers
            ObjectIdCollection blockIds = new ObjectIdCollection();

            try
            {
                // Read the DWG into a side database
                sourceDb.ReadDwgFile(filepath, System.IO.FileShare.Read, true, "");

                TransactionManager tm = sourceDb.TransactionManager;

                using (Transaction outsideTrans = tm.StartTransaction())
                {
                    // Open the block table
                    BlockTable bt = (BlockTable)outsideTrans.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, false);

                    // Check each block in the block table
                    foreach (ObjectId btrId in bt)
                    {
                        BlockTableRecord btr = (BlockTableRecord)outsideTrans.GetObject(btrId, OpenMode.ForRead, false);

                        if (BlockName.Length == 1 && BlockName[0].Equals("All", StringComparison.CurrentCultureIgnoreCase))
                        {
                            // Only add named & non-layout blocks to the copy list
                            if (!btr.IsAnonymous && !btr.IsLayout)
                            {
                                blockIds.Add(btrId);
                            }
                        }
                        else
                        {
                            foreach (string bName in BlockName)
                            {
                                if (btr.Name.Equals(bName, StringComparison.CurrentCultureIgnoreCase))
                                    blockIds.Add(btrId);
                            }
                        }
                        btr.Dispose();
                    }
                    outsideTrans.Commit();
                }
                // Copy blocks from source to destination database
                IdMapping mapping = new IdMapping();
                sourceDb.WblockCloneObjects(blockIds,
                                            destDb.BlockTableId,
                                            mapping,
                                            DuplicateRecordCloning.Replace,
                                            false);
                ed.WriteMessage("\nCopied "
                                + blockIds.Count.ToString()
                                + " block definitions from "
                                + filepath
                                + " to the current drawing.");
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                ed.WriteMessage("\nError during copy: " + ex.Message);
            }
            sourceDb.Dispose();
            return blockIds;

        }

 

And follow it up with a quick attsync all 

Active.Editor.Command("_.ATTSYNC", "Name", "*");
0 Likes