Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

lisp to insert a block, explode and rename the nested block with a suffix

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
tim
Participant
3252 Views, 11 Replies

lisp to insert a block, explode and rename the nested block with a suffix

ok so i have a block that i want to insert several times and revised each one to be different to do this i would have to insert it and rename the blocks that come in and then reinsert i would like to insert and have a lisp explode the main block and rename the other 2 blocks with a suffix at the end depending on how many times its in the drawing... can this even be done? My original block name that will be first inserted is "BENT PULLEY NOTES" it needs to explode after i drop it in and then there are 2 blocks within that called "Bent Pulley" and "Bent Pulley DYN" that i was hoping to keep the names but add a number at the end depending on how many are in the drawing already... any help would be great thanks..

 

here is a lisp that i have found thanks to the original author by the way... it would work if i didn't have nested blocks and only wanted the one block to be insert and not explode...

 

(defun c:Test (/ e i name Bname )
(setq e nil)
(command "_.-insert" "Drawing1.dwg" pause "" "" "")
(if (setq e (entlast))
  (progn
    (setq i 1)
    (setq name "My Block")
    (setq Bname (strcat name (itoa i)))
    (while (tblsearch "BLOCK" Bname)
      (setq Bname (strcat name (itoa (setq i (1+ i)))))
    )
    (command "_.-rename" "Block" (cdr (assoc 2 (entget e))) Bname )
     )
  (princ)
)
(princ)
)

11 REPLIES 11
Message 2 of 12
Kent1Cooper
in reply to: tim


@tim wrote:

... i have a block that i ... would like to insert and have a lisp explode the main block and rename the other 2 blocks with a suffix at the end depending on how many times its in the drawing.... My original block name that will be first inserted is "BENT PULLEY NOTES" it needs to explode after i drop it in and then there are 2 blocks within that called "Bent Pulley" and "Bent Pulley DYN" that i was hoping to keep the names but add a number at the end depending on how many are in the drawing already...

....


Assuming you do not ever retain [or already have in the drawing] Block definitions of "Bent Pulley" and "Bent Pulley DYN" without any numerical suffix, something like this, perhaps [untested]:

 

(defun c:Test (/ i)
  (command
    "_.insert" "BENT PULLEY NOTES" pause "" "" ""
    "_.explode" "_last" ""

;; [above line seems to work both with and without final "" in its own (command) function

;; in isolation at Command: line, but may need to be one way or the other in longer routine]
  ); command
  (foreach bname '("Bent Pulley" "Bent Pulley DYN")
    (setq i 1)
    (while (tblsearch "block" (strcat bname i)); is there one already with that number at the end?
      (setq i (1+ i)); if so, increase the number and look for the next
    ); while
    (command "_.rename" "block" bname (strcat bname i)); suffix whatever the number got up to
  ); foreach
  (princ)
); defun

 

You could also eliminate the Explode command by Inserting the drawing already exploded, with an asterisk before the name:
    "_.insert" "*BENT PULLEY NOTES"

You wouldn't see it dragging around at the insertion-point prompt, which may be a reason not to go that route.  But if you do, eliminate one of the "" Enters, because it will only ask for one scale factor.

Kent Cooper, AIA
Message 3 of 12
tim
Participant
in reply to: Kent1Cooper

Thanks for the reply and i would say yes to the option of it being exploded already but with others using it i think they may have issues with it not showing for them to see where its inserting but thanks... i ran what you had put together and i think i am missing something i get an error that reads:

 

Command: test _.insert Enter block name or [?] <BEND PULLEY NOTES>: BEND PULLEY NOTES.dwg Duplicate definition of block _ARCHTICK  ignored. Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate]: Enter X scale factor, specify opposite corner, or [Corner/XYZ] <1>: Enter Y scale factor <use X scale factor>: Specify rotation angle <0>: Command: _.explode Select object: _last Command: TEST Unknown command "TEST".  Press F1 for help.

Command: ; error: bad argument type: stringp 1

 

any thoughts... i am pretty new to these

Message 4 of 12
hmsilva
in reply to: tim

tim,  

Change

 

(strcat bname i)

 

to

 

(strcat bname (itoa i))

 

in (tblsearch) and (rename)

 

 

Henrique

EESignature

Message 5 of 12
tim
Participant
in reply to: tim

sorry i don't follow there are 2 places it states that so i changed them and get

 

error: malformed list on input

 

Message 6 of 12
hmsilva
in reply to: tim

in Kent Cooper's code change

 

(while (tblsearch "block" (strcat bname i)); i is an integer and requires a string

to

(while (tblsearch "block" (strcat bname (itoa i))); itoa converts an integer into a string

and

(command "_.rename" "block" bname (strcat bname i));

to

(command "_.rename" "block" bname (strcat bname (itoa i)))

EESignature

Message 7 of 12
tim
Participant
in reply to: hmsilva

Thanks everyone works great i had to add a purge at the end to purge out the first block and it works great thanks for all the help

Message 8 of 12
JamesMaeding
in reply to: tim

Tim,

while we are happy to help, you must wait for us which may be inconvenient sometimes.

In that light, try opening the visual lisp editor (type VLIDE at command line), and opening the lisp file.

 

You can easily detect missing or extra parens by double clicking before or after a paren.

It highlights the code to the closing paren, and you can see if it went too far.

 

So try double clicking right before the paren on the defun. It should highlight to the end of the function.

If not, do that for inner blocks of code until you find a spot where it stops short or long from where it should have.

That VLIDE is super slick for this purpose, and you can also check code with Tools->check code in editor.

No need to be stuck waiting.


internal protected virtual unsafe Human() : mostlyHarmless
I'm just here for the Shelties

Message 9 of 12
tim
Participant
in reply to: JamesMaeding

good to know thanks i will work with it Smiley Surprised

Message 10 of 12
pbejse
in reply to: tim


@tim wrote:

ok so i have a block that i want to insert several times and revised each one to be different to do this i would have to insert it and rename the blocks that come in and then reinsert i would like to insert and have a lisp explode the main block and rename the other 2 blocks with a suffix at the end depending on how many times its in the drawing... can this even be done? My original block name that will be first inserted is "BENT PULLEY NOTES" it needs to explode after i drop it in and then there are 2 blocks within that called "Bent Pulley" and "Bent Pulley DYN" that i was hoping to keep the names but add a number at the end depending on how many are in the drawing already... any help would be great thanks..

 


Another approach

 

<Not thoroughly tested>

 

(Defun c:test2 (/ _taken ss e)
(vl-load-com)
  (defun _taken	(nm i / nme)
    (if	(tblsearch "Block" (setq nme (strcat nm (itoa i))))
      (_taken nm (1+ i))
      nme
    ) ;_ end of if
  ) ;_ end of defun
 (repeat 4
  (vla-purgeall (vla-get-activedocument (vlax-get-acad-object)))
 )  
  (command
    "_.insert" "BENT PULLEY NOTES" "_scale" "1"	pause "" "_.explode" "_last"
;; [above line seems to work both with and without final "" in its own (command) function
;; in isolation at Command: line, but may need to be one way or the other in longer routine]
   ) ;_ end of command
  (if (setq ss (ssget "_P" '((0 . "insert") (2 . "Bent Pulley,Bent Pulley DYN"))))
    (repeat (sslength ss)
      (setq e (vlax-ename->vla-object (ssname ss 0)))
      (vlax-invoke e 'ConvertToStaticBlock (_taken (vla-get-effectivename e) 1))
      (ssdel (ssname ss 0) ss)
    ) ;_ end of repeat
  ) ;_ end of if
  (princ)
)

 

HTH

 

Message 11 of 12
Kent1Cooper
in reply to: hmsilva


@hmsilva wrote:

tim,  

Change

(strcat bname i)

to

(strcat bname (itoa i))

....


[Absolutely.  Sorry for leading you astray -- that's what comes of not testing things before posting them, but I was too lazy to set up the Block structure to try it out....]

Kent Cooper, AIA
Message 12 of 12
hmsilva
in reply to: Kent1Cooper

Kent Cooper,

I was trying to answer the same post,

but as always, it took me much longer ...

 

Cheers

Henrique

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost