Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
tim
Member
tim
Posts: 5
Registered: ‎10-22-2007
Message 1 of 12 (1,450 Views)
Accepted Solution

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

1450 Views, 11 Replies
11-30-2012 12:57 PM

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

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

*Expert Elite*
Kent1Cooper
Posts: 5,796
Registered: ‎09-13-2004
Message 2 of 12 (1,443 Views)

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

11-30-2012 01:48 PM 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
tim
Member
tim
Posts: 5
Registered: ‎10-22-2007
Message 3 of 12 (1,438 Views)

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

11-30-2012 02:06 PM 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

*Expert Elite*
hmsilva
Posts: 3,200
Registered: ‎12-17-2004
Message 4 of 12 (1,427 Views)

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

11-30-2012 02:28 PM in reply to: tim

tim,  

Change

 

(strcat bname i)

 

to

 

(strcat bname (itoa i))

 

in (tblsearch) and (rename)

 

 

Henrique

tim
Member
tim
Posts: 5
Registered: ‎10-22-2007
Message 5 of 12 (1,417 Views)

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

11-30-2012 02:59 PM 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

 

*Expert Elite*
hmsilva
Posts: 3,200
Registered: ‎12-17-2004
Message 6 of 12 (1,410 Views)

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

11-30-2012 03:11 PM 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)))

tim
Member
tim
Posts: 5
Registered: ‎10-22-2007
Message 7 of 12 (1,404 Views)

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

11-30-2012 03:29 PM 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

Distinguished Contributor
jmaeding
Posts: 1,045
Registered: ‎12-27-2006
Message 8 of 12 (1,401 Views)

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

11-30-2012 03:32 PM 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()
http://onesmallstepforcad.com/

tim
Member
tim
Posts: 5
Registered: ‎10-22-2007
Message 9 of 12 (1,398 Views)

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

11-30-2012 03:34 PM in reply to: jmaeding

good to know thanks i will work with it :smileysurprised:

*Expert Elite*
pbejse
Posts: 2,502
Registered: ‎11-24-2009
Message 10 of 12 (1,378 Views)

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

11-30-2012 11:01 PM 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

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!