jmerch Posted April 8, 2014 Posted April 8, 2014 Need some guidance here. I can't find anything similar on the web. I'm using Lee Mac's "Get Files" Program (thanks Lee) from his site. It allows the user to select multiple files. I then am creating a list with those files. I want to do a FOREACH that either inserts each file or xrefs each file in that list. I can't get it to work using: (foreach x mylist (command "-insert" x)) There's more I want to do after this but need to get this working first. Most FOREACH examples I find work but not for this. Quote
cwake Posted April 8, 2014 Posted April 8, 2014 Focus your attention on the (command "-insert" x) part. The insert command requires an insert point, a scale, (and depending on the block definition an x and y scale separately), and a rotation value. As it stands, the foreach is telling AutoCAD to insert the next block before it has been able to finish the first. Quote
jmerch Posted April 8, 2014 Author Posted April 8, 2014 Right, but after the "-insert" you have to tell it what block to insert so I thought by telling it 'x' it reads the first name from the list. That's where I'm getting stuck. If i just put the coordinates after "-insert", it will think that's the block name I'm trying to insert. Quote
cwake Posted April 8, 2014 Posted April 8, 2014 Which is why you have to provide all of the information as you would on the command line, eg. (foreach x mylist (command "-insert" x "0,0,0" "1.0" "" "0")) Quote
jmerch Posted April 8, 2014 Author Posted April 8, 2014 That does not work, for the same reason. It's not recognizing 'x' as the block name. Quote
cwake Posted April 8, 2014 Posted April 8, 2014 Have you confirmed that mylist is actually a list of filename strings? Lee's function returns a filelist like ("Z:\\blk.usr\\_GENERAL DRAFTING\\NORTH POINTS\\NORTH1.DWG" "Z:\\blk.usr\\_GENERAL DRAFTING\\NORTH POINTS\\NORTH2.DWG" "Z:\\blk.usr\\_GENERAL DRAFTING\\NORTH POINTS\\NORTH3.DWG" "Z:\\blk.usr\\_GENERAL DRAFTING\\NORTH POINTS\\NORTH4.dwg" "Z:\\blk.usr\\_GENERAL DRAFTING\\NORTH POINTS\\NORTH8.DWG" "Z:\\blk.usr\\_GENERAL DRAFTING\\NORTH POINTS\\NORTH9.DWG" "Z:\\blk.usr\\_GENERAL DRAFTING\\NORTH POINTS\\NORTH12.DWG") If the list is a list of suitable drawing filenames like this ^^, then the problem is not in the foreach and x. (If you don't know how to see what mylist is type !mylist (with a leading exclamation mark) at the command line) See how you go. I'm off to bed. It's 1am here. Quote
BlackBox Posted April 8, 2014 Posted April 8, 2014 This may be of use to you: (vl-load-com) (defun _InsertBlocks (blockList / *error* oSpace insertionPoint) (defun *error* (msg) (vla-endundomark acDoc) (cond ((not msg)) ; Normal exit ((member msg '("Function cancelled" "quit / exit abort"))) ; <esc> or (quit) ((princ (strcat "\n** Error: " msg " ** "))) ; Fatal error, display it ) (princ) ) (vla-startundomark (setq acDoc (vla-get-activedocument (vlax-get-acad-object))) ) (setq oSpace (vlax-get acDoc (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace ) ) ) (setq insertionPoint (vlax-3d-point '(0 0 0))) (foreach x blockList (vl-catch-all-apply 'vla-insertblock (list oSpace insertionPoint x 1. 1. 1. 0.) ) ) (*error* nil) ) Quote
jmerch Posted April 8, 2014 Author Posted April 8, 2014 @Clint - my list looks like yours that is created from Lee's program. So if the problem isn't the foreach, then what is it? Get a good nights sleep @blackbox - Is that suppose to be my routine for the 'foreach'? I tried (foreach x mylist (_insertblocks)) and it says too few arguments. Thanks! Quote
BlackBox Posted April 8, 2014 Posted April 8, 2014 @blackbox - Is that suppose to be my routine for the 'foreach'? I tried (foreach x mylist (_insertblocks)) and it says too few arguments. Try just supplying your mylist variable to the sub-function I posted. Example: ;; get mylist (_insertblocks [color="blue"]mylist[/color]) Quote
jmerch Posted April 8, 2014 Author Posted April 8, 2014 Well, hell ya! That is sweet! Now, what if instead of the insert functionality, i wanted it to xref the drawings from the list (attach)? You are awesome! Quote
BlackBox Posted April 8, 2014 Posted April 8, 2014 Well, hell ya! That is sweet! .... You are awesome! That is kind of you to say, jmerch; I'm happy to help. Now, what if instead of the insert functionality, i wanted it to xref the drawings from the list (attach)? A direct exchange between the insert, and attach functionality might be: (defun _AttachXrefs (blockList / *error* oSpace insertionPoint) (defun *error* (msg) (vla-endundomark acDoc) (cond ((not msg)) ; Normal exit ((member msg '("Function cancelled" "quit / exit abort"))) ; <esc> or (quit) ((princ (strcat "\n** Error: " msg " ** "))) ; Fatal error, display it ) (princ) ) (vla-startundomark (setq acDoc (vla-get-activedocument (vlax-get-acad-object))) ) (setq oSpace (vlax-get acDoc (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace ) ) ) (setq insertionPoint (vlax-3d-point '(0 0 0))) (foreach x blockList (setq oXref (vl-catch-all-apply 'vla-attachexternalreference (list oSpace x (vl-filename-base x) insertionPoint 1. 1. 1. 0. :vlax-true ) ) ) ;;<-- place each xref on a specific layer here (layer must exist) ) ;;<-- potential draworder changes here (*error* nil) ) ... However, it sounds as though you're more after something like the 'XrefUnderlay' routine I use for production, which allows me to quickly select multiple drawings, relative to the active document, remembers the last directory specified for each drawing, reloads XREFs that have previously been attached, and supports undo functionality: (vl-load-com) (defun c:XrefUnderlay (/ *error* flag xrefs ss acDoc layerName oLayer insertionPoint xrefName oXref space layerName ) (princ "\rXREFUNDERLAY \n") (defun *error* (msg) (if acDoc (vla-endundomark acDoc) ) (cond ((not msg)) ; Normal exit ((member msg '("Function cancelled" "quit / exit abort"))) ; <esc> or (quit) ((princ (strcat "\n** Error: " msg " ** "))) ; Fatal error, display it ) (princ) ) (if (and (setq flag (dos_version)) (setq xrefs (dos_getfilem "Select reference file to underlay" (if *XrefUnderlay_LastPath* *XrefUnderlay_LastPath* (getvar 'dwgprefix) ) "Drawing files (*.dwg)|*.dwg|All files (*.*)|*.*||" ) ) (setq *XrefUnderlay_LastPath* (car xrefs)) (setq ss (ssadd)) ) (progn (vla-startundomark (setq acDoc (vla-get-activeDocument (vlax-get-acad-object))) ) ;; layer check, will create layer if it does not already exist (setq oLayer (vla-add (vla-get-layers acDoc) (setq layerName "[color="red"]YourLayerNameHere[/color]") ) ) [color="blue"];;<-- setq layer properties here[/color] (setq space (vlax-get acDoc (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace ) ) ) (setq insertionPoint (vlax-3d-point '(0 0 0))) (foreach xref (cdr xrefs) (setq xref (strcat *XrefUnderlay_LastPath* xref)) (if (tblsearch "block" (setq xrefName (vl-filename-base xref)) ) (progn (prompt "\nReference already exists, reloading... ") (vl-catch-all-apply 'vla-reload (list (setq oXref (vla-item (vla-get-blocks acDoc) xrefName)) ) ) (princ "Done. ") ) (progn (prompt (strcat "\nAttaching \"" xrefName "\" reference... ") ) (setq oXref (vla-attachexternalreference space xref xrefName insertionPoint 1. 1. 1. 0. :vlax-true ) ) (vla-put-layer oXref layerName) (ssadd (vlax-vla-object->ename oXref) ss) (princ "Done. ") ) ) ) (sssetfirst nil ss) (ai_draworder "_b") ) (cond (flag) ((prompt "\n** DOSLib must be loaded ** ")) ) ) (*error* nil) ) Now, it is important to note that my offering is dependent on DOSLib, which I am perfectly fine with, as I use DOSLib for other things as well.... You may not share my sentiment; for this reason, please feel free to modify the code above as you like (i.e., if you'd like to stick with Lee's GetFiles routine, etc.). Cheers Quote
jmerch Posted April 8, 2014 Author Posted April 8, 2014 Thank you again. The xref one didn't work for me, but I think I can roll with the insert block one. Is there a way to explode the blocks upon insertion as if checking that option in the dialog box? I have been trying the following but it doesn't work here for some reason (probably something small I'm missing) (command "explode" (ssget "_X" '((0 . "INSERT")))) Quote
BlackBox Posted April 8, 2014 Posted April 8, 2014 Thank you again. The xref one didn't work for me, but I think I can roll with the insert block one. FWIW - I tested it before posting, and all drawings (filepath\\filename.dwg) were properly attached as an XREF. Is there a way to explode the blocks upon insertion as if checking that option in the dialog box? I have been trying the following but it doesn't work here for some reason (probably something small I'm missing) (command "explode" (ssget "_X" '((0 . "INSERT")))) First set your selectionset as being 'selected' via SSSETFIRST, then call EXPLODE Command. Quote
jmerch Posted April 8, 2014 Author Posted April 8, 2014 I did that as well using the following and that didn't work for me either. (setq blocks1 (ssget "_X" '((0 . "INSERT")))) (sssetfirst nil blocks1)(command "explode" "") I did try the xref one again and it did work now. I purged out previous block names so must have been trying to xref in the same file as i already inserted. Quote
BlackBox Posted April 8, 2014 Posted April 8, 2014 I did that as well using the following and that didn't work for me either. (setq blocks1 (ssget "_X" '((0 . "INSERT")))) (sssetfirst nil blocks1)(command "explode" "") I did try the xref one again and it did work now. I purged out previous block names so must have been trying to xref in the same file as i already inserted. Sorry - I must have responded as though we were talking about BURST, and not EXPLODE... This works here: (if (setq ss (ssget "_x" '((0 . "INSERT")))) (command "._explode" ss) ) Quote
jmerch Posted April 9, 2014 Author Posted April 9, 2014 Ah....."if". Good call. Thanks for all your help, works perfect! Quote
Recommended Posts
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.