public static Autodesk.DesignScript.Geometry.PolyCurve ConvertDynamoPolylineToPolyCurve(Autodesk.AutoCAD.DynamoNodes.Polyline dynamoPolyline) { Autodesk.AutoCAD.DatabaseServices.Polyline polyline = (Autodesk.AutoCAD.DatabaseServices.Polyline)dynamoPolyline.InternalDBObject; Autodesk.DesignScript.Geometry.PolyCurve retVal = null; if (polyline != null) { // extract each segment Autodesk.DesignScript.Geometry.Curve[] curves; // = new Autodesk.DesignScript.Geometry.Curve[polyline.NumberOfVertices]; if (polyline.Closed) { curves = new Autodesk.DesignScript.Geometry.Curve[polyline.NumberOfVertices]; } else { curves = new Autodesk.DesignScript.Geometry.Curve[polyline.NumberOfVertices - 1]; } // convert segment into Dynamo curve or line int curIndex = 0; while (curIndex <= polyline.NumberOfVertices) { if (polyline.GetSegmentType(curIndex) == SegmentType.Arc) { Autodesk.DesignScript.Geometry.Arc curCurve; Autodesk.DesignScript.Geometry.Point centerPt = Autodesk.DesignScript.Geometry.Point.ByCoordinates( polyline.GetArcSegment2dAt(curIndex).Center.X, polyline.GetArcSegment2dAt(curIndex).Center.Y, 0.0); Autodesk.DesignScript.Geometry.Point startPt = Autodesk.DesignScript.Geometry.Point.ByCoordinates( polyline.GetArcSegment2dAt(curIndex).StartPoint.X, polyline.GetArcSegment2dAt(curIndex).StartPoint.Y, 0.0); Autodesk.DesignScript.Geometry.Point endPt = Autodesk.DesignScript.Geometry.Point.ByCoordinates( polyline.GetArcSegment2dAt(curIndex).EndPoint.X, polyline.GetArcSegment2dAt(curIndex).EndPoint.Y, 0.0); curCurve = Autodesk.DesignScript.Geometry.Arc.ByCenterPointStartPointEndPoint(centerPt, endPt, startPt); curves.SetValue(curCurve, curIndex); } else if (polyline.GetSegmentType(curIndex) == SegmentType.Line) { Autodesk.DesignScript.Geometry.Line curLine; Autodesk.DesignScript.Geometry.Point startPt = Autodesk.DesignScript.Geometry.Point.ByCoordinates( polyline.GetLineSegment2dAt(curIndex).StartPoint.X, polyline.GetLineSegment2dAt(curIndex).StartPoint.Y, 0.0); Autodesk.DesignScript.Geometry.Point endPt = Autodesk.DesignScript.Geometry.Point.ByCoordinates( polyline.GetLineSegment2dAt(curIndex).EndPoint.X, polyline.GetLineSegment2dAt(curIndex).EndPoint.Y, 0.0); curLine = Autodesk.DesignScript.Geometry.Line.ByStartPointEndPoint(startPt, endPt); curves.SetValue(curLine, curIndex); } curIndex = curIndex + 1; } retVal = Autodesk.DesignScript.Geometry.PolyCurve.ByJoinedCurves(curves); } return(retVal); }
public static PolyLineModel Polyline2Model(Autodesk.AutoCAD.DatabaseServices.Polyline polyLine, AttributeModel atModel) { PolyLineModel polylineModel = new PolyLineModel(); polylineModel.individualName = ""; polylineModel.individualFactor = ""; polylineModel.individualCode = ""; polylineModel.individualStage = ""; // 增加个体编码、个体要素、个体名称 System.Data.DataTable tb = Method.AutoGenerateNumMethod.GetAllPolylineNumsEx(polyLine); if (tb.Rows != null && tb.Rows.Count > 0) { foreach (System.Data.DataRow row in tb.Rows) { if ((string)row["多段线id"] == polyLine.Id.Handle.Value.ToString()) { polylineModel.individualName = (string)row["个体名称"]; polylineModel.individualFactor = (string)row["个体要素"]; polylineModel.individualCode = (string)row["个体编码"]; polylineModel.individualStage = (string)row["个体阶段"]; } } } polylineModel.Area = polyLine.Area; polylineModel.Closed = polyLine.Closed; polylineModel.Color = polyLine.ColorIndex == 256 ? MethodCommand.GetLayerColorByID(polyLine.LayerId) : System.Drawing.ColorTranslator.ToHtml(polyLine.Color.ColorValue); polylineModel.Vertices = new System.Collections.ArrayList(); int vn = polyLine.NumberOfVertices; //lwp已知的多段线 for (int i = 0; i < vn; i++) { Point2d pt = polyLine.GetPoint2dAt(i); SegmentType st = polyLine.GetSegmentType(i); if (st == SegmentType.Arc) { ArcModel arc = new ArcModel(); CircularArc2d cir = polyLine.GetArcSegment2dAt(i); // arc.Center = new System.Drawing.PointF((float)cir.Center.X,(float)cir.Center.Y); arc.Center = Point2d2Pointf(cir.Center); arc.Radius = cir.Radius; arc.Startangel = cir.StartAngle; arc.EndAngel = cir.EndAngle; // arc.StartPoint = new System.Drawing.PointF((float)cir.StartPoint.X, (float)cir.StartPoint.Y); if (cir.HasStartPoint) { arc.StartPoint = Point2d2Pointf(cir.StartPoint); } // arc.EndPoint = new System.Drawing.PointF((float)cir.EndPoint.X, (float)cir.EndPoint.Y); if (cir.HasEndPoint) { arc.EndPoint = Point2d2Pointf(cir.EndPoint); } MyPoint spt = new MyPoint(arc.StartPoint.X, arc.StartPoint.Y); MyPoint ept = new MyPoint(arc.EndPoint.X, arc.EndPoint.Y); MyPoint center = new MyPoint(arc.Center.X, arc.Center.Y); arc.Color = polylineModel.Color; // arc.pointList = MethodCommand.GetRoationPoint(spt, ept, center, arc.Startangel,arc.EndAngel,cir.IsClockWise); arc.pointList = MethodCommand.GetArcPointsByPoint2d(cir.GetSamplePoints(20)); //arc.pointList = MethodCommand.GetArcPoints(arc.Center,arc.Startangel,arc.EndAngel,arc.Radius); // arc.pointList.Insert(0, arc.StartPoint); // arc.pointList.Add(arc.EndPoint); foreach (AttributeItemModel item in atModel.attributeItems) { string attValue = ""; switch (item.AtItemType) { case AttributeItemType.Area: attValue = polyLine.Area.ToString(); break; case AttributeItemType.TxtHeight: break; case AttributeItemType.Color: attValue = polylineModel.Color; break; case AttributeItemType.Content: break; case AttributeItemType.LayerName: attValue = polyLine.Layer; break; case AttributeItemType.LineScale: attValue = polyLine.LinetypeScale.ToString(); break; case AttributeItemType.LineType: attValue = GetLayerLineTypeByID(polyLine); break; case AttributeItemType.Overallwidth: attValue = polyLine.ConstantWidth.ToString(); break; case AttributeItemType.TotalArea: break; } if (!string.IsNullOrEmpty(attValue)) { item.AtValue = attValue; arc.attItemList.Add(item); } } polylineModel.Vertices.Add(arc); } else if (st == SegmentType.Line) { LineModel line = new LineModel(); LineSegment2d lineSe = polyLine.GetLineSegment2dAt(i); if (lineSe.HasStartPoint) { line.StartPoint = Point2d2Pointf(lineSe.StartPoint); } if (lineSe.HasEndPoint) { line.EndPoint = Point2d2Pointf(lineSe.EndPoint); } if (line.StartPoint.X == line.EndPoint.X && line.StartPoint.Y == line.EndPoint.Y) { line.Angle = 0; line.Length = 0; } else if (line.StartPoint.X == line.EndPoint.X) { line.Angle = 90; } line.Color = polylineModel.Color; foreach (AttributeItemModel item in atModel.attributeItems) { string attValue = ""; switch (item.AtItemType) { case AttributeItemType.Area: attValue = polyLine.Area.ToString(); break; case AttributeItemType.TxtHeight: break; case AttributeItemType.Color: attValue = polylineModel.Color; break; case AttributeItemType.Content: break; case AttributeItemType.LayerName: attValue = polyLine.Layer; break; case AttributeItemType.LineScale: attValue = polyLine.LinetypeScale.ToString(); break; case AttributeItemType.LineType: attValue = GetLayerLineTypeByID(polyLine); break; case AttributeItemType.Overallwidth: attValue = polyLine.ConstantWidth.ToString(); break; case AttributeItemType.TotalArea: break; } item.AtValue = attValue; line.attItemList.Add(item); } polylineModel.Vertices.Add(line); } } return(polylineModel); }
public static PolyLineModel Polyline2Model(Autodesk.AutoCAD.DatabaseServices.Polyline polyLine) { PolyLineModel polylineModel = new PolyLineModel(); polylineModel.individualName = ""; polylineModel.individualFactor = ""; polylineModel.individualCode = ""; polylineModel.individualStage = ""; // 增加个体编码、个体要素、个体名称 System.Data.DataTable tb = Method.AutoGenerateNumMethod.GetAllPolylineNumsEx(polyLine); if (tb.Rows != null && tb.Rows.Count > 0) { foreach (System.Data.DataRow row in tb.Rows) { if ((string)row["多段线id"] == polyLine.Id.Handle.Value.ToString()) { polylineModel.individualName = (string)row["个体名称"]; polylineModel.individualFactor = (string)row["个体要素"]; polylineModel.individualCode = (string)row["个体编码"]; polylineModel.individualStage = (string)row["个体阶段"]; } } } polylineModel.Area = polyLine.Area; polylineModel.Closed = polyLine.Closed; polylineModel.Color = polyLine.ColorIndex == 256 ? MethodCommand.GetLayerColorByID(polyLine.LayerId) : System.Drawing.ColorTranslator.ToHtml(polyLine.Color.ColorValue); polylineModel.Vertices = new System.Collections.ArrayList(); int vn = polyLine.NumberOfVertices; //lwp已知的多段线 //if(polylineModel.Closed) //{ // for (int i = 0; i < vn; i++) // { // Point2d pt = polyLine.GetPoint2dAt(i); // PointF ptf = new PointF((float)pt.X, (float)pt.Y); // polylineModel.Vertices.Add(ptf); // } //} //else //{ for (int i = 0; i < vn; i++) { Point2d pt = polyLine.GetPoint2dAt(i); SegmentType st = polyLine.GetSegmentType(i); if (st == SegmentType.Arc) { ArcModel arc = new ArcModel(); CircularArc2d cir = polyLine.GetArcSegment2dAt(i); // arc.Center = new System.Drawing.PointF((float)cir.Center.X,(float)cir.Center.Y); arc.Center = Point2d2Pointf(cir.Center); arc.Radius = cir.Radius; arc.Startangel = cir.StartAngle; arc.EndAngel = cir.EndAngle; // arc.StartPoint = new System.Drawing.PointF((float)cir.StartPoint.X, (float)cir.StartPoint.Y); if (cir.HasStartPoint) { arc.StartPoint = Point2d2Pointf(cir.StartPoint); } // arc.EndPoint = new System.Drawing.PointF((float)cir.EndPoint.X, (float)cir.EndPoint.Y); if (cir.HasEndPoint) { arc.EndPoint = Point2d2Pointf(cir.EndPoint); } MyPoint spt = new MyPoint(arc.StartPoint.X, arc.StartPoint.Y); MyPoint ept = new MyPoint(arc.EndPoint.X, arc.EndPoint.Y); MyPoint center = new MyPoint(arc.Center.X, arc.Center.Y); arc.Color = polylineModel.Color; // arc.pointList = MethodCommand.GetRoationPoint(spt, ept, center, arc.Startangel,arc.EndAngel,cir.IsClockWise); arc.pointList = MethodCommand.GetArcPointsByPoint2d(cir.GetSamplePoints(20)); //arc.pointList = MethodCommand.GetArcPoints(arc.Center,arc.Startangel,arc.EndAngel,arc.Radius); // arc.pointList.Insert(0, arc.StartPoint); // arc.pointList.Add(arc.EndPoint); polylineModel.Vertices.Add(arc); } else if (st == SegmentType.Line) { LineModel line = new LineModel(); LineSegment2d lineSe = polyLine.GetLineSegment2dAt(i); if (lineSe.HasStartPoint) { line.StartPoint = Point2d2Pointf(lineSe.StartPoint); } if (lineSe.HasEndPoint) { line.EndPoint = Point2d2Pointf(lineSe.EndPoint); } if (line.StartPoint.X == line.EndPoint.X && line.StartPoint.Y == line.EndPoint.Y) { line.Angle = 0; line.Length = 0; } else if (line.StartPoint.X == line.EndPoint.X) { line.Angle = 90; } line.Color = polylineModel.Color; polylineModel.Vertices.Add(line); } } //} polylineModel.isDashed = GetLayerLineTypeByIDEx(polyLine); return(polylineModel); }