Author Topic: Load block definition from file  (Read 4026 times)

0 Members and 1 Guest are viewing this topic.

GUIDO ROOMS

  • Guest
Load block definition from file
« 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.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Load block definition from file
« Reply #1 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.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Load block definition from file
« Reply #2 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.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Load block definition from file
« Reply #3 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.

GUIDO ROOMS

  • Guest
Re: Load block definition from file
« Reply #4 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.

GUIDO ROOMS

  • Guest
Re: Load block definition from file
« Reply #5 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...

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Load block definition from file
« Reply #6 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.
« Last Edit: November 25, 2012, 07:23:52 AM by Lee Mac »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Load block definition from file
« Reply #7 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  :-[
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Load block definition from file
« Reply #8 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!  :-)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Load block definition from file
« Reply #9 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

GUIDO ROOMS

  • Guest
Re: Load block definition from file
« Reply #10 on: November 25, 2012, 07:34:16 AM »
Good man, Lee!
Thanks a ton.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Load block definition from file
« Reply #11 on: November 25, 2012, 07:49:08 AM »
Good man, Lee!
Thanks a ton.

You're very welcome  8-)