public static double GetElevation(this Polyline2d pl) { double?height = null; using (var myTrans = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction()) { foreach (ObjectId oid in pl) { Vertex2d v2d = myTrans.GetObject(oid, OpenMode.ForRead) as Vertex2d; if (v2d != null) { height = v2d.Position.Z; } break; } myTrans.Commit(); } if (height.HasValue) { return(height.Value); } throw new InvalidOperationException("\nUngültige 2d-Polylinie!"); }
private static Polyline Polyline2dToPolyline(Polyline2d polyline2d) { Document doc = DatabaseUtil.GetActiveDocument(); Database db = doc.Database; Polyline oPoly = null; try { using (doc.LockDocument()) { using (Transaction tr = DatabaseUtil.GetTransaction(db)) { oPoly = new Polyline(); Vertex2d[] oVtcies = polyline2d.Cast <Vertex2d>().ToArray(); for (int i = 0; i < oVtcies.Length; i++) { oPoly.AddVertexAt(i, GeometryUtil.ToPoint2d(oVtcies[i].Position), 0, 0, 0); } oPoly.Layer = polyline2d.Layer; oPoly.Color = polyline2d.Color; } } return(oPoly); } catch { return(null); } }
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 void Constructor_ClosedOption_AddsVertexAndSegment( Polyline2d polyline, int expectedSegments) { Assert.True(polyline.Vertices.Count == expectedSegments + 1); Assert.True(polyline.Segments.Count == expectedSegments); }
public void LineToPolyLine() { TypedValue[] types = new TypedValue[] { new TypedValue(0, "Line") }; DBObjectCollection dBObjectCollection = PolyService.utils.SelectDBObjectCollection("请选择检查重点的多段线", types); using (Transaction transaction = PolyService.utils.GetDabase().TransactionManager.StartTransaction()) { foreach (Entity entity in dBObjectCollection) { if (entity is Polyline2d) { Polyline2d polyline2d = (Polyline2d)transaction.GetObject(entity.ObjectId, OpenMode.ForWrite, true); this.Poly2dToPolyLine(polyline2d, entity); } else if (entity is Line) { Line polyline2d2 = (Line)transaction.GetObject(entity.ObjectId, OpenMode.ForWrite, true); this.Poly2dToPolyLine(polyline2d2, entity); } } transaction.Commit(); } }
internal static CircleModel Polyline2DModel(Polyline2d line, AttributeModel atModel) { CircleModel dbModel = new CircleModel(); double length = line.Length; dbModel.pointList = MethodCommand.GetArcPoints(line, length); dbModel.Color = line.ColorIndex == 256 ? MethodCommand.GetLayerColorByID(line.LayerId) : System.Drawing.ColorTranslator.ToHtml(line.Color.ColorValue); foreach (AttributeItemModel item in atModel.attributeItems) { string attValue = ""; switch (item.AtItemType) { case AttributeItemType.Area: attValue = line.Area.ToString(); break; case AttributeItemType.TxtHeight: break; case AttributeItemType.Color: attValue = dbModel.Color; break; case AttributeItemType.Content: break; case AttributeItemType.LayerName: attValue = line.Layer; break; case AttributeItemType.LineScale: attValue = line.LinetypeScale.ToString(); break; case AttributeItemType.LineType: attValue = GetLayerLineTypeByID(line); break; case AttributeItemType.Overallwidth: attValue = line.ConstantWidth.ToString(); break; case AttributeItemType.TotalArea: break; } if (!string.IsNullOrEmpty(attValue)) { item.AtValue = attValue; dbModel.attItemList.Add(item); } } return(dbModel); }
public static Polyline GetProjectedPolyline(this Polyline2d pline, [NotNull] Plane plane, Vector3d direction) { var tol = new Tolerance(1e-9, 1e-9); if (plane.Normal.IsPerpendicularTo(direction, tol)) { return(null); } if (pline.Normal.IsPerpendicularTo(direction, tol)) { var dirPlane = new Plane(Point3d.Origin, direction); if (!pline.IsWriteEnabled) { // ReSharper disable once UpgradeOpen pline.UpgradeOpen(); } pline.TransformBy(Matrix3d.WorldToPlane(dirPlane)); var extents = pline.GeometricExtents; pline.TransformBy(Matrix3d.PlaneToWorld(dirPlane)); return(GeomExt.ProjectExtents(extents, plane, direction, dirPlane)); } return(GeomExt.ProjectPolyline(pline, plane, direction)); }
public static ObjectId SelectPolyline(out string entType) { entType = ""; ObjectId nullObjId = ObjectId.Null; Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; if (ed != null) { PromptEntityResult prmtEnt = ed.GetEntity("\nSELECT PROFILE LINE "); if (prmtEnt.Status == PromptStatus.OK) { using (Transaction tr = db.TransactionManager.StartTransaction()) { DBObject obj = tr.GetObject(prmtEnt.ObjectId, OpenMode.ForRead); Polyline lPline = obj as Polyline; Polyline2d pline2d = obj as Polyline2d; Entity ent = obj as Entity; if (lPline != null || pline2d != null) { entType = ent.GetType().ToString(); entType = entType.Substring(34, entType.Count() - 34); return(obj.ObjectId); } else { ed.WriteMessage("\n Unsupported entity type \n\n Please select a 2d Polyline"); } } } } return(nullObjId); }
} // end CheckLayers() // Convert2DPolyToLWPoly: Convert to "LWPOLYLINE" for performance reasons public static Polyline Convert2DPolyToLWPoly(Entity ent) { // Select the currently open AutoCAD drawing and... Document doc = Application.DocumentManager.MdiActiveDocument; // Open the Drawing Database of Objects in the current drawing Database db = doc.Database; try { Polyline poly = new Polyline(); using (Transaction tr = db.TransactionManager.StartTransaction()) { // Open the Block table for read BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; // Open the Block table record Model space for write BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; Polyline2d poly2d = (Polyline2d)ent; poly2d.UpgradeOpen(); poly.ConvertFrom(poly2d, false); btr.AppendEntity(poly); tr.AddNewlyCreatedDBObject(poly, true); tr.Commit(); } // End Transaction return(poly); } catch { throw new ApplicationException("2D Polyline to LWPolyline conversion failure"); } } // End Convert2DPolyToLWPoly()
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); }
/// <summary> /// 创建Polyline2d /// </summary> /// <param name="points">JArray(格式为[[x1,y1],[x2,y2],[x3,y3]])</param> /// <returns></returns> Polyline2d CreatePolyline2dByJSON(JArray points) { try { Point3dCollection vertices = new Point3dCollection(); DoubleCollection bulges = new DoubleCollection(); foreach (JArray point in points) { double x = Convert.ToDouble(point[0].ToString()); double y = Convert.ToDouble(point[1].ToString()); vertices.Add(new Point3d(x, y, 0)); bulges.Add(0); } bool closed = vertices[0] == vertices[vertices.Count - 1]; Polyline2d newEnt = new Polyline2d(Poly2dType.SimplePoly, vertices, 0, closed, 1, 1, bulges); //newEnt.Color = Teigha.Colors.Color.FromRgb(0, 0, 0);//颜色 newEnt.LineWeight = LineWeight.ByLayer;//线宽 newEnt.LayerId = dwgHelper.LayerMgr.GetLayerIdByName("0"); return(newEnt); } catch (Exception ex) { Logger.log("CreatePolyline2dByJSON", ex.Message); } return(null); }
/// <summary> /// Creates a new instance of PolylineSegmentCollection from a Polyline2d. /// </summary> /// <param name="pline">A Polyline2d instance.</param> public PolylineSegmentCollection([NotNull] Polyline2d pline) { var vertices = pline.GetVertices().ToArray(); var n = vertices.Length - 1; for (var i = 0; i < n; i++) { var vertex = vertices[i]; _contents.Add(new PolylineSegment( vertex.Position.Convert2d(), vertices[i + 1].Position.Convert2d(), vertex.Bulge, vertex.StartWidth, vertex.EndWidth)); } if (pline.Closed) { var vertex = vertices[n]; _contents.Add(new PolylineSegment( vertex.Position.Convert2d(), vertices[0].Position.Convert2d(), vertex.Bulge, vertex.StartWidth, vertex.EndWidth)); } }
public void Reparametrize_SetsAllSegmentsDomain(Polyline2d polyline) { polyline.Reparametrize(); Assert.True(polyline.Domain.End == polyline.Segments[^ 1].Domain.End); Assert.True(polyline.Domain.End == 1); }
public crawlAcDbPolyline(Polyline2d polyline) { Entity ent = (Entity)polyline; this.ObjectId = ent.ObjectId.ToString(); Length = polyline.Length; this.Layer = polyline.Layer; this.Linetype = polyline.Linetype; this.LineWeight = polyline.LineWeight.ToString(); this.Color = polyline.Color.ToString(); Vertixes = new List <crawlPoint3d>(); // Use foreach to get each contained vertex foreach (ObjectId vId in polyline) { Vertex2d v2d = (Vertex2d) vId.GetObject( OpenMode.ForRead ); double x = v2d.Position.X; double y = v2d.Position.Y; double z = v2d.Position.Z; Vertixes.Add(new crawlPoint3d(x, y, z)); } }
/// <summary> /// Creates a new instance of PolylineSegmentCollection from a Polyline2d. /// </summary> /// <param name="pline">A Polyline2d instance.</param> public PolylineSegmentCollection(Polyline2d pline) { Vertex2d[] vertices = pline.GetVertices().ToArray(); int n = vertices.Length - 1; for (int i = 0; i < n; i++) { Vertex2d vertex = vertices[i]; _contents.Add(new PolylineSegment( vertex.Position.Convert2d(), vertices[i + 1].Position.Convert2d(), vertex.Bulge, vertex.StartWidth, vertex.EndWidth)); } if (pline.Closed == true) { Vertex2d vertex = vertices[n]; _contents.Add(new PolylineSegment( vertex.Position.Convert2d(), vertices[0].Position.Convert2d(), vertex.Bulge, vertex.StartWidth, vertex.EndWidth)); } }
public ObjectId CreateBlock() { Database db = HostApplicationServices.WorkingDatabase; ObjectId blockId = ObjectId.Null; //用于返回所创建的块的对象Id BlockTableRecord record = new BlockTableRecord(); //创建一个BlockTableRecord类的对象,表示所要创建的块 record.Name = "Room"; //设置块名 using (Transaction trans = db.TransactionManager.StartTransaction()) { Point3dCollection points = new Point3dCollection();//用于表示组成块的多段线的顶点 points.Add(new Point3d(-18.0, -6.0, 0.0)); points.Add(new Point3d(18.0, -6.0, 0.0)); points.Add(new Point3d(18.0, 6.0, 0.0)); points.Add(new Point3d(-18.0, 6.0, 0.0)); Polyline2d pline = new Polyline2d(Poly2dType.SimplePoly, points, 0.0, true, 0.0, 0.0, null); //创建组成块的多段线 record.Origin = points[3]; //设置块的基点 record.AppendEntity(pline); //将多段线加入到新建的BlockTableRecord对象 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite); //以写的方式打开块表 if (!bt.Has("Room")) //判断是否存在名为"Room"的块 { blockId = bt.Add(record); //在块表中加入"Room"块 trans.AddNewlyCreatedDBObject(record, true); //通知事务处理 trans.Commit(); //提交事务 } } return(blockId);//返回创建的块的对象Id }
private static LineSegment2d GetExtendLineSegmentFromPolyline2d(Polyline2d polyline2d, Point3d point, Transaction transaction) { LineSegment2d result = null; var vertices = new List <Vertex2d>(); foreach (ObjectId objId in polyline2d) { var vertex = transaction.GetObject(objId, OpenMode.ForRead) as Vertex2d; vertices.Add(vertex); } var point2D = new Point2d(point.X, point.Y); if (polyline2d.EndPoint == point) { var startPoint = vertices[vertices.Count - 2].Position; var startPoint2D = new Point2d(startPoint.X, startPoint.Y); result = new LineSegment2d(startPoint2D, point2D); } else { var endPoint = vertices[1].Position; var endPoint2D = new Point2d(endPoint.X, endPoint.Y); result = new LineSegment2d(point2D, endPoint2D); } return(result); }
public RectangleClass DrawRectangle(string name, double xDist, double yDist, Point startPoint) { RectangleClass rect = new RectangleClass(name, xDist, yDist, startPoint); using (Application.DocumentManager.MdiActiveDocument.LockDocument()) { using (Transaction trans = db.TransactionManager.StartTransaction()) { BlockTableRecord space = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); Point3dCollection points = new Point3dCollection(); points.Add(new Point3d(rect.StartPoint.X, rect.StartPoint.Y, 0)); points.Add(new Point3d(rect.PointTwo.X, rect.PointTwo.Y, 0)); points.Add(new Point3d(rect.PointThree.X, rect.PointThree.Y, 0)); points.Add(new Point3d(rect.PointFour.X, rect.PointFour.Y, 0)); Polyline2d polyline2D = new Polyline2d(Poly2dType.SimplePoly, points, 0, true, 0, 0, null); space.AppendEntity(polyline2D); trans.AddNewlyCreatedDBObject(polyline2D, true); trans.Commit(); } } return(rect); }
public bool NumberOfVerticesMoreThan3(Curve curve) { Polyline polyline = curve as Polyline; if (polyline != null) { return(polyline.NumberOfVertices >= 3); } Polyline2d polyline2d = curve as Polyline2d; if (polyline2d != null) { int i = 0; foreach (var vertex in polyline2d) { i++; if (i >= 3) { return(true); } } } return(false); }
public static double?GetPolyLineArea(ObjectId selectedObjectId, global::Autodesk.AutoCAD.DatabaseServices.Database db) { //Document doc = Application.DocumentManager.MdiActiveDocument; //Editor ed = doc.Editor; //Database db = doc.Database; Transaction tr = db.TransactionManager.StartTransaction(); // Get the current UCS //CoordinateSystem3d ucs = // ed.CurrentUserCoordinateSystem.CoordinateSystem3d; using (tr) { DBObject obj = tr.GetObject(selectedObjectId, OpenMode.ForRead); // If a "lightweight" (or optimized) polyline Polyline lwp = obj as Polyline; if (lwp != null) { if (lwp.Closed) { return(lwp.Area); } } else { // If an old-style, 2D polyline Polyline2d p2d = obj as Polyline2d; if (p2d != null) { return(p2d.Area); } else { // If an old-style, 3D polyline Polyline3d p3d = obj as Polyline3d; if (p3d != null) { return(p3d.Area); } } } tr.Commit(); } return(null); }
/// <summary> /// Listing vertices of polylines (LWPOLYLINE, 2D & 3D POLYLINE) /// </summary> /// <param name="tr"></param> /// <param name="polylines">Polylines objectID</param> /// <returns></returns> public static Point3dCollection ListVertices(Transaction tr, ObjectIdCollection polylines) { Point3dCollection coords = new Point3dCollection(); using (tr) { foreach (ObjectId id in polylines) { DBObject obj = tr.GetObject(id, OpenMode.ForRead); // If a lwPolyline Polyline lwp = obj as Polyline; if (lwp != null) { int vn = lwp.NumberOfVertices; for (int i = 0; i < vn; i++) { // Could also get the 2D point here Point3d pt = lwp.GetPoint3dAt(i); coords.Add(pt); } } else { // If an old-style, 2D polyline Polyline2d p2d = obj as Polyline2d; if (p2d != null) { foreach (ObjectId vId in p2d) { Vertex2d v2d = (Vertex2d)tr.GetObject(vId, OpenMode.ForRead); coords.Add(v2d.Position); } } else { // If an old-style, 3D polyline Polyline3d p3d = obj as Polyline3d; if (p3d != null) { // Use foreach to get each contained vertex foreach (ObjectId vId in p3d) { PolylineVertex3d v3d = (PolylineVertex3d)tr.GetObject( vId, OpenMode.ForRead ); coords.Add(v3d.Position); } } } } } tr.Commit(); } return(coords); }
/// <summary> /// Gets the centroid of the polyline 2d. /// </summary> /// <param name="pl">The instance to which the method applies.</param> /// <returns>The centroid of the polyline 2d (WCS coordinates).</returns> public static Point3d Centroid(this Polyline2d pl) { var vertices = pl.GetVertices().ToArray(); var last = vertices.Length - 1; var vertex = vertices[0]; var p0 = vertex.Position.Convert2D(); _ = pl.Elevation; var cen = new Point2d(0.0, 0.0); var area = 0.0; var bulge = vertex.Bulge; double tmpArea; Point2d tmpPt; var tri = new Triangle2D(); var arc = new CircularArc2d(); if (bulge != 0.0) { arc = pl.GetArcSegment2DAt(0); tmpArea = arc.AlgebricArea(); tmpPt = arc.Centroid(); area += tmpArea; cen += (new Point2d(tmpPt.X, tmpPt.Y) * tmpArea).GetAsVector(); } for (var i = 1; i < last; i++) { var p1 = vertices[i].Position.Convert2D(); var p2 = vertices[i + 1].Position.Convert2D(); tri.Set(p0, p1, p2); tmpArea = tri.AlgebricArea; area += tmpArea; cen += (tri.Centroid * tmpArea).GetAsVector(); bulge = vertices[i].Bulge; if (bulge != 0.0) { arc = pl.GetArcSegment2DAt(i); tmpArea = arc.AlgebricArea(); tmpPt = arc.Centroid(); area += tmpArea; cen += (new Point2d(tmpPt.X, tmpPt.Y) * tmpArea).GetAsVector(); } } bulge = vertices[last].Bulge; if (bulge != 0.0 && pl.Closed) { arc = pl.GetArcSegment2DAt(last); tmpArea = arc.AlgebricArea(); tmpPt = arc.Centroid(); area += tmpArea; cen += (new Point2d(tmpPt.X, tmpPt.Y) * tmpArea).GetAsVector(); } cen = cen.DivideBy(area); return(new Point3d(cen.X, cen.Y, pl.Elevation).TransformBy(Matrix3d.PlaneToWorld(pl.Normal))); }
public static bool Clear(this Polyline2d obj) { if (Math.Abs(obj.Elevation) < TOLERANCE) { return(false); } obj.Elevation = 0.0; return(true); }
//Get needed Viewport information public static ViewportInfo[] SelectLockedViewportInfoOnLayout( Document dwg, string layoutName) { List <ViewportInfo> lst = new List <ViewportInfo>(); TypedValue[] vals = new TypedValue[] { new TypedValue((int)DxfCode.Start, "VIEWPORT"), new TypedValue((int)DxfCode.LayoutName, layoutName) }; PromptSelectionResult res = dwg.Editor.SelectAll(new SelectionFilter(vals)); if (res.Status == PromptStatus.OK) { using (Transaction tran = dwg.TransactionManager.StartTransaction()) { foreach (ObjectId id in res.Value.GetObjectIds()) { Viewport vport = (Viewport)tran.GetObject( id, OpenMode.ForRead); if (vport.Number != 1 && vport.Locked) { ViewportInfo vpInfo = new ViewportInfo(); vpInfo.ViewportId = id; vpInfo.NonRectClipId = vport.NonRectClipEntityId; if (!vport.NonRectClipEntityId.IsNull && vport.NonRectClipOn) { Polyline2d pl = (Polyline2d)tran.GetObject( vport.NonRectClipEntityId, OpenMode.ForRead); vpInfo.BoundaryInPaperSpace = GetNonRectClipBoundary(pl, tran); } else { vpInfo.BoundaryInPaperSpace = GetViewportBoundary(vport); } Matrix3d mt = PaperToModel(vport); vpInfo.BoundaryInModelSpace = TransformPaperSpacePointToModelSpace( vpInfo.BoundaryInPaperSpace, mt); lst.Add(vpInfo); } } tran.Commit(); } } return(lst.ToArray()); }
/// <summary> /// Creates the new polyliness. /// </summary> /// <param name="_sourcedb">The _sourcedb.</param> /// <param name="_collection">The _collection.</param> public static void CreateNewPolyliness(Autodesk.AutoCAD.DatabaseServices.Database _sourcedb, Point2dCollection _collection) { // Get the current document and database Document acDoc = Application.DocumentManager.MdiActiveDocument; Autodesk.AutoCAD.DatabaseServices.Database acCurDb = acDoc.Database; Editor ed = acDoc.Editor; // Lock the current document PlanarEntity plane; CoordinateSystem3d ucs = ed.CurrentUserCoordinateSystem.CoordinateSystem3d; plane = new Plane(ucs.Origin, ucs.Xaxis); using (DocumentLock acLckDocCur = acDoc.LockDocument()) { // Start a transaction using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { #region MyRegion // Open the Block table record for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; // Open the Block table record Model space for write BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; DoubleCollection bulges = new DoubleCollection(); double[] bulgelist = new double[] {}; Point3dCollection p3DCollection = new Point3dCollection(); foreach (var point in _collection) { Point3d p3D = new Point3d(point.X, point.Y, 0.0); p3DCollection.Add(p3D); bulges.Add(0); } Polyline2d polyline2D = new Polyline2d(Poly2dType.SimplePoly, p3DCollection, 0, true, 0, 0, bulges); polyline2D.Color = Color.FromColorIndex(ColorMethod.ByAci, 1); acBlkTblRec.AppendEntity(polyline2D); acTrans.AddNewlyCreatedDBObject(polyline2D, true); #endregion acTrans.Commit(); } // // Unlock the document } }
private static bool IsVertexesEquals(Polyline polyline, Polyline2d polyline2d, double tolerance, Transaction transaction) { var vertexesInPolyline2d = GetPoint3dCollection(polyline2d, transaction); if (polyline.NumberOfVertices != vertexesInPolyline2d.Count) { return(false); } return(IsPointsEquals(GetPoint3dCollection(polyline, transaction), vertexesInPolyline2d, tolerance)); }
public static Polyline ConvertToPolyline(this Polyline2d line2d) { /*Polyline pline = new Polyline(); * pline.ConvertFrom(line2d, false); * return pline;*/ Polyline pline = new Polyline(); pline.ConvertFrom(line2d, false); return(pline); }
poly2d_pnt3dColl(Polyline2d poly2d) { Point3dCollection pnts3d = new Point3dCollection(); using (Polyline poly = new Polyline()) { poly.ConvertFrom(poly2d, true); pnts3d = poly_pnt3dColl(poly); } return(pnts3d); }
public static double?GetPolyLineArea(ObjectId selectedObjectId, global::Autodesk.AutoCAD.DatabaseServices.Database db) { Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { DBObject obj = tr.GetObject(selectedObjectId, OpenMode.ForRead); // If a "lightweight" (or optimized) polyline Polyline lwp = obj as Polyline; if (lwp != null) { if (lwp.Closed) { return(lwp.Area); } } else { // If an old-style, 2D polyline Polyline2d p2d = obj as Polyline2d; if (p2d != null) { return(p2d.Area); } else { // If an old-style, 3D polyline Polyline3d p3d = obj as Polyline3d; if (p3d != null) { return(p3d.Area); } } } tr.Commit(); } return(null); }
/// <summary> /// Gets the centroid of the polyline 2d. /// </summary> /// <param name="pl">The instance to which the method applies.</param> /// <returns>The centroid of the polyline 2d (WCS coordinates).</returns> public static Point3d Centroid(this Polyline2d pl) { Vertex2d[] vertices = pl.GetVertices().ToArray(); int last = vertices.Length - 1; Vertex2d vertex = vertices[0]; Point2d p0 = vertex.Position.Convert2d(); double elev = pl.Elevation; Point2d cen = new Point2d(0.0, 0.0); double area = 0.0; double bulge = vertex.Bulge; double tmpArea; Point2d tmpPt; Triangle2d tri = new Triangle2d(); CircularArc2d arc = new CircularArc2d(); if (bulge != 0.0) { arc = pl.GetArcSegment2dAt(0); tmpArea = arc.SignedArea(); tmpPt = arc.Centroid(); area += tmpArea; cen += ((new Point2d(tmpPt.X, tmpPt.Y)) * tmpArea).GetAsVector(); } for (int i = 1; i < last; i++) { Point2d p1 = vertices[i].Position.Convert2d(); Point2d p2 = vertices[i + 1].Position.Convert2d(); tri.Set(p0, p1, p2); tmpArea = tri.SignedArea; area += tmpArea; cen += (tri.Centroid * tmpArea).GetAsVector(); bulge = vertices[i].Bulge; if (bulge != 0.0) { arc = pl.GetArcSegment2dAt(i); tmpArea = arc.SignedArea(); tmpPt = arc.Centroid(); area += tmpArea; cen += ((new Point2d(tmpPt.X, tmpPt.Y)) * tmpArea).GetAsVector(); } } bulge = vertices[last].Bulge; if ((bulge != 0.0) && (pl.Closed == true)) { arc = pl.GetArcSegment2dAt(last); tmpArea = arc.SignedArea(); tmpPt = arc.Centroid(); area += tmpArea; cen += ((new Point2d(tmpPt.X, tmpPt.Y)) * tmpArea).GetAsVector(); } cen = cen.DivideBy(area); return(new Point3d(cen.X, cen.Y, pl.Elevation).TransformBy(Matrix3d.PlaneToWorld(pl.Normal))); }
public static void DrawShape(PosShape shape, Point3d inspt, double height) { Extents3d? bounds = shape.Bounds; if (!bounds.HasValue) return; Point3d p1 = bounds.Value.MinPoint; Point3d p2 = bounds.Value.MaxPoint; double scale = height / (p2.Y - p1.Y); Matrix3d trans = Matrix3d.AlignCoordinateSystem(p1, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, inspt, Vector3d.XAxis * scale, Vector3d.YAxis * scale, Vector3d.ZAxis * scale); Database db = HostApplicationServices.WorkingDatabase; using (Transaction tr = db.TransactionManager.StartTransaction()) { try { BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); Point3dCollection vertices = new Point3dCollection(new Point3d[]{ new Point3d(p1.X, p1.Y, 0), new Point3d(p2.X, p1.Y, 0), new Point3d(p2.X, p2.Y, 0), new Point3d(p1.X, p2.Y, 0), }); Polyline2d rec = new Polyline2d(Poly2dType.SimplePoly, vertices, 0, true, 0, 0, null); rec.TransformBy(trans); btr.AppendEntity(rec); tr.AddNewlyCreatedDBObject(rec, true); ObjectId hiddenLayer = PosUtility.DefpointsLayer; foreach (PosShape.Shape item in shape.Items) { Entity en = null; if (item is PosShape.ShapeLine) { PosShape.ShapeLine line = (PosShape.ShapeLine)item; en = new Line(new Point3d(line.X1, line.Y1, 0), new Point3d(line.X2, line.Y2, 0)); } else if (item is PosShape.ShapeArc) { PosShape.ShapeArc arc = (PosShape.ShapeArc)item; en = new Arc(new Point3d(arc.X, arc.Y, 0), arc.R, arc.StartAngle, arc.EndAngle); } else if (item is PosShape.ShapeCircle) { PosShape.ShapeCircle circle = (PosShape.ShapeCircle)item; en = new Circle(new Point3d(circle.X, circle.Y, 0), Vector3d.ZAxis, circle.R); } else if (item is PosShape.ShapeText) { PosShape.ShapeText text = (PosShape.ShapeText)item; DBText dbobj = new DBText(); dbobj.TextString = text.Text; dbobj.Position = new Point3d(text.X, text.Y, 0); dbobj.TextStyleId = PosUtility.CreateTextStyle("ShapeDump_" + shape.Name, text.Font, text.Width); dbobj.Height = text.Height; dbobj.WidthFactor = text.Width; dbobj.HorizontalMode = text.HorizontalAlignment; if (text.VerticalAlignment == TextVerticalMode.TextBottom) dbobj.VerticalMode = TextVerticalMode.TextBase; else dbobj.VerticalMode = text.VerticalAlignment; if (dbobj.HorizontalMode != TextHorizontalMode.TextLeft || dbobj.VerticalMode != TextVerticalMode.TextBase) { dbobj.AlignmentPoint = new Point3d(text.X, text.Y, 0); } en = dbobj; } if (en != null) { en.Color = item.Color; if (!item.Visible) en.LayerId = hiddenLayer; en.TransformBy(trans); btr.AppendEntity(en); tr.AddNewlyCreatedDBObject(en, true); } } tr.Commit(); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show("Error: " + ex.Message, "RebarPos", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); } } }
private void Create_symbol1() { string blockName = "symbol1"; DBObjectCollection entityCollection = new DBObjectCollection(); Line ent1 = new Line(new Point3d(0, 0.12, 0), new Point3d(0, -0.12, 0)); ent1.Layer = "0"; entityCollection.Add(ent1); Point3dCollection verties = new Point3dCollection(); verties.Add(new Point3d(0, 0, 0)); verties.Add(new Point3d(0.077, 0.064, 0)); verties.Add(new Point3d(0.077, -0.064, 0)); verties.Add(new Point3d(0, 0, 0)); Polyline2d ent2 = new Polyline2d(Poly2dType.SimplePoly, verties, 0, false, 0, 0, new DoubleCollection(new double[] { 0, 0, 0, 0 })); ent2.Layer = "0"; entityCollection.Add(ent2); TryInsertBlockRecord(blockName, entityCollection); }