Results 1 to 5 of 5

Thread: Insert Block from another drawing

  1. #1
    Woo! Hoo! my 1st post
    Join Date
    2017-06
    Posts
    1
    Login to Give a bone
    0

    Default Insert Block from another drawing

    Code:
    ^C^C_insert;"Template";^C-insert;"*Block 1";0,0;;;
    I use the above macro script to insert 'Block 1' from Template.dwg into current drawing at 0,0,0 & explode it. I have the template's folder location in my search path.

    Please help to convert this macro script into a lisp that does exactly the same thing.

    Thanks in advance

  2. #2
    Certifiable AUGI Addict tedg's Avatar
    Join Date
    2005-06
    Location
    in the upper right corner
    Posts
    3,507
    Login to Give a bone
    0

    Default Re: Insert Block from another drawing

    Quote Originally Posted by tive29751290 View Post
    Code:
    ^C^C_insert;"Template";^C-insert;"*Block 1";0,0;;;
    I use the above macro script to insert 'Block 1' from Template.dwg into current drawing at 0,0,0 & explode it. I have the template's folder location in my search path.

    Please help to convert this macro script into a lisp that does exactly the same thing.

    Thanks in advance
    Check out answers IN YOUR OTHER THREAD there may be solutions that can help you over there..

  3. #3
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,109
    Login to Give a bone
    0

    Default Re: Insert Block from another drawing

    To insert just one block definition from another drawing requires a DBX module.

    If you go to this link you can see a simple version (with no error trapping)

    https://forums.autodesk.com/t5/visua...p/td-p/5519160

    I prefer to write functions that can be reused.

    I have vast libraries that include 1000's of functions.

    I prefer to build them with error trapping built in...

    I broke this routine up into reusable functions that perform the steps necessary to copy a block from another drawing.

    So...

    Code:
    ;___________________________________________________________________________________________________________|
    ;
    ; Written By: Peter Jamtgaard copyright 2017 All Rights Reserved
    ;___________________________________________________________________________________________________________|
    
    ;___________________________________________________________________________________________________________|
    
    ;___________________________________________________________________________________________________________|
    ;
    ; General Functions
    ;___________________________________________________________________________________________________________|
    
    ;* (BlockFromFile strFullName strBlockName)
    ;* Function to duplicate a block definition from an outside drawing
    
    ;* (CopyObjects2 objOwner lstObjects objCOllection)
    ;* Function to create a safearray of objects and run the copyobjects method
    
    ;* (DBXDocument strFullName)
    ;* Function to open a DBX Document
    
    ;* (DBXBlockDefinition objDBXDocument strBlockName)
    ;* Function to read a DBX document and return a specified block definition
    
    ;* (ErrorTrap symFunction)
    ;* Function to trap an error
    
    ;* (ListToSafeArray lstObjects symObjectType)
    ;* Function to create a safearray
    
    ;$ Header End
    
    ; Function Headers
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to duplicate a block definition from an outside drawing
    ;___________________________________________________________________________________________________________|
    
    (defun BlockFromFile (strFullName strBlockName / lstGlobalToRelease objBlocks objDBXDocument objDBXBlockDefinition)
     (if (and (setq objDBXDocument         (DBXDocument strFullName))
              (setq objDBXBlockDefinition  (DBXBlockDefinition objDBXDocument strBlockName))
              (setq objBlocks              (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))))
              (not (errortrap '(vla-item objBlocks strBlockName)))
              (CopyObjects2 objDBXDocument (list objDBXBlockDefinition) objBlocks)
         )
      (setq objBlockDefinition (errortrap '(vla-item objDBXBlockDefinition strBlockName)))
      (princ (strcat "\nError copying block: " strBlockName " from drawing " strFullName "..."))
     )
     (mapcar '(lambda (X)(errortrap (quote (vlax-release-object X)))) lstGlobalToRelease);<- Must release objects in reverse order 
     objDBXBlockDefinition
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to create a safearray of objects and run the copyobjects method
    ;___________________________________________________________________________________________________________|
    
    (defun CopyObjects2 (objOwner lstObjects objCOllection / lstObjects2 safObjects)
     (if (setq safObjects (ListToSafeArray vlax-vbobject lstObjects))
      (or (errortrap '(vla-CopyObjects objOwner
                                       safObjects
                                       objCollection
                      )
          )
          (princ "\nError using copyobjects: ")
      )
     )
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to open a DBX Document
    ;___________________________________________________________________________________________________________|
    
    (defun DBXDocument (strFullName / objDBXDocument strACADVersion strObjectDBX)
     (if (and (= (type strFullName) 'STR)
              (= (strcase (vl-filename-extension strFullName)) ".DWG")
              (setq strFullName         (findfile strFullName))
              (setq strACADVersion      (getvar "acadver"))
              (setq strObjectDBX        (strcat "ObjectDBX.AxDbDocument." (substr strACADVersion 1 2)))
              (setq objDBXDocument      (vla-GetInterfaceObject (vlax-get-acad-object) strObjectDBX ))
              (setq lstGlobalToRelease  (list objDBXDocument))
              (errortrap '(vla-open objDBXDocument strFullName))
         )
      objDBXDocument
      (princ "\nError creating DBX File Object!: ")
     )
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to read a DBX document and return a specified block definition
    ;___________________________________________________________________________________________________________|
    
    (defun DBXBlockDefinition (objDBXDocument strBlockName / objDBXBlockDefinition objDBXBlocks)
     (if (and
          (= (type strBlockName) 'STR)
          (setq objDBXBlocks           (vla-get-blocks objDBXDocument))
          (setq lstGlobalToRelease     (cons objDBXBlocks lstGlobalToRelease))
          (setq objDBXBlockDefinition  (errortrap '(vla-item objDBXBlocks strBlockName)))
          (setq lstGlobalToRelease     (cons objDBXBlockDefinition lstGlobalToRelease))
         )
      objDBXBlockDefinition
      (progn (princ "\nError reading DBX block definition object: ") nil)
     )
    )
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to trap an error
    ;___________________________________________________________________________________________________________|
    
    (defun ErrorTrap (symFunction / objError result)
     (if (vl-catch-all-error-p
          (setq objError (vl-catch-all-apply
                         '(lambda (XYZ)(set XYZ (eval symFunction)))
                          (list 'result))))
      nil
      (or result 
          'T
      )
     )
    )
    
    
    
    ;___________________________________________________________________________________________________________|
    ;
    ; Function to create a safearray
    ;___________________________________________________________________________________________________________|
    
    ;vlax-vbInteger  (2)  Integer 
    ;vlax-vbLong     (3)  Long integer 
    ;vlax-vbSingle   (4)  Single-precision floating-point number 
    ;vlax-vbDouble   (5)  Double-precision floating-point number 
    ;vlax-vbString   (8)  String 
    ;vlax-vbObject   (9)  Object 
    ;vlax-vbBoolean (11)  Boolean 
    ;vlax-vbVariant (12)  Variant 
    
    (defun ListToSafeArray (symVariableType lstValues / safObjects)
     (if (and (setq safValues (vlax-make-safearray symVariableType (cons 0 (1- (length lstValues)))))
              (errortrap      '(vlax-safearray-fill safValues lstValues))
         )
      safValues
     )
    )
    
    (princ "!")
    (vl-load-com)
    Attached Files Attached Files
    Last edited by peter; 2017-06-30 at 04:00 AM.
    AutomateCAD

  4. #4
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,667
    Login to Give a bone
    0

    Default Re: Insert Block from another drawing

    Or Check out answers IN YOUR OTHER OTHER THREAD there may be solutions that can help you over there..

    Don't know how many other sites you've posted this but it seems like you wasted a lot of peoples time to replace a simple macro that already does the job. Show a little respect and don't triple post.

  5. #5
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,109
    Login to Give a bone
    0

    Default Re: Insert Block from another drawing

    Hey Tom,

    I saw the other posts, but thought I would present one that had error trapping and reusable functions.

    In my libraries I look have them look in the open documents collection first too instead of dbx reads.

    Using a dbx object and the copy objects method was good warm up for my coding today.

    P=
    AutomateCAD

Similar Threads

  1. Get current drawing scale, then insert block via that scale
    By U.Rackharrow in forum VBA/COM Interop
    Replies: 10
    Last Post: 2016-12-09, 03:18 PM
  2. Replies: 16
    Last Post: 2015-07-20, 01:42 PM
  3. Block Insert - Right Click Block Open Drawing
    By Texan1 in forum ACA Wish List
    Replies: 1
    Last Post: 2011-11-26, 04:38 AM
  4. Replies: 2
    Last Post: 2007-05-17, 10:50 PM
  5. Insert block definition from one drawing into current drawing
    By ktisdale.118609 in forum VBA/COM Interop
    Replies: 2
    Last Post: 2006-08-08, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •