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

Insert common block

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
carminatithekid1
963 Views, 10 Replies

Insert common block

I would like to create a lisp that will insert a block from my current job folder. Currently I do this manually but I realized that the path is always the same and the block name in always the same as well. This is how it would work;

 

_insert

 

search for current job folder, find "temp" folder, and select "btitle.dwg"

insert the dwg file at 0,0

 

the issue is that every job has a different name, but every job has a temp folder. Is there a way to do this automatically?

 

What I have come up with is this,

(defun c:bb ()
(command "_insert" "..\temp\btitle.dwg" "o,o"))

 

It says "invalid block name"

 

I guess my issue is trying to get the file name correct. Any help is much appreciated as I am new to creating my own lisp.

Randy Carminati
10 REPLIES 10
Message 2 of 11

I would like to add one item. The block should be exploded after insert.

Randy Carminati
Message 3 of 11
phanaem
in reply to: carminatithekid1

Use (getvar 'dwgprefix) to get the correct path and

(strcat (strcat 'dwgprefix "\\temp\\btitle.dwg")) to get the block name.

You should check if the block is already inserted or if the btitle.dwg really exist.

 

something like this:

(setq block_name
    (cond
      ((tblsearch "block" "btitle") "btitle")
      ((findfile (strcat (getvar 'dwgprefix) "temp\\btitle.dwg")))
      (T nil)
    )
  )

Then insert the block

(if
    block_name
    (command "_insert" block_name ... )
  )

The command "_insert" arguments depend on your AutoCAD version. You can find the correct ones by typing (command "_insert") at the command prompt and note each required input.

For me, (command "_insert" block_name "_S" 1.0 "_R" 0.0 "_none" '(0.0 0.0)) works well.

 

To explode the block, use (command "_explode" (entlast)). However, you should check if the (entlast) is actually your block, like this:

(setq last_ent (entlast))

(setq block_name ....
(command "_insert" ....

(if
  (not (eq (entlast) last_ent))
  (command "_explode" (entlast))
)

 

Message 4 of 11
_Tharwat
in reply to: phanaem


@phanaem wrote:

 

To explode the block, use (command "_explode" (entlast)). However, you should check if the (entlast) is actually your block, like this:

(setq last_ent (entlast))

(setq block_name ....
(command "_insert" ....

(if
  (not (eq (entlast) last_ent))
  (command "_explode" (entlast))
)

 


Hi,

 

You can add asterisk before the name of the block and automatically it would be exploded on insertion. 

 

 (command "_.-insert" (strcat "*" block_name) '(0. 0.) "1" "1" "0.0")

 

Message 5 of 11

Instead of finding files in subdirectories, why not just keep the BTITLE.DWG in the same folder as the current drawing?  That way the insert command doesn't need a path because AutoCAD always looks in the current drawing's path first.  I also agree with the "*" prefix to bring it in exploded.

John F. Uhden

Message 6 of 11
carminatithekid1
in reply to: phanaem

From what I understand, of what you wrote. I am doing something wrong.

 

(defun c:bb ()
(setq last_ent (entlast))
(setq block_name
(cond
((tblsearch "block" "btitle") "btitle")
((findfile (strcat (getvar 'dwgprefix) "temp\\btitle.dwg")))
(T nil)
)
)
(if
block_name
(command "_insert" block_name ... )
)
)

Randy Carminati
Message 7 of 11

The reason the file isn't in the current directory is because we have 3 disciplines in one office that need access to that file. It makes more sense to us to keep it in a common folder.

Randy Carminati
Message 8 of 11

Once I paid some more attention to what you guys were actually saying, I figured it out.

 

(defun c:bb ()
(setq last_ent (entlast)
)
(setq block_name
(cond
((tblsearch "block" "btitle") "btitle")
((findfile (strcat (getvar 'dwgprefix) "temp\\btitle.dwg"))
)
(T nil)
)
)
(if block_name
(command "_insert" "btitle.dwg" "0,0" "" "" "")
)
(if
(not (eq (entlast) last_ent))
(command "_explode" (entlast))
)
)
)

 

Works like a charm! Thanks!

Randy Carminati
Message 9 of 11

There is no need to explode the block insertion after the fact.  Plus, you can't explode it if the current layer is locked.

 

Try this...

 

(defun c:bb ( / block_name)
  (setq block_name
    (cond
      ((tblsearch "block" "btitle") "btitle")
      ((findfile (strcat (getvar 'dwgprefix) "temp\\btitle.dwg")))
      (T nil)
    )
  )
  (if block_name
    (command "_.insert" (strcat "*" block_name) "0,0" "" "" "")
    (princ "\nBlock name 'BTITLE' not found.")
  )
  (princ)
)

Of course you should have Undo and error controls.  Want me to add them?

John F. Uhden

Message 10 of 11
wkmvrij
in reply to: john.uhden

And why would one not put the block in template files one for each discipline in your office and exploded if required. Each user might set the required template  as default....

Message 11 of 11
carminatithekid1
in reply to: wkmvrij

This has been solved. Thanks for replying.

Randy Carminati

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

Post to forums  

Forma Design Contest


AutoCAD Beta