@Anonymous wrote:
I haven't had any experience with writing lisp routines. Can anyone write one that does the following when closing a drawing?
I had one but it stopped working with 2018.
set to layer 0
set visretain to 1
purge drawing
zoom extents
save
Thanks for any help
Here's an adaptation of my own close-reactor (which i first created with a lot of help from here)
The blue part is where it all happens. (so any additional actions should go there.)
I left in my own zoom all/extents routine. It will "zoom all" on all layouts or "zoom extents" on model.
It has the option to make a pre-set layout current (by name). Currently i set it to nil. (i use it to allways make the
index sheet current)
I left out the 'save' as AutoCAD will ask you if it should be saved or not. Automatically saving will ALLWAYS save a
drawing when closed. Something i personally would not advise to do. (now you (still) have a choice)
If the drawing has no changes (since last save) and/or is opened as read-only, it will NOT do anything.
; =============================================================================
; REACTOR CLOSE
; =============================================================================
; Description:
; ------------
; Reactor for close function.
; When user closes drawing, this function runs first, to zoom all pages before
; saving (& closing)
;
; If started from a layout, it will "zoom all" every layout.
;
; IF starting from Modelspace & there's a "Layout1" it will only "zoom extents"
; on the Model tab, assuming there are no layouts.
;
; The zoom all/extents will only be invoked if the file has changed since last
; save & the file is NOT read-only
; =============================================================================
(vl-load-com)
; -----------------------------------------------------------------------------
; CLOSE reactor
; -----------------------------------------------------------------------------
(defun smart-command-reactor (commands StartCallback EndCallback / ended)
(vl-load-reactors)
(setq ended
(vlr-command-reactor nil
'(
(:vlr-commandEnded . internal-commandEnded)
(:vlr-commandCancelled . internal-commandEnded)
(:vlr-commandFailed . internal-commandEnded)
)
)
)
(vlr-remove ended)
(vlr-command-reactor
(list
ended
StartCallback
EndCallback
(if (listp commands)
(mapcar 'strcase commands)
(list (strcase commands))
)
)
'((:vlr-commandWillStart . internal-commandWillStart))
)
)
; -----------------------------------------------------------------------------
(defun internal-commandWillStart (reactor args / data result)
(setq data (vlr-data reactor))
(if
(and
(member (car args) (last data))
(setq result (apply (cadr data) (list (car args))))
)
(progn
(vlr-data-set
(car data)
(list (caddr data) result)
)
(vlr-add (car data))
)
)
)
; -----------------------------------------------------------------------------
(defun internal-commandEnded (reactor args / data)
(setq data (vlr-data reactor))
(vlr-remove reactor)
(apply
(car data)
(list
reactor
(vlr-current-reaction-name)
(car args)
(cadr data)
)
)
)
; ------------------------------------------------------------------------------
(if *my-smart-close-reactor* (vlr-remove *my-smart-close-reactor*))
; ------------------------------------------------------------------------------
(setq *my-smart-close-reactor*
(smart-command-reactor
; command(s) as list
'("CLOSE")
; StartCallback (called when CLOSE starts)
(function
(lambda (cmdname)
; ==================================================================
; Code to execute starts here
; ==================================================================
; Only if the file changed (since last save!) & its NOT read-only -> execute "zoom all"
(if (and (> (getvar "DBMOD") 0) ; File has changed
(> (getvar "writestat") 0) ; File is writeable
)
(progn
; ------------------------------------------------------------
; Init
; ------------------------------------------------------------
(setq starttab (getvar 'ctab)) ; store current layout
(setq setTab nil) ; layout to make current. Set to nil or use comment to keep active tab
(setvar "visretain" 1) ; XREF layer settings
(command-s "_.-layer" "S" "0" "") ; Set layer 0 as the Current layer
(command-s "_.-purge" "A" "" "N") ; Purge All
(if (not (and (= starttab "Model")(member "Layout1" (layoutlist))))
(progn
; ------------------------------------------------------
; Zoom all layouts
; ------------------------------------------------------
(princ "\nZooming all layouts")
(foreach lay (layoutlist)
(setvar 'ctab lay) ; activate each layout
(vla-zoomall (vlax-get-acad-object)) ; & Zoom All
)
; ------------------------------------------------------
; Activate starting (current) layout
; ------------------------------------------------------
(if setTab ; if a layout is set
(if (member setTab (layoutlist)) ; & the layout exists ...
(setq starttab setTab) ; make it current
)
)
(setvar 'ctab starttab) ; Return to initial/preset layout
)
; else
; ---------------------------------------------------------
; File is drawn in model tab, no need to walk thru layouts
; ---------------------------------------------------------
(progn
(princ "\nZoom Modelspace")
(vla-zoomextents (vlax-get-acad-object))
)
)
; ------------------------------------------------------------
)
)
; ==================================================================
; Code to execute ends here
; ==================================================================
)
)
; EndCallback (called when CLOSE ends)
(function
(lambda (data)
(setvars data)
(sssetfirst nil)
(setq app nil adoc nil)
)
)
)
)
; ------------------------------------------------------------------------------
(defun setvars (data)
(mapcar
(function
(lambda (v / r)
(setq r (getvar (car v)))
(setvar (car v) (cdr v))
(cons (car v) r)
)
)
data
)
)
; -----------------------------------------------------------------------------
(princ "\nCLOSE reactor enabled")
; -----------------------------------------------------------------------------
(princ)