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
| public partial class ACDOperation { public bool MoveBlock(string dwgFilePath, string newFilePath, IEnumerable<InsertBlockParam> blockOldPositions, IEnumerable<InsertBlockParam> blockNewPositions) { 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 = MoveBlock(blockDb, blockOldPositions, blockNewPositions); }
if (!result) File.Delete(newFilePath);
return result; }
public bool MoveBlock(IDynamicMetaObjectProvider database, IEnumerable<InsertBlockParam> blockOldPositions, IEnumerable<InsertBlockParam> blockNewPositions) { bool result = true;
ACDPoint3D blockRefPoint; BlockTable currblockTable; BlockReference blockReference; BlockTableRecord currblockTableRecord;
Database currdb = database as Database;
using (Transaction currentTrans = currdb.TransactionManager.StartTransaction()) { try { currblockTable = currentTrans.GetObject(currdb.BlockTableId, OpenMode.ForRead) as BlockTable;
currblockTableRecord = currentTrans.GetObject(currblockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
InsertBlockParam oldBlockPostion, newBlockPosition; foreach (var record in currblockTableRecord) { var entity = currentTrans.GetObject(record, OpenMode.ForWrite); if (entity is BlockReference) { blockReference = entity as BlockReference; blockRefPoint = new ACDPoint3D(blockReference.Position.X, blockReference.Position.Y, blockReference.Position.Z);
oldBlockPostion = blockOldPositions.FirstOrDefault(x =>x.InsertPoint.Equals(blockRefPoint)); if (oldBlockPostion != null) { newBlockPosition = blockNewPositions.FirstOrDefault(x => x.BlockId == oldBlockPostion.BlockId); if (newBlockPosition != null) { blockReference.Position = new Point3d(newBlockPosition.InsertPoint.X, newBlockPosition.InsertPoint.Y, newBlockPosition.InsertPoint.Z); } } } } currentTrans.Commit(); } catch (Exception) { currentTrans.Abort(); result = false; } } return result; } }
|