Author Topic: Draw bisector between 2 lines  (Read 847 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
Draw bisector between 2 lines
« on: February 19, 2013, 08:02:37 PM »
Code: [Select]
        [CommandMethod("bis", CommandFlags.UsePickSet)]
        public static void drawBisector()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = doc.Editor;
            PromptEntityOptions peo = new PromptEntityOptions("\nSelect first line: ");
            peo.SetRejectMessage("Wrong object or nothing selected.");
            peo.AddAllowedClass(typeof(Line), false);
            PromptEntityResult res;
            ObjectId objId1 = ObjectId.Null;
            ObjectId objId2 = ObjectId.Null;
            Entity ent;
            Point3d p1, p2;
            res = ed.GetEntity(peo);

            if (res.Status != PromptStatus.OK)
                return;;
            objId1 = res.ObjectId;
            p1 = res.PickedPoint;
            peo.Message="\nSelect second line: ";
            res = ed.GetEntity(peo);

            if (res.Status != PromptStatus.OK)
                return;
            objId2 = res.ObjectId;
            p2 = res.PickedPoint;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                ent = tr.GetObject(objId1, OpenMode.ForRead) as Entity;
                if (ent == null)   return;
                Line ln1 = ent as Line;
                if (ln1 == null) return;
                ent = tr.GetObject(objId2, OpenMode.ForRead) as Entity;
                if (ent == null) return;
                Line ln2 = ent as Line;
                if (ln2 == null) return;

                p1 = ln1.GetClosestPointTo(p1,false);
                p2 = ln2.GetClosestPointTo(p2, false);

                Point3d tmp,p1f, p2f, p1n, p2n;

                p1f = ln1.StartPoint;
                p2f = ln1.EndPoint;
                p1n = ln2.StartPoint;
                p2n = ln2.EndPoint;
                // swap poins for easier math
                if (p1f.DistanceTo(p1n) > p1f.DistanceTo(p2n))
                {
                    tmp = p2n;
                    p2n = p1n;
                    p1n=tmp;
                }
   
                Point3d mp1 = new Point3d((p1f.X + p1n.X) / 2, (p1f.Y + p1n.Y) / 2, (p1f.Z + p1n.Z) / 2);
                Point3d mp2 = new Point3d((p2f.X + p2n.X) / 2, (p2f.Y + p2n.Y) / 2, (p2f.Z + p2n.Z) / 2);
                Line bis = new Line(mp1, mp2);

                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                btr.AppendEntity(bis);
                tr.AddNewlyCreatedDBObject(bis, true);
                tr.Commit();
            }
        }