Create & Insert Blocks LISP Routines

Create & Insert Blocks LISP Routines

Anonymous
Not applicable
2,295 Views
13 Replies
Message 1 of 14

Create & Insert Blocks LISP Routines

Anonymous
Not applicable

Hi,

 

I'm looking for the functions or syntax needed to create a block from objects then insert that block. Can anyone help me find what I'm looking for? 

 

Thanks, Brad

0 Likes
Accepted solutions (1)
2,296 Views
13 Replies
Replies (13)
Message 2 of 14

Kent1Cooper
Consultant
Consultant

Just the regular Block command, if you check this:

BlockDialog.PNG

The result will be Inserted in place, but you can then Move it and/or Rotate and/or Scale, etc.

Kent Cooper, AIA
0 Likes
Message 3 of 14

Anonymous
Not applicable

So sorry, I'm looking to do this with a lisp routine. 

0 Likes
Message 4 of 14

ВeekeeCZ
Consultant
Consultant

If you want an anonymous block...

 

(defun c:MakeAnBlock (/ ss pt)  ;ctrl+shift B
  (if (and (setq ss (ssget ":L"))
	   (setq pt (getpoint "\nReference point:")))
    (command "_.COPYBASE" "_non" pt ss ""
             "_.PASTEBLOCK" "_non" pt
             "_.ERASE" ss ""))
  (princ)
)
0 Likes
Message 5 of 14

ВeekeeCZ
Consultant
Consultant
Accepted solution

...or named block.

 

(defun c:MakeBlock (/ ss pt name)  ;ctrl+shift N
  (if (and (setq ss (ssget ":L"))
	   (setq pt (getpoint "\nReference point:"))
           (setq name (getstring T "\nName: "))
           )
    (command "_.-BLOCK" name "_non" pt ss ""
             "_.-INSERT" name "_S" 1 "_R" 0 "_non" pt))
  (princ)
)
Message 6 of 14

Anonymous
Not applicable

A couple of questions.

 - When you say "anonymous" block, do you mean without a name? If so, I'd like to give it a name?

 - Also, I'd like for the block to include two entities, both polylines. Is the ssget where that would take place?

I'm quite the rookie when it comes to lisp, so I probably will have more questions.

 

0 Likes
Message 7 of 14

ВeekeeCZ
Consultant
Consultant

See the SCREENCAST how it works.

 

Yes, use the ssget function for the user selection - guess you want to select your two polylines, right?

If you want to restrict the selection on polylines only, use the filter:

(ssget '((0 . "LWPOLYLINE")))

 

See about the ssget in HELP

0 Likes
Message 8 of 14

Anonymous
Not applicable

Thanks! I haven't gotten it to work yet. Does the following line specify the insertion point of the block or the place in the drawing that I want to insert the block?

0 Likes
Message 9 of 14

Anonymous
Not applicable

@Anonymous
Try this:

(defun c:MakeBlock (/ ss pt name)
  (setq ss (ssget ":L")
	pt (getpoint "\nBase point: ")
	name (getstring T "\nBlock name: ")
	)
  (command "_.-Block" name pt ss "")
  (command "_.Insert" name "_Scale" 1 "_Rotate" 0)
  (princ)
)
(prompt "\n >> Type MBK <<")
(defun c:MBK nil (c:MakeBlock))

 

 

0 Likes
Message 10 of 14

Anonymous
Not applicable

Select only the polylines, and will also be inserted automatically in the base point:

 

(defun c:MBK nil (c:MakeBlock))
;|-------------------------------|;
(defun c:MakeBlock (/ ss pt name)
  (setq ss (ssget ":L" '((0 . "LWPOLYLINE")))
	pt (getpoint "\nBase point: ")
	name (getstring T "\nBlock name: ")
	)
  (command "_.-Block" name pt ss "")
  (command "_.Insert" name "_Scale" 1 "_Rotate" 0 pt)
  (princ)
)
(prompt "\n >> Type MBK <<")

 

0 Likes
Message 11 of 14

Anonymous
Not applicable

Thank you for your help. I think this will work.

0 Likes
Message 12 of 14

Kent1Cooper
Consultant
Consultant

@ВeekeeCZ wrote:

...or named block.

(defun c:MakeBlock (/ ss pt name)  ;ctrl+shift N
  (if (and (setq ss (ssget ":L"))
	   (setq pt (getpoint "\nReference point:"))
           (setq name (getstring T "\nName: "))
           )
    (command "_.-BLOCK" name "_non" pt ss ""
             "_.-INSERT" name "_S" 1 "_R" 0 "_non" pt))
  (princ)
)

This and several other suggestions on this thread can be done without most of the variables.  The Block command will supply its own prompts [though in a different order than the above].  And the Insert command will default to scales of 1 and rotation of 0.  You just need to save the name, because unlike in the Block dialog box  version, where you can choose whether or not to have it converted into a Block Insertion in the process, in the (command)-function version the pieces are always removed from the drawing, and you then need to Insert the same Block.

 

Try something like this:

(defun C:MakeBlock (/ name)
(command "_.block" (setq name (getstring)) pause (ssget "_:L") "" "_.insert" name "@" "" "" "" )
(princ)
)

Or, you could end the (command) function at the Block name, and leave the other things to the User after the end of the routine [without the (princ) at the end], if you don't necessarily always want it put in at the same location/size/rotation as the pieces selected to be in it.

 

[You can add the limitation of object-type filtering in the (ssget) function if you want.]

Kent Cooper, AIA
0 Likes
Message 13 of 14

Anonymous
Not applicable

Noted. Would any of you know how to write the lisp so that the block creation doesn't delete the objects used to create the block?

Message 14 of 14

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

Noted. Would any of you know how to write the lisp so that the block creation doesn't delete the objects used to create the block?


(defun C:MakeBlock (/ name)
  (command
    "_.block" (setq name (getstring)) pause (ssget "_:L") ""
    "_.oops"
  )
  (princ)
)

What the Block command does is equivalent in effect to Erasing the selected objects, and OOPS brings them back in just as if it were Erasing that removed them.

 

That will not  Insert a copy of the new Block [you can leave that part in if you want, but it will lie right on top of the pieces].  Or you can follow the OOPS with an Insert command that ends with the name, leaving it to the User to place one elsewhere.

Kent Cooper, AIA