Inserting a block from a DWG in LISP

Discussion in 'AutoCAD' started by Scott Davis, Nov 4, 2004.

  1. Scott Davis

    Scott Davis Guest

    I'm sure this can be done, but not sure how:

    Through a macro or LiSP, I want to insert a block that is in a drawing. The
    same as pulling a block from a DWG in Design Center, but at the command
    line. Any ideas?

    TIA!
     
    Scott Davis, Nov 4, 2004
    #1
  2. Scott Davis

    Jeff Mishler Guest

    Yep....... use a lisp routine that utilizes ObjectDBX. There are a number of
    examples that have been posted in the past year, or so. If you need help
    implementing after you've looked around feel free to ask for specific help.
    There is also an example I posted back in July in the "Lost in Stupid
    Parenthatheeth" forum at the site in my sig line that should be of help.
     
    Jeff Mishler, Nov 4, 2004
    #2
  3. Scott Davis

    Jim Claypool Guest

    You didn't say which version of AutoCAD you are using. This will work in
    2002 an up.
    The block name is case sensitive and the drawing must be in the support file
    search path.

    (defun get_blk (blkname blkdwg / *acad* doc bimport odbx Doctest path source
    blocks blist blkname lbklist)
    (vl-load-com)
    (setq blkdwg (vl-filename-base blkdwg))
    (setq
    *acad* (vlax-get-acad-object)
    doc (vla-get-activedocument *acad*)
    source (findfile (strcat blkdwg ".dwg"))
    )
    (if source
    (progn
    (vlax-for oDoc (vla-get-documents *acad*)
    (if (= (vla-get-fullname oDoc) source)
    (setq Doctest t)
    )
    )
    (if (not Doctest)
    (progn
    (if (> (atoi (getvar "AcadVer")) 15)
    (setq
    oDBX (vla-GetInterfaceObject
    *acad* "ObjectDBX.AxDbDocument.16")
    )
    (progn
    (if (not (vl-registry-read
    "HKEY_CLASSES_ROOT\\ObjectDBX.AxDbDocument\\CLSID"))
    (startapp "regsvr32.exe"
    (strcat "/s \"" (findfile "axdb15.dll") "\"")
    )
    )
    (setq oDBX (vla-GetInterfaceObject *acad* "ObjectDBX.AxDbDocument")
    )
    )
    )
    (vla-open oDBX source);; "opens" source for our use
    (setq names nil)
    (setq blocks (vla-get-blocks oDBX));; source block table
    (vlax-for name blocks
    (setq names (cons (vla-get-name name) names))
    )
    (if (> (vla-get-count blocks) 0);;ensure there are views to import
    (progn
    (vlax-for b blocks ;; cycle thru table & check if name exists in this
    drawing, omit if so
    (if (not (tblsearch "block" (vla-get-name b)))
    (progn
    (if (= (vla-get-name b) blkname)
    (setq blist (append (list b) blist ))
    );end if
    )
    );if
    );for
    (if blist
    (progn
    (setq bimport
    (vlax-safearray-fill
    (vlax-make-safearray vlax-vbObject
    (cons 0 (- (length blist) 1))
    ) blist
    )
    );; create safearray for use in ActiveX method
    (vla-copyobjects oDBX bimport (vla-get-blocks doc))
    ;;import the block
    );progn
    );if
    (setq blkfound 1)
    );progn
    (princ "\nSorry, no blocks found in source drawing, try again.")
    );if
    (setq names nil)
    (setq blocks (vla-get-blocks oDBX));; source layer table
    (vlax-for name blocks
    (setq names (cons (vla-get-name name) names))
    )
    (vlax-release-object oDBX);; we're done with it
    (if (tblsearch "BLOCK" blkname)
    (command ".insert" blkname)
    (alert (strcat "Block " blkname " Not found"))
    )
    );progn
    (alert "\nYou selected a file in use by Autocad, please close and try
    again....")
    );if
    );end progn
    (alert (strcat "Block Drawing " blkdwg " not found"))
    );end if
    (princ);; exit quietly
    )
     
    Jim Claypool, Nov 4, 2004
    #3
  4. Scott Davis

    Scott Davis Guest

    Wow! I was thinking it would be a one-liner! Thanks for the code!

    Scott
     
    Scott Davis, Nov 4, 2004
    #4
  5. Scott Davis

    Jeff Mishler Guest

    **<<sigh>>**
    Why do people strip off the headers of lisp routines?

    Here is what should have preceded the code that Jim posted (although the
    original code WAS modified slightly from what was originally posted).

    ;| Import block from another drawing, block name and
    path\drawing are passed as arguments
    (imp_blk "blockname" "path\\tp\\blocks.dwg")
    Jeff Mishler, Dec. 23, 2003
    Check for ObjectDBX installation by R. Robert Bell
    |;

    (defun imp_blk (bname source / *acad* doc odbx Doctest
    blocks blist bimport)
    (vl-load-com)
    ;;rest of code here.........
     
    Jeff Mishler, Nov 4, 2004
    #5
  6. Scott Davis

    Jim Claypool Guest

    Sorry about that Jeff. The header info was not there when I picked it up.
    I didn't know where it originated.
    The original code picked up all blocks in a drawing. I modified it to get
    just one.
     
    Jim Claypool, Nov 4, 2004
    #6
  7. Scott Davis

    Jeff Mishler Guest

    No problem, Jim. I didn't think it was you, hence my use of 'people'....

    Heh, I just noticed that this was an early version that I had re-worked from
    another routine I'd made for importing views and still has the misleading
    comment in this line:

    (if (> (vla-get-count blocks) 0);;ensure there are views to import

    Take care,
     
    Jeff Mishler, Nov 4, 2004
    #7
  8. Scott Davis

    hulioman Guest

    This may be easier

    (command "insert" "<<<full path and filename of block to be inserted>>>" "1" "1" "0")

    you may want to change the attreq system variable befor this to keep from having to input any possible attributes.
     
    hulioman, Nov 4, 2004
    #8
  9. Scott Davis

    Jeff Mishler Guest

    While this will, of course, insert a Drawing as a Block in the drawing.
    However, it will NOT insert just 1 Block defined in another drawing to the
    current drawing.
     
    Jeff Mishler, Nov 5, 2004
    #9
  10. Scott Davis

    Jim Claypool Guest

    I added the credits so it won't happen again (at least not from me).
    I also removed the misleading comment.
     
    Jim Claypool, Nov 5, 2004
    #10
  11. Scott Davis

    Scott Davis Guest

    Seems like this should be easier....maybe something like:

    Command: -insert
    Enter block name or [?]:
    F:\CAD\AutoCAD\R2005\Library\Architectural\symbols.dwg&blockname=ROOMTAG
    Specify insertion point or [Scale/X/Y/Z/Rotate/PScale/PX/PY/PZ/PRotate]:

    OR

    Command: -insert
    Enter Source Drawing:
    F:\CAD\AutoCAD\R2005\Library\Architectural\symbols.dwg
    Enter block name or [?]: ROOMTAG
    Specify insertion point or [Scale/X/Y/Z/Rotate/PScale/PX/PY/PZ/PRotate]:
     
    Scott Davis, Nov 16, 2004
    #11
  12. Scott Davis

    T.Willey Guest

    Here is a link to the way I did it. You will need the dcl file and the lisp file. It will ask you to pick a file, then it will load the dialog with all the blocks you could copy over. One thing that got pointed out is that if it already exist it won't over write it.

    http://discussion.autodesk.com/thread.jspa?threadID=367202

    Hope it helps.
    Tim
     
    T.Willey, Nov 16, 2004
    #12
  13. Scott Davis

    T.Willey Guest

    Forgot to mention it is set up to work on 2004 and 2005 Object BDX.

    Tim
     
    T.Willey, Nov 16, 2004
    #13
  14. Scott Davis

    Scott Davis Guest

    Thanks Tim! I will try it out!
     
    Scott Davis, Nov 16, 2004
    #14
  15. Scott Davis

    T.Willey Guest

    Scott,

    I just posted the final version for now.

    Tim
     
    T.Willey, Nov 16, 2004
    #15
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.