TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: andrew_nao on September 11, 2014, 11:02:30 AM

Title: save dwg as backup on open before its modified
Post by: andrew_nao on September 11, 2014, 11:02:30 AM
I have a few functions that save dwg before you close it and auto saves
however is it possible to save a dwg as soon as its opened? maybe with a reactor?

what im planning is a save function that will save a dwg to a backup directory as an original so that if something were to go wrong (IE: if entities were mistakenly deleted and dwg was saved  :whistling:) that the original dwg would still be intact.

thoughts?
Title: Re: save dwg as backup on open before its modified
Post by: Keith™ on September 11, 2014, 11:07:42 AM
Autocad automatically creates a backup of the drawing when you save, so you can always recover the last version .. asuming of course that you don't save the drawing every 5 minutes or something and overwrite the backup drawing.
Title: Re: save dwg as backup on open before its modified
Post by: ronjonp on September 11, 2014, 11:10:08 AM
...
however is it possible to save a dwg as soon as its opened? maybe with a reactor?
...
You don't need a reactor, just add something like so to your startup:
Code - Auto/Visual Lisp: [Select]
  1.   (strcat (getvar 'dwgprefix) (getvar 'dwgname))
  2.   (strcat "C:\\MYINITIALBACKUPPATH\\" (getvar 'dwgname))
  3. )

You'll have to make sure that the backup directory exists prior to copying though.
Title: Re: save dwg as backup on open before its modified
Post by: BlackBox on September 11, 2014, 11:35:29 AM
...
however is it possible to save a dwg as soon as its opened? maybe with a reactor?
...
You don't need a reactor, just add something like so to your startup:
Code - Auto/Visual Lisp: [Select]
  1.   (strcat (getvar 'dwgprefix) (getvar 'dwgname))
  2.   (strcat "C:\\MYINITIALBACKUPPATH\\" (getvar 'dwgname))
  3. )

You'll have to make sure that the backup directory exists prior to copying though.

You'll also have to check that the target file name does not already exist, and/or is successfully deleted:

More on vl-File-Copy: http://help.autodesk.com/view/ACD/2015/ENU/?guid=GUID-12910252-217C-427D-8874-1323611FA74C


... Unless you're going to name the target accordingly with date/time, etc.

Code - Auto/Visual Lisp: [Select]
  1. (strcat "C:\\MYINITIALBACKUPPATH\\"
  2.         (vl-filename-base dwgName)
  3.         "_"
  4.         (menucmd "M=$(edtime,$(getvar,date),YYYY-MO-DD)")
  5.         ".dwg"
  6. )
  7.  
Title: Re: save dwg as backup on open before its modified
Post by: Keith™ on September 11, 2014, 11:43:14 AM
... and you will need to verify that the drawing is not a new drawing that has never been saved to disk.
Title: Re: save dwg as backup on open before its modified
Post by: ronjonp on September 11, 2014, 11:50:01 AM
Both excellent suggestions. Perhaps this would work.  :)
RJP-XCOPY (http://www.theswamp.org/index.php?topic=39415.msg446782#msg446782)
XCOPY Switches (http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true)
Code - Auto/Visual Lisp: [Select]
  1. (if (= 1 (getvar 'dwgtitled))
  2.   (rjp-xcopy (getvar 'dwgprefix) (getvar 'dwgname) "C:\\MyInitialBackup\\" "/d/c/i/k/r/y/q")
  3. )
Title: Re: save dwg as backup on open before its modified
Post by: BlackBox on September 11, 2014, 12:09:40 PM
Both excellent suggestions. Perhaps this would work.  :)
RJP-XCOPY (http://www.theswamp.org/index.php?topic=39415.msg446782#msg446782)
XCOPY Switches (http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true)
Code - Auto/Visual Lisp: [Select]
  1. (if (= 1 (getvar 'dwgtitled))
  2.   (rjp-xcopy (getvar 'dwgprefix) (getvar 'dwgname) "C:\\MyInitialBackup\\" "/d/c/i/k/r/y/q")
  3. )

*Sigh*... What, no bit-coded switches? :roll:



 :-P  :-D
Title: Re: save dwg as backup on open before its modified
Post by: ronjonp on September 11, 2014, 12:21:47 PM
What is this "bitcode" you speak of ?  :whistling:
Title: Re: save dwg as backup on open before its modified
Post by: BlackBox on September 11, 2014, 12:24:30 PM
... and you will need to verify that the drawing is not a new drawing that has never been saved to disk.

Perhaps I've misunderstood the thread title, but the OP seems to be attempting to create a backup immediately after opening an existing drawing (not creating a new drawing).

My $0.02
Title: Re: save dwg as backup on open before its modified
Post by: ronjonp on September 11, 2014, 12:33:16 PM
We'll let him chime in .. perhaps we're both WAAAYYYY off the mark.  :)
Title: Re: save dwg as backup on open before its modified
Post by: dgorsman on September 11, 2014, 12:37:19 PM
Isn't all this moot?  This runs *after* the drawing is opened which can itself modify the content.  Might be more productive to create a backup on closing the drawing.  Or just use regular backups.
Title: Re: save dwg as backup on open before its modified
Post by: andrew_nao on September 11, 2014, 01:19:06 PM
... and you will need to verify that the drawing is not a new drawing that has never been saved to disk.

Perhaps I've misunderstood the thread title, but the OP seems to be attempting to create a backup immediately after opening an existing drawing (not creating a new drawing).

My $0.02

yes i want to save it, or copy it would probably be best, to another directory as an original file so i can then continue to modify that same dwg.
i only need 1 instance of the dwg to be saved though. if i copy the file, it shouldnt overwrite if the file exists, correct?
Title: Re: save dwg as backup on open before its modified
Post by: andrew_nao on September 11, 2014, 01:22:19 PM
Isn't all this moot?  This runs *after* the drawing is opened which can itself modify the content.  Might be more productive to create a backup on closing the drawing.  Or just use regular backups.

thats why i want to back it up, copy it, save it, to another directory so its not modified and will be labeled "original" while still opening the dwg to be modified.

just looking for ideas and thoughts and appreciate the feedback
Title: Re: save dwg as backup on open before its modified
Post by: Keith™ on September 11, 2014, 01:56:32 PM
... and you will need to verify that the drawing is not a new drawing that has never been saved to disk.

Perhaps I've misunderstood the thread title, but the OP seems to be attempting to create a backup immediately after opening an existing drawing (not creating a new drawing).

My $0.02

You are correct, however, the solution was proposed to be added to the startup lisp ... the same startup lisp that runs regardless of whether you are opening a drawing or starting a new blank drawing ...

this is why I get paid the big bucks ;-)
Title: Re: save dwg as backup on open before its modified
Post by: GDF on September 11, 2014, 02:45:09 PM
Code: [Select]

;|
Well copy the below code into your acaddoc.lsp and then get used to
typing "BAK" before you start any major (or minor for that matter)
changes to your drawing.  It will create a /BackUp directory, if not
already present, and a file with the date and time appended to the
drawing name: C:/MyProject/X_Site.dwg would be saved as
C:/MyProject/BackUp/X_Site-2006.03.30.18.32.39.dwg.
|;
;;;James Buzbee
;;;http://www.theswamp.org/index.php?topic=9453.0
(defun c:bak  (/ file dirs dir)
  (setq dirs(vl-directory-files(getvar "dwgprefix") nil -1))
  (if(not(or (member "backup" dirs)(member "archive" dirs)(member "archive" dirs)))
    (setq dir(vl-mkdir (strcat (getvar "dwgprefix")"archive")))
    (setq dir t)
    )
  (if dir
    (progn
  (setq file (strcat
       (getvar "dwgprefix")"archive\\"
       (vl-filename-base (getvar "dwgname")) "_"
       ;;(menucmd "M=$(edtime,$(getvar,date),YYYY.MO.DD.HH.MM.SS)")
               (menucmd "M=$(edtime,$(getvar,date),MO-DD-YYYY_HH.MM)")
       ".dwg"))
  (while (findfile file)
    (setq file (strcat
       (getvar "dwgprefix")"archive\\"
       (vl-filename-base (getvar "dwgname")) "_"
       ;;(menucmd "M=$(edtime,$(getvar,date),YYYY.MO.DD.HH.MM.SS)")
               (menucmd "M=$(edtime,$(getvar,date),MO-DD-YYYY_HH.MM)")
       ".dwg")))
  (if file
    (command "save" file))))
  (princ file)(princ))

Title: Re: save dwg as backup on open before its modified
Post by: ronjonp on September 11, 2014, 03:29:55 PM
I was not sure how Andrew was going to implement the solution just throwing ideas out there. My backup saves files in 15 minute increments while working. Has worked well for me.


Then there was THIS (http://www.cadtutor.net/forum/archive/index.php/t-40621.html) lisp I wrote way back when that created a folder called 'SUPERSCEDE' and copied the current drawing and xrefs into it.


Have fun !
Title: Re: save dwg as backup on open before its modified
Post by: BlackBox on September 11, 2014, 03:48:28 PM
... and you will need to verify that the drawing is not a new drawing that has never been saved to disk.

Perhaps I've misunderstood the thread title, but the OP seems to be attempting to create a backup immediately after opening an existing drawing (not creating a new drawing).

My $0.02

You are correct, however, the solution was proposed to be added to the startup lisp ... the same startup lisp that runs regardless of whether you are opening a drawing or starting a new blank drawing ...

this is why I get paid the big bucks ;-)

Big bucks you may be paid, but where I previously state that need not be considered, you cannot cite. :angel:

Title: Re: save dwg as backup on open before its modified
Post by: Keith™ on September 11, 2014, 03:52:04 PM
... and you will need to verify that the drawing is not a new drawing that has never been saved to disk.

Perhaps I've misunderstood the thread title, but the OP seems to be attempting to create a backup immediately after opening an existing drawing (not creating a new drawing).

My $0.02

You are correct, however, the solution was proposed to be added to the startup lisp ... the same startup lisp that runs regardless of whether you are opening a drawing or starting a new blank drawing ...

this is why I get paid the big bucks ;-)

Big bucks you may be paid, but where I previously state that need not be considered, you cannot cite. :angel:



WUT?

Who said anything about you? I was referring to the very first solution where it plainly says to add it to the startup. If you misunderstood, I cannot help that.
Title: Re: save dwg as backup on open before its modified
Post by: Matt__W on September 11, 2014, 03:56:45 PM
Why not simply use Ron's code (http://www.theswamp.org/index.php?topic=17552.msg319760#msg319760) to make a copy each time the DWG is saved??  It's essentially the same as making a copy of it before it's modified, isn't it??  I mean, the file that is copied (whether it's after a save or before an open) is the same thing.
Title: Re: save dwg as backup on open before its modified
Post by: BlackBox on September 11, 2014, 04:02:36 PM
... and you will need to verify that the drawing is not a new drawing that has never been saved to disk.

Perhaps I've misunderstood the thread title, but the OP seems to be attempting to create a backup immediately after opening an existing drawing (not creating a new drawing).

My $0.02

You are correct, however, the solution was proposed to be added to the startup lisp ... the same startup lisp that runs regardless of whether you are opening a drawing or starting a new blank drawing ...

this is why I get paid the big bucks ;-)

Big bucks you may be paid, but where I previously state that need not be considered, you cannot cite. :angel:



WUT?

Who said anything about you? I was referring to the very first solution where it plainly says to add it to the startup. If you misunderstood, I cannot help that.

You did. :wink:
Title: Re: save dwg as backup on open before its modified
Post by: alanjt on September 11, 2014, 05:57:06 PM
Here's a simple one I keep in my startup. If the file already exists (for the current day), it will not be copied over again, which is what I wanted.
I should change it to use a copy method other than vl-file-copy, just so I the original file edit date won't be changed, but it's never been a big deal for what this has been there for.

Code: [Select]
((lambda (/ path)
   (if (not (wcmatch (strcase (getvar 'DWGNAME)) "DRAWING*"))
     (progn
       (vl-mkdir (setq path "c:\\dwgBackup"))
       (vl-mkdir (setq path (strcat path "\\" (menucmd "M=$(edtime,$(getvar,date),YYYY.M.DD)") "\\")))
       (vl-file-copy (strcat (getvar 'DWGPREFIX) (getvar 'DWGNAME)) (strcat path (getvar 'DWGNAME)))
     )
   )
 )
)
Title: Re: save dwg as backup on open before its modified
Post by: BlackBox on September 11, 2014, 06:49:59 PM
I do something very similar in my VOID routine, which I used to make a dated copy of a drawing prior to making major design changes. I also have a routine which allows me to quickly 'step' through one or more previously created void copies (which are relative pathed).

Also, always good to see you in the forums AT. :beer:
Title: Re: save dwg as backup on open before its modified
Post by: Keith™ on September 12, 2014, 12:03:55 PM
... and you will need to verify that the drawing is not a new drawing that has never been saved to disk.

Perhaps I've misunderstood the thread title, but the OP seems to be attempting to create a backup immediately after opening an existing drawing (not creating a new drawing).

My $0.02

You are correct, however, the solution was proposed to be added to the startup lisp ... the same startup lisp that runs regardless of whether you are opening a drawing or starting a new blank drawing ...

this is why I get paid the big bucks ;-)

Big bucks you may be paid, but where I previously state that need not be considered, you cannot cite. :angel:



WUT?

Who said anything about you? I was referring to the very first solution where it plainly says to add it to the startup. If you misunderstood, I cannot help that.

You did. :wink:

WUT? X2
Title: Re: save dwg as backup on open before its modified
Post by: Lee Mac on September 13, 2014, 09:27:35 AM
Code: [Select]
(if (not (wcmatch (strcase (getvar 'DWGNAME)) "DRAWING*"))

FWIW, the above could be:
Code: [Select]
(if (= 1 (getvar 'dwgtitled))
To account for the possibility that a saved drawing starts with 'DRAWING...'
Title: Re: save dwg as backup on open before its modified
Post by: ChrisCarlson on September 15, 2014, 08:29:14 AM
Edited

Title: Re: save dwg as backup on open before its modified
Post by: alanjt on September 15, 2014, 08:38:25 AM
Code: [Select]
(if (not (wcmatch (strcase (getvar 'DWGNAME)) "DRAWING*"))

FWIW, the above could be:
Code: [Select]
(if (= 1 (getvar 'dwgtitled))
To account for the possibility that a saved drawing starts with 'DRAWING...'
I always forget about that variable. Thanks.
Granted, I seriously hope people aren't naming files starting with "Drawing".
Title: Re: save dwg as backup on open before its modified
Post by: andrew_nao on September 15, 2014, 09:45:17 AM
wow some good ideas in here.
the thought i was having was just to have a backup of a dwg like alajt's suggestion.
just 1 copy no overwrite, no delete and recopy. just 1 copy as "original".
that is very simple code too, i thought it would be a bit more complicated to implement.

appreciate everyones feedback
Title: Re: save dwg as backup on open before its modified
Post by: ChrisCarlson on September 15, 2014, 01:18:38 PM
Corrected the batch file

Code: [Select]
@ECHO OFF
SET _params=%*
CALL :sub "%_params%" 
:sub
ECHO %~nx1
set folder=C:\AutoCAD_Originals
set odate=%date:~4,2%_%date:~7,2%_%date:~10,4%
copy %~nx1 "%FOLDER%\%~n1-%ODATE%.dwg"
start %~nx1
exit

Code: [Select]
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\Drawing Backup]
"Position"="Top"
"Extended"=""
[HKEY_CLASSES_ROOT\*\shell\Drawing Backup\command]
@="cmd.exe /T:17 /K C:\\AutoCAD_Originals "%1""

Simply right click and open the file you want with Drawing Backup.

Does time stamps matter? I know going through lisp changes the last edit time stamp within the file.