Author Topic: Leader Jig Questions  (Read 2196 times)

0 Members and 1 Guest are viewing this topic.

rugaroo

  • Bull Frog
  • Posts: 378
  • The Other CAD Guy
Leader Jig Questions
« on: August 27, 2017, 02:16:35 PM »
I am trying to create a new .Net version of an old lisp due to some new direction from management. The code is below for reference, I can not take any credit for it either. What I am looking to do is basically mimic the way the lisp does the leader ghosting and "zig-zag". I have started with Kean's jigging example, but am kinda at a loss not having done anything like this yet. I know in C3D, it is easy enough to just create a style and move on, but for this I am targeting Vanilla AutoCAD.

I started off looking into possibly using the MLeader, but found that it just does not work really well. If anyone can give me some direction, I would greatly appreciate it. I am not worried about the text, bearing or anything else really, mainly the leader and getting it to preview as the user determines the placement.

Thanks in advance.

Code: [Select]
;-------------------------------- C:RL --------------------------------------
;
;  Description: Creates a radial line label for a point on a curve
;
;  Written By: Robert P. Ehrman
;
;  Operation: When you run the program, it will ask you for the "End point
;             you are describing", this point is the point that you creating
;             a radial line for. Then it askes you to  "Point on the arc",
;             this point is automatically snapped to the CENTER OF THE ARC
;             Finally It will ask you for the location of the label, at this
;             point you have three options, pick a point, using the ghost
;             outline supplied as a guide to accurately place the label, or
;             strike any of the four keys 'I', 'O', {space}, or {enter}.
;             'I', {space}, and {enter} denote "Inside", 'O' denotes
;             "Outside". If you pick Inside or Outside, the radial line will
;             originate at the point you are describing and the label will be
;             on the appropriate side. If you pick a point, the label will
;             originate at the point you pick, and the label will appear at
;             the location shown on the ghost image.
;             
;----------------------------------------------------------------------------

(defun C:RL   (/ End Ghost Osmode Cmde Osmode Clayr Cstyle Olderr Ltscale Pt1
           Pt2 Pt3 Cntr Brng1 Ns Ew Degree Minute Second Oldtrk Flag
           Track Temp Angl Angl2 Midtxt)

  (defun End (strng)
    (princ strng)
    (command "LAYER" "S" Clayr "")
    (command "TEXT" "S" Cstyle) (command)
    (setvar "CMDECHO" Cmde)
    (setvar "OSMODE"  Osmode)
    (setq *error* olderr)
    (princ)
  )
 
  (defun Ghost (p1 / Temp Angl2 p2 p3 p4 p5 p7 p8 p9)
    (if (> (distance Cntr P1)
        (distance Cntr Pt1))
      (setq P6 (polar P1 (angle Cntr Pt1) (* Ltscal 1.2)))
    ;else
      (setq P6 (polar P1 (angle Pt1 Cntr) (* Ltscal 1.2))))
    (setq Angl2 (angle P1 P6))
    (if (and (< Angl2 (* 1.5 pi))
          (> Angl2 (* 0.5 pi)))
      (setq Side 1.0)
    ;else
      (setq Side -1.0))
    (setq P9 (polar Pt1 Angl2 (+ (abs (- (distance P1 Cntr) (distance Pt1 Cntr)))
                          (* Ltscal 0.1)))
       P2 (polar P1 Angl2 (* Ltscal 0.6))
       P3 (polar P2 (+ Angl2 (* Side 0.5 pi)) (* Ltscal 0.12))
       P4 (polar P3 Angl2 (* Ltscal 0.2))
       P5 (polar P4 (- Angl2 (* Side 0.5 pi)) (* Ltscal 0.12))
       P7 (polar P6 (- Angl2 (* Side 0.5 pi)) (* Ltscal 0.12))
       P10 (polar P1 Angl2 (* Ltscal 0.2))
       P8 (polar P10 (- Angl2 (* Side 0.5 pi)) (* Ltscal 0.12)))
    (grdraw Pt1 P9 -1)
    (grdraw P9 P1 -1)
    (grdraw P1 P6 -1)
    (grdraw P2 P3 -1)
    (grdraw P3 P4 -1)
    (grdraw P4 P5 -1)
    (grdraw P6 P7 -1)
    (grdraw P7 P8 -1)
    (grdraw P8 P10 -1)
    (setq p9 p9)
  )
   

  (setq Cmde     (getvar "CMDECHO")
     Osmode   (getvar "OSMODE")
     Clayr    (getvar "CLAYER")
     Cstyle   (getvar "TEXTSTYLE")
     Olderr   *error*
     *error*  End
     Ltscal   (* 0.86 (getvar "LTSCALE"))
     )

  (setvar "CMDECHO" 0)

  (if (not (tblsearch "LAYER" "V-RADL"))
    (progn
      (princ "\nPlease wait while I create the layer.")
      (if (not (tblsearch "LTYPE" "G-EXIST"))
     (command ".LINETYPE" "load" "G-EXIST" "original" ""))
      (command ".LAYER" "N" "V-RADL"
            "C" "2" "V-RADL"
            "LT" "G-EXIST" "V-RADL" "")))
  (command ".LAYER" "S" "V-RADL" "")

  (setvar "OSMODE" 1025)
  (setq Pt1 (getpoint "\nPick end point on the arc : "))
  (setvar "OSMODE" 0)
  (while Pt1
    (setvar "OSMODE" 1028)
    (setq Cntr (getpoint "\nPick the arc : "))
    (setvar "OSMODE" 0)
    (setq Pt2 (polar Pt1 (angle Pt1 Cntr) (* Ltscal 1.2)))
    (setq Brng1 (angle Pt2 Pt1))
    (setq Brng1 (* 180.0 (/ Brng1 pi)))
    (cond ( (and (<= 0 Brng1)
           (> 90 Brng1))
         (setq ns "N"
            ew "E"
            Brng1 (- 90.0 Brng1)))
       ( (and (<= 90 Brng1)
           (> 180 Brng1))
         (setq ns "N"
            ew "W"
            Brng1 (- Brng1 90.0)))
       ( (and (<= 180 Brng1)
           (> 270 Brng1))
         (setq ns "S"
            ew "W"
            Brng1 (- 270.0 Brng1)))
       ( (and (<= 270 Brng1)
           (> 360 Brng1))
         (setq ns "S"
            ew "E"
            Brng1 (- Brng1 270.0))))
    (setq Degree (fix Brng1))
    (setq Brng1 (* 60.0 (- Brng1 (float (fix Brng1)))))
    (setq minute (fix Brng1))
    (setq Brng1 (* 60.0 (- Brng1 (float (fix Brng1)))))
    (setq second (fix Brng1))
    (setq Brng1 (- Brng1 (float (fix Brng1))))
    (if (<= 0.5 Brng1) (setq Second (1+ second)))
    (if (<= 60 Second)
      (progn
     (setq Minute (1+ Minute))
     (setq Second (- Second 60))))
    (if (<= 60 Minute)
      (progn
     (setq Degree (1+ Minute))
     (setq Minute (- Minute 60))))
    (if (> 10 Degree)
      (setq Brng1 (strcat ns "0" (itoa Degree) "%%d"))
    ;else
      (setq Brng1 (strcat ns (itoa Degree) "%%d")))
    (if (> 10 Minute)
      (setq Brng1 (strcat Brng1 "0" (itoa Minute) "'"))
    ;else
      (setq Brng1 (strcat Brng1 (itoa Minute) "'")))
    (if (> 10 Second)
      (setq Brng1 (strcat Brng1 "0" (itoa Second) "\"" ew))
    ;else
      (setq Brng1 (strcat Brng1 (itoa Second) "\"" ew)))
    (command "TEXT" "S" "S100")(command)
    (setq Oldtrk (cadr (grread 't)))
    (setq Pt5 (ghost Oldtrk))
    (princ "\nLocate label or Inside/Outside [I/O] <I> : \n")
    (setq Flag 1)
    (while Flag
      (setq Track (grread 'T))
      (cond ( (= 2 (car Track))                                 ;Keyboard
           (if (or (= "I" (chr (cadr Track)))                ;Inside
                (= "i" (chr (cadr Track)))
                (= 13  (cadr Track))
                (= 32  (cadr Track)))
          (progn
            (setq Pt5 (ghost Oldtrk))
            (setq Flag NIL)
            (command "DIM1" "LEADER" Pt1 Pt2) (command)
           ) )
           (if (or (= "O" (chr (cadr Track)))                ;Outside
                (= "o" (chr (cadr Track))))
          (progn
            (setq Pt5 (ghost Oldtrk))
            (setq Flag NIL)
            (setq Pt2 (polar Pt1 (angle Pt2 Pt1) (* Ltscal 1.2)))
            (command "DIM1" "LEADER" Pt1 Pt2) (command)
           ) )
           (if Flag (princ "\rInvalid keystroke.                 "))
         )
         ( (= 3 (car Track))                                 ;Point Selected
           (setq Pt5 (ghost Oldtrk))
           (setq Flag NIL)
           (setq Pt3 (cadr Track))
           (if (> (distance Cntr Pt3)
               (distance Cntr Pt1))
          (setq Pt2 (polar Pt3 (angle Pt2 Pt1) Ltscal))
           ;else
          (setq Pt2 (polar Pt3 (angle Pt1 Pt2) Ltscal)))
           (command "DIM1" "LEADER" Pt1 Pt5 Pt3 Pt2) (command)
           (setq Temp Pt1
              Pt1  Pt3
              Pt3 Temp)
         )
         ( (= 5 (car Track))                                 ;No Point
           (if (not (equal Oldtrk (cadr Track) 0.1))
          (progn
            (setq Pt5 (ghost Oldtrk))
            (setq Oldtrk (cadr Track))
            (setq Pt5 (ghost Oldtrk))
    ) )     ) ) )
    (setq Angl (angle Pt1 Pt2))
    (setq Midtxt (polar Pt1 Angl (* Ltscal 0.7)))
    (if (and (< Angl (* 1.5 pi))
          (> Angl (* 0.5 pi)))
      (setq Temp Pt2
         Pt2  Pt1
         Pt1  Temp
         Angl (+ Angl pi)))
    (setq Midtxt (polar Midtxt (+ Angl (/ Pi 2.0)) (* Ltscal 0.10)))
    (setq Angl2 (* 180.0 (/ Angl pi)))
    (command "TEXT" "M" Midtxt Angl2 Brng1)
    (setq Midtxt (polar Midtxt (- Angl (/ Pi 2.0)) (* Ltscal 0.20)))
    (command "TEXT" "M" Midtxt Angl2 "(R)")
    (setq Pt1 NIL)
    (initget 0)
    (setvar "OSMODE" 1025)
    (setq Pt1 (getpoint "\nPick point on the arc : "))
    (setvar "OSMODE" 0)
  )
  (End "Function completed.")
)
LDD06-09 | C3D 04-19 | Infraworks 360 | VS2012-VS2017

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Leader Jig Questions
« Reply #1 on: August 28, 2017, 11:55:00 PM »
Here is the jig part I use
Code: [Select]
[/
                protected override SamplerStatus Sampler(JigPrompts prompts)
                {

                    JigPromptPointOptions jigOpts = new JigPromptPointOptions();
                    jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates) |
                    UserInputControls.NoNegativeResponseAccepted;
                   
                    jigOpts.Message = jPrompts;
                    jigOpts.BasePoint = P1;
                    jigOpts.UseBasePoint = true;

                    dres = prompts.AcquirePoint(jigOpts);
                    if (dres.Status == PromptStatus.None)
                        return SamplerStatus.Cancel;


                    endPointTemp = dres.Value;

                    if (i == 2)
                    {
                        Pt1 = endPointTemp.TransformBy(ucs.Inverse());
                        Pt2 = EndP.TransformBy(ucs.Inverse());
                        Pt1 = new Point3d(Pt1.X, Pt2.Y,Pt2.Z);
                       
                        endPointTemp = Pt1.TransformBy(ucs);                       
                    }

                    if (endPointTemp != EndP)
                    {
                        EndP = endPointTemp;
                    }
                    else
                    {
                        return SamplerStatus.NoChange;
                    }
                    if (dres.Status == PromptStatus.Cancel)
                        return SamplerStatus.Cancel;
                    else
                        return SamplerStatus.OK;

                }//End sampler



                protected override bool Update()
                {
                    try
                    {
                        Leader leader = Entity as Leader; 
                        leader.SetVertexAt(i, EndP);
                    }
                    catch (System.Exception ex)
                    { 
                        Util.Debug.Print("\nError: " + ex.Message);
                        return false;
                    }
                    return true;

                }//End Update

                public Point3d endP()
                {
                    return EndP;
                }

                public PromptPointResult Res()
                {
                    return dres;
                }


                public Entity GetEntity()
                {
                    return Entity;
                }

                public void Addvertex()
                {
                    i++;
                    P1 = EndP;
                    leader.SetVertexAt(2, P1);
                    leader.AppendVertex(new Point3d(P1.X+0.1,P1.Y,P1.Z));
                }




            }// End LeaderJig : EntityJig
code]

rugaroo

  • Bull Frog
  • Posts: 378
  • The Other CAD Guy
Re: Leader Jig Questions
« Reply #2 on: August 29, 2017, 12:58:04 AM »
Thanks Bryco... I got to this a bit late, but I will see where this can take me. I think I already have a few questions, but will see where I can go first.
LDD06-09 | C3D 04-19 | Infraworks 360 | VS2012-VS2017

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Leader Jig Questions
« Reply #3 on: September 10, 2017, 12:00:55 PM »
1) use  ed.GetNestedEntity to pick the start pt of the leader, this also supplies the pickpoint.
Then find the nearest point to the entity you picked. (or set the nearest snap-a bit dodgy).
2)Start the leader jig

            public class LeaderJig : EntityJig
            {
                private Point3d P1,P2,EndP,endPointTemp,Pt1,Pt2;       
                private PromptPointResult dres;
                private Matrix3d ucs;
                private Plane pn;
                private string jPrompts;
                private Leader leader;
                public int i=1;

                public LeaderJig(Vector3d norm, Point3d BasePt,
                    Matrix3d Ucs, Plane Pn,string jigPrompts,bool IsSplined,bool HasArrowhead)
                    : base(new Leader())
                {
                    ucs = Ucs;
                    jPrompts = jigPrompts;
                    leader = (Leader)Entity;
                    pn = Pn;
                    P1 = BasePt;
                    P2 = new Point3d(P1.X + 1, P1.Y, P1.Z);
                    leader.SetDatabaseDefaults();
                    leader.SetPlane(pn);
                    leader.AppendVertex(P1);
                    leader.AppendVertex(P2);
                    leader.IsSplined = IsSplined;
                    leader.HasArrowHead = HasArrowhead;
                   
                    leader =(Leader) Layers.Layers.NotesAndDims(leader);
                    leader.Dimscale =(double) acadApp.GetSystemVariable("Dimscale");


//addcode to set the dimstyle  leader.SetDimstyleData
}
}

           public static ObjectId LeaderandMtext(string Msg, Point3d P1)
            {
                Document doc = acadApp.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                Editor ed = doc.Editor;
                Matrix3d ucs = ed.CurrentUserCoordinateSystem;
                CoordinateSystem3d cs = ucs.CoordinateSystem3d;
                Vector3d Norm = cs.Zaxis;
                Plane pn = new Plane(P1, Norm);
                Leader leader = new Leader();
                MText mt = new MText();
                mt.SetDatabaseDefaults();
                double dimgap = leader.Dimgap;
                bool bDone = false;
                //bool isMM = Util.Is.isMM();
                double scale = Util.Get.getscale();
                string prompt = "\nSpecify next point:";
                ObjectId id=ObjectId.Null;
                LeaderJig jig = new LeaderJig(Norm, P1, ucs, pn, prompt,false,true);

                for (int i = 0; i < 2; i++)
                {
                    PromptResult res = ed.Drag(jig);
                    if (res.Status == PromptStatus.Cancel) return id;
                    if (i == 0) jig.Addvertex();                 
                    if (i == 1 && res.Status == PromptStatus.OK) bDone = true;
                }
                if (bDone)
                {
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        leader = jig.GetEntity() as Leader;
                        Point2d pt1 = leader.GetPointAtParameter(1).Convert2d(pn);
                        Point2d pt2 = leader.EndPoint.Convert2d(pn); 
                        if (pt2.X < pt1.X) dimgap = -dimgap;                       
                        leader.DimensionStyle = Util.Get.CurrentDimstyle(leader.GetType().Name);
                        id=btr.AppendEntity(leader);
                        tr.AddNewlyCreatedDBObject(leader, true);
                        tr.Commit();
                    }

                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        if (Msg == string.Empty)
                            if (!Util.Get.getstring("\nType the text", "", true, out Msg)) return id;
                        mt.Contents = Msg;
                        mt.Layer = Layers.Layers.NNotes();
                        mt.Normal = Norm;
                        Point3d leaderEndpt=leader.EndPoint.TransformBy(ucs.Inverse());
                        Point3d location=new Point3d (leaderEndpt.X+dimgap,leaderEndpt.Y,leaderEndpt.Z).TransformBy(ucs);
                        mt.Location = location;
                        leader = tr.GetObject(leader.Id, OpenMode.ForWrite) as Leader;
                        if (leader.EndPoint.TransformBy(ucs.Inverse()).X >
                            leader.GetPointAtParameter(1).TransformBy(ucs.Inverse()).X)
                            mt.Attachment = AttachmentPoint.MiddleLeft;
                        else
                            mt.Attachment = AttachmentPoint.MiddleRight;

                       // mt.TextHeight =Util.Is.Mult()*scale *0.135;
                        //mt.TextStyleId =
                        ObjectId mtId=btr.AppendEntity(mt);
                        tr.AddNewlyCreatedDBObject(mt, true);
                        leader.Annotation = mtId;// mt.ObjectId;
                   
                        tr.Commit();
                    }
                }
                return id;
            } //End LeaderandMtext


Hope this gets you closer