Jump to content

Insert Multiple blocks into drawing?


skipsophrenic

Recommended Posts

Oops! Excuse me, I have not checked up this code. Excessive self-confidence... Now should work (may be :wink: ):

 

 

(defun c:test2(/ blLst diFol oldAtr *error*)

 (vl-load-com)

(defun BrowseFolder(Message / ShlObj Folder FldObj OutVal)
 (setq ShlObj (vla-getInterfaceObject
                 (vlax-get-acad-object)
                   "Shell.Application")
       Folder
        (vlax-invoke-method ShlObj 'BrowseForFolder 0 Message 0)
       ); end setq
 (vlax-release-object ShlObj)
 (if Folder
   (progn
     (setq
 	FldObj (vlax-get-property Folder 'Self)
 	OutVal (vlax-get-property FldObj 'Path)
     	)
     	(vlax-release-object Folder)
     	(vlax-release-object FldObj)
     OutVal
   )
 )
); end of BrowseFolder

(defun *error*(msg)
 (if oldAtr(setvar "ATTREQ" oldAtr))
 (setvar "CMDECHO" 1)
 (princ)
 ); end of *error*


; if folder selected and is drawings inside
(if(and
    (setq diFol(BrowseFolder "Select folder to insert blocks"))
    (setq blLst(vl-directory-files diFol "*.dwg" 1))
    ); end and

 (progn
 
 (setq oldAtr(getvar "ATTREQ"))
 (setvar "CMDECHO" 0)
 (setvar "ATTREQ" 0)
 
 ; foreach block name in list
 (foreach bl blLst
   ; if block found in drawing database

   ; if block not found in drawing, set block name as full path
   (if(not(tblsearch "BLOCK" bl))
     (setq bl(strcat diFol "\\" bl))
     ); end if

     ; insert block in point 0,0 with scale 1 and rotation 0
     (vl-cmdf "_.-insert" bl "0,0" "1" "" "0")

   );end foreach
 
 ; swich on echo output
 (setvar "CMDECHO" 0)
 (setvar "ATTREQ" oldAtr)
 
); end progn
 
 ; if nothing files found print error message
 (princ "\n<!> Nothing *.dwg files found <!> ")
 ); end if

 ; silent exit (without value of last expression)
 (princ)
); end of c:test2

Link to comment
Share on other sites

  • Replies 54
  • Created
  • Last Reply

Top Posters In This Topic

  • skipsophrenic

    13

  • Lee Mac

    8

  • Strix

    7

  • EBROWN

    5

Top Posters In This Topic

Posted Images

200 blocks at once... May be :(

 

try 1566 blocks! thats why i was hoping there was a quicker way - i gave up on exploding them for now to try keep filesize down (even though that what asked for)

Link to comment
Share on other sites

Did this one but not sure if it would work either.

;;  Block Import Lisp  08/12/2008
;;  CAB at TheSwamp.org

 ;;  Get user selection of folder
 ;;  Get all DWG files in folder
 ;;  INSERT dwg as block @ 0,0
 ;;  get Bounding Box of block
 ;;  Move Insert to right w/ gap between blocks
 ;;  Next Insert

(defun c:BlkImport (/ path LastDist gap space err newblk bname obj ll lr ur
              InsPt dist GetFolder)
 (vl-load-com)
 (defun GetFolder ( / DirPat msg)
  (setq msg "Open a folder and click on SAVE")
  (and
   (setq DirPat (getfiled "Browse for folder" msg " " 1))
   (setq DirPat (substr DirPat 1 (- (strlen DirPat) (strlen msg))))
  )
  DirPat
 )
 

 (defun activespace (doc)
   (if (or (= acmodelspace (vla-get-activespace doc))
           (= :vlax-true (vla-get-mspace doc)))
       (vla-get-modelspace doc)
       (vla-get-paperspace doc)
   )
 )

 (setq gap 20) ; this is the gap between blocks
 (setq LastDist 0.0) ; this is the cumulative distance
 
 (if (setq Path (GetFolder))
   (progn
     (setq space (activespace (vla-get-activeDocument (vlax-get-acad-object))))
     (prompt "\n***  Working, Please wait ......\n")
     (foreach bname (vl-directory-files Path "*.dwg" 1)
       ;;  OK, try & insert the Block
       (if (vl-catch-all-error-p
             (setq err (vl-catch-all-apply
               '(lambda () (setq newblk (vla-insertBlock space 
                               (vlax-3d-point '(0.0 0.0 0.0)) (strcat path bname) 1.0 1.0 1.0 0.0))
                  ))))
         ;;  Display the error message and block/file name
         (prompt (strcat "\n" bname " " (vl-catch-all-error-message err)))
         ;;  ELSE
         (progn ; INSERT was sucessful, move the block
           ;;  get bounding box
           (if (vl-catch-all-error-p
                 (setq err (vl-catch-all-apply 'vla-getboundingbox (list newblk 'll 'ur))))
              (prompt (strcat "\nBB Error - could not move " bname "\n  " (vl-catch-all-error-message err)))
              (progn
                (setq ll (vlax-safearray->list ll)
                      ur (vlax-safearray->list ur)
                      lr (list (car ur) (cadr ll))
                      dist (distance ll lr)
                      )
                ;;  MOVE the block
                (setq ;InsPt  (vla-get-insertionpoint Newblk)
                      NewPt (polar '(0. 0. 0.) 0.0 (+ LastDist Gap (* dist 0.5)))
                      LastDist (+ LastDist Gap dist)
                      )
                (vlax-put Newblk 'insertionpoint NewPt)
              )
           )
         )
        )
     )
   )
 )
 (princ)      
)
(princ)
(prompt "\nBlock Import Loadd, Enter BlkImport to run.")

Link to comment
Share on other sites

  • 2 weeks later...

I tried the lisp above and found that it worked, with the one exception. I can't get it to default to a specific directory. I keep getting locked up since once I set the derictory path, it now assumes I want to specifiy the filename.

 

(defun c:BlkImport (/ path LastDist gap space err newblk bname obj ll lr ur
              InsPt dist GetFolder)
 (vl-load-com)
 (defun GetFolder ( / DirPat msg)
  (setq msg "C:/KMS/JOBSNT/")
  (and
   (setq DirPat (getfiled "Browse for folder" "msg" " " 1))
   (setq DirPat (substr DirPat 1 (- (strlen DirPat) (strlen msg))))
  )
   (prompt Dirpat)
  DirPat
 )

 

Additionally, I have a routine I want to apply the other routine to. Below is a simple LISP that I need to preform to every DXF file in a directory. It does the same exact thing every time. Once this routine has been completed for all of the DXF files in a directory, I need to insert them into the active drawings just like the other guy needed.

 

(defun C:3dtrs (/ tfl tpth tfilnam tfilinfo)
 (command "erase" (ssget "X" (list (cons 8 "ELEMENTS"))) "")
 (command "_pedit" "M" (ssget "X") "" "y" "J" "" "")
 (command "-purge" "La" "ELEMENTS" "N")
 (command "_extrude" (ssget "X") "" "1.75" "")
 (command "-view" "_left")
 (command "rotate" (ssget "X") "" "0,0,0" "270")
 (setq tfl (getvar "dwgname"))
 (setq tpth (getvar "dwgprefix"))
 (setq tfilnam (substr tfl 1 (- (strlen tfl) 4)))
 (setq tfilinfo (strcat tpth tfilnam ".dwg"))
 (command "saveas" "" tfilinfo)
 (command "close")
 (princ)
 )

 

Any thoughts on how to apply this to a batch of files? Or did I just break a cardinal sin by introducing a seprate issue?

Edited by SLW210
Added Code Tags!!!!!
Link to comment
Share on other sites

  • 11 months later...
I tried the lisp above and found that it worked, with the one exception. I can't get it to default to a specific directory. I keep getting locked up since once I set the derictory path, it now assumes I want to specifiy the filename.

 

 

 

Additionally, I have a routine I want to apply the other routine to. Below is a simple LISP that I need to preform to every DXF file in a directory. It does the same exact thing every time. Once this routine has been completed for all of the DXF files in a directory, I need to insert them into the active drawings just like the other guy needed.

 

 

 

Any thoughts on how to apply this to a batch of files? Or did I just break a cardinal sin by introducing a seprate issue?

 

To a dxf.... guess it's difficult... I would batch convert all the dxf to dwg with acme cad converter... and then apply the batch lisp... with a starting script... that will load an apply your lisp to all the drawings... I already posted the batch + starting script files.

Link to comment
Share on other sites

  • 1 year later...
Is there a way to do this but for images? Insert multiple images at the same time in a draw?

 

You could do any of this with a script. Use the "-insert" command to suppress the dialog box. You'll need to do some prep work ahead of time so the script doesn't fail, however. All of the images, blocks, pdfs, etc will have to be in a directory with no spaces in the name, and you can't have any spaces in the filename either.

 

Script would look something like this:

-insert
c:\foldername\filename.jpg [i](or dwg, pdf, etc)
[/i]0,0 [i](or appropriate location[/i])
[i][leave a blank line here]
[leave second blank line here][/i]
[i][leave a third blank line here][/i]
-insert
c:\foldername\filename2.jpg [i](or dwg, pdf, etc)[/i]
0,0 [i](or appropriate location)
[leave a blank line here]
[leave second blank line here]
[/i][i][leave a third blank line here][/i]

 

And so on for each file. The filenames can be extracted and dumped into a comma delimited file, opened with a spreadsheet and exported to a text file where you could then paste in the rest of the stuff between the file names. The three blank lines will be interpreted as hitting the enter key three times to accept the default scale factors for x and y, and the default rotation angle of zero.

 

You should use a plain text editor like Notepad to construct scripts. Wordpad and other word processors sometimes have special formatting characters that will cause you grief. When you save your file be sure to click the pull down arrow on "save as type" and change that to "all files" and give your script the file extension of ".scr". If you forget to change the type of file, you'll get a text file that is named "*.scr.txt" and it won't work. All is not lost if you do that however, simply rename the file with the scr extension.

Link to comment
Share on other sites

  • 3 months later...
try 1566 blocks! thats why i was hoping there was a quicker way - i gave up on exploding them for now to try keep filesize down (even though that what asked for)

 

Hi Skipsophrenic,

 

Did you get an answer to your problem? I have the same task next week with 8400 dxf files. I would like to insert all of them in the same time in one new drawing.

Any help appreciated.

 

Greeteings,

Andrulex

Link to comment
Share on other sites

Hi Skipsophrenic,

 

Did you get an answer to your problem? I have the same task next week with 8400 dxf files. I would like to insert all of them in the same time in one new drawing.

Any help appreciated.

 

Greeteings,

Andrulex

 

8400? Whatever for?

Link to comment
Share on other sites

8400? Whatever for?

In shipbuilding one ship is split in sections and every section has thousands pieces.

Every piece is exported from a dedicated software as dxf file. To have an overview of this pieces i make a new drawing where I insert these components. With a script it will be much faster to insert all in once.

Why are you asking for?

 

Greetings,

Andrulex

Link to comment
Share on other sites

Not too different from those already posted, but try this:

 

(defun c:InsertAll ( / cmd dir extn )

   (setq extn "dxf") ;; Extension of files to Insert e.g "dwg"

   (if (setq dir (LM:DirectoryDialog (strcat "Select Directory of " (strcase extn) " Files to Insert") nil 512))
       (progn
           (setq cmd (getvar 'CMDECHO))
           (setvar 'CMDECHO 0)
           (foreach file (vl-directory-files dir (strcat "*." extn) 1)
               (vl-cmdf "_.-insert" (strcat dir "\\" file) "_S" 1.0 "_R" 1.0 "_non" '(0 0 0))
           )
           (setvar 'CMDECHO cmd)
       )
       (princ "\n*Cancel*")
   )
   (princ)
)

;;-------------------=={ Directory Dialog }==-----------------;;
;;                                                            ;;
;;  Displays a dialog prompting the user to select a folder   ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  msg  - message to display at top of dialog                ;;
;;  dir  - root directory (or nil)                            ;;
;;  flag - bit coded flag specifying dialog display settings  ;;
;;------------------------------------------------------------;;
;;  Returns:  Selected folder filepath, else nil              ;;
;;------------------------------------------------------------;;

(defun LM:DirectoryDialog ( msg dir flag / Shell Fold Self Path )
   (vl-catch-all-apply
       (function
           (lambda ( / ac HWND )
               (if
                   (setq Shell (vla-getInterfaceObject (setq ac (vlax-get-acad-object)) "Shell.Application")
                         HWND  (vl-catch-all-apply 'vla-get-HWND (list ac))
                         Fold  (vlax-invoke-method Shell 'BrowseForFolder (if (vl-catch-all-error-p HWND) 0 HWND) msg flag dir)
                   )
                   (setq Self (vlax-get-property Fold 'Self)
                         Path (vlax-get-property Self 'Path)
                         Path (vl-string-right-trim "\\" (vl-string-translate "/" "\\" Path))
                   )
               )
           )
       )
   )
   (if Self  (vlax-release-object  Self))
   (if Fold  (vlax-release-object  Fold))
   (if Shell (vlax-release-object Shell))
   Path
)

(vl-load-com) (princ)
 

 

DirectoryDialog function from here.

Edited by Lee Mac
Link to comment
Share on other sites

I use this from time-to-time...

 

(defun c:InsertAll (/ _bbUL _bbUR space temp dir lst)
 ;; Insert all file(s) of specified type and within directory of selected file
 ;; Alan J. Thompson

 (defun _bbUL (o / a b)
   (vla-getboundingbox o 'a 'b)
   (list (car (vlax-safearray->list a)) (cadr (vlax-safearray->list b)) 0.)
 )

 (defun _bbUR (o / a b) (vla-getboundingbox o 'a 'b) (vlax-safearray->list b))

 (setq space (vlax-get (cond (*AcadDoc*)
                             ((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object))))
                       )
                       (if (eq (getvar 'CVPORT) 1)
                         'paperspace
                         'modelspace
                       )
             )
 )

 (if (setq temp (getfiled "Select file type to import" (getvar 'DWGPREFIX) "" 2))
   (foreach file (vl-directory-files
                   (setq dir (vl-filename-directory temp))
                   (strcat "*" (vl-filename-extension temp))
                   1
                 )
     (if (cadr (setq lst
                      (cons (vlax-invoke
                              space
                              'insertblock
                              '(0. 0. 0.)
                              (strcat dir "\\" file)
                              1.
                              1.
                              1.
                              0.
                            )
                            lst
                      )
               )
         )
       (vlax-invoke (car lst) 'move (_bbUL (car lst)) (mapcar '+ (_bbUR (cadr lst)) '(1. 0. 0.)))
     )
   )
 )
 (princ)
)

Link to comment
Share on other sites

Thank you Lee Mac. This works very good. If i may dare to ask you a last question: now, all dxf's are inserted in the same insertion point overlapping one over each other. There are hundreds of small plates in this case. Is it possible to "arrange" every block let's say on Y axis within a specified distance between them? A sort of routine "block arrangement". Only if this is not too complicated. Thank you in advance.

Regards,

Andrulex

Link to comment
Share on other sites

Andrulex said:
Thank you Lee Mac. This works very good. If i may dare to ask you a last question: now, all dxf's are inserted in the same insertion point overlapping one over each other. There are hundreds of small plates in this case. Is it possible to "arrange" every block let's say on Y axis within a specified distance between them? A sort of routine "block arrangement". Only if this is not too complicated. Thank you in advance.

 

Not too difficult to incorporate, give this a try:

 

(defun c:InsertAll ( / cmd dir extn pt vec )

   (setq extn "dxf") ;; Extension of files to Insert e.g "dwg"

   (if
       (and
           (setq dir (LM:DirectoryDialog (strcat "Select Directory of " (strcase extn) " Files to Insert") nil 512))
           (setq vec (getpoint "\nPick Spacing Vector: " '(0 0 0)))
       )
       (progn
           (setq cmd (getvar 'CMDECHO))
           (setvar 'CMDECHO 0)
           (setq pt '(0 0 0))
           (foreach file (vl-directory-files dir (strcat "*." extn) 1)
               (vl-cmdf "_.-insert" (strcat dir "\\" file) "_S" 1.0 "_R" 1.0 "_non" pt)
               (setq pt (mapcar '+ pt vec))
           )
           (setvar 'CMDECHO cmd)
       )
       (princ "\n*Cancel*")
   )
   (princ)
)

;;-------------------=={ Directory Dialog }==-----------------;;
;;                                                            ;;
;;  Displays a dialog prompting the user to select a folder   ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  msg  - message to display at top of dialog                ;;
;;  dir  - root directory (or nil)                            ;;
;;  flag - bit coded flag specifying dialog display settings  ;;
;;------------------------------------------------------------;;
;;  Returns:  Selected folder filepath, else nil              ;;
;;------------------------------------------------------------;;

(defun LM:DirectoryDialog ( msg dir flag / Shell Fold Self Path )
   (vl-catch-all-apply
       (function
           (lambda ( / ac HWND )
               (if
                   (setq Shell (vla-getInterfaceObject (setq ac (vlax-get-acad-object)) "Shell.Application")
                         HWND  (vl-catch-all-apply 'vla-get-HWND (list ac))
                         Fold  (vlax-invoke-method Shell 'BrowseForFolder (if (vl-catch-all-error-p HWND) 0 HWND) msg flag dir)
                   )
                   (setq Self (vlax-get-property Fold 'Self)
                         Path (vlax-get-property Self 'Path)
                         Path (vl-string-right-trim "\\" (vl-string-translate "/" "\\" Path))
                   )
               )
           )
       )
   )
   (if Self  (vlax-release-object  Self))
   (if Fold  (vlax-release-object  Fold))
   (if Shell (vlax-release-object Shell))
   Path
)

(vl-load-com) (princ)
 

I hope the use of the 'Spacing Vector' is clear and understandable.

Edited by Lee Mac
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...