lisp routine for 2018

lisp routine for 2018

Anonymous
Not applicable
3,969 Views
9 Replies
Message 1 of 10

lisp routine for 2018

Anonymous
Not applicable

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

 

0 Likes
3,970 Views
9 Replies
Replies (9)
Message 2 of 10

Ranjit_Singh
Advisor
Advisor

I imagine you will need a reactor to do that with every close. Unless you were using a custom routine to close the drawing? Can you provide what you have so far? I am sure some of us have 2018 and can test it for you.

0 Likes
Message 3 of 10

john.uhden
Mentor
Mentor

Why would 2018 have anything to do with this request?  It can all be done with Vlisp (catch-all 'vla-delete).  Yes, a reactor would be in order.

John F. Uhden

0 Likes
Message 4 of 10

rapidcad
Collaborator
Collaborator

Greg, what was your program working with previously? What version of AutoCAD? If you post the program we can figure out what is wrong, but I wonder if you previously had a version of AutoCAD that did not have the Trusted Locations feature introduced in 2014 (IIRC). That might be preventing an older program from working but it will alert you if it is blocking the program. I'm not aware of any changes in lisp related to 2018 specifically. My point is that maybe a setting is keeping your program from running.

If I wanted to accomplish that I would try undefining the close command and supplying my own lisp - something like this...

(defun C:close (/)
  (setvar "clayer" "0")
  (setvar "visretain" 1)
  (command "_.-purge" "a" "" "n" )
  (command "'_zoom" "_extents")
  (command "'_zoom" "_extents")
  (command "_.qsave")
  (command "_.end")
  )

  A script could also be written to do this as well.

Instead of the above, a lot of us do such things on startup like adding the example below to acaddoc.lsp

(acad-push-dbmod);grabs dbmod value so user's don't get prompted to save empty drawings
(defun-q RAPSTARTUP ()
(setvar "coords" 2)
(setvar "osmode" 4259)
(setvar "blipmode" 0)								
(setvar "PICKSTYLE" 1)
(setvar "mirrtext" 0)
	 (setvar "CMDECHO" 0)
(setvar "visretain" 1)
(setvar "regenmode" 1)
(setvar "SAVETIME" 15)
(setvar "IMAGEFRAME" 2)
(if(= (getvar "CTAB") "Model")	 (setvar "NAVVCUBEDISPLAY" 1))
(setvar "plinewid" 0)
(SETVAR "PUBLISHCOLLATE" 0)
(setvar "highlight" 1)
(setvar "HPDRAWORDER" 1)	 
(setvar "celtype" "bylayer")
(setvar "cecolor" "bylayer")
(princ)
)
(setq S::STARTUP (append S::STARTUP RAPSTARTUP)
)

You could just add a startup lisp like that to your acaddoc.lsp and the only thing you would need to do to close drawings then would be to, well, close them. But maybe you are thinking of making your drawings all uniform for others when they open them.

 

HTH

ADN CAD Developer/Operator
0 Likes
Message 5 of 10

roland.r71
Collaborator
Collaborator

@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)
0 Likes
Message 6 of 10

Anonymous
Not applicable
Thanks I will try some of these
0 Likes
Message 7 of 10

jtuttle
Participant
Participant

Where do I load the smart close reactor?  Acaddoc?

0 Likes
Message 8 of 10

john.uhden
Mentor
Mentor

I dug this out from 2000­± and redacted it for your use.  The meatier version has been working for me for decades.

Yes, you would put the whole thing in your acaddoc.lsp file, OR

save the whole thing to say "CLOSE_REACTOR.LSP" and have acaddoc.lsp (load "CLOSE_REACTOR")

 

 

;;++++++++++++++++++++++++++++++++++++++++++++++++++
;; Set up reactor callback function for a drawing close:
;;
(defun @JU_drawing_close (calling-reactor commandInfo / dwgname cmdnames)
   (setq  dwgname   (vlax-get *doc* 'fullname)
               cmdnames  (strcase (getvar "cmdnames"))
   )
   (if (= test_mode 1) ;; this is something the user can set to see what this reaction shows
      (progn
         (princ "\n\nPerforming @JU_drawing_close")
         (princ "\nCurrent Reaction: ")(prin1 (vlr-current-reaction-name))
         (princ "\nDocument fullname: ")(prin1 dwgname)
         (princ "\nCalling Reactor: ")(prin1 calling-reactor)
         (princ "\nCalling Reactor Type: ")(prin1 (type calling-reactor))
         (princ "\nCommand Info: ")(prin1 CommandInfo)
         (princ "\nCommand Info Type: ")(prin1 (type CommandInfo))
         (princ "\n@JU_drawing_close CMDNAMES: ")(prin1 cmdnames)
         (princ)
      )
   )
   ;; added "*INSERT*" 12-18-00
   (if (or (not cmdnames)(not (wcmatch cmdnames "*COPY*,*CLIP*,*PASTE*,*INSERT*")))
      (progn
        ;; ****************************
        ;; DO
        ;; YOUR
        ;; REACTION
        ;; OPERATIONS
        ;; HERE
        ;; ****************************
        ;; for example...
        (alert "Drawing is about to close")
      )
   )
   (if (= test_mode 1)
       (princ "\nEnd of @JU_drawing_close.\n")
   )
   (princ)
)

;; Set up the reactor (vl-load-com) (or *doc* (setq *doc* vla-get-activedocument (vlax-get-acad-object))) ;; global (if (= (type JU_dwg_reactor) 'VLR-DWG-Reactor) (if (not (vlr-added-p JU_dwg_reactor)) (vlr-add JU_dwg_reactor) ) (setq JU_dwg_reactor (vlr-dwg-reactor "JU Drawing Reactor" '( ; (:vlr-dwgFileOpened . @JU_end_open) ;; not provided ; (:vlr-endDwgOpen . @JU_end_open) ;; not provided (:vlr-BeginClose . @JU_drawing_close) ; (:vlr-BeginSave . @JU_begin_save) ;; not provided ; (:vlr-SaveComplete . @JU_end_save) ;; not provided ; (:vlr-beginDwgOpen . @JU_begin_open) ;; not provided ) ) ) )

 

John F. Uhden

0 Likes
Message 9 of 10

jtuttle
Participant
Participant

Thanks, I'll give it a shot.

0 Likes
Message 10 of 10

roland.r71
Collaborator
Collaborator

Yes. or use 'appload' (way easier)

0 Likes