Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Member
Paul1084
Posts: 5
Registered: ‎10-18-2010
Message 1 of 9 (1,701 Views)
Accepted Solution

Can I get Autolisp to create blocks and name them in sequence?

1701 Views, 8 Replies
07-13-2012 04:25 AM

Hi,

 

I am looking for some help. i have created an Autolisp routine that creates a drawing made up of structural members, x-refs & surfaces based on a few different user inputs. I now want the program to insert the drawing as a block rather than individual elements. My Idea was simply to use the block command in the lisp routine to create a block and then have the drawing create itself in block editor, and close once it has done. however I am having difficulty thinking of a way that I can get AutoCAD to automatically name this block depending on other blocks in the drawing, i.e. in sequence. The name is irrelevent really, but it would make sence for it to be logical, so... if the last block inserted was "b7" then the next one should be "b8" and so on.

 

Any Ideas? or am I just expecting too much? or is there a better way of doing it all together? I have read a bit into "entmake" but i'm not sure on how to use that function.

 

any help will be much appreciated

 

Cheers


Paul1084 wrote:
I now want the program to insert the drawing as a block rather than individual elements. My Idea was simply to use the block command in the lisp routine to create a block and then have the drawing create itself in block editor, and close once it has done. however I am having difficulty thinking of a way that I can get AutoCAD to automatically name this block depending on other blocks in the drawing, i.e. in sequence. The name is irrelevent really, but it would make sence for it to be logical, so... if the last block inserted was "b7" then the next one should be "b8" and so on.



Example .... and you should change the name of Block with the full path of the block 's location as highlighted

in red in the routine .

 

(defun c:Test (/ e i name Bname )
(setq e nil)
 (command "_.-insert" "C:\\Users\\Tharwat\\Desktop\\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) )

 

*Expert Elite*
dbroad
Posts: 1,604
Registered: ‎08-16-2007
Message 2 of 9 (1,686 Views)

Re: Can I get Autolisp to create blocks and name them in sequence?

07-13-2012 05:34 AM in reply to: Paul1084

Keep track of things created during the command by adding them to a selection set.  Then use the selection set when creating the block (either with the block command, the entmake function, or using vla methods.)  Then insert the block created.  It may get complicated trying to decide on names.  If you are interested in entmake, do a search on that term here.  You will get hundreds of posts of illustrations of how to use it.  With entmake, you will need to refer to the dxf codes for blocks.

Distinguished Mentor
_Tharwat
Posts: 612
Registered: ‎07-02-2010
Message 3 of 9 (1,680 Views)

Re: Can I get Autolisp to create blocks and name them in sequence?

07-13-2012 05:53 AM in reply to: Paul1084

Paul1084 wrote:
I now want the program to insert the drawing as a block rather than individual elements. My Idea was simply to use the block command in the lisp routine to create a block and then have the drawing create itself in block editor, and close once it has done. however I am having difficulty thinking of a way that I can get AutoCAD to automatically name this block depending on other blocks in the drawing, i.e. in sequence. The name is irrelevent really, but it would make sence for it to be logical, so... if the last block inserted was "b7" then the next one should be "b8" and so on.



Example .... and you should change the name of Block with the full path of the block 's location as highlighted

in red in the routine .

 

(defun c:Test (/ e i name Bname )
(setq e nil)
 (command "_.-insert" "C:\\Users\\Tharwat\\Desktop\\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) )

 

____________________________________________________
Get learn and learn and practice , to become experienced guy .
Member
Paul1084
Posts: 5
Registered: ‎10-18-2010
Message 4 of 9 (1,677 Views)

Re: Can I get Autolisp to create blocks and name them in sequence?

07-13-2012 05:57 AM in reply to: dbroad

Thanks for the reply, I had thought of that way initially, I assume that is a better method than using the block editor then? My main problem is still how to name the blocks. I think entmake is probably the way forward perhaps writing the entities to an anonymous block? ... If all else fails, I will just prompt for another user input to name the block, I could possibly make use of this elsewhere in the routine. For the amount of times this routine and many similar routines will be used, I wanted to reduce user input to a minimum.

Distinguished Mentor
_Tharwat
Posts: 612
Registered: ‎07-02-2010
Message 5 of 9 (1,671 Views)

Re: Can I get Autolisp to create blocks and name them in sequence?

07-13-2012 06:54 AM in reply to: Paul1084

Paul .

 

Did not my code help you at all with your needs ?

____________________________________________________
Get learn and learn and practice , to become experienced guy .
Member
Paul1084
Posts: 5
Registered: ‎10-18-2010
Message 6 of 9 (1,669 Views)

Re: Can I get Autolisp to create blocks and name them in sequence?

07-13-2012 07:06 AM in reply to: _Tharwat

Thanks Tharwat, sorry for the delay in my reply, I am still new to Lisp, takes me a while to get my head around and fully understand a piece of code :s

 

This works great thanks, I have altered it to work with writing a selection set to a block with the code you have written to name that block (see below). However the only thing confusing me now, is why once written to a block, it dissapears?

 

ps. sorry I'm not sure how you paste code in here so it displays as clearly and correctly as yours did?

 

(defun c:Test (/ e i name Bname )

(setq e nil)

(setq ip (getpoint "\nInsertion Point : "))

 

(if (setq e (ssget))  

    (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 "_.-block" Bname ip e "" )     

       )  

    (princ) 

)

(princ)

)

Distinguished Mentor
_Tharwat
Posts: 612
Registered: ‎07-02-2010
Message 7 of 9 (1,663 Views)

Re: Can I get Autolisp to create blocks and name them in sequence?

07-13-2012 07:28 AM in reply to: Paul1084

You may mean this code which would ask you to select a block and after that it would reinsert it with all its properties and rename it as well . (this is what I understood from your modifications of my previous codes .

 

 

(defun c:Test (/ e i name Bname ip x)
  (prompt "Select single block to copy and to rename it ...... ")
  (if (and (setq e (ssget "_+.:S" '((0 . "INSERT"))))
           (setq ip (getpoint "\nInsertion Point : "))
      )
    (progn
      (setq i 1 name "My Block")
      (setq Bname (strcat name (itoa i)))
      (while (tblsearch "BLOCK" Bname)
        (setq Bname (strcat name (itoa (setq i (1+ i)))))
      )
      (command "_.-insert"
               (cdr (assoc 2 (entget (ssname e 0))))
               ip
               ""
               ""
               ""
      )
      (setq x (entlast))
      (command "_.explode" x "")
      (command "_.-block" bname ip (ssget "_P") "")
      (command "_.erase" (ssget "_P") "")
      (command "_.-insert" bname ip "" "" "")
    )
    (princ)
  )
  (princ)
)

 

____________________________________________________
Get learn and learn and practice , to become experienced guy .
Member
Paul1084
Posts: 5
Registered: ‎10-18-2010
Message 8 of 9 (1,658 Views)

Re: Can I get Autolisp to create blocks and name them in sequence?

07-13-2012 07:39 AM in reply to: _Tharwat

What I have is a collection of entities, drawn using a lisp routine. in that routine, I am going to add all those entities to a selection set, so at the end, I can simply write that selection set to a block to keep the drawing organised, and make it easier to edit. Your initial code worked great, it was just that once the entities were written to a block, the block dissapeared, the block definition was there, I guess I can just tell autoCAD to re-insert the block

New Member
jaapmoggre
Posts: 1
Registered: ‎01-28-2013
Message 9 of 9 (1,368 Views)

Re: Can I get Autolisp to create blocks and name them in sequence?

01-28-2013 02:29 AM in reply to: Paul1084

Create Block names with the help of one of the ‘empty’ system variables useri1 up to useri5

The USERI#-system variables can be used to store an integer. Use this system variable as a counter.

 

Create the Blockname in this way:

 

(setq name (strcat “autoblk (itoa (+ (getvar “useri2”) 1))))

(setvar “useri2” (+ (getvar “useri2”) 1)) 

; next time useri2 is ready for creating a new unique name

 

Jaap Moggre, Leiden, the Netherlands

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.