Jump to content

Recommended Posts

Posted

Hi friends 👋 

Source:https://www.cadtutor.net/forum/topic/68426-create-automatic-level-in-metric/
(Defun c:BT()
  (Setq sst(ssget)
    sset(ssget "p" '((0 . "text")))
    tv (cdr(Assoc 1 (Entget(ssname sset 0)))))
  (command "move" sst "" "@" "@")
  (setq lset (ssget "p" '((0 . "line"))))
    (Setq st 1)
  (while (= 0.0 (ATOF (substr tv st 1)))
    (Setq st (+ 1 st)))
  (Setq et 1)
  (while (/=  "\""  (substr tv et 1))
    (Setq et (+ 1 et)))
  (setq str(distof (substr tv st
            (- et st))))
  (command "copy" sset "" (setq a (getpoint "\n Specify the source point: "))
       (Setq b(getpoint "\n Specify the Desitnation point: " a)))
(if (< (- (cadr a )(cadr b)) 0)
  (command "change" (entlast) "" "" "" "" "" "" (strcat "EL. "(rtos (+ str (- (cadr B )(cadr A)))4 2)""))
  (command "change" (entlast) "" "" "" "" "" "" (strcat "EL. "(rtos (+ str (- (cadr B )(cadr A)))4 2)"")))
  (command "Copy" lset "" a b)
  )

 

I use a command in CAD to mark elevations, and it works well with positive elevation values in imperial units. For example, if I move a marker like 'EL. 1'-0"' up by 2', it correctly updates to 'EL. 3'-0".' However, it doesn't handle negative values as expected. If I move 'EL. -1'-0"' by 2', it changes to EL. -3'-0"' instead of the correct EL. 1'-0". Another example is if I move EL. -5'-0" up by 2', it should update to 'EL. -3'-0",' but instead, it incorrectly changes to EL. -7'-0".

 

Thanks in advance for your help.

Posted

I don't have time to dig all the way down. Is it possible you're looking for digits only when you parse your text? By taking only digits, you're ignoring any minus sign that might be there.

 

It would help if you documented your variable names.

  • Thanks 1
Posted

@harimaddddy

It would also Help if you provide a simple example DWG with Before and after. I don't have any idea by your description what your selecting and what your intent is with it. 

  • Thanks 1
Posted (edited)

Waiting for dwg but I would use. Note space after setq in some cases with no space can be interpreted as a defun. Makes code easier to read.

 

(Setq sst (ssget '(0 . "TEXT,LINE")))

 

Edited by BIGAL
  • Like 1
  • Thanks 1
Posted (edited)

Hi @BIGAL @pkenewell @CyberAngel. Attached has the example. Thank you all for your kind help.

When you work with elevation marks, 0'-0" is your starting point. This point can be below or above ground level. If something with a negative elevation (like -2') moves up towards 0'-0", its elevation number gets smaller. For example, if it moves from -2' to -1', that’s a decrease in the elevation value.

On the other hand, if it moves down from 0'-0", the elevation number will get bigger in a negative way. So, moving from -2' to -3' means the number is increasing, even though it’s still below zero. It can be a bit confusing, but that’s how it works!

 

Thanks alot. This could help me alot.

Elevation mark.dwg

Edited by harimaddddy
Posted (edited)

@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)
)

 

Edited by pkenewell
  • Like 1
  • Thanks 1
Posted

Hi @pkenewell it works very well as expected. Thanks alot. And i have been learnt a new code structure.

  • Like 1
Posted

@harimaddddy Your welcome. Glad it is working for you and you learned a bit. 😀

  • Thanks 1

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...