CloudJack Posted October 10, 2021 Share Posted October 10, 2021 I am trying to create an Autolisp to draw a leader with a certain angle (say 60 degree) and landing distance. Two case: 1. Pick two points (For a box) 2. Pick a point and Pick a text (for mtext), then keep an offset to mtext Conditions: A. Keep 60/120 degree for all leader B. Create a style for all leader C. Offset text 200 horizontal and 250 vertical from text's base point and the max. length of mtext D. No Mtext create together with leader E. Prefer leader instead of Mleader for further modification location of text / box by using STRETCH F. Autolisp can apply to any UCS I have searched from google and modified two Autolisp. Leader_Version 1.lsp Leader_Version 2.lsp The Leader Version 1 is to pick two points for creating a leader / mleader. The Leader Version 2 is to pick a point and a Mtext for creating a leader / mleader. The above Autolisp have three major problem: 1. When picking point with snap point from other object/layout, the angle / length of landing distance will be distorted. 2. When adding an offset to Mtext, the end point of leader will always at the Mtext basepoint for leader direction from left to right. 3. It cannot control Style of leader if not using Mleader. Sample drawing is attached for your information. Leader Sample Drawing.dwg Would anyone help to point out the problem of my Autolisp. (may be use entmake for leader instead of command? Don't know how to script the entmake) Quote Link to comment Share on other sites More sharing options...
mhupp Posted October 11, 2021 Share Posted October 11, 2021 (edited) Came up with this after seeing this post https://www.cadtutor.net/forum/topic/20342-leader-line-dynamic-block/?do=findComment&comment=165930 This will get your Leader and landing and text (A,C,D,E). when drawing the leader it will highlight when your on the 60/120 Degree angle and somewhat stick to it. (defun C:LDR (/ PT1 PT2 PT3 PT4 hgt) (defun DtR (d) (* pi (/ d 180.0))) (setq PT1 (getpoint "\nPick starting point:")) (setvar 'POLARANG 60) (setvar 'AUTOSNAP 127) (setq str "PLACEHOLDER TEXT") (vl-cmdf "_.Line" PT1 pause "") (entdel (entlast)) (setq PT2 (getvar "lastpoint")) (cond ((and (< (car PT1)(car PT2)) (< (cadr PT1) (cadr PT2))) (setq PT3 (polar PT2 0 4213)) (setq PT4 (polar PT3 (DtR 51.3402) 320.1562)) (vl-cmdf "_.Text" "S" "RomanS" "J" "TL" PT4 "450" "0" str) ) ((and (> (car PT1)(car PT2)) (< (cadr PT1) (cadr PT2))) (setq PT3 (polar PT2 pi 4213)) (setq PT4 (polar PT3 (DtR 130.5986) 3521)) (vl-cmdf "_.Text" "S" "RomanS" "J" "TR" PT4 "450" "0" str) ) ((and (> (car PT1)(car PT2)) (> (cadr PT1) (cadr PT2))) (setq PT3 (polar PT2 pi 4213)) (setq PT4 (polar PT3 (DtR 130.5986) 3521)) (vl-cmdf "_.Text" "S" "RomanS" "J" "TR" PT4 "450" "0" str) ) ((and (< (car PT1)(car PT2)) (> (cadr PT1) (cadr PT2))) (setq PT3 (polar PT2 0 4213)) (setq PT4 (polar PT3 (DtR 51.3402) 320.1562)) (vl-cmdf "_.Text" "S" "RomanS" "J" "TL" PT4 "450" "0" str) ) ) (vl-cmdf "_.Leader" PT1 PT2 PT3 "" "" "N") (princ) ) F10 to turn off polar tracking Edited October 12, 2021 by mhupp Quote Link to comment Share on other sites More sharing options...
CloudJack Posted October 13, 2021 Author Share Posted October 13, 2021 Thank you for giving some hints. Below is my updated version. It is no any distortion by turning off the snap mode before leader command. ;;Modified 2021-10-13 Version 1 (defun c:CD1 ( / leader leader2 s e e1 p p1 n TextLen pt1 pt2 mp1 layer x1 x2 x3 y1 y2 y3 len1 osm echo dim) (setq osm (getvar "osmode") echo (getvar "cmdecho") dim (getvar 'dimstyle) ) ;;Create Dimstyle (setvar "cmdecho" 0) ;Set Arrow Size to 300 (setvar "DIMASZ" 300) (_NewDimStyle "HKHA_Leader") (setvar "cmdecho" echo) ;;End Create Dimstyle (if (and (setq pt1 (getpoint "\nSelect First Point: ")) (setq pt2 (getpoint "\nSelect Second Point: ")) ) (progn (setq x1 (nth 0 pt1) y1 (nth 1 pt1)) (setq x2 (nth 0 pt2) y2 (nth 1 pt2)) (cond ((and (> x2 x1) (> y2 y1)) ; (princ "\nx2 > x1\n") (princ "y2 > y1") (setq x3 (+ x1 (/ (- y2 y1) (tan 60))) y3 y2) (setq mp1 (list x3 y3)) (setq len1 (- x2 x3)) ) ((and (> x2 x1) (< y2 y1)) ; (princ "\nx2 > x1\n") (princ "y2 < y1") (setq x3 (+ x1 (/ (- y2 y1) (tan 300))) y3 y2) (setq mp1 (list x3 y3)) (setq len1 (- x2 x3)) ) ((and (< x2 x1) (> y2 y1)) ; (princ "\nx2 < x1\n") (princ "y2 > y1") (setq x3 (+ x1 (/ (- y2 y1) (tan 120))) y3 y2) (setq mp1 (list x3 y3)) (setq len1 (- x3 x2)) ) ((and (< x2 x1) (< y2 y1)) ; (princ "\nx2 < x1\n") (princ "y2 < y1") (setq x3 (+ x1 (/ (- y2 y1) (tan 240))) y3 y2) (setq mp1 (list x3 y3)) (setq len1 (- x3 x2)) ) ) (setvar "osmode" 0) (setvar "cmdecho" 0) (vl-cmdf "leader" pt1 mp1 pt2 "" "" "N") ;(command "mleader" pt1 mp1 len1) (setvar "osmode" osm) (setvar "cmdecho" echo) (_SetDimStyleCurrent dim) ) ) (princ) ) ; defun End (defun c:CD2 ( / leader leader2 s e e1 p p1 n TextLen pt1 pt2 mp1 layer x1 x2 x3 y1 y2 y3 len1 osm echo dim) (setq osm (getvar "osmode") echo (getvar "cmdecho") dim (getvar 'dimstyle) ) ;;Create Dimstyle (setvar "cmdecho" 0) (_NewDimStyle "HKHA_Leader") (setvar "cmdecho" echo) ;Set Arrow Size to 300 (setvar "DIMASZ" 300) ;;End Create Dimstyle (if (and (setq pt1 (getpoint "\nSelect First Point: ")) (setq s (ssget "_:S:L" '((0 . "TEXT,MTEXT")))) ) (progn ;;Get Mtext BasePoint Start p1 (setq e (entget (ssname s 0)) ;Get selection set first item n (cdr (assoc 210 e)) ;Coordinate Reference TextLen (cdr (assoc 42 e)) ;Text Length ) (if (or (wcmatch (cdr (assoc 0 e)) "MTEXT") (and (zerop (cdr (assoc 72 e))) (zerop (cdr (assoc 73 e))) ) ) (progn (setq p (cdr (assoc 10 e))) (setq p1 (trans p n 1)) ;transfer point to current UCS ) (progn (setq p (cdr (assoc 11 e))) (setq p1 (trans p n 1)) ;transfer point to current UCS ) ) ;;Get Mtext BasePoint End (setq x1 (nth 0 pt1) y1 (nth 1 pt1)) (setq x2 (nth 0 p1) y2 (nth 1 p1)) (cond ((and (> x2 x1) (> y2 y1)) ; (princ "\nx2 > x1\n") (princ "y2 > y1") (setq x2 (- x2 200) y2 (- y2 250)) (setq x3 (+ x1 (/ (- y2 y1) (tan 60)))) (setq pt2 (list x2 y2 0)) (setq mp1 (list x3 y2 0)) (setq len1 (- x2 x3)) ) ((and (> x2 x1) (< y2 y1)) ; (princ "\nx2 > x1\n") (princ "y2 < y1") (setq x2 (- x2 200) y2 (- y2 250)) (setq x3 (+ x1 (/ (- y2 y1) (tan 300)))) (setq pt2 (list x2 y2)) (setq mp1 (list x3 y2)) (setq len1 (- x2 x3)) ) ((and (< x2 x1) (> y2 y1)) ; (princ "\nx2 < x1\n") (princ "y2 > y1") (setq x2 (+ x2 (+ TextLen 200)) y2 (- y2 250)) (setq x3 (+ x1 (/ (- y2 y1) (tan 120)))) (setq pt2 (list x2 y2)) (setq mp1 (list x3 y2)) (setq len1 (- x3 x2)) ) ((and (< x2 x1) (< y2 y1)) ; (princ "\nx2 < x1\n") (princ "y2 < y1") (setq x2 (+ x2 (+ TextLen 200)) y2 (- y2 250)) (setq x3 (+ x1 (/ (- y2 y1) (tan 240)))) (setq pt2 (list x2 y2)) (setq mp1 (list x3 y2)) (setq len1 (- x3 x2)) ) ) (setvar "osmode" 0) (setvar "cmdecho" 0) (vl-cmdf "._leader" pt1 mp1 pt2 "" "" "N") ;(command "mleader" pt1 mp1 len1) (setvar "osmode" osm) (setvar "cmdecho" echo) (_SetDimStyleCurrent dim) ) ) (princ) ) ; defun End (defun tan ( x ) (if (not (equal 0.0 (cos x) 1e-10)) (/ (sin (* pi (/ x 180.0)))(cos (* pi (/ x 180.0)))) ) ) (defun _SetDimStyleCurrent (dim / acdoc) (setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object))) (if (tblsearch "DIMSTYLE" dim) (vla-put-activeDimstyle acdoc (vla-item (vla-get-Dimstyles acdoc) dim)) ) (princ) ) (defun _NewDimStyle (dim / acdoc) (setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object))) (if (not (tblsearch "DIMSTYLE" dim)) (command "-dimstyle" "s" dim) (_SetDimStyleCurrent dim) ) (princ) ) (vl-load-com) (princ) Quote Link to comment Share on other sites More sharing options...
mhupp Posted October 13, 2021 Share Posted October 13, 2021 2 minutes ago, CloudJack said: It is no any distortion by turning off the snap mode before leader command. use "_non" so you don't have to mess with 'osmode (vl-cmdf "leader" "_non" pt1 "_non" mp1 "_non" pt2 "" "" "N") Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.