Author Topic: Limit Pline Lengh  (Read 1350 times)

0 Members and 1 Guest are viewing this topic.

djee

  • Newt
  • Posts: 49
Limit Pline Lengh
« on: September 24, 2015, 04:36:24 PM »
I would like to create a utility that would limit a pline lengh to a pre-determined value... Being a novice i'm not sure where to start...? Should I look at overule, jigs or something else? Any suggestion...?

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Limit Pline Lengh
« Reply #1 on: September 24, 2015, 06:57:54 PM »
Hi,

Maybe you can get some inspiration from the following code I wrote sometimes ago to simulate a fire protection system range of action (réseau incendie armée (RIA) in French).



Code - C#: [Select]
  1.         private double _maxLength = 35.0;
  2.  
  3.         [CommandMethod("RIA", CommandFlags.Modal)]
  4.         public void Ria()
  5.         {
  6.             Document doc = AcAp.DocumentManager.MdiActiveDocument;
  7.             Database db = doc.Database;
  8.             Editor ed = doc.Editor;
  9.  
  10.             PromptDistanceOptions pdo =
  11.                 new PromptDistanceOptions("\nSpecify maximum length: ");
  12.             pdo.AllowNegative = false;
  13.             pdo.AllowNone = true;
  14.             pdo.AllowZero = false;
  15.             pdo.DefaultValue = _maxLength;
  16.             pdo.UseDefaultValue = true;
  17.             PromptDoubleResult pdr = ed.GetDistance(pdo);
  18.             if (pdr.Status != PromptStatus.OK)
  19.                 return;
  20.             _maxLength = pdr.Value;
  21.  
  22.             PromptPointOptions ppo = new PromptPointOptions("\nSpecify start point: ");
  23.             PromptPointResult ppr = ed.GetPoint(ppo);
  24.             if (ppr.Status != PromptStatus.OK)
  25.                 return;
  26.  
  27.             Matrix3d ucsMat = ed.CurrentUserCoordinateSystem;
  28.             CoordinateSystem3d ucs = ucsMat.CoordinateSystem3d;
  29.             Point3d startPt = ppr.Value.TransformBy(ucsMat);
  30.             Plane plane = new Plane(Point3d.Origin, ucs.Zaxis);
  31.             using (Transaction tr = db.TransactionManager.StartTransaction())
  32.             {
  33.                 BlockTableRecord space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  34.                 Polyline pline = new Polyline();
  35.                 pline.Normal = ucs.Zaxis;
  36.                 pline.Elevation = Point3d.Origin.TransformBy(ucsMat).Z;
  37.                 pline.AddVertexAt(0, startPt.Convert2d(plane), 0.0, 0.0, 0.0);
  38.                 pline.AddVertexAt(1, startPt.Convert2d(plane), 0.0, 0.0, 0.0);
  39.                 pline.ColorIndex = 8;
  40.                 space.AppendEntity(pline);
  41.                 tr.AddNewlyCreatedDBObject(pline, true);
  42.                 Circle circle = new Circle(ppr.Value.TransformBy(ucsMat), ucs.Zaxis, _maxLength);
  43.                 circle.ColorIndex = 8;
  44.                 space.AppendEntity(circle);
  45.                 tr.AddNewlyCreatedDBObject(circle, true);
  46.                 db.TransactionManager.QueueForGraphicsFlush();
  47.                 while (true)
  48.                 {
  49.                     RiaJig jig = new RiaJig(pline, circle, _maxLength);
  50.                     PromptResult pr = ed.Drag(jig);
  51.                     if (pr.Status == PromptStatus.Cancel)
  52.                     {
  53.                         pline.Erase();
  54.                         circle.Erase();
  55.                         break;
  56.                     }
  57.                     if (pr.Status != PromptStatus.OK)
  58.                     {
  59.                         pline.RemoveVertexAt(pline.NumberOfVertices - 1);
  60.                         circle.Erase();
  61.                         pline.ColorIndex = 256;
  62.                         break;
  63.                     }
  64.                     if (_maxLength - pline.Length <= 1e-12)
  65.                     {
  66.                         circle.Erase();
  67.                         pline.ColorIndex = 256;
  68.                         break;
  69.                     }
  70.                     pline.AddVertexAt(pline.NumberOfVertices, pline.EndPoint.Convert2d(plane), 0.0, 0.0, 0.0);
  71.                 }
  72.                 tr.Commit();
  73.             }
  74.         }
  75.  
  76.         class RiaJig : DrawJig
  77.         {
  78.             private Circle circle;
  79.             private Polyline pline;
  80.             private double maxRad;
  81.             Point3d dragPt;
  82.             Plane plane;
  83.  
  84.             public RiaJig(Polyline pline, Circle circle, double maxRad)
  85.             {
  86.                 this.pline = pline;
  87.                 this.circle = circle;
  88.                 this.maxRad = maxRad;
  89.                 dragPt = circle.Center;
  90.                 plane = new Plane(Point3d.Origin, pline.Normal);
  91.             }
  92.  
  93.             protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
  94.             {
  95.                 Autodesk.AutoCAD.GraphicsInterface.WorldGeometry geo = draw.Geometry;
  96.                 if (geo != null)
  97.                 {
  98.                     geo.PushModelTransform(Matrix3d.Identity);
  99.                     geo.Draw(circle);
  100.                     geo.Draw(pline);
  101.                     geo.PopModelTransform();
  102.                 }
  103.                 return true;
  104.             }
  105.  
  106.             protected override SamplerStatus Sampler(JigPrompts prompts)
  107.             {
  108.                 JigPromptPointOptions opts =
  109.                     new JigPromptPointOptions("\nSpecify next point: ");
  110.                 opts.UserInputControls = UserInputControls.NullResponseAccepted;
  111.                 PromptPointResult ppr = prompts.AcquirePoint(opts);
  112.                 if (ppr.Value.DistanceTo(dragPt) < Tolerance.Global.EqualPoint)
  113.                     return SamplerStatus.NoChange;
  114.                 dragPt = ppr.Value;
  115.                 Update();
  116.                 return SamplerStatus.OK;
  117.             }
  118.  
  119.             private void Update()
  120.             {
  121.                 circle.Center = dragPt;
  122.                 circle.Radius = Math.Max(maxRad - pline.Length, 1e-12);
  123.                 int nov = pline.NumberOfVertices;
  124.                 Point2d pt = dragPt.Convert2d(plane);
  125.                 pline.SetPointAt(nov - 1, pt);
  126.                 if (pline.Length > maxRad)
  127.                 {
  128.                     Point2d lastPt = pline.GetPoint2dAt(nov - 2);
  129.                     Vector2d dir = (pt - lastPt).GetNormal();
  130.                     pline.SetPointAt(nov - 1, lastPt + dir * (maxRad - pline.GetDistanceAtParameter(nov - 2)));
  131.                 }
  132.             }
  133.         }
« Last Edit: September 24, 2015, 07:08:03 PM by gile »
Speaking English as a French Frog

djee

  • Newt
  • Posts: 49
Re: Limit Pline Lengh
« Reply #2 on: September 24, 2015, 07:41:02 PM »
Did you say "Inspired"... You just slam it right there!!! Very impressed with your approach! Now I'll have to dissect & understand... Thanks a bunch!  :-D