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

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

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
Paul1084
4731 Views, 8 Replies

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

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

8 REPLIES 8
Message 2 of 9
dbroad
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.

Architect, Registered NC, VA, SC, & GA.
Message 3 of 9
_Tharwat
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) )

 

Message 4 of 9
Paul1084
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.

Message 5 of 9
_Tharwat
in reply to: Paul1084

Paul .

 

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

Message 6 of 9
Paul1084
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)

)

Message 7 of 9
_Tharwat
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)
)

 

Message 8 of 9
Paul1084
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

Message 9 of 9
jaapmoggre
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

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

Post to forums  

Autodesk Design & Make Report

”Boost