Jump to content

LISP to select lines and text according to "Z" values


Recommended Posts

Posted (edited)

If all the primitives (TEXT,MTEXT,INSERT,LINE,LWP,Circle,Mleader,Dimension) are perceived as objects, 
then you can use this code:
 

;; Set coordinates of objects (TEXT,MTEXT,INSERT,LINE,LWP,Circle,Mleader,Dimension) Z in 0
(defun zeroz-in-list (lst /)
  (cond
    ;; The list of coordinates-vertex
    ((and (listp lst)
          (= (length lst) 2)
          (numberp (car lst))
          (numberp (cadr lst)))
      ;; if only XY - expanding to XYZ
      (list (car lst) (cadr lst) 0.0)
    )
    ((and (listp lst)
          (= (length lst) 3)
          (numberp (car lst))
          (numberp (cadr lst))
          (numberp (caddr lst)))
     ;; if already XYZ -  just do Z=0.0
     (list (car lst) (cadr lst) 0.0)
    )
    ;; If it's a large list, it's probably nested
    ((listp lst)
     (mapcar 'zeroz-in-list lst)
    )
    (t lst)
  )
)

(defun c:ObjZ0 (/ ss n e el newel)
  (prompt "
Select objects (all types, including polylines, mleader, dimension): ")
  (if (setq ss (ssget))
    (progn
      (setq n 0)
      (while (< n (sslength ss))
        (setq e (ssname ss n)
              el (entget e)
              newel nil
        )
        (foreach pair el
          (cond
            ;; height LWPOLYLINE (code 38)
            ((and (= (car pair) 38) (numberp (cdr pair)))
             (setq newel (cons (cons 38 0.0) newel))
            )
            ;;Point codes (for example, 10, 11, 12...), etc.
            ((and (numberp (car pair))
                  (not (= (car pair) 210))) ; Don't touch the normal
             (if (listp (cdr pair))
                 (setq newel (cons (cons (car pair) (zeroz-in-list (cdr pair))) newel))
                 (setq newel (cons pair newel))
             )
            )
            ;; The rest
            (t (setq newel (cons pair newel)))
          )
        )
        ;; Restoring order and modifying the object
        (entmod (reverse newel))
        (setq n (1+ n))
      )
    )
  )
  (princ)
)

If the Z coordinate is not displayed in the properties (example, for dimensions, for Mleader), then you need to use the _LIST command.

Don't think of me as a programmer...
The code is written using AI.

Edited by Nikon
  • Like 1
Posted
On 8/31/2025 at 5:03 PM, Steven P said:

 

Use what you have done previously but for dx code 10:

 

          (foreach dxf le
            (if (= (car dxf) 10)
              (setq le (subst (cons 10 (zeroz (cdr dxf))) dxf le))
            )
          )

 

I applied this and the problem is solved for Hatches, too. Thank you 🙏

  • Like 1
Posted
On 8/31/2025 at 5:25 PM, Nikon said:

If all the primitives (TEXT,MTEXT,INSERT,LINE,LWP,Circle,Mleader,Dimension) are perceived as objects, 
then you can use this code:
 

;; Set coordinates of objects (TEXT,MTEXT,INSERT,LINE,LWP,Circle,Mleader,Dimension) Z in 0
(defun zeroz-in-list (lst /)
  (cond
    ;; The list of coordinates-vertex
    ((and (listp lst)
          (= (length lst) 2)
          (numberp (car lst))
          (numberp (cadr lst)))
      ;; if only XY - expanding to XYZ
      (list (car lst) (cadr lst) 0.0)
    )
    ((and (listp lst)
          (= (length lst) 3)
          (numberp (car lst))
          (numberp (cadr lst))
          (numberp (caddr lst)))
     ;; if already XYZ -  just do Z=0.0
     (list (car lst) (cadr lst) 0.0)
    )
    ;; If it's a large list, it's probably nested
    ((listp lst)
     (mapcar 'zeroz-in-list lst)
    )
    (t lst)
  )
)

(defun c:ObjZ0 (/ ss n e el newel)
  (prompt "
Select objects (all types, including polylines, mleader, dimension): ")
  (if (setq ss (ssget))
    (progn
      (setq n 0)
      (while (< n (sslength ss))
        (setq e (ssname ss n)
              el (entget e)
              newel nil
        )
        (foreach pair el
          (cond
            ;; height LWPOLYLINE (code 38)
            ((and (= (car pair) 38) (numberp (cdr pair)))
             (setq newel (cons (cons 38 0.0) newel))
            )
            ;;Point codes (for example, 10, 11, 12...), etc.
            ((and (numberp (car pair))
                  (not (= (car pair) 210))) ; Don't touch the normal
             (if (listp (cdr pair))
                 (setq newel (cons (cons (car pair) (zeroz-in-list (cdr pair))) newel))
                 (setq newel (cons pair newel))
             )
            )
            ;; The rest
            (t (setq newel (cons pair newel)))
          )
        )
        ;; Restoring order and modifying the object
        (entmod (reverse newel))
        (setq n (1+ n))
      )
    )
  )
  (princ)
)

If the Z coordinate is not displayed in the properties (example, for dimensions, for Mleader), then you need to use the _LIST command.

Don't think of me as a programmer...
The code is written using AI.

WOW. Great. It works 👌

Thank you 🙏

Posted
19 hours ago, pefi said:

Did you try FLATTEN command?

The FLATTEN command explodes MLeader and does not work with dimensions.

Posted

As I posted in the other similar thread, I have found for most cases both FLATTEN and FLATSHOT of very little use.

 

Posted somewhere on here are some TOOLBAR MACROs, which IMO does a better job for not too complex drawings to simply get a lot of basic objects to Z=0, as well as LISPs using the same method of moving everything then moving them all back.

 

Initially it was needed for LT, not sure if something better can be used for newer LT with LISP.

 

I'll repost the other thread which gives more on FLATTEN more complicated objects.

 

Performance helps for Large-Scale Z-Flattening (Z0) AutoLISP Routine? - AutoLISP, Visual LISP & DCL - AutoCAD Forums

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...