Author Topic: Roll Your Own Leader Routine, Need UCS help  (Read 9829 times)

0 Members and 1 Guest are viewing this topic.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Roll Your Own Leader Routine, Need UCS help
« on: January 21, 2008, 03:49:57 PM »
I have been working on my own version of the Qleader routine.
It is a version to call from a lisp & this version is a 3 point standard leader. (my standard)
The routine uses the vla-addleader.
See the routine & test call to the routine.

The remaining problem I am trying to solve  is that the Middle of the Top/Bottom Line attachment
is giving me problems. The Leader dxf code 213 is used as a vector to locate the Mtext.
With all the other justifications it is '(213 0.0 0.0 0.0)

So my problem is a UCS WCS one.

This code seems close to working:
Code: [Select]
  (cond
    ((= just "MT") ;  Middle of Top Line Justification Adjustment
     (setq elst (subst (cons 213 (trans (list 0.0 (/ txtht 2.0) 0.0) ent 1)) (assoc 213 elst) elst))
     (vla-put-insertionPoint mtextobj (vlax-3d-point (trans(polar p3 (/ pi 2) (/ txtht 2.0)) 1 0)))
     (vla-put-insertionPoint mtextobj (vla-get-insertionPoint mtextobj))
    )
    ((= just "MB") ;  Middle of Bottom Line Justification Adjustment
     (setq elst (subst (cons 213 (trans (list 0.0 (/ txtht -2.0) 0.0) ent 1)) (assoc 213 elst) elst))
     (vla-put-insertionPoint mtextobj (vlax-3d-point (trans(polar p3 (* pi 1.5) (/ txtht 2.0)) 1 0)))
     (vla-put-insertionPoint mtextobj (vla-get-insertionPoint mtextobj))
    )
  )
 
But this (/ txtht 2.0) needs to be (/ txtht -2.0) some of the time. I suspect there is more to it than that.
My test have been in WCS with a rotation (90 deg about the Z axis).

Any UCS suggestions?

Code: [Select]
;;================================================================
;;   Add a 3 point leader 
;;================================================================

(defun addLeader (p1 p2 p3 txt txtht just / mtextobj doc elst ent ptlist
                   gap ldrobj leaderatt mspace p4 tmparray  xdata)
  ;;  points are UCS
  ;;  just = "T" "M" "B" "MT" "MB" "U"
 
  (setq doc (vla-get-activeDocument (vlax-get-acad-object)))
  (setq mspace (vla-get-modelSpace doc))

 
  ;;  Note DimGap is negative when the Style calls for the text to be boxed
  ;;  there are occasions when DimScale is zero
  (setq gap (abs(* (if (zerop (getvar "DIMSCALE")) 1. (getvar "DIMSCALE")) (getvar "DIMGAP"))))

  (setq p4 (polar p3 (angle p2 p3) gap)) ; Position for Mtext
 
  ;;  add an mtext object to associate with the leader:
  ;;  uses current text style & height, zero width mtext
  (setq mtextobj (vla-addMText mspace (vlax-3d-point (trans p4 1 0)) 0.0 txt))
  (vla-put-height mtextobj txtht)

  ;;  Adjust Mtext Justification for Left/Right Leader
  (if (> (car p3)(car p2))
      (cond
        ((or (= just "T")(= just "MT"))
          (vla-put-attachmentPoint mtextobj acAttachmentPointTopLeft)
       )
        ((= just "M")
          (vla-put-attachmentPoint mtextobj acAttachmentPointMiddleLeft)
        )
        ((or (= just "B")(= just "MB")(= just "U"))
          (vla-put-attachmentPoint mtextobj acAttachmentPointBottomLeft)
        )
      )
      (cond
        ((or (= just "T")(= just "MT"))
          (vla-put-attachmentPoint mtextobj acAttachmentPointTopRight)
       )
        ((= just "M")
          (vla-put-attachmentPoint mtextobj acAttachmentPointMiddleRight)
        )
        ((or (= just "B")(= just "MB")(= just "U"))
          (vla-put-attachmentPoint mtextobj acAttachmentPointBottomRight)
        )
      )
  )
 
  ;;  Need to reposition Mtext as changing the acAttachmentPoint will move it
  (vla-put-insertionPoint mtextobj (vlax-3d-point (trans p4 1 0)))
  (vla-update mtextobj) ; updates the screen, remove after debug
 
 
  ;; build an array of coordinates [UCS to WCS]
  (setq ptlist (apply 'append (mapcar '(lambda(x) (trans x 1 0)) (list p1 p2 p3))))

  ;;  Create the Leader & associate the text
  ;;  text is moved to position TL TR BL BR if needed
  ;;  The array of 3D WCS coordinates specifying the leader.
  (setq tmparray (vlax-make-safearray vlax-vbDouble (cons 0 (- (length ptlist) 1))))
  (vlax-safearray-fill tmparray ptlist)
  ;;  not needed (setq tmp (vlax-make-variant tmparray))
  (setq ldrObj (vla-addLeader mspace tmparray mtextobj acLineWithArrow) )
  (vla-put-verticaltextposition ldrObj acVertCentered)
 
  ;;  adjust leader vector for 'Middle of Top/Bottom Line'
  (setq elst (entget (setq ent (vlax-vla-object->ename ldrObj))))

 
  (cond
    ((= just "MT") ;  Middle of Top Line Justification Adjustment
     (setq elst (subst (cons 213 (trans (list 0.0 (/ txtht 2.0) 0.0) ent 1)) (assoc 213 elst) elst))
     (vla-put-insertionPoint mtextobj (vlax-3d-point (trans(polar p3 (/ pi 2) (/ txtht 2.0)) 1 0)))
     (vla-put-insertionPoint mtextobj (vla-get-insertionPoint mtextobj))
    )
    ((= just "MB") ;  Middle of Bottom Line Justification Adjustment
     (setq elst (subst (cons 213 (trans (list 0.0 (/ txtht -2.0) 0.0) ent 1)) (assoc 213 elst) elst))
     (vla-put-insertionPoint mtextobj (vlax-3d-point (trans(polar p3 (* pi 1.5) (/ txtht 2.0)) 1 0)))
     (vla-put-insertionPoint mtextobj (vla-get-insertionPoint mtextobj))
    )
  )

  ;;==========================================
  ;;  Add Xdata needed to make this a QLeader
  ;;==========================================

  ;;  make it a QLeader
  (setq LeaderAtt (min (vl-position Just '("T" "MT" "M" "MB" "B" "U")) 4))
    (setq xdata (list (list -3
                    (list "ACAD" '(1000 . "DSTYLE") '(1002 . "{")
                          '(1070 . 147) (cons 1040 (vla-get-textgap ldrObj)) ; Dimgap
                          '(1070 . 77) (cons 1070 0) ; Underline Text
                          '(1070 . 41) (cons 1040 (getvar "DIMASZ"))  ; Arrow head Size
                          '(1070 . 40) (cons 1040 (getvar "DIMSCALE"))  ; DIMSCALE
                          '(1002 . "}")))))
  (entmod (append elst xdata))


  ;;  Clean Up
  (vla-update ldrObj) ; updates the screen
  (vlax-release-object ldrObj)
 
  (vla-update mtextobj) ; updates the screen
  (vlax-release-object mtextobj)
  (princ)
)



Code: [Select]
;; -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;;                T E S T   R O U T I N E                 
;; -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
(defun c:test (/ p1 p2 p3 LA usrorm)
  (vl-load-com)
  ;;  Get the 2nd point
  ;;  Draw temporary Leader w/ arrow using grvecs
  (defun GetPoint2 (p0 / gr org pt ang_arrow sz)
    (defun arrow (p0 arw_ang col size / p1 p2 p3)
      (setq size (* size 0.6)
            p2 (polar p0 (+ arw_ang pi) size)
            p1 (polar p2 (+ arw_ang (* pi 0.85)) size)
            p3 (polar p2 (+ arw_ang (* pi 1.15)) size))
      (grvecs (list col p0 p1 p1 p2 p2 p3 p3 p0))
    )
 
    (setq sz (abs(* (if (zerop (getvar "DIMSCALE")) 1. (getvar "DIMSCALE"))
                    (getvar "DIMASZ"))))
    (while (= (car (setq gr (grread nil 7))) 5)
      (setq pt        (cadr gr)
            ang_arrow (angle pt p1)
      )
      (redraw)
      (arrow p1 ang_arrow 256 sz)
      (grvecs (list 256 p1 pt))
    )
    pt
  )

  (setq usrorm (getvar "orthomode"))
  (setvar "orthomode" 0)
  ;(or (setq LA "2") ; Debug
  (initget "1 2 3 4 5") ; Leader Attachment -
  (setq LA (cond ((getkword "\nAttach? 1Top 2MidTop 3Middle 4MidBottom 5Bottom:"))
                 ("1")
           )
  )
 ; ) ; debug
  (if (and (setq p1 (getpoint "\nPick start of 3 point leader."))
           ;;(setq p2 (getpoint p1 "\nPick next point of leader."))
           (princ "\nPick next point of leader.")
           (setq p2 (getpoint2 p1))
      )
    (progn
      ;;(grvecs (list 256 p1 p2))
      (setvar "orthomode" 1)
      (if (setq p3 (getpoint p2 "\nPick end of leader."))
        (setq p3 (subst (cadr p2) (cadr p3) p3))
      )
      (redraw)
      (if (and p1 p2 p3)
        (progn
          (addLeader p1 p2 p3
                     "This is a test\\PSecond Line\\PThird Line"
                     (getvar "TEXTSIZE")
                     (nth (atoi LA) '("" "T" "MT" "M" "MB" "B" "U"))
          )
        )
      )
    )
  )
  (setvar "orthomode" usrorm)
  (princ)
)
<edit: updated code, wrong version posted>
« Last Edit: January 22, 2008, 09:16:13 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Roll Your Own Leader Routine, Need UCS help
« Reply #1 on: January 21, 2008, 05:10:26 PM »
Oops, just updated the code. I did not post the latest version.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Roll Your Own Leader Routine, Need UCS help
« Reply #2 on: January 21, 2008, 06:09:37 PM »
CAB,

If I may be so bold, why are you wanting to roll your own leader routine?  What don't you like about the core routine?  Or is just to see if you can do it? 
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Roll Your Own Leader Routine, Need UCS help
« Reply #3 on: January 21, 2008, 06:49:40 PM »
My reasons are:
To avoid using the COMMAND in my lisp. Also in ACAD2000 the qleader miss behaves in rotated UCS.
I would also be able to control the leader style options without setting & restoring the current style options.
Or modifying the leader after the command. You can also catch the user ESCAPE before the leader is created.
I like to have control over the prompts & setting options & lock the user out. This is for specific
lisp routines. I am working on my TextInsert.lsp at this time & need to be able to override the text height.

I know ACAD Leaders have improved but I still want to do it my way in my lisp routines.

Thanks for asking Ted.
« Last Edit: January 21, 2008, 08:47:11 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Roll Your Own Leader Routine, Need UCS help
« Reply #4 on: January 21, 2008, 07:24:48 PM »
Okay.

I do not question the reasons of Lisp Gods with air of judgment but rather with air of curiosity so that I may learn. :-)
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Roll Your Own Leader Routine, Need UCS help
« Reply #5 on: January 21, 2008, 08:12:53 PM »
Thanks for the kind words. If there is any attitude in my reply it was not my intention.
Please chalk it up to my long Monday. :doa:

It's just that I want to control the way the user interacts with MyLeader routine. :-)

Here is an earlier attempt at reining in the Leader command.
http://www.theswamp.org/index.php?topic=20353.0
So you see I've been trying to conquer this for some time. 8-)
« Last Edit: January 21, 2008, 08:21:22 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Joe Burke

  • Guest
Re: Roll Your Own Leader Routine, Need UCS help
« Reply #6 on: January 22, 2008, 06:04:56 AM »
Hi Alan,

I assume the mtext is supposed to be associated with the leader. If so, the problem I'm seeing is caused by the cond statement you mentioned. Moving the mtext after the fact is causing the third point of the leader to change.  Or am I missing something?

BTW, there's a line near the top of the test routine which needs to be commented out.

whdjr

  • Guest
Re: Roll Your Own Leader Routine, Need UCS help
« Reply #7 on: January 22, 2008, 08:18:44 AM »
CAB,

I used to have the same problem in a routine I used a while back.  I don't remember which routine it was or what it did, but I know it would go haywire if I tried to run it in a non world UCS.  What I did was store the current UCS, change it to WORLD, run routine, then restore the UCS.  It may not be what you want to do but it may be a solution for you.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Roll Your Own Leader Routine, Need UCS help
« Reply #8 on: January 22, 2008, 08:43:15 AM »
Hi Alan,

I assume the mtext is supposed to be associated with the leader. If so, the problem I'm seeing is caused by the cond statement you mentioned. Moving the mtext after the fact is causing the third point of the leader to change.  Or am I missing something?

BTW, there's a line near the top of the test routine which needs to be commented out.
Yes Joe, the Mtext is positioned correctly but when you move the mtext the leader repositions to TOP when it should be 'Middle of Top Line'.

You mean the line that prevents the user from selecting the justification type. I commented it out to allow user selection now.

Thanks
« Last Edit: January 22, 2008, 08:52:11 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Roll Your Own Leader Routine, Need UCS help
« Reply #9 on: January 22, 2008, 08:49:42 AM »
Hey Will,
Yes the problem comes when I try to create the Leader & Text. Even in a WCS that is rotated about the Z axis.

You see the Leader uses 3 DXF codes to control this:
Code: [Select]
210    Normal vector    DXF: X value; APP: 3D vector
211    "Horizontal" direction for leader     DXF: X value; APP: 3D vector
213    Offset of last leader vertex from annotation placement point
                     DXF: X value; APP: 3D vector
It's to the point I need to give it a rest & come back to it.
The fresh look at a problem often yields the answers.  8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Joe Burke

  • Guest
Re: Roll Your Own Leader Routine, Need UCS help
« Reply #10 on: January 22, 2008, 09:04:38 AM »
Alan,

I mean this line in the test routine, which causes load failure.

(defun GetPoint2 (p1 / gr org pt ang_arrow sz)

You forgot to comment it out.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Roll Your Own Leader Routine, Need UCS help
« Reply #11 on: January 22, 2008, 09:17:34 AM »
Oh, that's what I get for trying to edit part of the posted code!
Thanks, extra code removed.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Joe Burke

  • Guest
Re: Roll Your Own Leader Routine, Need UCS help
« Reply #12 on: January 22, 2008, 09:24:37 AM »
Alan,

I'm still not sure whether the mtext is supposed to be associated with the leader or not.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Roll Your Own Leader Routine, Need UCS help
« Reply #13 on: January 22, 2008, 09:33:17 AM »
Oh, yes definitely.
Using the vla-addleader makes it so. If you want it disassociated you need to remove the DXF 340 code.
But you already knew that.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Joe Burke

  • Guest
Re: Roll Your Own Leader Routine, Need UCS help
« Reply #14 on: January 22, 2008, 10:01:01 AM »
Alan,

Thanks for confirmation.

Given that, what I don't understand is how you might move the insertion point of the mtext without changing the third point of the leader. The two are tied together. Moving the mtext will cause the the third leader point to change, which make no sense to me.