public static IEnumerable <Point3d> GetPointsFromPolyline([NotNull] this Database database, ObjectId id) { using (OpenCloseTransaction transAction = database.TransactionManager.StartOpenCloseTransaction()) { object objectX = transAction.GetObject(id, OpenMode.ForRead); switch (objectX.GetType().ToString()) { case "Autodesk.AutoCAD.DatabaseServices.Polyline3d": { //Get a list of 3D points in parameter order using (Polyline3d pl1 = (Polyline3d)objectX) { // We get curve here, its faster to get the // parameters and point on a non database object var curve = pl1.GetGeCurve(new Tolerance(1e-10, 1e-10)); var polylineEndParam = (int)pl1.EndParam; // Initialize list with dimensions specified (speed) Point3d[] arrRes = new Point3d[polylineEndParam + 1]; for (int i = 0; i <= polylineEndParam; i++) { var distOnPl = pl1.GetDistanceAtParameter(i); arrRes[i] = curve.EvaluatePoint(curve.GetParameterAtLength(0, distOnPl, true, 1e-10)); } transAction.Commit(); return(arrRes); } } default: throw new Exception("\r\n\t=> Selected object is not supported for this function."); } } }
public static IEnumerable <DistanceBetween2PolylinesSectionResult> CalculateDistanceBetween3dPolylines(Database database, ObjectId pl1Id, ObjectId pl2Id) { Curve3d curve1, curve2 = null; int polyline1EndParam; Point3d[] parameterListPoints = null; double[] parameterListLength = null; using (Transaction transAction = database.TransactionManager.StartTransaction()) { object objectX1 = transAction.GetObject(pl1Id, OpenMode.ForRead); object objectX2 = transAction.GetObject(pl2Id, OpenMode.ForRead); //Get a list of 3D points in parameter order using (Polyline3d pl1 = (Polyline3d)objectX1) using (Polyline3d pl2 = (Polyline3d)objectX2) { // We get curve here, its faster to get the parameters and // point on a non database object curve1 = pl1.GetGeCurve(new Tolerance(1e-10, 1e-10)); curve2 = pl2.GetGeCurve(new Tolerance(1e-10, 1e-10)); polyline1EndParam = (int)pl1.EndParam; parameterListPoints = new Point3d[polyline1EndParam + 1]; parameterListLength = new double[polyline1EndParam + 1]; for (int y = 0; y <= polyline1EndParam; y++) { parameterListPoints[y] = pl1.GetPointAtParameter(y); parameterListLength[y] = pl1.GetDistanceAtParameter(y); } } // Initialize list with dimensions specified (speed) DistanceBetween2PolylinesSectionResult[] arrRes = new DistanceBetween2PolylinesSectionResult[polyline1EndParam + 1]; //for (int i = 0; i <= polyline1EndParam; i++) Parallel.For(0, polyline1EndParam + 1, (i) => { Point3d pointOnCurve1, pointOnCurve2; lock (_curveLock) { pointOnCurve1 = parameterListPoints[i]; pointOnCurve2 = curve2.GetClosestPointTo(pointOnCurve1).Point; } // Add result to array lock (_arrayLock) arrRes[i] = new DistanceBetween2PolylinesSectionResult( parameterListLength[i], pointOnCurve1.Z - pointOnCurve2.Z, pointOnCurve1.T2d().GetDistanceTo(pointOnCurve2.T2d()), pointOnCurve1.DistanceTo(pointOnCurve2), pointOnCurve1, pointOnCurve2); }); transAction.Commit(); return(arrRes); } }
private int getPolylineCount(ObjectId[] objectId_0) { int num = 0; if (objectId_0 != null) { using (Transaction transaction = this.database_0.TransactionManager.StartTransaction()) { for (int i = 0; i < objectId_0.Length; i++) { DBObject @object = transaction.GetObject(objectId_0[i], (OpenMode)0); Polyline2d polyline2d = @object as Polyline2d; Polyline polyline = @object as Polyline; Polyline3d polyline3d = @object as Polyline3d; if (polyline2d != null) { if (!polyline2d.Closed) { throw new ArgumentException("At least one boundary polyline is not closed.\n"); } double num2 = 0.0; List <Edge> list = Conversions.ToCeometricEdgeList(polyline2d); for (int j = 0; j < list.Count; j++) { num2 += list[j].Length; } double distanceAtParameter = polyline2d.GetDistanceAtParameter(polyline2d.EndParam); if (!Global.AlmostEquals(num2, distanceAtParameter)) { throw new System.Exception("At least one boundary polyline contains curved segments. Decurve the polyline first.\n"); } num++; } else if (polyline != null) { if (!polyline.Closed) { throw new ArgumentException("At least one boundary polyline is not closed.\n"); } if (!polyline.IsOnlyLines) { throw new System.Exception("At least one boundary polyline contains curved segments. Decurve the polyline first.\n"); } num++; } else { if (!(polyline3d != null)) { throw new ArgumentException("Invalid polyline object: " + @object.Handle.ToString() + "\nObject type: " + @object.GetType().ToString()); } if (!polyline3d.Closed) { throw new ArgumentException("At least one boundary polyline is not closed.\n"); } double num3 = 0.0; List <Edge> list2 = Conversions.ToCeometricEdgeList(polyline3d); for (int k = 0; k < list2.Count; k++) { num3 += list2[k].Length; } double distanceAtParameter2 = polyline3d.GetDistanceAtParameter(polyline3d.EndParam); if (!Global.AlmostEquals(num3, distanceAtParameter2)) { throw new System.Exception("At least one boundary polyline contains curved segments. Decurve the polyline first.\n"); } num++; } } } } return(num); }
private void btn_CADtoKML_Click(object sender, EventArgs e) { Editor ed = cadSer.Application.DocumentManager.MdiActiveDocument.Editor; //两直线合并误差 double error_len = Convert.ToDouble(tbx_minLineSegmentLength.Text.Trim()); try { double centerLongitude = Convert.ToDouble(tbx_CentrelBL.Text.Trim()); bool is80 = cbx_Is80.Checked; double estDelta = Convert.ToDouble(tbx_EastDelta.Text); double northDelta = Convert.ToDouble(tbx_NorthDelta.Text); Database db = HostApplicationServices.WorkingDatabase; var middoc = cadSer.Application.DocumentManager.MdiActiveDocument; List <LineStringKML> lineListKML = new List <LineStringKML>(); List <TextKML> textListKML = new List <TextKML>(); ObjectId[] objectIdsArr = ed.GetSelection().Value.GetObjectIds(); ed.WriteMessage("==================================================\n"); // ed.WriteMessage(objectIds.Count.ToString() + "\n"); var doc_lock = middoc.LockDocument(); DBObject dbo; //提取polyline line spline circel arc ellipse polyline3d mtext dbtext foreach (ObjectId tempObjectId in objectIdsArr) { using (Transaction trans = db.TransactionManager.StartTransaction()) { dbo = trans.GetObject(tempObjectId, OpenMode.ForRead); #region 所有类型 //if (dbo.GetType().Equals(typeof(Polyline)) || dbo.GetType().Equals(typeof(Line)) || dbo.GetType().Equals(typeof(Spline)) || dbo.GetType().Equals(typeof(Circle)) || dbo.GetType().Equals(typeof(Arc)) || dbo.GetType().Equals(typeof(Ellipse)) || dbo.GetType().Equals(typeof(Polyline3d))) //{ // Curve dbo_bR = (Curve)dbo; // double length = dbo_bR.GetDistanceAtParameter(dbo_bR.EndParam); // LineStringKML lineStringKML = new LineStringKML(); // for (double i = 0; i <length; i+=error_len) // { // Point3d tmpPoint = dbo_bR.GetPointAtDist(i); // lineStringKML.Add(new zPointXY(tmpPoint.X, tmpPoint.Y)); // } // Point3d tmpPoint_1 = dbo_bR.GetPointAtParameter(dbo_bR.EndParam); // lineStringKML.Add(new zPointXY(tmpPoint_1.X, tmpPoint_1.Y)); // lineListKML.Add(lineStringKML); //} #endregion if (dbo.GetType().Equals(typeof(Line))) { Curve dbo_bR = (Curve)dbo; LineStringKML lineStringKML = new LineStringKML(); Point3d tmpPoint_0 = dbo_bR.StartPoint; lineStringKML.Add(new zPointXY(tmpPoint_0.X, tmpPoint_0.Y)); Point3d tmpPoint_1 = dbo_bR.EndPoint; lineStringKML.Add(new zPointXY(tmpPoint_1.X, tmpPoint_1.Y)); lineListKML.Add(lineStringKML); } if (dbo.GetType().Equals(typeof(Polyline))) { Polyline dbo_bR = (Polyline)dbo; LineStringKML lineStringKML = new LineStringKML(); // foreach(var tmp in dbo_bR.para) double length = dbo_bR.GetDistanceAtParameter(dbo_bR.EndParam); for (int i = 0; i < dbo_bR.EndParam; i++) { // Point3d tmpPoint = dbo_bR.GetPointAtParameter(i); if (dbo_bR.GetSegmentType(i) == SegmentType.Line) { Point3d tmpPoint_0 = dbo_bR.GetPointAtParameter(i); lineStringKML.Add(new zPointXY(tmpPoint_0.X, tmpPoint_0.Y)); } else if (dbo_bR.GetSegmentType(i) == SegmentType.Arc) { for (double isegment = i; isegment < i + 1; isegment += 0.05) { Point3d tmpPoint_0 = dbo_bR.GetPointAtParameter(isegment); lineStringKML.Add(new zPointXY(tmpPoint_0.X, tmpPoint_0.Y)); } } } Point3d tmpPoint_1 = dbo_bR.GetPointAtParameter(dbo_bR.EndParam); lineStringKML.Add(new zPointXY(tmpPoint_1.X, tmpPoint_1.Y)); if (dbo_bR.Closed) { Point3d tmpPoint_0 = dbo_bR.GetPointAtParameter(dbo_bR.StartParam); lineStringKML.Add(new zPointXY(tmpPoint_0.X, tmpPoint_0.Y)); } lineListKML.Add(lineStringKML); } if (dbo.GetType().Equals(typeof(Circle)) || dbo.GetType().Equals(typeof(Arc)) || dbo.GetType().Equals(typeof(Ellipse))) { Curve dbo_bR = (Curve)dbo; double length = dbo_bR.GetDistanceAtParameter(dbo_bR.EndParam); LineStringKML lineStringKML = new LineStringKML(); for (double i = dbo_bR.StartParam; i < dbo_bR.EndParam; i += (dbo_bR.EndParam - dbo_bR.StartParam) / 20.0) { Point3d tmpPoint = dbo_bR.GetPointAtParameter(i); lineStringKML.Add(new zPointXY(tmpPoint.X, tmpPoint.Y)); } Point3d tmpPoint_1 = dbo_bR.GetPointAtParameter(dbo_bR.EndParam); lineStringKML.Add(new zPointXY(tmpPoint_1.X, tmpPoint_1.Y)); if (dbo_bR.Closed) { Point3d tmpPoint_0 = dbo_bR.GetPointAtParameter(dbo_bR.StartParam); lineStringKML.Add(new zPointXY(tmpPoint_0.X, tmpPoint_0.Y)); } lineListKML.Add(lineStringKML); } if (dbo.GetType().Equals(typeof(Polyline3d))) { Polyline3d dbo_bR = (Polyline3d)dbo; LineStringKML lineStringKML = new LineStringKML(); if (dbo_bR.PolyType == Poly3dType.SimplePoly) { for (int i = 0; i < dbo_bR.EndParam; i++) { Point3d tmpPoint_0 = dbo_bR.GetPointAtParameter(i); lineStringKML.Add(new zPointXY(tmpPoint_0.X, tmpPoint_0.Y)); } } else { double length = dbo_bR.GetDistanceAtParameter(dbo_bR.EndParam); for (double i = 0; i < length; i += error_len) { Point3d tmpPoint = dbo_bR.GetPointAtDist(i); lineStringKML.Add(new zPointXY(tmpPoint.X, tmpPoint.Y)); } } Point3d tmpPoint_1 = dbo_bR.GetPointAtParameter(dbo_bR.EndParam); lineStringKML.Add(new zPointXY(tmpPoint_1.X, tmpPoint_1.Y)); if (dbo_bR.Closed) { Point3d tmpPoint_0 = dbo_bR.GetPointAtParameter(dbo_bR.StartParam); lineStringKML.Add(new zPointXY(tmpPoint_0.X, tmpPoint_0.Y)); } lineListKML.Add(lineStringKML); } if (dbo.GetType().Equals(typeof(Spline))) { Spline dbo_bR = (Spline)dbo; double length = dbo_bR.GetDistanceAtParameter(dbo_bR.EndParam); LineStringKML lineStringKML = new LineStringKML(); for (double i = 0; i < length; i += error_len) { Point3d tmpPoint = dbo_bR.GetPointAtDist(i); lineStringKML.Add(new zPointXY(tmpPoint.X, tmpPoint.Y)); } Point3d tmpPoint_1 = dbo_bR.GetPointAtParameter(dbo_bR.EndParam); lineStringKML.Add(new zPointXY(tmpPoint_1.X, tmpPoint_1.Y)); if (dbo_bR.Closed) { Point3d tmpPoint_0 = dbo_bR.GetPointAtParameter(dbo_bR.StartParam); lineStringKML.Add(new zPointXY(tmpPoint_0.X, tmpPoint_0.Y)); } lineListKML.Add(lineStringKML); } if (dbo.GetType().Equals(typeof(DBText))) { DBText dbtext = (DBText)dbo; // string name = dbtext.TextString; // Point3d point3D = dbtext.Position; TextKML tmpkml = new TextKML(dbtext.TextString, new zPointXY(dbtext.Position.X, dbtext.Position.Y)); textListKML.Add(tmpkml); } if (dbo.GetType().Equals(typeof(MText))) { MText dbtext = (MText)dbo; // string name = dbtext.Text; //Point3d point3D = dbtext.Location; TextKML tmpkml = new TextKML(dbtext.Text.Replace('\r', ' '), new zPointXY(dbtext.Location.X, dbtext.Location.Y)); textListKML.Add(tmpkml); } trans.Commit(); } } doc_lock.Dispose(); ed.WriteMessage("===============读取完成=======================\n"); //foreach(var tmp in lineListKML ) //{ // zjyCAD.zPolylineCreate(tmp.pointList,"zjy_assiaaaaaaaaaaaa"); //} //foreach(var tmp in textListKML) //{ // zjyCAD.zCreatText(tmp.text, tmp.pointXY, 10, 0.8, "zjy_assiaaaaaaaaaaaa"); //} string pmPath = tbx_SaveKMLPath.Text.Trim(); List <string> xmlhead = new List <string>() { "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", // "<kml xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:atom=\"http://www.w3.org/2005/Atom\">", " <kml xmlns=\"http://earth.google.com/kml/2.1\">", "<Document>", "<name>zjy</name>", // "<open>1</open>", // "<description>zjy生成</description>", "<Style id=\"yellowLineGreenPoly\" >", " <LineStyle>", " <color>ff000000</color>", " <width>4</width>", " </LineStyle>", " </Style>", "<Folder>", "<name>zjy</name>", " <visibility>1</visibility>", }; List <string> xmlend = new List <string>() { "</Folder>", "</Document>", "</kml>" }; //生成kml文件 List <string> kmlFile = new List <string>(); kmlFile.AddRange(xmlhead); //生成路线信息图像 foreach (var tmp in lineListKML) { List <string> roadList = new List <string>() { " <Placemark>", " <styleUrl>#yellowLineGreenPoly</styleUrl>", " <LineString>", " <tessellate>1</tessellate>", " <coordinates>" }; foreach (zPointXY linePoint in tmp.pointList) { zPointXY xy = linePoint; if (is80) { xy = linePoint + new zPointXY(estDelta, northDelta); } zPointXY tmp_BL = zjyCAD.WGS84_XYToBL(xy, centerLongitude); roadList.Add(" " + tmp_BL.y.ToString() + "," + tmp_BL.x.ToString() + ",0.0000"); } roadList.Add(" </coordinates>"); roadList.Add(" </LineString>"); roadList.Add(" </Placemark>"); kmlFile.AddRange(roadList); } //生成标签信息 List <string> roadBQList = new List <string>(); foreach (var tmp in textListKML) { zPointXY xy = tmp.pointXY; if (is80) { xy = tmp.pointXY + new zPointXY(estDelta, northDelta); } zPointXY tmp_BL = zjyCAD.WGS84_XYToBL(xy, centerLongitude); roadBQList.Add(" <Placemark>"); roadBQList.Add(" <name>" + tmp.text + "</name>"); roadBQList.Add(" <Point>"); roadBQList.Add(" <coordinates>" + tmp_BL.y.ToString() + "," + tmp_BL.x.ToString() + ",0.0000" + "</coordinates>"); roadBQList.Add(" </Point>"); // roadBQList.Add(" <markerStyle>-2</markerStyle>"); roadBQList.Add(" </Placemark>"); } kmlFile.AddRange(roadBQList); kmlFile.AddRange(xmlend); if (!Directory.Exists(pmPath)) { Directory.CreateDirectory(pmPath); } File.WriteAllLines(pmPath + "/" + tbx_KMLfileName.Text.Trim(), kmlFile.ToArray()); } catch { ed.WriteMessage("出错\n"); } }