After testing this function for a few days, I’ve found out that it can run a script, and the reason for the occasional “Execution error” was that it must be run from the last in the list of open drawings. I added the c:CDC, Current Drawing Commands, to be able to test your commands in the current drawing before running them on all open drawings. I’ve attached OpenDwgsCmds.dvb, the vba part of the code, for those who may unfamiliar or uncomfortable with creating vba code.
;-------------------------------------------------------------------------------
; OpenDwgsCmds - Main function that runs the other functions
; Arguments: 1
; ListCmds@ = List of commands and functions to execute
; Returns: Executes list of commands and functions on all open drawings
; Syntax example: (OpenDwgsCmds (list "line 0,0 100,100" "" "(c:PurgeAll)" "zoom e"))
; Note: The current drawing must be the last in the list of open drawings.
;-------------------------------------------------------------------------------
(defun OpenDwgsCmds (ListCmds@ / Cmd$ CurrentDwg$ FileName%)
(setq CurrentDwg$ (strcat (getvar "DWGPREFIX") (getvar "DWGNAME")))
(if (/= CurrentDwg$ (last (GetDwgsList)))
(progn
(alert (strcat "To run the function OpenDwgsCmds,\n"
"the current drawing must be the last\n"
"in the list of open drawings.")
);alert
(ActivateDwg (last (GetDwgsList)))
(exit)
);progn
);if
(if (not (findfile "C:\\Temp\\Temp.scr"))
(progn (vl-load-com)(vl-mkdir "C:\\Temp"))
);if
(setq FileName% (open "C:\\Temp\\Temp.scr" "w"))
(foreach Cmd$ ListCmds@
(write-line Cmd$ FileName%)
);foreach
(close FileName%)
(command "vbaload" "OpenDwgsCmds.dvb")
(command "-vbarun" "thisdrawing.Main")
(command "vbaunload" "OpenDwgsCmds.dvb")
(princ)
);defun OpenDwgsCmds
;-------------------------------------------------------------------------------
; c:CDC - Current Drawing Commands version of the OpenDwgsCmds function
;-------------------------------------------------------------------------------
(defun c:CDC (/ Cmd$ FileName% ListCmds@ Loop)
(princ "\nExecute commands and functions on current drawing")
(setq Loop t)
(while Loop
(setq Cmd$ (getstring "\nEnter command or function, or type 'END' to end: " t))
(if (= (strcase Cmd$) "END")
(setq Loop nil)
(setq ListCmds@ (append ListCmds@ (list Cmd$)))
);if
);while
(if ListCmds@
(progn
(if (not (findfile "C:\\Temp\\Temp.scr"))
(progn (vl-load-com)(vl-mkdir "C:\\Temp"))
);if
(setq FileName% (open "C:\\Temp\\Temp.scr" "w"))
(foreach Cmd$ ListCmds@
(write-line Cmd$ FileName%)
);foreach
(close FileName%)
(command "SCRIPT" "C:\\Temp\\Temp.scr")
);progn
);if
(princ)
);defun c:CDC
;-------------------------------------------------------------------------------
; c:ODC - Open Drawings Commands version of the OpenDwgsCmds function
; Note: The current drawing must be the last in the list of open drawings.
;-------------------------------------------------------------------------------
(defun c:ODC (/ Cmd$ CurrentDwg$ ListCmds@ Loop)
(princ "\nExecute commands and functions on all open drawings")(princ)
(setq CurrentDwg$ (strcat (getvar "DWGPREFIX") (getvar "DWGNAME")))
(if (/= CurrentDwg$ (last (GetDwgsList)))
(progn
(alert (strcat "To run the function ODC, Open Drawings\n"
"Commands, the current drawing must be\n"
"the last in the list of open drawings.")
);alert
(ActivateDwg (last (GetDwgsList)))
(exit)
);progn
);if
(setq Loop t)
(while Loop
(setq Cmd$ (getstring "\nEnter command or function, or type 'END' to end: " t))
(if (= (strcase Cmd$) "END")
(setq Loop nil)
(setq ListCmds@ (append ListCmds@ (list Cmd$)))
);if
);while
(if ListCmds@
(OpenDwgsCmds ListCmds@)
);if
(princ)
);defun c:ODC
;-------------------------------------------------------------------------------
; c:OpenDwgsCmds - Executed by OpenDwgsCmds.dvb
;-------------------------------------------------------------------------------
(defun c:OpenDwgsCmds ()
(command "SCRIPT" "C:\\Temp\\Temp.scr")
(princ)
);defun c:OpenDwgsCmds
;-------------------------------------------------------------------------------
; Start of OpenDwgsCmds Support Utility Functions
;-------------------------------------------------------------------------------
; GetDwgsList - Returns a list of open drawings
; Use (length (GetDwgsList)) for the number of open drawings.
;-------------------------------------------------------------------------------
(defun GetDwgsList (/ AcadOBJ DocsOBJ DwgsList@)
(if (>= (atoi (getvar "ACADVER")) 15)
(progn
(setq AcadOBJ (vlax-get-acad-object)
DocsOBJ (vlax-get-property AcadOBJ "Documents")
DwgsList@ nil
);setq
(vlax-for ForItem DocsOBJ
(setq DwgsList@ (cons (strcat (vlax-get-property ForItem "Path") "\\"
(vlax-get-property ForItem "Name")) DwgsList@))
);vlax-for
(setq DwgsList@ (reverse DwgsList@))
);progn
(setq DwgsList@ (list (strcat (getvar "DWGPREFIX") (getvar "DWGNAME"))))
);if
DwgsList@
);defun GetDwgsList
;-------------------------------------------------------------------------------
; ActivateDwg - Switches between open drawings
; Arguments: 1
; DwgPathFilename$ = Path and filename string of drawing
; Returns: Makes the drawing of DwgPathFilename$ current
;-------------------------------------------------------------------------------
(defun ActivateDwg (DwgPathFilename$ / Cnt# Num#)
(setq Cnt# 0 Num# 0)
(foreach Item (GetDwgsList)
(if (= (strcase DwgPathFilename$) (strcase Item))
(setq Num# Cnt#)
);if
(setq Cnt# (1+ Cnt#))
);foreach
(command "vbastmt" (strcat "Application.Documents.Item(" (itoa Num#) ").Activate"))
(princ)
);defun ActivateDwg
;-------------------------------------------------------------------------------
; OpenDwgsCmds.dvb - vba part of the code
;-------------------------------------------------------------------------------
;Option Explicit
;Sub Main()
; Dim objDwg As AcadDocument
; Dim objAcad As AcadApplication
; Dim intDwgCnt As Integer
; Dim strThisDwg As String
; Dim intThisDwg As Integer
; Set objAcad = AcadApplication.Application
; intDwgCnt = 0
; strThisDwg = ThisDrawing.FullName
; For Each objDwg In objAcad.Documents
; If objAcad.Documents.Item(intDwgCnt).FullName <> strThisDwg Then
; objAcad.Documents.Item(intDwgCnt).Activate
; objDwg.SendCommand "(load ""OpenDwgsCmds.lsp"")" & vbCr & "OpenDwgsCmds" & vbCr
; Else
; intThisDwg = intDwgCnt
; End If
; intDwgCnt = intDwgCnt + 1
; Next objDwg
; objAcad.Documents.Item(intThisDwg).Activate
; ThisDrawing.SendCommand "(load ""OpenDwgsCmds.lsp"")" & vbCr & "OpenDwgsCmds" & vbCr
;End Sub
;-------------------------------------------------------------------------------
(princ);End of OpenDwgsCmds