示例#1
0
        public override void Draw()
        {
            using (var l = Application.DocumentManager.MdiActiveDocument.LockDocument())
            {
                var db = HostApplicationServices.WorkingDatabase;
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    if (!bt.Has(BlockName))
                    {
                        return;
                    }
                    using (var ent = new BlockReference(Point3d.Origin, bt[BlockName]))
                    {
                        if (LayerName != null)
                        {
                            LayerManager.SetLayer(db, ent, LayerName, true);
                        }

                        var space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        space.AppendEntity(ent);
                        tr.AddNewlyCreatedDBObject(ent, true);
                        var jig = new Jig.BlockReferenceJig(ent as BlockReference);
                        if (jig.Run())
                        {
                            tr.Commit();
                            DataManager.WriteDictionary(ent.ObjectId, Data);
                        }
                    }
                }
            }
        }
示例#2
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);
        }