TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: GUIDO ROOMS on November 25, 2012, 01:47:53 AM

Title: Load block definition from file
Post by: GUIDO ROOMS on November 25, 2012, 01:47:53 AM
Does anyone know a method to load a block definition into the blocks table from a file?
Thanks in advance, as ever.
Title: Re: Load block definition from file
Post by: Lee Mac on November 25, 2012, 05:38:04 AM
Two ways:

Code - Auto/Visual Lisp: [Select]
  1. (command "_.-insert" <full-filename-of-block> nil)

Code - Auto/Visual Lisp: [Select]
  1.         (vlax-3D-point 0.0 0.0 0.0)
  2.         <full-filename-of-block>
  3.         1.0
  4.         1.0
  5.         1.0
  6.         0.0
  7.     )
  8. )

By using the full filename when inserting the block, the block definition will be reloaded into the drawing.
Title: Re: Load block definition from file
Post by: irneb on November 25, 2012, 06:28:11 AM
Of course you may also want to erase the insert/reference after the command. You can also make a new blank block definition and then use the CopyFrom method through ObjectDBX, but that would be a whole lot of extra code simply to avoid an erase.
Title: Re: Load block definition from file
Post by: Lee Mac on November 25, 2012, 07:06:17 AM
Of course you may also want to erase the insert/reference after the command.

This is not required in my example since the Insert command is cancelled (by the presence of the nil) before the reference is inserted but after the definition is loaded into the drawing.
Title: Re: Load block definition from file
Post by: GUIDO ROOMS on November 25, 2012, 07:07:22 AM
Thank you Lee for the reply.

In fact, that's what I've been doing so far.
One can also do it using the .net api, but I was looking for a lisp function to add a block definition directly to the table without inserting and then erasing it (maybe I should have been more clear about that in my first post).

Perhaps Irneb's reply is what I was looking for: the answer is "no", at least there's no straightforward & simple way to do it in lisp.

Thanks to both of you.
Greetings.
Title: Re: Load block definition from file
Post by: GUIDO ROOMS on November 25, 2012, 07:09:47 AM
Quote
This is not required in my example since the Insert command is cancelled (by the presence of the nil) before the reference is inserted but after the definition is loaded into the drawing.

True! I'd overlooked that one...
Title: Re: Load block definition from file
Post by: Lee Mac on November 25, 2012, 07:19:35 AM
I was looking for a lisp function to add a block definition directly to the table without inserting and then erasing it (maybe I should have been more clear about that in my first post).

This is also possible, consider the following example:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:makeblock ( / bnm doc ins lst sel )
  2.     (while
  3.         (and (/= "" (setq bnm (getstring t "\nSpecify Block Name: ")))
  4.             (or
  5.                 (not (snvalid bnm))
  6.                 (tblsearch "BLOCK" bnm)
  7.             )
  8.         )
  9.         (princ "\nBlock name invalid or already exists.")
  10.     )
  11.     (if
  12.         (and
  13.             (/= "" bnm)
  14.             (ssget '((410 . "Model")))
  15.             (setq ins (getpoint "\nSpecify Base Point: "))
  16.         )
  17.         (progn
  18.             (vlax-for obj (setq sel (vla-get-activeselectionset doc))
  19.                 (setq lst (cons obj lst))
  20.             )
  21.             (vla-delete sel)
  22.             (vlax-invoke doc 'copyobjects (reverse lst)
  23.                 (vla-add
  24.                     (vla-get-blocks doc)
  25.                     (vlax-3D-point (trans ins 1 0))
  26.                     bnm
  27.                 )
  28.             )
  29.         )
  30.     )
  31.     (princ)
  32. )
  33.  

Or, if you wish to create the block definition from scratch:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:makeblock ( / bnm )
  2.     (while
  3.         (and (/= "" (setq bnm (getstring t "\nSpecify Block Name: ")))
  4.             (or
  5.                 (not (snvalid bnm))
  6.                 (tblsearch "BLOCK" bnm)
  7.             )
  8.         )
  9.         (princ "\nBlock name invalid or already exists.")
  10.     )
  11.     (if (/= "" bnm)
  12.         (vla-addcircle
  13.             (vla-add
  14.                 (vlax-3D-point 0 0 0)
  15.                 bnm
  16.             )
  17.             (vlax-3D-point 0 0 0)
  18.             10.0
  19.         )
  20.     )
  21.     (princ)
  22. )

Otherwise, you could also use ObjectDBX and the copyobjects method with an external drawing file.
Title: Re: Load block definition from file
Post by: irneb on November 25, 2012, 07:23:51 AM
This is not required in my example since the Insert command is cancelled (by the presence of the nil) before the reference is inserted but after the definition is loaded into the drawing.
Ah! Sorry ... yes missed that  :-[
Title: Re: Load block definition from file
Post by: Lee Mac on November 25, 2012, 07:25:04 AM
This is not required in my example since the Insert command is cancelled (by the presence of the nil) before the reference is inserted but after the definition is loaded into the drawing.
Ah! Sorry ... yes missed that  :-[

No problem!  :-)
Title: Re: Load block definition from file
Post by: Lee Mac on November 25, 2012, 07:25:59 AM
Otherwise, you could also use ObjectDBX and the copyobjects method with an external drawing file.

Here is a general example of this method:

http://lee-mac.com/steal.html (http://lee-mac.com/steal.html)
Title: Re: Load block definition from file
Post by: GUIDO ROOMS on November 25, 2012, 07:34:16 AM
Good man, Lee!
Thanks a ton.
Title: Re: Load block definition from file
Post by: Lee Mac on November 25, 2012, 07:49:08 AM
Good man, Lee!
Thanks a ton.

You're very welcome  8-)