Author Topic: draw line with angle  (Read 1544 times)

0 Members and 1 Guest are viewing this topic.

A-SABER

  • Mosquito
  • Posts: 11
draw line with angle
« on: February 02, 2021, 10:34:34 AM »
in this example, I make 2 double arrays for dist and angle. I try to draw it but the result is wrong cant draw with an interior angle
need help for draw this

Code - C#: [Select]
  1. PromptPointOptions inspoint = new PromptPointOptions("\nSpecify insertion point : ");
  2.                     PromptPointResult presult = ed.GetPoint(inspoint);
  3.                     Point2d pinsert = new Point2d(presult.Value.X, presult.Value.Y);
  4.                     Polyline poly2 = new Polyline();
  5.                     poly2.AddVertexAt(0, pinsert, 0, 0, 0);
  6.                     Point2d p2, pr;
  7.                     Point3d p3a, p3b;
  8.                     double angconvert;
  9.                     double   calc1, calc2, leang;  
  10. for (int i = 0; i < dangle.Count(); i++)
  11.                     {
  12.                         angconvert =  (dangle[i] * 0.0174532925);                                        
  13.                         calc1 =Math.Abs( dline[i] * Math.Cos(angconvert));
  14.                         calc2 = Math.Abs( dline[i] * Math.Sin(angconvert));
  15.                         pr = poly2.GetPoint2dAt(i);
  16.                         if (dangle[i]<=90 && dangle[i]>0)
  17.                         {
  18.                             p2 = new Point2d(pr.X + calc1, pr.Y + calc2);
  19.                         }
  20.                         else if (dangle[i] <= 180 && dangle[i] > 90)
  21.                         {
  22.                             p2 = new Point2d(pr.X - calc1, pr.Y + calc2);
  23.                         }
  24.                         else if (dangle[i] <= 270 && dangle[i] > 180)
  25.                         {
  26.                             p2 = new Point2d(pr.X - calc1, pr.Y - calc2);
  27.                         }
  28.                         else
  29.                         {
  30.                             p2 = new Point2d(pr.X + calc1, pr.Y - calc2);
  31.                         }
  32.  
  33.                         poly2.AddVertexAt(i + 1, p2, 0, 0, 0);                      
  34.                        
  35.                     }
  36.                   poly2.Closed = true;
  37.                     btr.AppendEntity(poly2);
  38.                 trans.AddNewlyCreatedDBObject(poly2, true);
  39.                    
  40.                     trans.Commit();
  41.                 }

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: draw line with angle
« Reply #1 on: February 02, 2021, 04:19:41 PM »
Give this a try:
Code - C#: [Select]
  1.         [CommandMethod("drawPlineTest")]
  2.         public void drawpline()
  3.         {
  4.             double[] dangle = { 147.27, 218.65, 54.49, 120.02, 300.87, 64.39, 66.02 };
  5.             double[] dline = { 11.96, 8.16, 29.09, 22.93, 5.64, 17.06, 39.6 };
  6.             var doc = Application.DocumentManager.MdiActiveDocument;
  7.             var ed = doc.Editor;
  8.             PromptPointOptions inspoint = new PromptPointOptions("\nSpecify insertion point : ");
  9.             PromptPointResult presult = ed.GetPoint(inspoint);
  10.             Point2d p1 = new Point2d(presult.Value.X, presult.Value.Y);
  11.             Polyline poly2 = new Polyline();
  12.             poly2.AddVertexAt(0, p1, 0, 0, 0);
  13.             Point2d nextPt = Point2d.Origin;
  14.             var lastDir = double.NaN; ;
  15.             for (int i = 0; i < dangle.Count(); i++)
  16.             {
  17.                 var ang = dangle[i] * Math.PI / 180.0;
  18.                 if (nextPt == Point2d.Origin)
  19.                 {
  20.                     nextPt = GetPolarPoint(p1, ang, dline[i]);
  21.                     lastDir = ang;
  22.                 }
  23.                 else
  24.                 {
  25.                     try
  26.                     {
  27.                         lastDir = lastDir - Math.PI + (dangle[i] * Math.PI / 180.0);
  28.                         nextPt = GetPolarPoint(nextPt, lastDir, dline[i]);
  29.                     }
  30.                     catch { }
  31.                 }
  32.                 poly2.AddVertexAt(i + 1, nextPt, 0, 0, 0);
  33.  
  34.             }
  35.             poly2.Closed = true;
  36.             using (Transaction trans = doc.Database.TransactionManager.StartTransaction())
  37.             {
  38.                 var bt = (BlockTable)trans.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
  39.                 var btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  40.                 btr.AppendEntity(poly2);
  41.                 trans.AddNewlyCreatedDBObject(poly2, true);
  42.  
  43.                 trans.Commit();
  44.             }
  45.  
  46.         }
  47.    
  48.  
  49.         /// Calculates a new point, using the specified angle and distance.  Angle should be specified
  50.         /// counter-clockwise from the X-axis.
  51.         /// </summary>
  52.         public Point2d GetPolarPoint(Point2d ptBase, double angle, double distance)
  53.         {
  54.             return new Point2d(ptBase.X + (distance * Math.Cos(angle)), ptBase.Y + (distance * Math.Sin(angle)));
  55.         }
  56.