Clever_Elf Posted July 23, 2023 Posted July 23, 2023 Hello, I have a string "distance-final" and i would like to replace the . character with | character ;; Rounding distance to 3 figures (setq distance-final (atof (rtos scaled-distance 2 3))) ;; replace . with | (vl-string-subst "|" "." distance-final) (command "text" pt3 "2" bearing distance-final) The above obliviously is not doing it for me. I have been spinning the wheels for a while now with no traction. HELP Quote
Tsuky Posted July 24, 2023 Posted July 24, 2023 Don't use (atof) for distance-final before use (vl-string-subst). It do not replace for the real number 1 Quote
Steven P Posted July 24, 2023 Posted July 24, 2023 If I am reading this right, distance-final is going from scaled-distance number -> string (rtos) -> number (atof) There is no '.' to replace really in a number. You want to keep it as a string to do the substitution Quote
exceed Posted July 28, 2023 Posted July 28, 2023 (edited) vl-string-subst can take string value only, so you have to change this (vl-string-subst "|" "." distance-final) like this (vl-string-subst "|" "." (vl-princ-to-string distance-final)) or (setq distance-final (atof (rtos scaled-distance 2 3))) to (setq distance-final (rtos scaled-distance 2 3)) like above comments way because you use (command ~~ that don't care it's not a numeric value. Edited July 28, 2023 by exceed 1 Quote
Clever_Elf Posted August 11, 2023 Author Posted August 11, 2023 (edited) I still can not get this to work. I need the Lisp to return the result in Red. Currently it returns the other one. I want to replace: . in the distance with | d in the bearing with %%d ' in the bearing with %%135 " in the bearing with %%136 This gives the desired result when using the SU font i have. Find code below. ;;Bearing and Distance LISP (defun c:bd () (setq scale (getreal "\nEnter the drawing scale: ")) (setq pt1 (getpoint "\nEnter the first point: ")) (setq pt2 (getpoint "\nEnter the second point: ")) ;; Calculate the unscaled distance (setq dis(distance pt1 pt2)) ;; Calculate Scale factor (setq scalefactor(/ scale 1000)) ;; Calculate the scaled distance (setq scaled-distance(* dis scalefactor)) ;; Calculate the angle in Radians (setq BearRad (angle pt1 pt2)) ;; Calculate DMS (setq bearing (angtos BearRad)) ;; Set Layer to DIM (command "._CLAYER" "DIM") (setvar 'CELTYPE "ByLayer") (setvar 'CECOLOR "ByLayer") (setvar 'textsize 2) (setvar 'textstyle "SS") ;; pt3 midpoint of line pt1 pt2 (setq pt3(polar pt1(angle pt1 pt2) (/(distance pt1 pt2)2))) ;; offset ptDIM ;; (setq ptDIM(pt3(bearing)-90)5) This don't work ;; Rounding distance to 3 figures (setq distance-final (rtos scaled-distance 2 3)) ;; Convert distance-final to string for replace (vl-string-subst "|" "." distance-final) ;; Print Distance on screen on line (command "text" pt3 "2" bearing distance-final) ;; Set Layer to BEAR (command "._CLAYER" "BEAR") (setvar 'CELTYPE "ByLayer") (setvar 'CECOLOR "ByLayer") (setvar 'textsize 2) (setvar 'textstyle "SU") ;;(vl-string-subst "%%d" "d" bearing) ;;(vl-string-subst "%%135" "'" bearing) ;;(vl-string-subst "%%136" """ bearing) ;; Print Bearing on screen on line (command "text" pt3 "2" bearing bearing) ) Edited August 11, 2023 by SLW210 Added Code Tags! Quote
SLW210 Posted August 11, 2023 Posted August 11, 2023 Please use Code Tags. (press the <> button in the reply toolbar) Quote
Lee Mac Posted August 11, 2023 Posted August 11, 2023 Consider the following code - (defun c:bd ( / ang ber dis mid pt1 pt2 scl ) (initget 6) (if (and (setq scl (getreal "\nSpecify drawing scale: ")) (setq pt1 (getpoint "\nSpecify 1st point: ")) (setq pt2 (getpoint "\nSpecify 2nd point: " pt1)) ) (progn (setq ang (angle pt1 pt2) dis (* scl 0.001 (distance pt1 pt2)) mid (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) pt1 pt2) ) (entmake (list '(000 . "TEXT") '(008 . "DIM") '(062 . 256) '(006 . "BYLAYER") '(040 . 2.0) '(072 . 1) '(073 . 2) (cons 007 (if (tblsearch "style" "SS") "SS" (getvar 'textstyle))) (cons 010 (polar mid (+ ang (/ pi 2.0)) 5.0)) (cons 011 (polar mid (+ ang (/ pi 2.0)) 5.0)) (cons 050 ang) (cons 001 (vl-string-translate "." "|" (rtos dis 2 3))) ) ) (setq ber (angtos ang) ber (vl-string-subst "%%d" "d" ber) ber (vl-string-subst "%%135" "'" ber) ber (vl-string-subst "%%136" "\"" ber) ) (entmake (list '(000 . "TEXT") '(008 . "BEAR") '(062 . 256) '(006 . "BYLAYER") '(040 . 2.0) '(072 . 1) '(073 . 2) (cons 007 (if (tblsearch "style" "SU") "SU" (getvar 'textstyle))) (cons 010 (polar mid (+ ang (/ pi 2.0)) 2.5)) (cons 011 (polar mid (+ ang (/ pi 2.0)) 2.5)) (cons 050 ang) (cons 001 ber) ) ) ) ) (princ) ) 2 Quote
BIGAL Posted August 12, 2023 Posted August 12, 2023 (edited) Another way around the bearing is to look at the decimal version of the bearing, something like 42.4094444444444 so brg is (fix ang) you can then split the 0.4094444444444 further into minutes and seconds (fix (* 60 0.4094444444444)) 24 then do again for seconds. So easy strcat to make into 42d24'34" Edited August 12, 2023 by BIGAL Quote
Clever_Elf Posted August 13, 2023 Author Posted August 13, 2023 Lee Mac, You win the internet today my friend. That works perfectly. So much more elegant than my clumsy attempt. Quote
Clever_Elf Posted August 14, 2023 Author Posted August 14, 2023 Lee, In the list what do the 000. numbers mean? 040 is text height? 072 Width factor? How can i repeat the line selction part without inputting the scale again until esc is used to end (multiple line labels). I ask a lot don't i also the distance comes in as SS font but diplays as SU. weirdness Quote
Lee Mac Posted August 14, 2023 Posted August 14, 2023 16 hours ago, Clever_Elf said: You win the internet today my friend. That works perfectly. So much more elegant than my clumsy attempt. You're most welcome, I'm glad it helps. 4 hours ago, Clever_Elf said: In the list what do the 000. numbers mean? 040 is text height? 072 Width factor? Those are DXF group code (I simply pad them to 3 digits to please my OCD) - here is a DXF reference. Navigate to ENTITIES and then review Common Group Codes and those for TEXT entity. 4 hours ago, Clever_Elf said: How can i repeat the line selction part without inputting the scale again until esc is used to end (multiple line labels). We can change the acquisition of the points from an if to a while - consider the following: (defun c:bd ( / ang ber dis mid pt1 pt2 scl ) (initget 6) (if (setq scl (getreal "\nSpecify drawing scale: ")) (while (and (setq pt1 (getpoint "\nSpecify 1st point <exit>: ")) (setq pt2 (getpoint "\nSpecify 2nd point <exit>: " pt1)) ) (setq ang (angle pt1 pt2) dis (* scl 0.001 (distance pt1 pt2)) mid (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) pt1 pt2) ) (entmake (list '(000 . "TEXT") '(008 . "DIM") '(062 . 256) '(006 . "BYLAYER") '(040 . 2.0) '(072 . 1) '(073 . 2) (cons 007 (if (tblsearch "style" "SS") "SS" (getvar 'textstyle))) (cons 010 (polar mid (+ ang (/ pi 2.0)) 5.0)) (cons 011 (polar mid (+ ang (/ pi 2.0)) 5.0)) (cons 050 ang) (cons 001 (vl-string-translate "." "|" (rtos dis 2 3))) ) ) (setq ber (angtos ang) ber (vl-string-subst "%%d" "d" ber) ber (vl-string-subst "%%135" "'" ber) ber (vl-string-subst "%%136" "\"" ber) ) (entmake (list '(000 . "TEXT") '(008 . "BEAR") '(062 . 256) '(006 . "BYLAYER") '(040 . 2.0) '(072 . 1) '(073 . 2) (cons 007 (if (tblsearch "style" "SU") "SU" (getvar 'textstyle))) (cons 010 (polar mid (+ ang (/ pi 2.0)) 2.5)) (cons 011 (polar mid (+ ang (/ pi 2.0)) 2.5)) (cons 050 ang) (cons 001 ber) ) ) ) ) (princ) ) 2 Quote
Clever_Elf Posted August 16, 2023 Author Posted August 16, 2023 Ok Lee that took me a while to digest. I fixed the Obliquing text issue with the distance by inserting '(051 . 0.349006763285) at the end of the first list. 051 not 51 0.349006763285 is a calculated guess (Trial and error) that lands me within 12" of the desired Obliquing angle of 70. I'm sure there is mathematical relationship to calc this exactly but i have not found that correlation . I also changed the 5 to -2.5 in the cons 011 in the first list to offset the distance to the other side of the line. This makes sense as the distance from the middle of the line to the text insertion point is 2.5. I'm unsure what the 5.0 in Cons 010 does in the line above that. Despite of being unsure as to how my edits work, they do. I have one last task in this project. I need to round the bearing to the nearest 5" (seconds) and display with 2 figures. So 5" is displayed as 05". Similarly in need say 5' to display as 05' (minutes) . Degrees is fine. I assume i will be using fix and strcat (as suggested by BIGAL above) Should i start doing that just before" (setq ber (angtos ang)" substution part? (defun c:bd ( / ang ber dis mid pt1 pt2 scl ) (initget 6) (if (setq scl (getreal "\nSpecify drawing scale: ")) (while (and (setq pt1 (getpoint "\nSpecify 1st point <exit>: ")) (setq pt2 (getpoint "\nSpecify 2nd point <exit>: " pt1)) ) (setq ang (angle pt1 pt2) dis (* scl 0.001 (distance pt1 pt2)) mid (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) pt1 pt2) ) (entmake (list '(000 . "TEXT") '(008 . "DIM") '(062 . 256) '(006 . "BYLAYER") '(040 . 2.0) '(072 . 1) '(073 . 2) '(051 . 0.349006763285) (cons 007 (if (tblsearch "style" "SS") "SS" (getvar 'textstyle))) (cons 010 (polar mid (+ ang (/ pi 2.0)) 5.0)) (cons 011 (polar mid (+ ang (/ pi 2.0)) -2.5)) (cons 050 ang) (cons 001 (vl-string-translate "." "|" (rtos dis 2 3))) ) ) (setq ber (angtos ang) ber (vl-string-subst "%%d" "d" ber) ber (vl-string-subst "%%135" "'" ber) ber (vl-string-subst "%%136" "\"" ber) ) (entmake (list '(000 . "TEXT") '(008 . "BEAR") '(062 . 256) '(006 . "BYLAYER") '(040 . 2.0) '(072 . 1) '(073 . 2) (cons 007 (if (tblsearch "style" "SU") "SU" (getvar 'textstyle))) (cons 010 (polar mid (+ ang (/ pi 2.0)) 2.5)) (cons 011 (polar mid (+ ang (/ pi 2.0)) 2.5)) (cons 050 ang) (cons 001 ber) ) ) ) ) (princ) ) Quote
BIGAL Posted August 16, 2023 Posted August 16, 2023 angle of 70 ie 20 Autocad stores angles in radians. (dtr 20) ; degrees to radians 0.349065850398866 Something like this needs the rounding factor to be added. ;The dtr function converts degrees to radians ;The rtd function converts radians to degrees (defun dtr (a) (* pi (/ a 180.0)) ) ; (defun rtd (a) (/ (* a 180.0) pi) ) (setq pt1 (getpoint "\nSpecify 1st point <exit>: ")) (setq pt2 (getpoint "\nSpecify 2nd point <exit>: " pt1)) (setq ang (rtd (angle pt1 pt2))) (setq deg (fix ang)) (setq mins (* (- ang deg) 60.0)) (setq min (fix mins)) (setq secs (* (- mins min) 60.0)) (setq deg (rtos deg 2 0)) (if (< min 10.0) (setq min (strcat "0" (rtos min 2 0))) (setq min (rtos min 2 0)) ) (if (< secs 10.0) (setq secs (strcat "0" (rtos secs 2 0))) (setq secs (rtos secs 2 0)) ) (setq str (strcat deg "d" min "'" secs "\"")) Quote
Clever_Elf Posted August 17, 2023 Author Posted August 17, 2023 Cheers BIGAL (setq str (strcat deg "d" min "'" secs "\"")) The above puts everything back together i assume. That has to applied to the variable ber ? Can i solve the ber (vl-string-subst "%%d" "d" ber) stuff in one go here maybe something like (setq str (strcat deg "%%d" min "%%135" secs "%%136")) (defun c:bd ( / ang ber dis mid pt1 pt2 scl ) (initget 6) (if (setq scl (getreal "\nSpecify drawing scale: ")) (while (and (setq pt1 (getpoint "\nSpecify 1st point <exit>: ")) (setq pt2 (getpoint "\nSpecify 2nd point <exit>: " pt1)) ) (setq ang (angle pt1 pt2) dis (* scl 0.001 (distance pt1 pt2)) mid (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) pt1 pt2) ) (entmake (list '(000 . "TEXT") '(008 . "DIM") '(062 . 256) '(006 . "BYLAYER") '(040 . 2.0) '(072 . 1) '(073 . 2) '(051 . 0.349006763285) (cons 007 (if (tblsearch "style" "SS") "SS" (getvar 'textstyle))) (cons 010 (polar mid (+ ang (/ pi 2.0)) 5.0)) (cons 011 (polar mid (+ ang (/ pi 2.0)) -2.5)) (cons 050 ang) (cons 001 (vl-string-translate "." "|" (rtos dis 2 3))) ) ) (setq ber (angtos ang) ber (vl-string-subst "%%d" "d" ber) ber (vl-string-subst "%%135" "'" ber) ber (vl-string-subst "%%136" "\"" ber) ) (entmake (list '(000 . "TEXT") '(008 . "BEAR") '(062 . 256) '(006 . "BYLAYER") '(040 . 2.0) '(072 . 1) '(073 . 2) (cons 007 (if (tblsearch "style" "SU") "SU" (getvar 'textstyle))) (cons 010 (polar mid (+ ang (/ pi 2.0)) 2.5)) (cons 011 (polar mid (+ ang (/ pi 2.0)) 2.5)) (cons 050 ang) (cons 001 ber) ) ) ) ) (princ) ) Quote
exceed Posted August 17, 2023 Posted August 17, 2023 (edited) (defun c:bd ( / ang ber dis mid pt1 pt2 scl ) (vl-load-com) ;The dtr function converts degrees to radians ;The rtd function converts radians to degrees (defun dtr (a) (* pi (/ a 180.0)) ) ; (defun rtd (a) (/ (* a 180.0) pi) ) (initget 6) (if (setq scl (getreal "\nSpecify drawing scale: ")) (while (and (setq pt1 (getpoint "\nSpecify 1st point <exit>: ")) (setq pt2 (getpoint "\nSpecify 2nd point <exit>: " pt1)) ) (setq ang (angle pt1 pt2) dis (* scl 0.001 (distance pt1 pt2)) mid (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) pt1 pt2) ) (entmake (list '(000 . "TEXT") '(008 . "DIM") '(062 . 256) '(006 . "BYLAYER") '(040 . 2.0) '(072 . 1) '(073 . 2) '(051 . 0.349006763285) (cons 007 (if (tblsearch "style" "SS") "SS" (getvar 'textstyle))) (cons 010 (polar mid (+ ang (/ pi 2.0)) 5.0)) (cons 011 (polar mid (+ ang (/ pi 2.0)) -2.5)) (cons 050 ang) (cons 001 (vl-string-translate "." "|" (rtos dis 2 3))) ) ) (setq ber (rtos (rtd ang) 2 4)) (setq ber (strcat (substr ber 1 (vl-string-position (ascii ".") ber)) "%%d" (substr ber (- (strlen ber) 3) 2) "%%135" (substr ber (- (strlen ber) 1)) "%%136")) (entmake (list '(000 . "TEXT") '(008 . "BEAR") '(062 . 256) '(006 . "BYLAYER") '(040 . 2.0) '(072 . 1) '(073 . 2) (cons 007 (if (tblsearch "style" "SU") "SU" (getvar 'textstyle))) (cons 010 (polar mid (+ ang (/ pi 2.0)) 2.5)) (cons 011 (polar mid (+ ang (/ pi 2.0)) 2.5)) (cons 050 ang) (cons 001 ber) ) ) ) ) (princ) ) i cannot test cuz that 135 136 code is not in my arial ttf fonts. but it works maybe Edited August 17, 2023 by exceed Quote
mhupp Posted August 17, 2023 Posted August 17, 2023 (edited) 12 minutes ago, exceed said: i cannot test cuz that 135 136 code is not in my arial ttf fonts. but it works maybe replace "%%135" with (chr 135) -Edit Might need to be 0135 and 0136 --Double Edit Hold down alt and type on the numb pad 0135 and 0136 Edited August 17, 2023 by mhupp Quote
exceed Posted August 17, 2023 Posted August 17, 2023 (edited) may he wants like this? (defun c:bd ( / ang ber dis mid pt1 pt2 scl ) ;The dtr function converts degrees to radians ;The rtd function converts radians to degrees (defun dtr (a) (* pi (/ a 180.0)) ) ; (defun rtd (a) (/ (* a 180.0) pi) ) (initget 6) (if (setq scl (getreal "\nSpecify drawing scale: ")) (while (and (setq pt1 (getpoint "\nSpecify 1st point <exit>: ")) (setq pt2 (getpoint "\nSpecify 2nd point <exit>: " pt1)) ) (setq ang (angle pt1 pt2) dis (* scl 0.001 (distance pt1 pt2)) mid (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) pt1 pt2) ) (entmake (list '(000 . "TEXT") '(008 . "DIM") '(062 . 256) '(006 . "BYLAYER") '(040 . 2.0) '(072 . 1) '(073 . 2) '(051 . 0.349006763285) (cons 007 (if (tblsearch "style" "SS") "SS" (getvar 'textstyle))) (cons 010 (polar mid (+ ang (/ pi 2.0)) 5.0)) (cons 011 (polar mid (+ ang (/ pi 2.0)) -2.5)) (cons 050 ang) (cons 001 (vl-string-translate "." "\u+00b7" (rtos dis 2 3))) ) ) (setq ber (rtos (rtd ang) 2 4)) (setq ber (strcat (substr ber 1 (vl-string-position (ascii ".") ber)) "\u+00B0" (substr ber (- (strlen ber) 3) 2) "\u+2032" (substr ber (- (strlen ber) 1)) "\u+2033")) (entmake (list '(000 . "TEXT") '(008 . "BEAR") '(062 . 256) '(006 . "BYLAYER") '(040 . 2.0) '(072 . 1) '(073 . 2) (cons 007 (if (tblsearch "style" "SU") "SU" (getvar 'textstyle))) (cons 010 (polar mid (+ ang (/ pi 2.0)) 2.5)) (cons 011 (polar mid (+ ang (/ pi 2.0)) 2.5)) (cons 050 ang) (cons 001 ber) ) ) ) ) (princ) ) ============================= add 5 seconds roundup function lm:roundup and at:numfix in mhupp's comment (defun c:bd ( / ang ber dis mid pt1 pt2 scl deg min sec ) ;The dtr function converts degrees to radians ;The rtd function converts radians to degrees (defun dtr (a) (* pi (/ a 180.0)) ) ; (defun rtd (a) (/ (* a 180.0) pi) ) ;; Round Up - Lee Mac ;; Rounds 'n' up to the nearest 'm' (defun LM:roundup ( n m ) ((lambda ( r ) (cond ((equal 0.0 r 1e-8) n) ((< n 0) (- n r)) ((+ n (- m r))))) (rem n m)) ) (defun AT:NumFix (s n) ;; Fix number string with leading zeros ;; s - Number string to fix ;; n - Number of characters for final string ;; Alan J. Thompson, 10.29.09 ;; (AT:NumFix i 2) i= 5 = 05 (if (< (strlen s) n) (AT:NumFix (strcat "0" s) n) s ) ) (initget 6) (if (setq scl (getreal "\nSpecify drawing scale: ")) (while (and (setq pt1 (getpoint "\nSpecify 1st point <exit>: ")) (setq pt2 (getpoint "\nSpecify 2nd point <exit>: " pt1)) ) (setq ang (angle pt1 pt2) dis (* scl 0.001 (distance pt1 pt2)) mid (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) pt1 pt2) ) (entmake (list '(000 . "TEXT") '(008 . "DIM") '(062 . 256) '(006 . "BYLAYER") '(040 . 2.0) '(072 . 1) '(073 . 2) '(051 . 0.349006763285) (cons 007 (if (tblsearch "style" "SS") "SS" (getvar 'textstyle))) (cons 010 (polar mid (+ ang (/ pi 2.0)) 5.0)) (cons 011 (polar mid (+ ang (/ pi 2.0)) -2.5)) (cons 050 ang) (cons 001 (vl-string-translate "." "\u+00b7" (rtos dis 2 3))) ) ) (setq ber (rtos (rtd ang) 2 4)) (setq deg (substr ber 1 (vl-string-position (ascii ".") ber))) (setq min (substr ber (- (strlen ber) 3) 2)) (setq sec (AT:NumFix (vl-princ-to-string (lm:roundup (atoi (substr ber (- (strlen ber) 1))) 5)) 2)) (if (= sec "100") (progn (setq sec "00") (setq min (vl-princ-to-string (fix (+ (atoi min) 1)))) ) ) (setq ber (strcat deg "\u+00B0" min "\u+2032" sec "\u+2033")) (entmake (list '(000 . "TEXT") '(008 . "BEAR") '(062 . 256) '(006 . "BYLAYER") '(040 . 2.0) '(072 . 1) '(073 . 2) (cons 007 (if (tblsearch "style" "SU") "SU" (getvar 'textstyle))) (cons 010 (polar mid (+ ang (/ pi 2.0)) 2.5)) (cons 011 (polar mid (+ ang (/ pi 2.0)) 2.5)) (cons 050 ang) (cons 001 ber) ) ) ) ) (princ) ) Edited August 17, 2023 by exceed Quote
Clever_Elf Posted August 17, 2023 Author Posted August 17, 2023 Cheers Mhupp. That text stuff works alright. Stuck on the round bearing to 5" and display Minutes and seconds to be 05 not just 5 Quote
mhupp Posted August 17, 2023 Posted August 17, 2023 2 minutes ago, Clever_Elf said: Cheers Mhupp. That text stuff works alright. Stuck on the round bearing to 5" and display Minutes and seconds to be 05 not just 5 Think this should do it (defun AT:NumFix (s n) ;; Fix number string with leading zeros ;; s - Number string to fix ;; n - Number of characters for final string ;; Alan J. Thompson, 10.29.09 ;; (AT:NumFix i 2) i= 5 = 05 (if (< (strlen s) n) (AT:NumFix (strcat "0" s) n) s ) ) 3 Quote
Clever_Elf Posted August 17, 2023 Author Posted August 17, 2023 OMG just when I thought..... Final QA revels AutoCAD has gone mad and thinks north is east and a positive rotation is anti clockwise . It also thinks 62" is a thing. see below. Quote
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.