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
| public partial class ACDOperation { public bool InsertBlock(string dwgFilePath, string newFilePath, IEnumerable<InsertBlockParam> blocks) { bool result = true;
CheckFilePath(dwgFilePath, newFilePath);
File.Copy(dwgFilePath, newFilePath, true);
using (Database blockDb = new Database(false, true)) { blockDb.ReadDwgFile(newFilePath, FileOpenMode.OpenForReadAndWriteNoShare, true, null); blockDb.CloseInput(true);
result = InsertBlock(blockDb, blocks); }
if (!result) File.Delete(newFilePath);
return result; }
public bool InsertBlock(IDynamicMetaObjectProvider database, IEnumerable<InsertBlockParam> blocks) { bool result = true;
LayerTable layers; Database wblockDb; ObjectId wblockId; Point3d insertPoint; BlockTable currblockTable; LayerTableRecord layer; BlockReference blockReference; BlockTableRecord currblockTableRecord;
Database currdb = database as Database;
using (Transaction currtransaction = currdb.TransactionManager.StartTransaction()) { try { currblockTable = currtransaction.GetObject(currdb.BlockTableId, OpenMode.ForRead) as BlockTable;
currblockTableRecord = currtransaction.GetObject(currblockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
layers = currtransaction.GetObject(currdb.LayerTableId, OpenMode.ForRead) as LayerTable;
foreach (var param in blocks) { using (wblockDb = new Database(false, true)) { wblockDb.ReadDwgFile(param.BlockPath, FileOpenMode.OpenForReadAndWriteNoShare, true, null); wblockDb.CloseInput(true);
wblockId = currdb.Insert(param.BlockName, wblockDb, false); }
insertPoint = new Point3d(param.InsertPoint.X, param.InsertPoint.Y, param.InsertPoint.Z); blockReference = new BlockReference(insertPoint, wblockId);
if (layers.Has(param.LayerName)) { layer = currtransaction.GetObject(layers[param.LayerName], OpenMode.ForWrite) as LayerTableRecord; blockReference.LayerId = layer.Id; } currblockTableRecord.AppendEntity(blockReference);
currtransaction.AddNewlyCreatedDBObject(blockReference, true); } currtransaction.Commit(); } catch (Exception) { currtransaction.Abort();
result = false; } } return result; } }
|