Lisp Question

Lisp Question

keshishian1
Participant Participant
813 Views
6 Replies
Message 1 of 7

Lisp Question

keshishian1
Participant
Participant

Hello,
I'm working on a LISP routine that should move all blocks named BB1, BB2, etc. to a layer called SDC-BUBBLES. However, the code below doesn’t seem to be functioning as expected. I'd really appreciate any suggestions ti make it work.

 

(defun c:MoveBlocksToLayer ( / ss i ent entData)
;; Select all block references named bb1 or bb2 from the drawing
(setq ss (ssget "_X" '((0 . "INSERT") (2 . "bb1,bb2"))))

;; If selection set is not empty, proceed
(if ss
(progn
(setq i 0) ;; Initialize counter
;; Iterate through all selected blocks
(repeat (sslength ss)
(setq ent (ssname ss i)) ;; Get the entity at index i
(setq entData (entget ent)) ;; Retrieve entity data list
;; Replace the layer (group 8) with "sdc-bubbles"
(setq entData (subst (cons 8 "sdc-bubbles") (assoc 8 entData) entData))
(entmod entData) ;; Apply the modified data to entity
(setq i (1+ i)) ;; Increment counter
)
(princ "\nBlocks moved to layer sdc-bubbles.") ;; Confirmation message
)
)
(princ) ;; Exit quietly
)

 

Also, I’d like to incorporate this into another routine I have (Print_SDC) so that everything runs in a single LISP command. Here's that additional code for reference that works fine.

 

(defun c:Print_SDC ()
(command "-LAYER" "OFF" "0-SDC-DYNAMIC-HIDE" "")
(command "HATCHTOBACK")

;; Define a list of layers to process
(setq layerList '("SDC-GRIDLINES" "SDC-PL" "SDC-BACKGROUND" "SDC-BACKGROUND-LIGHT" )) ; Add more layer names as needed

;; Loop through each layer and apply draw order
(foreach layerName layerList
(setq ss (ssget "X" (list (cons 8 layerName))))
(if ss
(command "._DRAWORDER" ss "" "_Back")
)
)
(princ)

;(initdia)
; (command "PLOT" "")
)

 

Thank You.

0 Likes
Accepted solutions (1)
814 Views
6 Replies
  • Lisp
Replies (6)
Message 2 of 7

Moshe-A
Mentor
Mentor

@keshishian1 hi,

 

The lisp is ok except (entmod) function expect the properties (e.g layer, ltype) you use, should be exist. so to solve this add the code to search for the layer and if it does not exist, create it. if your blocks are dynamic, change the first line to this:

(setq ss (ssget "_X" '((0 . "INSERT") (2 . "`*U*,bb1,bb2"))))

 

to incorporate lisp command from another lisp, call it like any other lisp function:

(c:MoveBlocksToLayer)

 

Moshe

 

0 Likes
Message 3 of 7

paullimapa
Mentor
Mentor

actually using subst & entmod if the layer does not exist, it'll create it automatically


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 4 of 7

Moshe-A
Mentor
Mentor
Accepted solution

@paullimapa ,

 

OPPS paul, you are right 😀

 

since the program is ok, i guess dynamic blocks is the problem

here is the full fix

 

(defun c:MoveBlocksToLayer ( / ss i ent entData AcDbBlkRef)
 ;; Select all block references named bb1 or bb2 from the drawing
 (setq ss (ssget "_X" '((0 . "INSERT") (2 . "`*U*,bb1,bb2"))))
 ;; If selection set is not empty, proceed
 (if ss
  (progn
   (setq i 0) ;; Initialize counter
   ;; Iterate through all selected blocks
   (repeat (sslength ss)
    (setq ent (ssname ss i)) ;; Get the entity at index i
    (setq AcDbBlkRef (vlax-ename->vla-object ent))
    (if (wcmatch (strcase (vla-get-effectiveName AcDbBlkRef)) (strcase "bb1,bb2"))
     (progn
      (setq entData (entget ent)) ;; Retrieve entity data list
      ;; Replace the layer (group 8) with "sdc-bubbles"
      (setq entData (subst (cons 8 "sdc-bubbles") (assoc 8 entData) entData))
      (entmod entData) ;; Apply the modified data to entity
     ); progn
    ); if
    (setq i (1+ i)) ;; Increment counter
    (vlax-release-object AcDbBlkRef)
   ); repeat
   (princ "\nBlocks moved to layer sdc-bubbles.") ;; Confirmation message
  ); progn
 ); if
  
 (princ) ;; Exit quietly
)

 

0 Likes
Message 5 of 7

paullimapa
Mentor
Mentor

Learned that from @Kent1Cooper 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 6 of 7

keshishian1
Participant
Participant

Thank You, this works fine and yes they are all dynamic blocks.

0 Likes
Message 7 of 7

keshishian1
Participant
Participant

Thank You! This works great.

0 Likes