@harimaddddy
It has to do with how you are getting the elevation numbers from the text: 1) since there is more than 1 text, the 1st entity in the selection set is not always the "EL. X", 2) the negative "-" sign does not equate to a number and it skips it in your loop to look for numbers. It's better just to look for a specific string and parse it as always having the same format.
Other things:
- Doing a "move to nowhere" is not the best way to get the non-text out of the selection set. it's better just to iterate through the selection and filter out the non-text entities into a new selection set.
- Note I use command modifiers as standard practice, "_" helps if using non-english versions, "." ensures you are always using the original AutoCAD command even if someone has over written it with a LISP function.
- I added some error handling "IF" statements to prevent errors if the user doesn't select the proper items or if they do not select / enter anything for the displacement points.
- I use "_non" modifiers on commands that ask for points to prevent any object snaps from interfering with the copies. Not totally necessary, but I do it standard whenever i use predefined points in a command.
- I localized all the variables
- I added undo marks and turned off the CMDECHO system variable to get rid of all the extra prompts being shown.
Here is my update to your program:
; Source:https://www.cadtutor.net/forum/topic/68426-create-automatic-level-in-metric/
; Re-written by PJK 11/14/2024
(Defun c:BT (/ a b ct i lset oe n sst sset str te tv)
(command "._undo" "_be")
(setq oe (getvar "cmdecho"))
(setvar "cmdecho" 0)
(if (setq sst (ssget))
(progn
(setq sset (ssget "P" '((0 . "TEXT")))
lset (ssadd)
)
; Filter out non-text and add to new selection "lset"
(repeat (sslength sst)
(if (/= "TEXT" (cdr (assoc 0 (entget (ssname sst (setq i (if i (1+ i) 0)))))))
(ssadd (ssname sst i) lset)
)
)
; search through the text selection set to find the Elevation note.
(repeat (sslength sset)
(setq te (ssname sset (setq n (if n (1+ n) 0)))
tv (cdr (assoc 1 (entget te)))
)
; if the prefix is found,
(if (= "EL." (substr tv 1 3))
(progn
; get the numeric value and save the entity.
(setq str (distof (substr tv 5)) ct te)
; remove it from the selection set.
(ssdel te sset)
)
)
)
; if everything is found and the source and destination points are supplied,
(if (and lset sset str
(setq a (getpoint "\n Specify the source point: "))
(Setq b (getpoint a "\n Specify the Desitnation point: "))
)
(progn
;; Copy the elevation text up.
(command "._copy" ct "" "_non" a "_non" b)
;; change the elevation text to the new value.
(command "change" (entlast) "" "" "" "" "" "" (strcat "EL. " (rtos (+ str (- (cadr B )(cadr A))) 4 2)))
; Copy the rest of the stuff.
(command "._Copy" lset sset "" "_non" a "_non" b)
)
(princ "\nWrong selection or no displacement supplied.")
)
)
)
(command "._undo" "_end")
(setvar "cmdecho" oe)
(princ)
)