Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Geometry;
- /*
- Faceting AutoCAD curves using .NET - Through the Interface
- https://www.keanw.com/2009/12/faceting-autocad-curves-using-net.html
- */
- namespace Razbivka
- {
- public class RazbivkaPlugin // : IExtensionApplication
- {
- [CommandMethod("RAZBIVKA")]
- static public void Razbivka()
- {
- Document document = Application.DocumentManager.MdiActiveDocument;
- Editor editor = document.Editor;
- {
- PromptEntityResult per;
- {
- PromptEntityOptions peo = new PromptEntityOptions("\nВыберите объект разбивки: ");
- peo.SetRejectMessage(
- "\nКоманда работает с дугами, окружностями, сплайнами, полилиниями."
- );
- peo.AddAllowedClass(typeof(Curve), false);
- // Get a curve or a keyword
- per = editor.GetEntity(peo);
- if (per.Status == PromptStatus.OK)
- {
- // Now we facet our curve
- try
- {
- FacetCurve(document, per.ObjectId);
- }
- catch (Exception ex)
- {
- editor.WriteMessage(
- "\nОшибка при разбивке: {0}",
- ex
- );
- }
- }
- }
- }
- void FacetCurve(Document doc, ObjectId curId)
- {
- double segmentLength = 200;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- Transaction tr = doc.TransactionManager.StartTransaction();
- using (tr)
- {
- // Open our curve
- DBObject obj = tr.GetObject(curId, OpenMode.ForRead);
- Curve cur = obj as Curve;
- if (cur != null)
- {
- // We'll gather the points along the curve in a collection
- Point3dCollection pts = null;
- {
- // "By fixed segment length" also needs some work. The algorithm uses planar intersection, so cannot work with non-planar curves
- if (!cur.IsPlanar)
- {
- ed.WriteMessage("\nАлгоритм работает только на плоских объектах.");
- return;
- }
- // If planar, get the plane
- Plane p = cur.GetPlane();
- // Initialize our results collection, add the 1st point
- pts = new Point3dCollection();
- pts.Add(cur.StartPoint);
- // Loop along the length of the curve
- bool last = false;
- while (!last)
- {
- // We check the intersection between the curve and a circle with the fixed segment length as its radius
- Circle c = new Circle(pts[pts.Count - 1], p.Normal, /*razbivkaPlugin.FixedSegmentLength*/segmentLength);
- Point3dCollection intPts = new Point3dCollection();
- cur.IntersectWith(c, Intersect.ExtendArgument, intPts, 0, 0);
- // We'll look for the closest of the intersection points to the base point
- Point3d closest = new Point3d();
- if (intPts.Count < 1)
- {
- // Found no intersections: use the curve's end point
- closest = cur.EndPoint;
- last = true;
- }
- else
- {
- // Found one or more intersections: take the closest of them
- double baseParam = cur.GetParameterAtPoint(pts[pts.Count - 1]);
- double minParam = 999999; // Big number
- bool found = false;
- foreach (Point3d pt in intPts)
- {
- // Check the point's parameter
- double param = cur.GetParameterAtPoint(pt);
- // If it's larger than the base parameter but smaller than the minumum found so far, use it
- if (param > baseParam && param < minParam)
- {
- minParam = param;
- closest = pt;
- found = true;
- // If it's the same as the curve's end point, no need to loop again
- last = (pt == cur.EndPoint);
- }
- }
- // 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
- if (!found)
- {
- closest = cur.EndPoint;
- last = true;
- }
- }
- pts.Add(closest);
- }
- }
- // Now we can go and create our lines/polyline
- if (pts != null && pts.Count > 0)
- {
- // Open the current space for write
- BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
- //if (!ad.CreatePolyline || pts.Count <= 2)
- //if (pts.Count <= 2)
- {
- // Create a sequence of line entities
- if (pts.Count >= 2)
- {
- for (int i = 0; i < pts.Count - 1; i++)
- {
- Line ln = new Line();
- ln.StartPoint = pts[i];
- ln.EndPoint = pts[i + 1];
- btr.AppendEntity(ln);
- tr.AddNewlyCreatedDBObject(ln, true);
- }
- }
- }
- }
- }
- tr.Commit();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement