So I've just started to get into Autolisp and VLA and I have to tell you that I'm learning a lot just from studying this post, T.Willey.
I'm trying to modify it but running into trouble understanding, I would sincerely appreciate any thoughts that anyone could lend.
Two challenges:
1.This code renames a file based on one attribute in one block. How could it be modified to concatenate three attributes, from one block, into one title separated by hyphens?
2.And this is much more abstract to me: I'm running two versions of AutoCAD; I have 2014 and 2007 to check for full backwards compatibility. Using this code, even running from 2007, it resaves the file as a 2013 formatted drawing. I'm sure that it has to do with the line (getvar "acadver") and the following line, but I'm very unsure of how to fix it to save as a 2007 version.
Again, T.Willey has made some awesome stuff.
Any thoughts would be much appreciated!
;; By Tony Tanzillo
;; Modified by Tim Willey
(defun c:BatchRename (/ *error* Directory-Dia BlkName BlkNameA TagName TagNameA Dir dbxApp FileName RenameList )
(vl-load-com)
(defun *error* (msg)
(if dbxApp (vlax-release-object dbxApp))
(setq dbxApp nil)
(vl-bt)
)
(defun Directory-Dia ( Message / sh folder folderobject result)
(vl-load-com)
(setq sh
(vla-getInterfaceObject
(vlax-get-acad-object)
"Shell.Application"
)
)
(setq folder
(vlax-invoke-method
sh
'BrowseForFolder
(vla-get-HWND (vlax-get-Acad-Object))
Message
0
)
)
(vlax-release-object sh)
(if folder
(progn
(setq folderobject
(vlax-get-property folder 'Self)
)
(setq result
(vlax-get-property FolderObject 'Path)
)
(vlax-release-object folder)
(vlax-release-object FolderObject)
(if (/= (substr result (strlen result)) "\\")
(setq result (strcat result "\\"))
result
)
)
)
)
(setq BlkName "CIR-NOTE")
(setq TagName "NOTE")
;So here I'm sure is where to give define two more variables.
(if (setq Dir (Directory-Dia "Select directory to rename."))
(progn
(setq dbxApp
(if (< (atoi (setq oVer (substr (getvar "acadver") 1 2))) 16)
;And this is what I believe is causing the save version.
(vla-GetInterfaceObject (vlax-get-acad-object) "ObjectDBX.AxDbDocument")
(vla-GetInterfaceObject (vlax-get-acad-object) (strcat "ObjectDBX.AxDbDocument." oVer))
)
)
(foreach file (mapcar (function (lambda (x) (strcat Dir x))) (vl-directory-files Dir "*.dwg" 1))
(if (vl-catch-all-error-p (vl-catch-all-apply 'vla-Open (list dbxApp file)))
(prompt (strcat "\n *Error opening file: " file))
(vlax-for lo (vla-get-Layouts dbxApp)
(vlax-for obj (vla-get-Block lo)
(if
(and
(= (vla-get-ObjectName obj) "AcDbBlockReference")
(= (vla-get-Name obj) BlkName)
)
(foreach att (vlax-invoke obj 'GetAttributes)
; I've tried to put an AND function nested in the if to use two variables but couldn't get it to work.
(setq FileName (vla-get-TextString att))
(if (= (vla-get-TagString att) TagName)
(setq FileName (vla-get-TextString att))
)
)
)
)
)
)
(setq RenameList (cons (cons file FileName) RenameList))
(setq FileName nil)
)
)
)
(vlax-release-object dbxApp)
(setq dbxApp nil)
(foreach i RenameList
(if
(or
(null (cdr i))
(not (vl-file-rename (car i) (strcat Dir (cdr i) ".dwg")))
)
(prompt "\n *Was not able to rename: " (car i))
)
)
(princ)
)
Hello, I know this post is 13 years old, but does anyone know how to make this work with two or three attributes.