Advertisement
reset_man

разбивка

May 14th, 2024
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.69 KB | Source Code | 0 0
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Runtime;
  5. using Autodesk.AutoCAD.Geometry;
  6.  
  7. /*
  8. Faceting AutoCAD curves using .NET - Through the Interface
  9. https://www.keanw.com/2009/12/faceting-autocad-curves-using-net.html
  10. */
  11.  
  12. namespace Razbivka
  13. {
  14.     public class RazbivkaPlugin // : IExtensionApplication
  15.     {
  16.         [CommandMethod("RAZBIVKA")]
  17.         static public void Razbivka()
  18.         {
  19.             Document document = Application.DocumentManager.MdiActiveDocument;
  20.             Editor editor = document.Editor;
  21.             {
  22.                 PromptEntityResult per;
  23.                 {
  24.                     PromptEntityOptions peo = new PromptEntityOptions("\nВыберите объект разбивки: ");
  25.                     peo.SetRejectMessage(
  26.                       "\nКоманда работает с дугами, окружностями, сплайнами, полилиниями."
  27.                     );
  28.                     peo.AddAllowedClass(typeof(Curve), false);
  29.                     // Get a curve or a keyword
  30.                     per = editor.GetEntity(peo);
  31.                     if (per.Status == PromptStatus.OK)
  32.                     {
  33.                         // Now we facet our curve
  34.                         try
  35.                         {
  36.                             FacetCurve(document, per.ObjectId);
  37.                         }
  38.                         catch (Exception ex)
  39.                         {
  40.                             editor.WriteMessage(
  41.                               "\nОшибка при разбивке: {0}",
  42.                               ex
  43.                             );
  44.                         }
  45.                     }
  46.                 }
  47.             }
  48.  
  49.             void FacetCurve(Document doc, ObjectId curId)
  50.             {
  51.                 double segmentLength = 200;
  52.                 Database db = doc.Database;
  53.                 Editor ed = doc.Editor;
  54.                 Transaction tr = doc.TransactionManager.StartTransaction();
  55.                 using (tr)
  56.                 {
  57.                     // Open our curve
  58.                     DBObject obj = tr.GetObject(curId, OpenMode.ForRead);
  59.                     Curve cur = obj as Curve;
  60.                     if (cur != null)
  61.                     {
  62.                         // We'll gather the points along the curve in a collection
  63.                         Point3dCollection pts = null;
  64.                         {
  65.                             // "By fixed segment length" also needs some work. The algorithm uses planar intersection, so cannot work with non-planar curves
  66.                             if (!cur.IsPlanar)
  67.                             {
  68.                                 ed.WriteMessage("\nАлгоритм работает только на плоских объектах.");
  69.                                 return;
  70.                             }
  71.  
  72.                             // If planar, get the plane
  73.                             Plane p = cur.GetPlane();
  74.  
  75.                             // Initialize our results collection, add the 1st point
  76.                             pts = new Point3dCollection();
  77.                             pts.Add(cur.StartPoint);
  78.  
  79.                             // Loop along the length of the curve
  80.                             bool last = false;
  81.                             while (!last)
  82.                             {
  83.                                 // We check the intersection between the curve and a circle with the fixed segment length as its radius
  84.                                 Circle c = new Circle(pts[pts.Count - 1], p.Normal, /*razbivkaPlugin.FixedSegmentLength*/segmentLength);
  85.  
  86.                                 Point3dCollection intPts = new Point3dCollection();
  87.                                 cur.IntersectWith(c, Intersect.ExtendArgument, intPts, 0, 0);
  88.  
  89.                                 // We'll look for the closest of the intersection points to the base point
  90.                                 Point3d closest = new Point3d();
  91.  
  92.                                 if (intPts.Count < 1)
  93.                                 {
  94.                                     // Found no intersections: use the curve's end point
  95.                                     closest = cur.EndPoint;
  96.                                     last = true;
  97.                                 }
  98.                                 else
  99.                                 {
  100.                                     // Found one or more intersections: take the closest of them
  101.                                     double baseParam = cur.GetParameterAtPoint(pts[pts.Count - 1]);
  102.                                     double minParam = 999999; // Big number
  103.                                     bool found = false;
  104.  
  105.                                     foreach (Point3d pt in intPts)
  106.                                     {
  107.                                         // Check the point's parameter
  108.                                         double param = cur.GetParameterAtPoint(pt);
  109.  
  110.                                         // If it's larger than the base parameter but smaller than the minumum found so far, use it
  111.                                         if (param > baseParam && param < minParam)
  112.                                         {
  113.                                             minParam = param;
  114.                                             closest = pt;
  115.                                             found = true;
  116.  
  117.                                             // If it's the same as the curve's end point, no need to loop again
  118.                                             last = (pt == cur.EndPoint);
  119.                                         }
  120.                                     }
  121.  
  122.                                     // If we didn't find a close intersection, it means we're at the end. Use the curve's end-point for the last vertex
  123.                                     if (!found)
  124.                                     {
  125.                                         closest = cur.EndPoint;
  126.                                         last = true;
  127.                                     }
  128.                                 }
  129.                                 pts.Add(closest);
  130.                             }
  131.                         }
  132.  
  133.                         // Now we can go and create our lines/polyline
  134.                         if (pts != null && pts.Count > 0)
  135.                         {
  136.                             // Open the current space for write
  137.                             BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  138.                             BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  139.  
  140.                             //if (!ad.CreatePolyline || pts.Count <= 2)
  141.                             //if (pts.Count <= 2)
  142.                             {
  143.                                 // Create a sequence of line entities
  144.                                 if (pts.Count >= 2)
  145.                                 {
  146.                                     for (int i = 0; i < pts.Count - 1; i++)
  147.                                     {
  148.                                         Line ln = new Line();
  149.                                         ln.StartPoint = pts[i];
  150.                                         ln.EndPoint = pts[i + 1];
  151.                                         btr.AppendEntity(ln);
  152.                                         tr.AddNewlyCreatedDBObject(ln, true);
  153.                                     }
  154.                                 }
  155.                             }
  156.                         }
  157.                     }
  158.                     tr.Commit();
  159.                 }
  160.             }
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement