Example #1
0
        static public void test()
        {
            string      comp = "D:\\Temp\\test.dwg";
            Document    doc  = Application.DocumentManager.MdiActiveDocument;
            Editor      ed   = doc.Editor;
            Transaction tr   = doc.TransactionManager.StartTransaction();

            try
            {
                string   dwgName = HostApplicationServices.Current.FindFile(comp, Application.DocumentManager.MdiActiveDocument.Database, FindFileHint.Default);
                Database db      = new Database(false, false);
                db.ReadDwgFile(dwgName, System.IO.FileShare.Read, true, "");
                ObjectId BlkId;
                BlkId = doc.Database.Insert(dwgName, db, false);
                BlockTable       bt   = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead, true);
                BlockTableRecord btr  = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                BlockReference   bref = new BlockReference(new Point3d(10, 10, 0), BlkId);
                btr.AppendEntity(bref);
                tr.AddNewlyCreatedDBObject(bref, true);
                bref.ExplodeToOwnerSpace();
                bref.Erase();
                tr.Commit();
            }
            finally
            {
                tr.Dispose();
            }
        }
        public static void ExplodeToOwnerSpace()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db  = doc.Database;
            Editor   ed  = doc.Editor;

            PromptEntityOptions options = new PromptEntityOptions("\nSelect block reference");

            options.SetRejectMessage("\nSelect only block reference");
            options.AddAllowedClass(typeof(BlockReference), false);

            PromptEntityResult acSSPrompt = ed.GetEntity(options);

            using (Transaction tx = db.TransactionManager.StartTransaction())
            {
                BlockReference blockRef = tx.GetObject(acSSPrompt.ObjectId, OpenMode.ForRead) as BlockReference;
                //add event
                ids.Clear();
                db.ObjectAppended += new ObjectEventHandler(db_ObjectAppended);
                blockRef.ExplodeToOwnerSpace();
                //remove event
                db.ObjectAppended -= new ObjectEventHandler(db_ObjectAppended);

                foreach (ObjectId id in ids)
                {
                    //get each entity....
                    ed.WriteMessage("\n" + id.ToString());
                }

                tx.Commit();
            }
        }
Example #3
0
        public static ObjectIdCollection ExplodeBlock(this BlockReference br, Transaction tr, Database db,
                                                      bool erase = true)
        {
            // We'll collect the BlockReferences created in a collection
            var toExplode = new ObjectIdCollection();
            var objIds    = new ObjectIdCollection();

            // Define our handler to capture the nested block references
            void Handler(object s, ObjectEventArgs e)
            {
                switch (e.DBObject)
                {
                case BlockReference _:
                    toExplode.Add(e.DBObject.ObjectId);
                    break;

                default:
                    objIds.Add(e.DBObject.ObjectId);
                    break;
                }
            }

            // Add our handler around the explode call, removing it
            // directly afterwards

            db.ObjectAppended += Handler;
            br.ExplodeToOwnerSpace();
            db.ObjectAppended -= Handler;

            // Go through the results and recurse, exploding the
            // contents

            foreach (ObjectId bid in toExplode)
            {
                var nBr = (BlockReference)tr.GetObject(bid, OpenMode.ForRead);

                var exIds = nBr.ExplodeBlock(tr, db);

                foreach (ObjectId id in exIds)
                {
                    objIds.Add(id);
                }
            }


            // We might also just let it drop out of scope
            toExplode.Clear();

            // To replicate the explode command, we're delete the
            // original entity

            if (erase)
            {
                br.UpgradeOpen();
                br.Erase();
                br.DowngradeOpen();
            }

            return(objIds);
        }
        protected static void ExplodeBlockReference(BlockReference ent)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;

            using (var doclock = doc.LockDocument())
            {
                var db = HostApplicationServices.WorkingDatabase;
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    ent.ExplodeToOwnerSpace();
                    var e = tr.GetObject(ent.Id, OpenMode.ForWrite);
                    ent.Erase();
                    tr.Commit();
                }
            }
        }
Example #5
0
        public static Point3d?CopyAllEntity(FileInfo file, Point3d basepoint)
        {
            Point3d?result = null;

            if (!file.Exists)
            {
                throw new FileNotFoundException("找不到文件", file.FullName);
            }
            var db  = HostApplicationServices.WorkingDatabase;
            var doc = Application.DocumentManager.MdiActiveDocument;

            using (doc.LockDocument())
            {
                var blockName       = Guid.NewGuid().ToString();
                var importObjectIds = new ObjectIdCollection();
                var layoutIds       = new ObjectIdCollection();
                using (var sourceDb = new Database(false, true))
                {
                    if (file.Extension.ToLower() == ".dwg")
                    {
                        sourceDb.ReadDwgFile(file.FullName, FileShare.Read, true, "");
                    }
                    else if (file.Extension.ToLower() == ".dxf")
                    {
                        sourceDb.DxfIn(file.FullName, $@"{Environment.CurrentDirectory}\logs\importdxf\{file.Name}.log");
                    }
                    else
                    {
                        throw new Exception("不能识别的文件类型:" + file.FullName);
                    }
                    using (var tran = sourceDb.TransactionManager.StartTransaction())
                    {
                        //读取源数据库
                        using (var bt = (BlockTable)tran.GetObject(sourceDb.BlockTableId, OpenMode.ForRead, false))
                        {
                            BlockTableRecord btr = (BlockTableRecord)tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead, false);
                            foreach (ObjectId id in btr)
                            {
                                var ent = tran.GetObject(id, OpenMode.ForRead) as Entity;
                                if (ent == null)
                                {
                                    continue;
                                }
                                importObjectIds.Add(id);
                            }
                        }
                    }
                    using (var tr = db.TransactionManager.StartTransaction())
                    {
                        using (var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite, false))
                        {
                            using (var btr = new BlockTableRecord())
                            {
                                btr.Name       = blockName;
                                btr.Explodable = true;
                                bt.Add(btr);
                                tr.AddNewlyCreatedDBObject(btr, true);
                                sourceDb.WblockCloneObjects(importObjectIds, btr.Id, new IdMapping(), DuplicateRecordCloning.Replace, false);
                                foreach (var oid in btr)
                                {
                                    var ent = tr.GetObject(oid, OpenMode.ForWrite) as Entity;
                                    if (ent != null)
                                    {
                                        var mtx = Matrix3d.Displacement(Point3d.Origin - basepoint);
                                        ent.TransformBy(mtx);
                                    }
                                }
                                tr.Commit();
                            }
                        }
                    }
                }
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    using (var ent = new BlockReference(Point3d.Origin, bt[blockName]))
                    {
                        var space = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                        space.AppendEntity(ent);
                        tr.AddNewlyCreatedDBObject(ent, true);
                        var jig = new Jig.BlockReferenceJig(ent);
                        if (jig.Run())
                        {
                            ent.ExplodeToOwnerSpace();
                            var e = tr.GetObject(ent.Id, OpenMode.ForWrite);
                            ent.Erase();
                            var b = tr.GetObject(bt[blockName], OpenMode.ForWrite);
                            b.Erase();
                            tr.Commit();
                            result = ent.Position;
                        }
                    }
                }
            }
            return(result);
        }