Author Topic: write polyline dimantion  (Read 1756 times)

0 Members and 2 Guests are viewing this topic.

A-SABER

  • Mosquito
  • Posts: 11
write polyline dimantion
« on: February 07, 2021, 10:48:22 PM »
in the following code, I try to write dimension for all segments, need hele to adjust the rotation angle for text in the segment line, and continue code in the segment  arc
Code - C#: [Select]
  1.  [CommandMethod("PLDIM")]
  2.         public void polylinewritedim()
  3.         {
  4.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;
  7.             using (Transaction trans = db.TransactionManager.StartTransaction())
  8.             {
  9.                 try
  10.                 {                    
  11.                     BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  12.                     BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  13.                     PromptDoubleOptions pdouble = new PromptDoubleOptions("\nEnter Text Highte : ");
  14.                     pdouble.AllowNegative = false;
  15.                     pdouble.AllowZero = false;
  16.                     pdouble.AllowNone = false;
  17.                     PromptDoubleResult pdoubelresulr = ed.GetDouble(pdouble);
  18.                     if (pdoubelresulr.Status != PromptStatus.OK)
  19.                     {
  20.                         return;
  21.                     }
  22.                     if (pdoubelresulr.Status == PromptStatus.OK)
  23.                     {
  24.                         double txth = pdoubelresulr.Value;
  25.                         TypedValue[] tv = new TypedValue[1];
  26.                         tv.SetValue(new TypedValue((int)DxfCode.Start, "LWPOLYLINE"), 0);
  27.                         SelectionFilter filter = new SelectionFilter(tv);
  28.                         PromptSelectionResult ssPrompt;
  29.                         ssPrompt = ed.GetSelection(filter);
  30.                         if (ssPrompt.Status == PromptStatus.OK)
  31.                         {
  32.                             SelectionSet ss = ssPrompt.Value;
  33.                            
  34.                             foreach (SelectedObject sObj in ss)
  35.                             {
  36.                                 Polyline poly = trans.GetObject(sObj.ObjectId, OpenMode.ForWrite) as Polyline;
  37.                                      Plane plan = poly.GetPlane();
  38.                                 for (int i = 0; i < poly.NumberOfVertices; i++)
  39.                                 {
  40.                                     if (poly.GetSegmentType(i) == SegmentType.Line)
  41.                                     {
  42.                                         LineSegment3d segmline = poly.GetLineSegmentAt(i);
  43.                                         Vector3d vec = segmline.Direction;
  44.                                         double angle = vec.AngleOnPlane(plan);
  45.                                         DBText dimtxtline = new DBText();
  46.                                         dimtxtline.SetDatabaseDefaults();
  47.                                         dimtxtline.Height = txth;
  48.                                         dimtxtline.Rotation = angle;
  49.                                         dimtxtline.TextString = $@"{segmline.Length:0.00}";
  50.                                         dimtxtline.Position = segmline.MidPoint;
  51.                                         btr.AppendEntity(dimtxtline);
  52.                                         trans.AddNewlyCreatedDBObject(dimtxtline, true);
  53.                                     }
  54.                                     else if (poly.GetSegmentType(i) == SegmentType.Arc)
  55.                                     {
  56.                                         CircularArc3d segmarc = poly.GetArcSegmentAt(i);
  57.                                         double angle = segmarc.EndAngle;
  58.                                        
  59.                                     }                        
  60.                                 }
  61.  
  62.                             }
  63.                         }
  64.                     }
  65.                     trans.Commit();
  66.                 }
  67.                 catch (System.Exception ex)
  68.                 {
  69.                     ed.WriteMessage("Error pls chek " + ex.Message);
  70.                     trans.Abort();
  71.                 }
  72.             }
  73.         }

pera

  • Mosquito
  • Posts: 3
Re: write polyline dimantion
« Reply #1 on: February 11, 2021, 02:44:52 PM »
You could try following code

Code - C#: [Select]
  1. [CommandMethod("PLDIM")]
  2. public void polylinewritedim()
  3. {
  4.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;
  7.             using (Transaction trans = db.TransactionManager.StartTransaction())
  8.             {
  9.                 try
  10.                 {
  11.                     BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  12.                     BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  13.                     PromptDoubleOptions pdouble = new PromptDoubleOptions("\nEnter Text Highte : ");
  14.                     pdouble.AllowNegative = false;
  15.                     pdouble.AllowZero = false;
  16.                     pdouble.AllowNone = false;
  17.                     PromptDoubleResult pdoubelresulr = ed.GetDouble(pdouble);
  18.                     if (pdoubelresulr.Status != PromptStatus.OK)
  19.                     {
  20.                         return;
  21.                     }
  22.                     if (pdoubelresulr.Status == PromptStatus.OK)
  23.                     {
  24.                         double txth = pdoubelresulr.Value;
  25.                         TypedValue[] tv = new TypedValue[1];
  26.                         tv.SetValue(new TypedValue((int)DxfCode.Start, "LWPOLYLINE"), 0);
  27.                         SelectionFilter filter = new SelectionFilter(tv);
  28.                         PromptSelectionResult ssPrompt;
  29.                         ssPrompt = ed.GetSelection(filter);
  30.                         if (ssPrompt.Status == PromptStatus.OK)
  31.                         {
  32.                             SelectionSet ss = ssPrompt.Value;
  33.  
  34.                             foreach (SelectedObject sObj in ss)
  35.                             {
  36.                                 Autodesk.AutoCAD.DatabaseServices. Polyline poly = trans.GetObject(sObj.ObjectId, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices. Polyline;
  37.                                 for (int i = 0; i < poly.NumberOfVertices; i++)
  38.                                 {
  39.                                     if (poly.GetSegmentType(i) == SegmentType.Line)
  40.                                     {
  41.                                         LineSegment3d segmline = poly.GetLineSegmentAt(i);
  42.                                         double angle = AngleFromXAxis(segmline.StartPoint, segmline.EndPoint);
  43.                                         MText dimtxtline = new MText();
  44.                                         dimtxtline.Attachment = AttachmentPoint.MiddleCenter;
  45.                                         dimtxtline.TextHeight = txth;
  46.                                         dimtxtline.Rotation = angle;
  47.                                         dimtxtline.Contents = $@"{segmline.Length:0.00}";
  48.                                         dimtxtline.Location = segmline.MidPoint;
  49.                                         btr.AppendEntity(dimtxtline);
  50.                                         trans.AddNewlyCreatedDBObject(dimtxtline, true);
  51.                                     }
  52.                                     else if (poly.GetSegmentType(i) == SegmentType.Arc)
  53.                                     {
  54.                                         CircularArc3d segmArc = poly.GetArcSegmentAt(i);
  55.                                         Curve curve = Curve.CreateFromGeCurve(segmArc);
  56.                                         double arcLen = CurveLength(curve);
  57.                                         double dist2Middle = arcLen / 2;
  58.                                         Point3d midPt = GetMidpoint(curve);
  59.  
  60.                                         MText mt = new MText();
  61.                                         mt.Contents = $@"R{segmArc.Radius:0.00}";
  62.                                         mt.TextHeight = txth;
  63.                                         mt.Height = txth;
  64.                                         Point3d location = PolarPoint(segmArc.Center, AngleFromXAxis(segmArc.Center, midPt), segmArc.Radius);
  65.                                         mt.Location = location;
  66.                                         mt.Attachment = AttachmentPoint.MiddleCenter;
  67.  
  68.                                         double dist1 = mt.ActualWidth / 2; double dist2 = mt.ActualWidth / 2; //helper to find points for text rotation near mid point of arc
  69.  
  70.                                         if (dist2Middle - dist1 < 0) dist1 = dist2Middle;
  71.                                         if (dist2Middle + dist2 > arcLen) dist2 = arcLen - dist2Middle;
  72.                                         Point3d p1 = curve.GetPointAtDist(dist2Middle - dist1);
  73.                                         Point3d p2 = curve.GetPointAtDist(dist2Middle + dist2);
  74.  
  75.                                         double angle = AngleFromXAxis(p1, p2);
  76.                                         //adjust angle for text to be "most readable"
  77.                                         if (angle > Math.PI && angle < 3 * Math.PI / 2) angle -= Math.PI;
  78.                                         else if (angle > Math.PI / 2 && angle <= Math.PI) angle += Math.PI;
  79.                                         mt.Rotation = angle;
  80.  
  81.                                         btr.AppendEntity(mt);
  82.                                         trans.AddNewlyCreatedDBObject(mt, true);
  83.                                     }
  84.                                 }
  85.                             }
  86.                         }
  87.                     }
  88.                     trans.Commit();
  89.                 }
  90.                 catch (System.Exception ex)
  91.                 {
  92.                     ed.WriteMessage("Error pls chek " + ex.Message);
  93.                     trans.Abort();
  94.                 }
  95.             }
  96. }
  97.  
  98. Point3d GetMidpoint(Curve curve) //https://forums.autodesk.com/t5/net/mid-point-on-arc/td-p/2720851
  99. {
  100.         double d1 = curve.GetDistanceAtParameter(curve.StartParam);
  101.         double d2 = curve.GetDistanceAtParameter(curve.EndParam);
  102.         return curve.GetPointAtDist(d1 + ((d2 - d1) / 2.0));
  103. }
  104. public double AngleFromXAxis(Point3d p1, Point3d p2) //TT
  105. {
  106.         return new Vector2d(p2.X - p1.X, p2.Y - p1.Y).Angle;
  107. }
  108. public double CurveLength(Curve curve)
  109. {
  110.        double d1 = curve.GetDistanceAtParameter(curve.StartParam);
  111.        double d2 = curve.GetDistanceAtParameter(curve.EndParam);
  112.  
  113.        return d2 - d1;
  114. }
  115. public Point3d PolarPoint(Point3d basepoint, double angle, double distance) //TT
  116. {
  117.        return new Point3d(basepoint.X + (distance * Math.Cos(angle)), basepoint.Y + (distance * Math.Sin(angle)), basepoint.Z);
  118. }
  119.