Author Topic: Saving at regular intervals  (Read 10729 times)

0 Members and 1 Guest are viewing this topic.

jonesy

  • SuperMod
  • Seagull
  • Posts: 15502
Saving at regular intervals
« Reply #15 on: April 15, 2005, 09:42:15 AM »
Quote from: ronjonp
Jeff's save by number of commands works great.

Thanks, I will look at that, but as I am learning lisp here at the swamp, I would like to try to have a go myself too. It will probably be a real mess and I will probably end up using that one :!:
Thanks for explaining the word "many" to me, it means a lot.

daron

  • Guest
Saving at regular intervals
« Reply #16 on: April 15, 2005, 11:16:02 AM »
Jonesy, post what you have as you build it and we can guide you to completion, then you'll only want to use yours, because you made it.

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Saving at regular intervals
« Reply #17 on: April 18, 2005, 03:09:04 PM »
Jeff's function works great!  I made 2 small changes, 1) I reduced the number of commands to 15, and 2) I reset command_count to 0 if the user does a save manually.  That way it doesn't have the potential to save right after a save.
Code: [Select]

Option Explicit
Dim command_count As Long

Private Sub AcadDocument_EndCommand(ByVal CommandName As String)

'***Below code is to AUTO save the drawing every 15 commands,
  If ThisDrawing.GetVariable("DWGTITLED") = 1 Then 'don't use on new, unsaved drawings
  Select Case CommandName
    Case UCase("undo"), UCase("u"), UCase("zoom"), UCase("z"), UCase("pan")
        command_count = command_count
    Case UCase("QSAVE"), UCase("SAVE")
        command_count = 0
    Case Else
        command_count = command_count + 1
        If command_count = 15 Then 'change this to any number you want
            ThisDrawing.Save
            command_count = 0
        End If
    End Select
  End If
End Sub
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

ronjonp

  • Needs a day job
  • Posts: 7025
Re: Saving at regular intervals
« Reply #18 on: September 07, 2005, 12:09:46 PM »
I added And ThisDrawing.ReadOnly = False so if you are refediting something and it trys to save the routine won't crash.

Option Explicit
Dim command_count As Long



Private Sub AcadDocument_EndCommand(ByVal CommandName As String)

'***Below code is to AUTO save the drawing every 25 commands,
If ThisDrawing.GetVariable("DWGTITLED") = 1 And ThisDrawing.ReadOnly = False Then 'don't use on new, unsaved drawings or readonly
  Select Case CommandName
    Case UCase("UNDO"), UCase("U"), UCase("ZOOM"), UCase("Z"), UCase("PAN")
        command_count = command_count
    Case UCase("QSAVE"), UCase("SAVE")
        command_count = 0
    Case Else
        command_count = command_count + 1
        If command_count = 25 Then 'change this to any number you want
              ThisDrawing.Save
            command_count = 0
        End If
    End Select
  End If
End Sub

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Saving at regular intervals
« Reply #19 on: September 07, 2005, 12:41:09 PM »
I'll throw this into the mix too.
Designed to be run at timed intevals or by user, or from/partof another routine

Simply saves current and makes a backup to an Archive folder with the Date and time suffixed to the file name.


Code: [Select]
(defun copy_backup_as (/ *error* activedoc docfullname backuppath archivename)
  ;; IAcadDocument Object
  (setq activedoc (vla-get-activedocument (vlax-get-acad-object)))
  ;;
  ;;------ Set Error Trap --------------------------
  ;;
  (defun *error* (msg /) (kb:on-error msg))
  (vla-endundomark activedoc)                     ; end any open undo group
  (vla-startundomark activedoc)                   ; start new group
  ;;
  ;;----- Main --------------------------
  ;;
  (vla-save activedoc)
  (if (and (not (= 0 (strlen (setq docfullname (vla-get-fullname activedoc)))))
           (setq backuppath (dos_mkdir (strcat (vl-filename-directory docfullname)
                                               "\\DRAWINGArchive"
                                       )
                            )
           )
           (setq archivename (strcat backuppath
                                     (vl-filename-base (vla-get-name activedoc))
                                     "-"
                                     (rtos (* (getvar "cdate") 1000000) 2 0)
                                     ".dwg"
                             )
           )
      )
    (vl-file-copy docfullname archivename)
    (alert "Ooooops. Unable to establish ArchiveName.")
  )
  (prompt (strcat "\n Archived file : " archivename))
  (*error* nil)
  (princ)
)
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Bob Wahr

  • Guest
Re: Saving at regular intervals
« Reply #20 on: September 07, 2005, 01:05:42 PM »
Quote
Does autosave do a full save to dwg format?
I was always under the impression that autosave was an emergency only option, and should only be re-named and used in the direst of circumstances.
Yes it does.  The only difference between a dwg and an sv$ is the extension.  There is nothing unsafe or scary about using an autosave.  That being said, if you still want a time interval based save, you can set your savetime to the time between saves and use this.

Code: [Select]
Private Sub AcadDocument_EndSave(ByVal FileName As String)
If Right(FileName, 3) = "sv$" Then
  ThisDrawing.Save
End If
End Sub

Bob Wahr

  • Guest
Re: Saving at regular intervals
« Reply #21 on: September 07, 2005, 01:10:16 PM »
Actually, ronjonp has a good point on the readonly so
Code: [Select]
Private Sub AcadDocument_EndSave(ByVal FileName As String)
If Right(FileName, 3) = "sv$" Then
  If ThisDrawing.ReadOnly = False Then
    ThisDrawing.Save
  End If
End If
End Sub

ronjonp

  • Needs a day job
  • Posts: 7025
Re: Saving at regular intervals
« Reply #22 on: September 07, 2005, 03:48:57 PM »
Kerry,

Here is my/frankenstein version of backing up drawings on save.

Code: [Select]
;_____________________________________________________________________________
;;MAKES COPY OF DRAWING IN C:\AutoCAD-BAK
;_____________________________________________________________________________


(defun copydwg (/ destdir dwgdir *date* hour)
(if (not (wcmatch (getvar 'DWGNAME) "Drawing*.dwg"))
    (progn

   (defun *date* (/ curdate curtime)
     (setq   curdate (rtos (fix (getvar 'cdate)) 2 0)
      curtime (strcat (substr curdate 5 2) "-" (substr curdate 7 2) "-" (substr curdate 1 4)))
   )

(defun hour (/ ctime ampm stime)
  (setq ctime (rtos (rem (getvar 'cdate) 1) 2 6)
       ampm  (if (<= (atof (substr ctime 3 2)) 12) "AM" "PM")
       stime (strcat (substr ctime 3 2)))
)

               ;Makes C:\AutoCAD-BAK\currentdate\hour if it doesn't already exist

   (if (not
       (vl-file-directory-p (strcat "C:/AutoCAD-BAK/")))
       (vl-mkdir (strcat "C:/AutoCAD-BAK/"))
   )

   (if (not
       (vl-file-directory-p (strcat "C:/AutoCAD-BAK/" (*date*))))
       (vl-mkdir (strcat "C:/AutoCAD-BAK/" (*date*)))
   )


   (if (not
       (vl-file-directory-p (strcat "C:/AutoCAD-BAK/" (*date*) "/" (hour))))
       (vl-mkdir (strcat "C:/AutoCAD-BAK/" (*date*) "/" (hour)))
   )

               ;For some reason the files get huge if not deleted then saved on top of

   (if (findfile (strcat "C:/AutoCAD-BAK/" (*date*) "/" (hour) "/" (getvar 'dwgname)))
     (vl-file-delete (strcat "C:/AutoCAD-BAK/" (*date*) "/" (hour) "/" (getvar 'dwgname)))
   )

   (setq destdir (strcat "C:/AutoCAD-BAK/" (*date*) "/" (hour) "/" (getvar 'dwgname))
         dwgdir  (strcat (getvar 'dwgprefix) (getvar 'dwgname))
   )
   (vl-file-copy dwgdir destdir T)
      )
    )
  )
(copydwg)


;_____________________________________________________________________________
;;COMMAND REACTORS
;_____________________________________________________________________________


(vlr-command-reactor nil '((:vlr-commandEnded . endCommand)))


(defun endCommand (calling-reactor endcommandInfo / thecommandend)
(setq thecommandend (nth 0 endcommandInfo))
(cond
  ((= thecommandend "QSAVE") (copydwg))
  ((= thecommandend "SAVE") (copydwg))
)
)

EDIT: Fixed code tags/format
« Last Edit: April 06, 2006, 09:07:36 PM by nivuahc »

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

johnp

  • Guest
Re: Saving at regular intervals
« Reply #23 on: April 04, 2007, 10:06:28 AM »
Here is a lisp to autosave your drawings, this does an actual save.

Be aware it will save over your drawings so if you don't want to save changes you need to undo everything prior to closing a drawing.

I stopped using it after I overwrote an architects drawings and had to beg him for another copy.

Code: [Select]
; =========================================================
;   AUTOSAVE.LSP             By:   Jeff Pilcher
;
;   This is an AutoLISP routine for use with the
;   AutoCAD drafting software.
;
;   I am providing this routine to other users of
;   AutoCAD under the SHAREWARE concept. I would
;   like to recover some of the cost of my time
;   spent developing this and other AutoLISP
;   routines for AutoCAD, by asking for voluntary
;   payments.
;
;   If you find this routine is useful to you in
;   doing paying work, please send $5 to:
;
;              Jeffrey S. Pilcher
;
;              Production Systems
;              P.O. Box 218
;              Richmond, IL  60071
;
;              (815) 678-4562
;
;   Obviously $5 is not going to make me rich, or break
;   your bank account.  It will however, show me that
;   someone out there appreciates my efforts, and give
;   me the incentive to write more useful routines.
;
;   I would be very interested in seeing any routines
;   written by you or anyone else. In other words, an
;   exchange of routines would make me as happy as a
;   $5 payment, (well, almost as happy).
;
;   Feel free to pass this routine on to other AutoCAD
;   users, but PLEASE, do not remove these comments.
;
;   Good luck, have fun, and LONG LIVE AutoDESK !!!
;
; =========================================================
;
;
;   This is a routine to automatically save your drawing at
;   regular intervals during the editing session.
;
;   There are a lot of autosave routines out there, but I think
;   this is the most straight forward of the bunch.
;
;   AUTOSAVE.LSP main features:
;
;          1) Can be turned on or off as you see fit.
;
;          2) You specify how often you want to AUTOSAVE.
;
;          3) When it is not time to AUTOSAVE, it tells
;             you how much time is left so you know exactly
;             what is happenning.
;
;          4) When it is AUTOSAVING, it tells you the name
;             of the drawing being AUTOSAVED.
;
;   INSTALLATION:
;
;     To utilize this routine you should include the code portion
;     in your ACAD.LSP file.
;
;     After you have installed AUTOSAVE.LSP in your ACAD.LSP file
;     you will need to include the command "(c:setauto)" as the
;     last statement in your ACAD.LSP file.  This will cause
;     AutoCAD to prompt the user with the message:
;
;         Do you want to do AUTOSAVES ?
;
;     upon entering the drawing editor.
;
;     Then include the command AUTOSAVE  under many of the menu
;     picks in your tablet menu.
;
;
;   OPERATION:
;
;     When you enter the drawing editor, and as the ACAD.LSP
;     file is being loaded, the operator will be asked if he/she
;     would like to do AUTOSAVES:
;
;          Do you want to do AUTOSAVES ?
;
;     A response of [ y ] or [ Y ] will turn AUTOSAVE on, AutoCAD
;     will tell the operator that AUTOSAVE is on with the prompt:
;
;          AUTOSAVE is on.
;
;     Any other response will turn AUTOSAVE off, AutoCAD will
;     tell the operator that AUTOSAVE is off with the prompt:
;
;          AUTOSAVE is off.
;
;     If the operator has chosen to do AUTOSAVES by responding
;     [ y ] or [ Y ] he/she will be asked how often to AUTOSAVE
;     with the prompt:
;
;          How often would you like to AUTOSAVE, in minutes <15>:
;
;     The operator should at this time enter a number indicating
;     how often they would like AutoCAD to AUTOSAVE thier drawing.
;     The default is 15 minutes. If 15 minutes is acceptable,
;     simply hit <ENTER>.
;
;     Upon picking one of the tablet picks which contains the
;     AUTOSAVE command, AutoCAD will check to see how long it has
;     been since it last AUTOSAVED your drawing.  If the time
;     since the last AUTOSAVE has exceeded the desired interval,
;     AutoCAD will automatically save your drawing for you, and
;     tell you what is happenning with the prompt:
;
;          AUTOSAVING:  <dwgname>
;          Please stand by ....
;
;     (never again will you loose 4 hours of work due to a systems
;     crash or power failure).
;
;     If the time since the last AUTOSAVE has not exceeded the
;     desired interval, AutoCAD will tell you with the prompt:
;
;          Not time yet .... xx.x minutes left.
;
;     As you can see, it will tell how much time is left until the
;     next AUTOSAVE.
;
;
;   EXTRA FEATURES:
;
;     The AUTOSAVE routine can be turned on or off at any time you
;     desire while in the drawing editor by typing [ SETAUTO ], at
;     the command prompt, and answering the prompt:
;
;          Do you want to do AUTOSAVES ?
;
;     with the proper response. This prompt is the same prompt you
;     get when you first enter the drawing editor. Therefore you
;     will also be asked to enter a time interval for the AUTOSAVE
;     program. So not only can you turn AUTOSAVVES on or off, but,
;     you can also change the time interval whenever you like.
;
;

(defun setauto ()
  (setq answer (strcase (getstring "\nDo you want to do AUTOSAVES ?  ")))
  (if (> (strlen answer) 1)
    (setq answer (substr answer 1 1))
  )
  (if (= answer "Y")
    (progn
      (prompt "\nAUTOSAVE is on.")
      (print)
      (setq minutes (getreal "\nHow often would you like to AUTOSAVE, in minutes <15>: "))
      (if (= minutes nil)
        (setq minutes 15.0)
      )
    )
    (prompt "\nAUTOSAVE is off.")
  )
  (print)
)
(defun C:autosave ()
  (if (= answer "Y")
    (saveit)
    (prompt "\nAUTOSAVE is turned off !")
  )
  (print)
)
(defun saveit ()
  (if (= D1 nil)
    (setq D1 (getvar "DATE"))
  )
  (if (> (* (- (getvar "DATE") D1) 1440.0) minutes)
    (progn
      (setvar "cmdecho" 0)
      (prompt (strcat "\nAUTOSAVING:  " (getvar "dwgname")))
      (prompt "\nPlease stand by .... ")
      (print)
      (command "SAVE" "")
      (prompt "\nAUTOSAVE complete.")
      (prompt "\nThank you for waiting.")
      (setq D1 (getvar "DATE"))
      (setvar "cmdecho" 1)
    )
    (progn
      (prompt "\nNot time yet .... ")
      (princ (rtos (- minutes (* (- (getvar "DATE") D1) 1440.0)) 2 1))
      (princ " minutes left.")
    )
  )
  (print)
)
;
(setauto)

i know this is an old post but just wanted to say that when i try to run this i get the following message

Command: AUTOSAVE
; error: bad argument type: numberp: "1.5000"

but yet there isnt a 1.5000 anywhere in the code
i think im lost

joseguia

  • Guest
Re: Saving at regular intervals
« Reply #24 on: April 04, 2007, 11:28:11 AM »
Jeff_M, ...

thanks for the code, .. I had been meaning to do something like this for quite some time. I modified it to dump an autosave to a different drive on our network for added redundancy:

Code: [Select]
Private Sub AcadDocument_EndCommand(ByVal CommandName As String)

On Error GoTo Err_Trapper
'***Below code is to AUTO save the drawing every 20 commands,
   
    Select Case CommandName
      Case UCase("UNDO"), UCase("U"), UCase("ZOOM"), UCase("Z"), UCase("PAN")
          command_count = command_count
      Case UCase("QSAVE"), UCase("SAVE")
          command_count = 0
      Case Else
          command_count = command_count + 1
          If command_count > 20 Then 'change this to any number you want
         
              Dim strFN As String
                strFN = "H:\backup\AutoSaves\" & ThisDrawing.GetVariable("LOGINNAME") & "-" & ThisDrawing.Name
                ThisDrawing.SaveAs strFN
             
              command_count = 0
             
          End If
    End Select

Exit Sub
 
Err_Trapper:
  Err.Clear
  Exit Sub

End Sub




Hmm, .. still needs some tweaking ...
« Last Edit: April 04, 2007, 12:03:03 PM by joseguia »

joseguia

  • Guest
Re: Saving at regular intervals
« Reply #25 on: April 04, 2007, 12:29:57 PM »
Darn doesn't work 'cause it switches you over to working in the backup directory .
DOH


joseguia

  • Guest
Re: Saving at regular intervals
« Reply #26 on: April 04, 2007, 01:21:18 PM »
Same as the other - place this inside of the ThisDrawing module:

it exports the current drawing as a DXF to a folder you specify in the code. It doesn't check if the folder exists so be sure it exists beforehand.



Quote
Option Explicit
Dim command_count As Long

Private Sub AcadDocument_EndCommand(ByVal CommandName As String)

On Error GoTo Err_Trapper
'***Below code is to AUTO save the drawing every 20 commands,
   
    Select Case CommandName
      Case UCase("UNDO"), UCase("U"), UCase("ZOOM"), UCase("Z"), UCase("PAN")
          command_count = command_count
      Case UCase("QSAVE"), UCase("SAVE")
          command_count = 0
      Case Else
          command_count = command_count + 1
          If command_count > 20 Then 'change this to any number you want
         
         
              Dim strFN As String
              Dim sset As AcadSelectionSet
              Set sset = vbdPowerSet("testSS")

                strFN = "H:\backup\AutoSaves\" & ThisDrawing.GetVariable("LOGINNAME") & "-" & ThisDrawing.Name
                ThisDrawing.Export strFN, "DXF", sset
             
              command_count = 0
             
          End If
    End Select

Exit Sub
 
Err_Trapper:
  Err.Clear
  Exit Sub

End Sub
« Last Edit: April 04, 2007, 01:23:30 PM by joseguia »

joseguia

  • Guest
Re: Saving at regular intervals
« Reply #27 on: April 04, 2007, 01:22:46 PM »
oh yeah I use vbdesign.net's vbdPowerSet ,,. .

  '''Use this to create a selection set that way it will never be doubled

Quote
Public Function vbdPowerSet(strName As String) As AcadSelectionSet
  '''Use this to create a selection set that way it will never be doubled
  Dim objSelSet As AcadSelectionSet
  Dim objSelCol As AcadSelectionSets
  Set objSelCol = ThisDrawing.SelectionSets
    For Each objSelSet In objSelCol
      If objSelSet.Name = strName Then
        objSelCol.Item(strName).Delete
        Exit For
      End If
    Next
  Set objSelSet = objSelCol.Add(strName)
  Set vbdPowerSet = objSelSet
End Function

sinc

  • Guest
Re: Saving at regular intervals
« Reply #28 on: April 09, 2007, 06:51:06 PM »
Quote
Does autosave do a full save to dwg format?
I was always under the impression that autosave was an emergency only option, and should only be re-named and used in the direst of circumstances.
Yes it does.  The only difference between a dwg and an sv$ is the extension.  There is nothing unsafe or scary about using an autosave.

Indeed.  The way I understand it, ISAVEPERCENT dates back to a day when disk drives were considerably slower than they are now.  Basically, when you would open a file that you had previously saved, Autocad would only write your new modifications to the end of the file.  So when you ERASE an entity, Autocad would not actually remove the reference to the entity from the DWG file, it would just append a note to the end of the file saying that you had erased the entity.  Then, the next time you open the drawing, it would parse the file consecutively, creating a reference to the entity, and then erasing it.

Obviously, this creates "wasted space" in the DWG file.  Autocad can estimate the amount of "wasted space".  When this value gets above ISAVEPERCENT, Autocad will save a completely new version of the DWG file, which completely omits references to the erased entities.  This completely cuts deleted objects out of the DWG file, but takes longer than a "quick-save", which can simply append some data to the end of an existing file.

These days, computers tend to be fast enough that there's usually little reason to keep ISAVEPERCENT at anything other than 0, unless you work with very large files.

But the part about not trusting Autocad's default Autosave files is right on the money.  Autocad tries to "clean up" Autosave files, and it may delete the Autosave before you get a chance to use it.  Similarly, something about saving a RECOVER file seems to delete the Autosave files, so if you attempt to do a RECOVER, you will likely lose your Autosave file unless you grab it BEFORE you tell Autocad to try saving a RECOVER file.

With Civil-3D, at least as of the 2007 edition, Autosave is basically useless, because attempting to save a drawing while parcels are being edited causes problems.  Unfortunately, Jeff's routine (and the others like it) also suffer the problem.  Turning Autosave completely off is the only way to avoid the issue, at least without getting into more-complicated code that checks the integrity of parcels before doing the save.  Hopefully Autodesk fixes this problem soon (hopefully it's already fixed in 2008...?).  Even though the Autosaves are very untrustworthy, they are sometimes helpful.

GDF

  • Water Moccasin
  • Posts: 1990
Re: Saving at regular intervals
« Reply #29 on: April 10, 2007, 02:00:48 PM »
I am looking for advice.

I am hoping to be able to write a lisp which performs a qsave every 5 or 10 minutes. Can anyone give me any ideas on where to start, and if there is a function in lisp that calculates time.

Be prepared for "dumb" questions, as I am trying to write my first proper lisp command. :?

Many thanks an advance


Here is what I use:

;;;James Allen, EIT
;;;Malicoat-Winslow Engineers, P.C.
;;;Columbia, MO

;;;http://discussion.autodesk.com/thread.jspa?messageID=4295603

;;; If AutoCAD's autosave feature is turned on, this code maintains a
;;; copy of the .sv$ file.
;;; It will copy the file whenever (1) a command is issued and (2) the time
;;; increment specified by the savetime system variable has passed
;;; (since the file was opened or the last copy, whichever is shorter).
;;;
;;; This entire code should be loaded from a single file for each document opened.
;;; For example, typically acaddoc.lsp would load a file containing this code.

Gary









Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2019x64 Windows 10x64