George4414 Posted April 17 Posted April 17 (edited) Hello, I would like to have the following setup: Prepate a csv file with the necessary data: (C1)File path (C2) Layout name (C3) Dyn block name (C4) attribute tag (C5) new value The script would work in the following manner: 1. Start command 2. Chose the CSV file 3. Run 4. 1 by 1 open, change, save and close each dwg. All autocad files in the first column of the CSV will be opened 1 by 1 and apply the value change using the rest of the data in the CSV. I would also like to be able to add for the same file multiple posibilities on different rows in the CSV like maybe on a different layout change other's block value. So the script would have to first check the C1 file path column and if it finds the same file multiple times will apply all the changes to it before saving and closing it. I tried an AI generated script however this is not working properly. (defun vl-string-split (str delim / pos result) (while (setq pos (vl-string-search delim str)) (setq result (cons (substr str 1 pos) result)) (setq str (substr str (+ pos (strlen delim) 1)))) (reverse (cons str result)) ) (defun safe-open-doc (filepath) (if (findfile filepath) (vl-catch-all-apply (function (lambda () (vla-open (vla-get-Documents (vlax-get-acad-object)) filepath))) ) ) ) (defun c:UpdateDynamicBlockAttributesFromCSV ( / csv fh line parts filePath blockName layoutName attrTag newVal doc layoutSpace layouts layout found updated) (vl-load-com) (setq csv (getfiled "Select CSV File" "" "csv" 4)) (if (and csv (setq fh (open csv "r"))) (progn (while (setq line (read-line fh)) (setq parts (vl-string-split line ",")) (if (>= (length parts) 5) (progn (setq filePath (nth 0 parts) blockName (nth 1 parts) layoutName (nth 2 parts) attrTag (nth 3 parts) newVal (nth 4 parts)) (princ (strcat "\n Opening: " filePath)) (setq doc (safe-open-doc filePath)) (if (and doc (not (vl-catch-all-error-p doc))) (progn (vla-activate doc) (setq found nil) ;; Try switching to the layout (setq layouts (vla-get-Layouts doc)) (vlax-for layout layouts (if (= (strcase (vla-get-name layout)) (strcase layoutName)) (progn (vla-put-ActiveLayout doc layout) (setq found T) ) ) ) (if (not found) (progn (princ (strcat "\n Layout not found: " layoutName)) (vla-close doc :vlax-false) ) (progn ;; Get the correct space (setq layoutSpace (if (= (strcase layoutName) "MODEL") (vla-get-ModelSpace doc) (vla-get-PaperSpace doc) ) ) (if layoutSpace (progn (setq updated nil) ;; Search block references (vlax-for obj layoutSpace (if (and (= (vla-get-ObjectName obj) "AcDbBlockReference") (= (strcase (vla-get-EffectiveName obj)) (strcase blockName))) (progn (if (vlax-method-applicable-p obj 'GetAttributes) (vlax-for att (vlax-invoke obj 'GetAttributes) (if (= (strcase (vla-get-TagString att)) (strcase attrTag)) (progn (vla-put-TextString att newVal) (setq updated T) (princ (strcat "\n Updated [" attrTag "] to [" newVal "]")) ) ) ) ) ) ) ) (if (not updated) (princ "\n No matching block or attribute found.")) (vla-save doc) (vla-close doc) ) (progn (princ "\n Could not get layout space.") (vla-close doc :vlax-false) ) ) ) ) ) (princ (strcat "\n Could not open file: " filePath)) ) ) (princ "\n Invalid CSV line (expecting at least 5 values).") ) ) (close fh) (princ "\n Done updating all DWG files.") ) (princ "\n Failed to open CSV file.") ) (princ) ) Thank you! Edited April 17 by SLW210 Aded Code Tags!! Quote
SLW210 Posted April 17 Posted April 17 Please use Code Tags for your code in the future. (<> in the editor) Quote
BIGAL Posted April 18 Posted April 18 (edited) If your happy to use a script then it can be done easy, you would read the csv and write a script file like this. open "c:\\yourpath\\dwg1" (load "dynupdate") (dynupdate layout blockname tag val) ... repeat for each layout close Y open dwg2 (load "dynupdate") and so on Edited April 19 by BIGAL Quote
BIGAL Posted April 19 Posted April 19 Like @SLW210 you can read an Excel from CAD without need for the csv file. 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.