Author Topic: save a dwg before opening another dwg  (Read 5318 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
save a dwg before opening another dwg
« on: August 19, 2011, 02:04:43 PM »
does anyone have any suggestions on what would be the best way via lisp code to do the follow:

lets say you have dwg A opened, you do some work and dont sve and you want to open dwg B but before dwg B can open dwg A is automatically saved and then dwg B can now open

ronjonp

  • Needs a day job
  • Posts: 7527
Re: save a dwg before opening another dwg
« Reply #1 on: August 19, 2011, 02:19:11 PM »
A command reactor comes to mind:

Code: [Select]
(or *cmdreactors*
    (setq *cmdreactors* (vlr-command-reactor nil '((:vlr-commandwillstart . strtcmd))))
)
(defun strtcmd (calling-reactor strtcmdinfo /)
  (or *adoc* (setq *adoc* (vla-get-activedocument (vlax-get-acad-object))))
  (if (and (wcmatch (car strtcmdinfo) "OPEN") (= (getvar 'dwgtitled) 1))
    (vla-save *adoc*)
  )
)

Note this will not work when "tabbing" through drawings.
« Last Edit: August 19, 2011, 02:25:37 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: save a dwg before opening another dwg
« Reply #2 on: August 19, 2011, 02:36:42 PM »
I thought something like this:

Code: [Select]
(if (null *save-reactor*)
    (setq *save-reactor* (vlr-editor-reactor nil '((:vlr-begindwgopen . savedrawing))))
)
(defun savedrawing ( reactor params )
    (if (= 1 (getvar 'DWGTITLED))
        (vla-save
            (cond
                (   *activedoc*   )
                (   (setq *activedoc* (vla-get-activedocument (vlax-get-acad-object)))   )
            )
        )
    )
    (princ)
)

Since a drawing may be opened from Windows Explorer, without using the Open command, but I can't get it to fire  :-(

ribarm

  • Gator
  • Posts: 3250
  • Marko Ribar, architect
Re: save a dwg before opening another dwg
« Reply #3 on: August 19, 2011, 04:48:30 PM »
I use, simply this :

Code: [Select]
(defun c:opsave ( / aobj adoc )
(vl-load-com)
(setq aobj (vlax-get-acad-object))
(setq adoc (vla-get-activedocument aobj))
(vla-save adoc)
(vlax-release-object adoc)
(vla-open (vla-get-documents aobj) (getfiled "Open dwg file" "" "dwg" 4) :vlax-false)
(vl-cmdf "close" "")
(princ)
)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

BlackBox

  • King Gator
  • Posts: 3770
Re: save a dwg before opening another dwg
« Reply #4 on: August 19, 2011, 05:20:59 PM »
I use, simply this :

Code: [Select]
(defun c:opsave ( / aobj adoc )
(vl-load-com)
(setq aobj (vlax-get-acad-object))
(setq adoc (vla-get-activedocument aobj))
(vla-save adoc)
(vlax-release-object adoc)
(vla-open (vla-get-documents aobj) (getfiled "Open dwg file" "" "dwg" 4) :vlax-false)
(vl-cmdf "close" "")
(princ)
)

 :whistle:

Code: [Select]
(vla-open (vla-get-documents aobj) nil :vlax-false)
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: save a dwg before opening another dwg
« Reply #5 on: August 19, 2011, 05:30:02 PM »
Building on my last post, I'd slightly revise Marko's code:

Code: [Select]
(defun c:OPSAVE  ( / file)
  (vl-load-com)
  (if (setq file (getfiled "Open dwg file" "" "dwg" 4))
    ((lambda (acObj)
       (vla-save (vla-get-activedocument acObj))
       (vla-open (vla-get-documents acObj) file :vlax-false)
       (vl-cmdf "close" ""))
      (vlax-get-acad-object)))
  (princ))

@ Marko - While it is good practice to release vla-objects, there's no need to do so with internal objects such as the acad-object, and activeDocument. Just saying.
« Last Edit: August 26, 2011, 12:57:33 PM by RenderMan »
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: save a dwg before opening another dwg
« Reply #6 on: August 19, 2011, 05:33:37 PM »
lets say you have dwg A opened, you do some work and dont sve and you want to open dwg B but before dwg B can open dwg A is automatically saved and then dwg B can now open

FWIW - You could also explore the SDI System Variable (not sure of what version, or vertical you work with?). SDI = 1 the user is prompted for save upon OPEN command.
"How we think determines what we do, and what we do determines what we get."

andrew_nao

  • Guest
Re: save a dwg before opening another dwg
« Reply #7 on: August 25, 2011, 02:40:54 PM »
maybe someone can help me get this code modified

i got this code form the augi forums here
http://forums.augi.com/showthread.php?t=27428

it saves your dwg after 10 commands and
I want to use my custom save lisp instead of the VLA-SAVE function
but when i try to put my lisp call in there it comes up as an invalid command

can anyone guide me on how i can get my callout working

Code: [Select]
(defun C:Vas ()
  (vl-load-com)
  (setq ASRXN1 (vlr-editor-reactor nil '((:vlr-commandended . ASCOM1)))
     ASRXN2 (vlr-lisp-reactor nil   '((:vlr-lispWillStart . ASCOM2)))
     ASRXN3 (vlr-lisp-reactor nil   '((:vlr-lispended . ASCOM3)))
  )
 )
 (defun ASCOM1 (CALL CALLBACK)
  (if (not LISPRUN)
   (progn
    (cond
  ((= (car CALLBACK) "SAVE")
  (setq ASCOUNT 0)
  )
  ((= (car CALLBACK) "QSAVE")
  (setq ASCOUNT 0)
  )
    )
    (if (not ASCOUNT)
  (setq ASCOUNT 0)
  (if (> ASCOUNT 9)
  (progn
    (setq ASCOUNT 0)
    ;(vla-save (vla-get-activedocument (vlax-get-acad-object)))
  (c:nsave)
; (print "AutoSave")
  )
  (progn
    (setq ASCOUNT (1+ ASCOUNT))
    (print ASCOUNT)
  )
  )
    )
   )
  )
 )
 (defun ASCOM2 (CALL CALLBACK)
  (setq LISPRUN 'T)
 )
 (defun ASCOM3 (CALL CALLBACK)
  (setq LISPRUN nil)
 )

ronjonp

  • Needs a day job
  • Posts: 7527
Re: save a dwg before opening another dwg
« Reply #8 on: August 25, 2011, 03:24:30 PM »
What is (c:nsave)? If I remember correctly, command calls will error in a reactor.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

andrew_nao

  • Guest
Re: save a dwg before opening another dwg
« Reply #9 on: August 25, 2011, 03:59:19 PM »
What is (c:nsave)? If I remember correctly, command calls will error in a reactor.

that is a custom lisp call

i modified the code to work for me, however if i use a custom macro this code countdown returns back to 1
anyone have any thoughts on why?

Code: [Select]
(defun copy_backup_as (/ activedoc docfullname backuppath archivename)
  (setq activedoc (vla-get-activedocument (vlax-get-acad-object)))
  ;;
  ;;----- Main --------------------------
  ;;

  (if (and (not (= 0 (strlen (setq docfullname (vla-get-fullname activedoc)))))
           (setq backuppath "C:\\ACADBACK\\")
           (setq archivename (strcat backuppath
                                     (vl-filename-base (vla-get-name activedoc))
                                     ".dwg"
                             )
           )
      )
    (vl-file-copy docfullname archivename)
    (alert "Ooooops. Unable to establish ArchiveName.")
  )
  (prompt (strcat "\nFile Saved..." archivename))
  (princ)
)

(defun asave ()
  (vl-load-com)
  (setq ASRXN1 (vlr-editor-reactor nil '((:vlr-commandended . ASCOM1)))
     ASRXN2 (vlr-lisp-reactor nil   '((:vlr-lispWillStart . ASCOM2)))
     ASRXN3 (vlr-lisp-reactor nil   '((:vlr-lispended . ASCOM3)))
  )
  (princ)
 )
 (defun ASCOM1 (CALL CALLBACK)
  (if (not LISPRUN)
   (progn
    (cond
  ((= (car CALLBACK) "SAVE")
  (setq ASCOUNT 0)
  )
  ((= (car CALLBACK) "QSAVE")
  (setq ASCOUNT 0)
  )
    )
    (if (not ASCOUNT)
  (setq ASCOUNT 0)
  (if (> ASCOUNT 9)
  (progn
    (setq ASCOUNT 0)
    (vla-save (vla-get-activedocument (vlax-get-acad-object)))
  (vl-file-delete (strcat "C:\\ACADBACK\\"
                    (vla-get-name
                      (vla-get-activedocument (vlax-get-acad-object))
)
   )
  )
  (copy_backup_as)
  (prompt (strcat "\nFile Saved..." (vla-get-fullname (vla-get-activedocument (vlax-get-acad-object)))))
)
  (progn
    (setq ASCOUNT (1+ ASCOUNT))
    (print ASCOUNT)
  )
  )
    )
   )
  )
 )

 (defun ASCOM2 (CALL CALLBACK)
  (setq LISPRUN 'T)
 )
 (defun ASCOM3 (CALL CALLBACK)
  (setq LISPRUN nil)
 )
 (asave)
 

rhino

  • Guest
Re: save a dwg before opening another dwg
« Reply #10 on: November 01, 2011, 10:31:44 PM »
I use this vba proggie:
http://www.afralisp.net/visual-basic-for-applications/tutorials/a-better-autosave.php

This works by setting a counter that starts when a command is called - it then resets on the user set interval/time. Sometimes in a hurry you forget to save the drawing you've worked on...this way you'll always lose only the last 10mins of your work  :mrgreen:

The code needed to be modified though; I'll post the .dvb file later tonight.

Perhaps someone here can also expand it to include an error check; for say when you open a read only drawing or the location of the save is write protected (thick CD).

rhino

  • Guest
Re: save a dwg before opening another dwg
« Reply #11 on: November 02, 2011, 03:14:27 PM »
heres the modded vba code:
Code: [Select]
Private Sub AcadDocument_BeginCommand(ByVal CommandName As String)

Dim OldTime
Dim NewTime

Select Case CommandName

'trigger when user selects
Case "LINE", "PLINE", "DDEDIT", "DIMLINEAR", "COPY", "MOVE", "ZOOM"

'get the time now
NewTime = (Hour(Time) * 60) + Minute(Time)

'get the time stored in USERI2
OldTime = ThisDrawing.GetVariable("Useri2")

'If the time has not been set
If OldTime = 0 Then

'set the time
ThisDrawing.SetVariable ("Useri2"), NewTime

'then end
End

End If

'if the difference is greater that 15 minutes
If (NewTime - OldTime) > 5 Then

'save the drawing
ThisDrawing.Save

'and then reset Useri2
ThisDrawing.SetVariable ("Useri2"), NewTime

End If

'end of the Select
End Select

End

End Sub

Private Sub AcadDocument_BeginSave(ByVal FileName As String)

End Sub


also attached the compiled dvb