Sei sulla pagina 1di 4

;;------------------=={ ObjectDBX Wrapper }==-----------------;;

;;
;;
;; Evaluates a function on all drawings in a supplied list ;;
;; or in a selected directory (and subdirectories)
;;
;;------------------------------------------------------------;;
;; Author: Lee Mac, Copyright 2011 - www.lee-mac.com
;;
;;------------------------------------------------------------;;
;; Arguments:
;;
;; foo
- a function taking a two arguments
;;
;;
(the ODBXDocument) and (the ActiveDocument)
;;
;;
;;
;; dwglst - [Optional] List of DWG Filenames, if nil,
;;
;;
BrowseForFolder Dialog is displayed
;;
;;
;;
;; save - Boolean flag determining whether drawings should ;;
;;
be saved after function evaluation (T=saved)
;;
;;------------------------------------------------------------;;
;; Returns: List of ((<DWGFilename> . <FunctionResult>)...) ;;
;;
;;
;; <DWGFilename>
is the drawing filename string
;;
;; <FunctionResult> is the result of evaluating the function ;;
;; 'foo' on the document object representing the drawing
;;
;; filename - note: this could be an Error Object
;;
;;------------------------------------------------------------;;
(defun LM:ODBX ( foo dwglst save / *error* _ReleaseObject acapp acdoc acdocs dbx
dbxx dbxdoc err path result ) (vl-load-com)
(setq acapp (vlax-get-acad-object)
acdoc (vla-get-ActiveDocument acapp)
acdocs (vlax-for x (vla-get-documents acapp) (setq acdocs (cons (cons (s
trcase (vla-get-fullname x)) x) acdocs)))
dbxdoc (LM:ObjectDBXDocument acapp)
)
(defun *error* ( msg )
(mapcar '_ReleaseObject (list dbx dbxdoc))
(or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
(princ (strcat "\n** Error: " msg " **")))
(princ)
)
(defun _ReleaseObject ( obj )
(and obj (eq 'VLA-OBJECT (type obj)) (not (vlax-object-released-p obj))
(not
(vl-catch-all-error-p
(vl-catch-all-apply
(function vlax-release-object) (list obj)
)
)
)
)
)
(if
(setq dwgLst
(cond
( dwgLst )
( (setq Path (LM:DirectoryDialog "Select Directory to Process" nil 832))

(initget "Yes No")


(LM:GetAllFiles Path (eq "Yes" (cond ( (getkword "\nProcess SubDirecto
ries? <Yes> : ") ) ( "Yes" ))) "*.dwg")
)
)
)
(foreach dwg dwgLst
(if
(or (setq dbxx (cdr (assoc (strcase dwg) acdocs)))
(progn
(setq err
(vl-catch-all-apply 'vla-open (list dbxdoc dwg)) dbx dbxdoc
)
(not (vl-catch-all-error-p err))
)
)
(progn
(setq result (cons (cons dwg (vl-catch-all-apply foo (list dbx dbxx)))
result))
(if save (vla-saveas dbx dwg))
)
(princ (strcat "\n--> Error Opening File: " (vl-filename-base dwg) ".dwg
"))
)
)
)
(mapcar '_ReleaseObject (list dbx dbxdoc))
(reverse Result)
)
;;-------------------=={ 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-a
ll-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 "/" "\\" Pa
th))

)
)
)
)
)
(if Self (vlax-release-object Self))
(if Fold (vlax-release-object Fold))
(if Shell (vlax-release-object Shell))
Path
)
;;--------------------=={ Get All Files }==-------------------;;
;;
;;
;; Retrieves all files or those of a specified filetype that ;;
;; reside in a directory (and, optionally, subdirectories) ;;
;;------------------------------------------------------------;;
;; Author: Lee Mac, Copyright 2011 - www.lee-mac.com
;;
;;------------------------------------------------------------;;
;; Arguments:
;;
;; Dir
- Directory to search
;;
;; Subs
- Boolean, if T, subdirectories are included
;;
;; Filetype - (optional) Filter for filetype (DOS pattern) ;;
;;------------------------------------------------------------;;
;; Returns: List of filenames, else nil if none are found ;;
;;------------------------------------------------------------;;
(defun LM:GetAllFiles ( Dir Subs Filetype / _GetSubFolders )
(defun _GetSubFolders ( folder )
(apply 'append
(mapcar
(function
(lambda ( f )
(cons (setq f (strcat folder "\\" f)) (_GetSubFolders f))
)
)
(vl-remove "." (vl-remove ".." (vl-directory-files folder nil -1)))
)
)
)
(apply 'append
(mapcar
(function
(lambda ( Filepath )
(mapcar
(function
(lambda ( Filename ) (strcat Filepath "\\" Filename))
)
(vl-directory-files Filepath Filetype 1)
)
)
)
(cons Dir (if subs (_GetSubFolders Dir)))
)
)
)
;;-----------------=={ ObjectDBX Document }==-----------------;;
;;
;;

;; Retrieves a version specific ObjectDBX Document object


;;
;;------------------------------------------------------------;;
;; Author: Lee Mac, Copyright 2011 - www.lee-mac.com
;;
;;------------------------------------------------------------;;
;; Arguments:
;;
;; _acad - AutoCAD VLA Application Object
;;
;;------------------------------------------------------------;;
;; Returns: VLA ObjectDBX Document object, else nil
;;
;;------------------------------------------------------------;;
(defun LM:ObjectDBXDocument ( _acad / acVer )
(vla-GetInterfaceObject _acad
(if (< (setq acVer (atoi (getvar "ACADVER"))) 16)
"ObjectDBX.AxDbDocument" (strcat "ObjectDBX.AxDbDocument." (itoa acVer))
)
)
)
;;------------------------------------------------------------;;
;;
End of File
;;
;;------------------------------------------------------------;;

Potrebbero piacerti anche