コード例 #1
0
        //https://www.modelical.com/en/explode-autocad-block-references-using-net/
        public bool ExplodeBlockByNameCommand(string blockToExplode)
        {
            bool explodeResult = true;
            Document bDwg = Application.DocumentManager.MdiActiveDocument;
            Editor ed = bDwg.Editor;
            Database db = bDwg.Database;
            Autodesk.AutoCAD.DatabaseServices.TransactionManager bTransMan = bDwg.TransactionManager;
            using (Transaction bTrans = bTransMan.StartTransaction())
            {
                try
                {
                    BlockTable bt = (BlockTable)bTrans.GetObject(db.BlockTableId, OpenMode.ForRead);

                    ed.WriteMessage("\nProcessing {0}", blockToExplode);

                    if (bt.Has(blockToExplode))
                    {
                        ObjectId blkId = bt[blockToExplode];
                        BlockTableRecord btr = (BlockTableRecord)bTrans.GetObject(blkId, OpenMode.ForRead);
                        ObjectIdCollection blkRefs = btr.GetBlockReferenceIds(true, true);

                        foreach (ObjectId blkXId in blkRefs)
                        {
                            //create collection for exploded objects
                            DBObjectCollection objs = new DBObjectCollection();

                            //handle as entity and explode
                            Entity ent = (Entity)bTrans.GetObject(blkXId, OpenMode.ForRead);
                            ent.Explode(objs);
                            ed.WriteMessage("\nExploded an Instance of {0}", blockToExplode);

                            //erase Block
                            ent.UpgradeOpen();
                            ent.Erase();

                            BlockTableRecord btrCs = (BlockTableRecord)bTrans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

                            foreach (DBObject obj in objs)
                            {
                                Entity ent2 = (Entity)obj;
                                btrCs.AppendEntity(ent2);
                                bTrans.AddNewlyCreatedDBObject(ent2, true);
                            }

                        }
                    }

                    bTrans.Commit();
                }
                catch
                {
                    ed.WriteMessage("\nSomething went wrong");
                    explodeResult = false;
                }
                finally
                {
                }
                ed.WriteMessage("\n");
                bTrans.Dispose();
                bTransMan.Dispose();
            }

            return explodeResult; //return wheter the method was succesful or not

        }
コード例 #2
0
ファイル: Class1.cs プロジェクト: kfpopeye/MyAutoCadApp
        public void ExplodeBlockByNameCommand(Editor ed, Document doc, string blockToExplode)
        {
            Document bDwg  = doc;
            Database db    = bDwg.Database;
            Database olddb = HostApplicationServices.WorkingDatabase;

            HostApplicationServices.WorkingDatabase = db;
            Autodesk.AutoCAD.DatabaseServices.TransactionManager bTransMan = bDwg.TransactionManager;

            using (Transaction bTrans = bTransMan.StartTransaction())
            {
                try
                {
                    LayerTable lt = (LayerTable)bTrans.GetObject(db.LayerTableId, OpenMode.ForRead);
                    BlockTable bt = (BlockTable)bTrans.GetObject(db.BlockTableId, OpenMode.ForRead);

                    if (bt.Has(blockToExplode))
                    {
                        ObjectId           blkId   = bt[blockToExplode];
                        BlockTableRecord   btr     = (BlockTableRecord)bTrans.GetObject(blkId, OpenMode.ForRead);
                        ObjectIdCollection blkRefs = btr.GetBlockReferenceIds(true, true);

                        foreach (ObjectId blkXId in blkRefs)
                        {
                            //create collection for exploded objects
                            DBObjectCollection objs = new DBObjectCollection();

                            //handle as entity and explode
                            Entity ent = (Entity)bTrans.GetObject(blkXId, OpenMode.ForRead);
                            ent.Explode(objs);

                            //erase Block
                            ent.UpgradeOpen();
                            ent.Erase();

                            BlockTableRecord btrCs = (BlockTableRecord)bTrans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

                            foreach (DBObject obj in objs)
                            {
                                Entity ent2 = (Entity)obj;
                                if (!ent2.Linetype.Equals("ByLayer", StringComparison.CurrentCultureIgnoreCase) &&
                                    !ent2.Linetype.Equals("Continuous", StringComparison.CurrentCultureIgnoreCase))
                                {
                                    string   layer = "EQUIPMENT-" + ent2.Linetype;
                                    ObjectId oid;
                                    if (!lt.Has(layer))
                                    {
                                        using (Transaction bTrans2 = bTransMan.StartTransaction())
                                        {
                                            ed.WriteMessage("\nCreating layer {0}", layer);
                                            using (LayerTableRecord ltr = new LayerTableRecord())
                                            {
                                                LayerTable lt2 = (LayerTable)bTrans2.GetObject(db.LayerTableId, OpenMode.ForWrite);
                                                ltr.Color            = Color.FromColorIndex(ColorMethod.ByAci, 3);
                                                ltr.Name             = layer;
                                                ltr.LinetypeObjectId = ent2.LinetypeId;
                                                oid = lt2.Add(ltr);
                                                bTrans2.AddNewlyCreatedDBObject(ltr, true);
                                                bTrans2.Commit();
                                            }
                                        }
                                        lt = (LayerTable)bTrans.GetObject(db.LayerTableId, OpenMode.ForRead);
                                    }
                                    else
                                    {
                                        oid = lt[layer];
                                    }
                                    ed.WriteMessage("\nSetting entity properties.");
                                    ent2.LayerId  = oid;
                                    ent2.Linetype = "ByLayer";
                                }
                                btrCs.AppendEntity(ent2);
                                bTrans.AddNewlyCreatedDBObject(ent2, true);
                            }
                        }

                        //purge block
                        ObjectIdCollection blockIds = new ObjectIdCollection();
                        blockIds.Add(btr.ObjectId);
                        db.Purge(blockIds);
                        btr.UpgradeOpen();
                        btr.Erase();
                    }
                    else
                    {
                        ed.WriteMessage("\nCould not find block named {0}", blockToExplode);
                    }

                    bTrans.Commit();
                }
                catch (System.Exception err)
                {
                    ed.WriteMessage("\nSomething went wrong: {0}", blockToExplode);
                    File.AppendAllText(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\error.log", err.ToString());
                }
                finally
                {
                }
                bTrans.Dispose();
                bTransMan.Dispose();
                HostApplicationServices.WorkingDatabase = olddb;
                ed.WriteMessage("\n");
            }
        }