1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| public static ObjectId AddBlockTableRecord(Document doc, string blockName, List<ObjectId> ents) { Database db = doc.Database; ObjectId resId = ObjectId.Null; using (Transaction transaction = db.TransactionManager.StartTransaction()) { BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForWrite) as BlockTable; if (bt.Has(blockName)) doc.RemoveBlockDefinition(blockName); BlockTableRecord btr = new BlockTableRecord(); btr.Name = blockName; resId = bt.Add(btr); transaction.AddNewlyCreatedDBObject(btr, true); ObjectIdCollection collection = new ObjectIdCollection(ents.ToArray()); IdMapping mapping = new IdMapping(); db.DeepCloneObjects(collection, resId, mapping, false); transaction.Commit(); } return resId; }
public static ObjectId InsertBlockReference(Database db, string blockName, Point3d position, Scale3d scale, double rotateAngle) { ObjectId blockReferenceId; using (Transaction transaction = db.TransactionManager.StartTransaction()) { BlockTable blockTable = db.BlockTableId.GetObject(OpenMode.ForWrite) as BlockTable; if (!blockTable.Has(blockName)) return ObjectId.Null; BlockTableRecord blockTableRecord = transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; BlockReference blockReference = new BlockReference(position, blockTable[blockName]); blockReference.ScaleFactors = scale; blockReference.Rotation = rotateAngle; blockReferenceId = blockTableRecord.AppendEntity(blockReference); transaction.AddNewlyCreatedDBObject(blockReference, true); blockTable.DowngradeOpen(); transaction.Commit(); } return blockReferenceId; }
public static void RemoveBlockRefrences(this Document doc, string name) { List<BlockReference> blocks = GetBlockReferenceByName(doc, name); ObjectId[] objectIds = new ObjectId[blocks.Count]; for (int i = 0; i < blocks.Count; i++) { BlockReference block = blocks[i]; objectIds[i] = block.ObjectId; } DwgPainter.DeleteEntities(doc.Database, objectIds); }
public static List<BlockReference> GetBlockReferenceByName(this Document doc, string name) { List<BlockReference> blockReferences = new List<BlockReference>(); Database db = doc.Database; using (Transaction transaction = doc.TransactionManager.StartTransaction()) { BlockTable bt = transaction.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord btr = transaction.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord; foreach (ObjectId id in btr) { Entity entity = transaction.GetObject(id, OpenMode.ForRead) as Entity; if (entity is BlockReference && (entity as BlockReference).Name == name) { blockReferences.Add(entity as BlockReference); } } transaction.Commit(); } return blockReferences; }
public static List<Entity> GetEntitiesInBlockReference(BlockReference block) { if (block == null) return null; List<Entity> entities = new List<Entity>(); Queue<BlockReference> blocks = new Queue<BlockReference>(); blocks.Enqueue(block); while (blocks.Count > 0) { BlockReference blockReference = blocks.Dequeue(); DBObjectCollection objects = new DBObjectCollection(); blockReference.Explode(objects); foreach (var obj in objects) { if (obj is BlockReference) blocks.Enqueue(obj as BlockReference); else if (obj is Line || obj is Polyline || obj is Circle || obj is Arc || obj is Polyline2d) entities.Add(obj as Entity); } } return entities; }
public static bool GetPointInTotalSystem(BlockReference reference, Point3d pointInLocalSystem, out Point3d pointInTotalSystem) { pointInTotalSystem = Point3d.Origin; if (reference == null) return false; Point3d insertPoint = reference.Position; double factor = reference.ScaleFactors.X; double angle = reference.Rotation; double dist = Math.Sqrt(pointInLocalSystem.X * pointInLocalSystem.X + pointInLocalSystem.Y * pointInLocalSystem.Y) * factor; double angleDefiniton = DwgPainter.Arctan(pointInLocalSystem.Y, pointInLocalSystem.X); angle += angleDefiniton; if (angle > Math.PI * 2) { angle -= Math.PI * 2; } pointInTotalSystem = new Point3d(insertPoint.X + dist * Math.Cos(angle), insertPoint.Y + dist * Math.Sin(angle), 0); return true; }
|