Script to save a file in two locations?

Script to save a file in two locations?

JOSHHALVERSON
Contributor Contributor
3,782 Views
8 Replies
Message 1 of 9

Script to save a file in two locations?

JOSHHALVERSON
Contributor
Contributor

Hello,

 

I want to create a custom Save button that will save an open .dwg file to two different locations simultaneously.

 

The scenario would be that I'm making a change to an existing .dwg file that is located on our corporate server.  A project manager out at the job site accesses a copy of this file through a shared Dropbox account, or some similar cloud service.  When I make the updates and hit this new Save button, the file saves to it's native location and also saves (or creates a copy) to this cloud folder.  This way the project manager always has access to the most recently saved version of the file and I don't need to remember to manually send them a copy every time I hit Save.

 

I'm familiar with creating/modifying buttons and very basic commands in the CUI, but have no idea where to start with the coding to achieve this solution.  One part of the button command would be to save (overwrite) to the original file location (which varies).  The other part of the command would be to save the file (or overwrite existing) to the cloud based folder.  That folder would always be the same destination, and would be connected to my desktop through a connector app, so the file path should be able to remain consistent.

 

Can anyone help me with this?  I just need to know how that code would be written and how to apply it to a custom Save button that I've created.

 

Much appreciated!

0 Likes
Accepted solutions (1)
3,783 Views
8 Replies
Replies (8)
Message 2 of 9

Kent1Cooper
Consultant
Consultant
Accepted solution

Here's what I use for a very similar purpose.  Both commands QSAVE the drawing [in its native location, which varies as in your description, typically on our Server] and also SAVE it to [in my case] my local computer.  So in addition to being a secondary backup copy, in case anything goes wrong with the Server or my connection to it, I always have the latest version of any drawing I've worked on [assuming I have used these commands with it] on my computer, so I can work in it until the Server connection is restored.  I hope you are able to substitute a path of some sort for your cloud folder.

(defun C:SS (/ cmde); = Save twice [and stay in drawing]
  (setq cmde (getvar 'cmdecho))
  (setvar 'cmdecho 0)
  (command "_.qsave" "_.save" (strcat "C:/My/Local/Backup/Filepath/" (getvar "DWGNAME")) "Yes")
  (setvar 'cmdecho cmde)
  (prompt "\nDrawing Saved in original file and to local-computer backup.")
  (princ)
)

(defun C:SSC (); = Save twice and Close, incl. set Layer to 0, Zoom to limits
  (command
    "_.layer" "_thaw" "0" "_set" "0" ""
    "_.zoom" (getvar 'limmin) (getvar 'limmax)
    "_.qsave" "_.save" (strcat "C:/My/Local/Backup/Filepath/" (getvar "DWGNAME")) "Yes"
    "_.close"
  )
)

One thing to be aware of:  they have the "Yes" answer to the question of whether to replace the file built in.  On first use  only in a particular drawing, that file won't be there yet, so it won't ask that question, which means "Yes" will go in at the command prompt, and an unknown-command message will go by.  It seemed not worth the complexity of trying to avoid that, and it doesn't bother the operation.

Kent Cooper, AIA
Message 3 of 9

JOSHHALVERSON
Contributor
Contributor

Your description sounds right along the lines of what I'm looking to accomplish.  I am able to get the file path of my desired backup folder, but I have a few questions if you'll bear with me.  Here's what I've done so far...

 

I copied your code text in full and pasted it into Notepad.  I changed both your instances of "C:/My/Local/Backup/Filepath/" to "C:\Users\jhalverson\Dropbox" which is the secondary location I want the file saved to.  I saved this as "save.scr" and placed it in a folder.  I went into Options and under the Files tab added the "save.scr" folder location to the Support File Search Path list.  Then in the CUI in AutoCAD I navigate to my custom save button.  In the Macro field I have the text "^C^C_script "save".  When I go back into the main screen and click on my button I get the following text in the command line...

 

Command:
Command:
Command: _script
Enter script file name <C:\Users\jhalverson\Desktop\Drawing3.scr>: "save"
Command: (defun C:SS (/ cmde); = Save twice [and stay in drawing]
(_> (setq cmde (getvar 'cmdecho))
(_> (setvar 'cmdecho 0)
(_> (command "_.qsave" "_.save" (strcat "C:\Users\jhalverson\Dropbox" (getvar "DWGNAME")) "Yes")
(_> (setvar 'cmdecho cmde)
(_> (prompt "\nDrawing Saved in original file and to local-computer backup.")
(_> (princ)
(_> )
C:SS
Command:
SCRIPT
Enter script file name <C:\Users\jhalverson\Desktop\Drawing3.scr>: (defun C:SSC (); = Save twice and Close, incl. set Layer to 0, Zoom to limits
(_> (command
((_> "_.layer" "_thaw" "0" "_set" "0" ""
((_> "_.zoom" (getvar 'limmin) (getvar 'limmax)
((_> "_.qsave" "_.save" (strcat "C:\Users\jhalverson\Dropbox" (getvar "DWGNAME")) "Yes"
((_> "_.close"
((_> )

 

I'm obviously doing something wrong somewhere, but I'm VERY unfamiliar with how to implement this code.  Can you tell what I'm doing wrong?

0 Likes
Message 4 of 9

Kent1Cooper
Consultant
Consultant

The code I posted is not a Script, but an AutoLisp definition of two command names.  Assuming you have full AutoCAD [with or without overlay software such as Civil3D or something], not LT  that can't use AutoLisp, you should save the code to a file with a .lsp filetype ending.  You can use APPLOAD to load it, and then those two commands will be defined.  You can include the code in an acaddoc.lsp file [add to it if you have such a file already, or start such a file with it], and they will be defined in every drawing you open, automatically.

 

Once they're loaded, you can call either command in a Tool Palette button, or just type in the command names [that's what I do -- they're short left-hand-only names, so it's as easy as going to any menu item].

 

NOTE:  In AutoLisp file-related functions, you must make file paths with either forward  slashes [as in my code] or double  backslashes, not  single backslashes [which are triggers for special characters].

 

If you use LT and can't use AutoLisp, turning them into Script or command-macro format could be a problem since they use an AutoLisp (strcat) function to put the file path and drawing name together.  There may be a way to do that in Diesel language, but I'm not well versed in that.

Kent Cooper, AIA
0 Likes
Message 5 of 9

JOSHHALVERSON
Contributor
Contributor

Oh my gosh, it works!  Thank you so much!  Go get yourself a beer, and tell the bartender to put it on Josh's tab.  Someone named Josh probably has one open.

 

🍻 🍺 🍻 🍺

0 Likes
Message 6 of 9

asyrafQ3ZXS
Participant
Participant
Hi, can you share the lisp file itself because I did copy and paste your code and save it as .lsp and load into the drawing but when I use QSAVE command, it just save the drawing and not make a duplicate file at the desktop (I changed the blue path in your coding to the desktop).
or am I doing it wrongly.
0 Likes
Message 7 of 9

Kent1Cooper
Consultant
Consultant

@asyrafQ3ZXS wrote:
.... when I use QSAVE command, it just save the drawing and not make a duplicate file ....
or am I doing it wrongly.

Yes, you're doing it wrongly.  It does not change the operation of the QSAVE command to make a double save.  It defines two custom commands to do it.  The command name is SS if you only want to Save in two places and continue working, or SSC if you want to Save in two places and Close the drawing.  [The command name is always the part immediately following (defun C: in these kinds of files.]

Kent Cooper, AIA
Message 8 of 9

asyrafQ3ZXS
Participant
Participant
ah thank you for the code! it works! and I will use it in the other lisp code as initial code so if anything happen to the file (like crash), it will save the drawing and I have the backup.
0 Likes
Message 9 of 9

Sea-Haven
Mentor
Mentor

It is possible to capture the standard save & close commands using a reactor and do something different in this case save the file twice. I got help for this, not sure about Qsave. You must have the lisp autoloaded for it to work. The code attached is NOT working code just method.

 

;Reactor callback function
(defun BeginSaveFunc (reactor lst / variables)
do your save here
)

(cond
((= (vlr-current-reaction-name) ':VLR-beginSave) (Princ "\nThis function has been triggered by a Document Save event."))
((= (vlr-current-reaction-name) ':VLR-beginClose)(princ "\nThis function has been triggered by a Document Close event."))
)
(princ)
)
(if (not _BeginCloseReactor) (setq _BeginCloseReactor (VLR-Dwg-Reactor nil '((:VLR-beginClose . BeginCloseFunc)))))
(if (not _BeginSaveReactor ) (setq _BeginSaveReactor  (VLR-Dwg-Reactor nil '((:VLR-beginSave  . BeginCloseFunc)))))

 

0 Likes