Author Topic: Breaking a curve at a point  (Read 589 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Breaking a curve at a point
« on: May 20, 2012, 07:05:11 PM »
Code: [Select]
// based on code by Balaji Ramamoorthy
// http://adndevblog.typepad.com/autocad/2012/05/breaking-a-curve-at-a-point.html
       [CommandMethod("bre")]
        public static void ADSProjectBreak()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Matrix3d ucs = ed.CurrentUserCoordinateSystem;

            Database db = HostApplicationServices.WorkingDatabase;

            ObjectId id;

            Entity ent;

            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

                    // Let the user select a curve   

                    id = ed.GetEntity("\nSelect curve: ").ObjectId;

                    if (id == ObjectId.Null) return;

                    ent = (Entity)tr.GetObject(id, OpenMode.ForRead, false);

                    if (ent == null) return;

                    // Check that it is really a curve   

                    Curve Curve = ent as Curve;

                    if (Curve == null) return;

                    // Get the break position and convert to WC coordinates   

                    Point3d breakPnt;

                    breakPnt = ed.GetPoint("\nPick a point on the curve: ").Value;

                    breakPnt.TransformBy(ucs.Inverse());

                    // Check that the point is on the curve

                    breakPnt = Curve.GetClosestPointTo(breakPnt, false);

                    Point3dCollection breakPoints = new Point3dCollection();

                    DBObjectCollection newCurves = new DBObjectCollection();

                    // Get the segments according to the trim points   

                    breakPoints.Add(breakPnt);

                    newCurves = Curve.GetSplitCurves(breakPoints);

                    if (newCurves == null)
                    {

                        ed.WriteMessage("\nGetSplitCurves failed :  Error");

                        return;

                    }

                    // Here we add the segments to the database with different colors   

                    for (int i = 0; i < newCurves.Count; i++)
                    {

                        Entity pent = (Entity)newCurves[i] as Entity;

                        pent.SetPropertiesFrom(ent);

                        pent.ColorIndex = i + 1;// to display result only

                        btr.AppendEntity(pent);

                        tr.AddNewlyCreatedDBObject(pent, true);

                        tr.TransactionManager.QueueForGraphicsFlush();
                    }

                    ent.UpgradeOpen();

                    ent.Erase();

                    doc.TransactionManager.FlushGraphics();

                    tr.Commit();
                }

            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
                ed.WriteMessage("\nPokey");
            }
        }

~'J'~