Polyline2dHardwired() { m_db = Utils.Db.GetCurDwg(); using (CompBldrCurSpace compBldr = new CompBldrCurSpace(m_db)) { compBldr.Start(); compBldr.PushXform(Utils.Db.GetUcsMatrix(m_db)); Polyline2d pline2d = new Polyline2d(); pline2d.Color = Color.FromRgb(42, 184, 185); // polyline2d needs to be in database before the vertices // can be added to it. The polyline2d owns the vertices. compBldr.AddToDb(pline2d); Point3d[] pts = new Point3d[4]; pts[0] = new Point3d(0.0, 0.0, 0.0); pts[1] = new Point3d(15.0, 0.0, 0.0); pts[2] = new Point3d(15.0, 8.0, 0.0); pts[3] = new Point3d(0.0, 8.0, 0.0); Vertex2d vertX2d; for (int i = 0; i < pts.Length; i++) { vertX2d = new Vertex2d(pts[i], 0.0, 0.0, 0.0, 0.0); pline2d.AppendVertex(vertX2d); compBldr.Transaction.AddNewlyCreatedDBObject(vertX2d, true); } pline2d.Closed = true; compBldr.Commit(); } }
public static ObjectId?addPolyline(List <Point3d> plist, ObjectId layerId) { try { Document acDoc = Application.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; using (Transaction tran = acCurDb.TransactionManager.StartTransaction()) { BlockTable bt = tran.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord modelSpace = tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; Polyline2d b = new Polyline2d(); ObjectId oid = modelSpace.AppendEntity(b); for (int i = 0; i < plist.Count; i++) { b.AppendVertex(new Vertex2d(plist[i], 0, 0, 0, 0)); } b.Closed = true; if (layerId != ObjectId.Null) { b.LayerId = layerId; } tran.AddNewlyCreatedDBObject(b, true); tran.Commit(); return(oid); } } catch (System.Exception ex) { } return(null); }
public static void TranslateCoordinates() { // 获取当前文档和数据库,启动事务 var acDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument; var acCurDb = acDoc.Database; using (var acTrans = acCurDb.TransactionManager.StartTransaction()) { // 以读模式打开块表 BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; // 以写模式打开块表记录模型空间 BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // 创建2D多段线 var acPoly2d = new Polyline2d(); // 将新对象添加到块表记录和事务 acBlkTblRec.AppendEntity(acPoly2d); acTrans.AddNewlyCreatedDBObject(acPoly2d, true); // 先将多段线添加到块表记录,然后才能给它添加顶点 var acPts2dPoly = new Point3dCollection(); acPts2dPoly.Add(new Point3d(1, 1, 0)); acPts2dPoly.Add(new Point3d(1, 2, 0)); acPts2dPoly.Add(new Point3d(2, 2, 0)); acPts2dPoly.Add(new Point3d(3, 2, 0)); acPts2dPoly.Add(new Point3d(4, 4, 0)); foreach (Point3d acPt3d in acPts2dPoly) { var acVer2d = new Vertex2d(acPt3d, 0, 0, 0, 0); acPoly2d.AppendVertex(acVer2d); acTrans.AddNewlyCreatedDBObject(acVer2d, true); } // Set the normal of the 2D polyline acPoly2d.Normal = new Vector3d(0, 1, 2); // Get the first coordinate of the 2D polyline var acPts3d = new Point3dCollection(); Vertex2d acFirstVer = null; foreach (ObjectId acObjIdVert in acPoly2d) { acFirstVer = acTrans.GetObject(acObjIdVert, OpenMode.ForRead) as Vertex2d; acPts3d.Add(acFirstVer.Position); break; } // Get the first point of the polyline and // use the elevation for the Z value var pFirstVer = new Point3d(acFirstVer.Position.X, acFirstVer.Position.Y, acPoly2d.Elevation); // Translate the OCS to WCS var mWPlane = Matrix3d.WorldToPlane(acPoly2d.Normal); var pWCSPt = pFirstVer.TransformBy(mWPlane); Autodesk.AutoCAD.ApplicationServices.Core.Application.ShowAlertDialog( "The first vertex has the following " + "coordinates:" + "\nOCS: " + pFirstVer + "\nWCS: " + pWCSPt); // 提交事务 acTrans.Commit(); } }