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 114
| public partial class ACDOperation { public bool LinkLine(string dwgFilePath, string newFilePath, IEnumerable<LinkLineParam> lines) { 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 = LinkLine(blockDb, lines); }
if (!result) File.Delete(newFilePath);
return result; }
public bool LinkLine(IDynamicMetaObjectProvider database, IEnumerable<LinkLineParam> lines) { bool result = true;
Line line; LayerTable layers; LayerTableRecord layer = null; Point3d startPoint, endPoint;
Database curDb = database as Database;
using (Transaction curTrans = curDb.TransactionManager.StartTransaction()) { try { layers = curTrans.GetObject(curDb.LayerTableId, OpenMode.ForRead) as LayerTable;
BlockTableRecord curSpace = curTrans.GetObject(curDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
foreach (var param in lines) { endPoint = new Point3d(param.EndPoint.X, param.EndPoint.Y, param.EndPoint.Z); startPoint = new Point3d(param.StartPoint.X, param.StartPoint.Y, param.StartPoint.Z);
if (layers.Has(param.LayerName)) { layer = curTrans.GetObject(layers[param.LayerName], OpenMode.ForWrite) as LayerTableRecord; } if (param.LineType == LineType.DimensionLine) { //线性标注 AlignedDimension dimension = new AlignedDimension(); dimension.XLine1Point = startPoint; dimension.XLine2Point = endPoint; dimension.DimLinePoint = GetDimLinePoint(startPoint, endPoint,param.DimPosition);
if (!layer.IsNull()) dimension.LayerId = layer.Id;
curSpace.AppendEntity(dimension); curTrans.AddNewlyCreatedDBObject(dimension, true); } else { //轮廓线 line = new Line(startPoint, endPoint);
if (!layer.IsNull()) line.LayerId = layer.Id;
curSpace.AppendEntity(line); curTrans.AddNewlyCreatedDBObject(line, true); } } curTrans.Commit(); } catch (Autodesk.AutoCAD.Runtime.Exception) { curTrans.Abort(); result = false; } } return result; }
private Point3d GetDimLinePoint(Point3d startPoint, Point3d endPoint, double dimPosition) { double x = 0, y = 0, z = 0;
double maxY = Math.Max(endPoint.Y, startPoint.Y); double minY = Math.Min(endPoint.Y, startPoint.Y); double balanceY = maxY - minY; if (balanceY != 0) { x = endPoint.X + dimPosition; y = minY + balanceY / 2d; return new Point3d(x, y, z); }
double maxX = Math.Max(endPoint.X, startPoint.X); double minX = Math.Min(endPoint.X, startPoint.X); double balanceX = maxX - minX; if (balanceX != 0) { y = endPoint.Y + dimPosition; x = minX + balanceX / 2d; return new Point3d(x, y, z); }
return new Point3d(x, y, z); } }
|