• This forum is the machine-generated translation of www.cad3d.it/forum1 - the Italian design community. Several terms are not translated correctly.

apply lisp function to selected blocks

psluca91

Guest
Good afternoon to all,

I apologize in advance if the subject has already been dealt with in other topics and for the banality of the matter, but I have not been able to find a guide or discussions that help me through the search function.

I wrote the following lisp by taking code from other lisp found on the net:
Code:
;;change color by layer in the respective color and layer "0"
(defun c:ccb (/ b_name col)
(setq b_name (cdr (assoc 2 (entget (car)))))))
(command "-bedit" b_name)
(setq listobj (ssget "x"))

(setq nobj (sslength listobj)

(setq control 0)

(while (< nobj control)




(setq obj (entget (ssname listobj control) ))
(setq color (assoc '62 obj))

(if (= color nil)
(progn)
(setq objlay (assoc '8 obj))
(setq laycol (cdr (assoc '62 (tblsearch "layer"))))
(command "_change" (cdar obj) "_prop" "_color" laycol ")
);
);

(setq control (+ control 1))

) ; while control closure
(command "_chprop" "_all" "_layer" 0 "_bclose"")
); defunct closure
the lisp works but I have to select the blocks one by one. How can I do to perform the lisp on multiple blocks at once?

thanks in advance!
 
you name the block to change to the variable b_name(setq b_name (cdr (assoc 2 (entget (car)))))))if you want to select more than one block, you must first select (by means of a thing like SECTION)
a list of blocks and assign them to a variable, we say blklist.
to not make the lisp go wrong, I recommend a filtering on the ssget that only take the blocks (ssget(0. "block")then count how many blocks you have selected (sslength blklist)and a variable check - say oneset a counter we will call count a 0 (sec count 0)

and then pack all your lisp into a repeat of how many items you have on the list blklist

;;change color by layer in the respective color and layer "0"
(defun c:ccb (/ b_name col)
multiple selection and calculation number of objects

(
repeat one
(
sec b_name ssname blklist count)
(
sec count (+ count 1)).... your lisp

) ;; closure of repeat

); defunct closure
 
Crystal, while I thank you for the quick answer!

I tried to create the code as from your suggestion, but I found errors:

- if imposed (setq blklist (ssget '(0 . "block")))))), it excludes me from the selection (including blocks) and I cannot therefore start the lisp that gives me the following error: "select objects: ; error: bad argument type: lselsetp nil".

- if I remove the "block" filter and manually select the blocks, it always gives me the same mistake... .

I'll get the code back from me.
Code:
;;;cambia colore blocco e layer 0
(defun c:ccb4 (/ b_name col)
(setq blklist (ssget ))

(setq num (sslength listaobj))

(setq count 0)

(repeat num
(setq b_name ssname blklist count)
(setq count (+ count 1))
(command "-bedit" b_name)
(setq listaobj (ssget "x"))

(setq nobj (sslength listaobj))

(setq controllo 0)

(while (< controllo nobj)




 (setq obj (entget (ssname listaobj controllo) ))
 (setq color (assoc '62 obj))

  (if (= color nil)
   (progn
    (setq objlay (assoc '8 obj))
    (setq laycol (cdr (assoc '62 (tblsearch "layer" (cdr objlay)))))
    (command "_change" (cdar obj) "" "_prop" "_color" laycol "")
    );; chiusura progn
   );; chiusura if color nil

(setq controllo (+ controllo 1))

) ;;chiusura while controllo
(command "_chprop" "_all" "" "_layer" 0 "" "_bclose" "")
) ;;; chiusura repeat
);;chiusura defun
Where am I wrong?
 
I found a mistake here:

setq blklist (ssget )

(setq num (sslength listobj)

correct in:

setq blklist (ssget )

(setq num (sslength blklist)

now the command starts, but it stops at the opening of the block, where it appears to me as the name of the block "-change" and inside the block there is nothing. In the command bar, this word appears to me:
"command: ccb4
select objects: opposite corner: 2 found
select objects: - What?
enter block name or [?]: :
Invalid block name.
Enter block name or [?]: _change regenerating model.
command: <bad entity="" ffa818b0="" name:="">"</bad>
 
(0) “Block” must become (0 . "insert") - this for filtering

(setq num (sslength) listaobj) - count the number of entities to a list that does not exist. test by replacing it with (setq num (sslength blklist))
 
ho apportato le modifiche come da te suggerito, ma continua a bloccarsi all'interno del editor blocchi di un blocco inesistente....

queste le righe di comando che mi escono...

command: _appload ccb5.lsp successfully loaded.
command: ; error: malformed list on input
command:
command: ccb5
select objects: specify opposite corner: 2 found
select objects: -bedit
enter block name or [?]: :
Invalid block name.
Enter block name or [?]: _change regenerating model.
command: <bad a468a920="" entity="" name:=""></bad>
 
the problem lies in the use of the bedit command.

the name of the entity you hold in the variable b_name is the inner name of that and only that entity.
if you have 5 instances of the block poplar, you will have 5 values b_name different, I don't know if I was clear

to the bedit command you must pass the name of the block, not that of its instance, otherwise bedit believes you want to edit a block that is called <entity 7ff75520dc10="" name:=""></entity> e non il poplar that you want to edit

How to solve? quickly, assigning the block name (not its instance) to a new variable, we say Name_blcome si fa?
(setq name_bl (cdr (assoc '2) b_name))

you are saying assign to name_bl the remaining part (cdr) of group 2 (assoc '2) taking it from the complete list (entget) of the entity b_name.

at this point at the bedit you do not have to pass more b_name, but name_bl
(command "-bedit" name_bl)

However, since you select a number of different blocks in which there will be more instances of the same block, you will find yourself editing the same block already edited several times, and I don't know if this can generate further program crashes.
you may also have problems on dynamic blocks, as these last ones when varied compared to the "base" take a new name, anonymous type (start for asterisk)

to have more speed of execution you should deepen the tblsearch command and try to get the names of the existing blocks in the design so as to edit once the source block, not its instances
 

Forum statistics

Threads
44,997
Messages
339,767
Members
4
Latest member
ibt

Members online

No members online now.
Back
Top