AutoLisp routine to save .dwg in two different locations

Vengatesh_Dayalan2
Explorer

AutoLisp routine to save .dwg in two different locations

Vengatesh_Dayalan2
Explorer
Explorer

Hello community,

 

My first post here!

After a good read on AutoLisp basics, I've decided to write my first routine to speed up my drafting process.

 

As the title suggests, I'm looking to save a .dwg file in two different locations: the organisation's drive location and my machine's local hard drive.

 

Looked into the community forum and the best match that I've found is this post which has been provided a solution by one of the community members (@Kent1Cooper).

The solution as per referred post

(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)
)

The outcome that I desire to attain is not too different from the one that's been given as a solution apart from the below,

 

I want the parent drawing (.dwg) file to remember the location of the alternative clone saved. In essence, imagine if I'm working on drawing1.dwg saved in location A and drawing2.dwg in location B. Is there any way to make the drawing1.dwg remember the respective cloned location i.e. location A? 

 

The parent file is always stored in the local drive and the clone is in the organisation's drive.

Also, rather than hardcoding the file path into the script, is there any convenient way such as an inbuilt AutoCAD wizard that I can use within this routine?

 

Thanks,

Venkey

 

0 Likes
Reply
471 Views
11 Replies
Replies (11)

paullimapa
Mentor
Mentor

"I want the parent drawing (.dwg) file to remember the location of the alternative clone saved. In essence, imagine if I'm working on drawing1.dwg saved in location A and drawing2.dwg in location B. Is there any way to make the drawing1.dwg remember the respective cloned location i.e. location A? ."

 

Perhaps you can clarify the following:

1. Both locations actually have the same file name drawing.dwg and not drawing1.dwg & drawing2.dwg, correct?

2. Are you currently opening & working on the dwg (parent drawing) from your local drive but want to automatically save a copy (clone) to the organization's drive?

3. Or are you currently opening & working on the dwg (parent drawing) from your organization's drive but want to automatically save a copy (clone) to your local drive?

4. Do you want the SS lisp function when ran for the first time with the dwg (parent drawing) opened to prompt for the location where the copy should be saved?

Folder Selection Options:

If you have Express Tools loaded then you can use the following function to select a folder:

(acet-ui-pickdir "Select Folder" (getvar"dwgprefix"))

 Otherwise you can use the following (aec_get_folder "Select Folder:" nil) function:

;;;--- aec_get_folder function 
; uses shell.application to launch GUI for user to select 
; Network shared files 
; credit to Tony Tanzillo - Tim Willey
; https://groups.google.com/g/autodesk.autocad.customization/c/-SHIm7gQK9c/m/Vz9U3tLc2LcJ%5B1-25%5D
; "::{F02C1A0D-BE21-4350-88B0-7367FC96EF3C}" = network shares
; Lee Mac 'BrowseForFolder parameters:
; http://lee-mac.com/directorydialog.html
; https://www.daniweb.com/programming/code/516427/vbscript-browse-for-folder-or-file
; vla-get-HWND forces dialog in middle of AutoCAD screen and user must select folder before can minimize AutoCAD
; credit to Don Grout for pointing this out
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/why-vla-get-hwnd-ping-tim-willey-and-tony-tanzillo/td-p/1481901
; Arguments:
; msg = message on top of folder list
; fld = start folder location - note only subfolders are show but folders above given folder are not shown 
; Examples:
; (aec_get_folder "Select Folder:" (getvar "dwgprefix")) ; only folders under current drawing path is listed for selection
; (aec_get_folder "Select Folder:" "") ; only folders under My PC are listed for selection
; (aec_get_folder "Select Folder:" nil) ; only folders under Desktop are listed for selection
(defun aec_get_folder (msg fld / sh folder folderobject fil)
  (setq sh (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application"))
;  (setq folder (vlax-invoke-method sh 'BrowseForFolder (vla-get-HWND (vlax-get-Acad-Object)) msg 30 0))
  (setq folder (vlax-invoke-method sh 'BrowseForFolder (vla-get-HWND (vlax-get-Acad-Object)) msg 30 fld))
  (vlax-release-object sh)
   (if folder
    (progn
     (setq folderobject (vlax-get-property folder 'Self))
     (setq fil (vlax-get-property FolderObject 'Path))
     (vlax-release-object folder)
     (vlax-release-object FolderObject)
     (if (wcmatch fil "::{*")
      (progn(alert"Invalid Folder Selection")(setq fil nil))
     ) ; if
    ) ; progn
   ) ; if
   fil ; return folder selection if any
) ; defun

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes

Sea-Haven
Mentor
Mentor

Where I used to work every dwg had a directory as we had multi information saved on the server. So why not duplicate the dwg directory name on your local drive.  

You can trap "quit" with a reactor and so save it twice, 1st to the local drive then to server so recent files is in correct order. 

Example make directory

; check that pdf directory exists
(setq dwgpre (strcat (getvar "dwgprefix") "\pdf"))
(if (= (vl-file-directory-p dwgpre) nil)
(vl-mkdir dwgpre)
)

Example reactor

;Reactor callback function
(defun BeginCloseFunc (reactor lst / )
do save 1
do save 2
)
(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

Vengatesh_Dayalan2
Explorer
Explorer

@paullimapa, thanks for the response. The answer to your queries,

 

1. Yes, the drawings have two different names, drawing1.dwg and drawing2.dwg. This is to easily distinguish the difference between the file version that is saved on the local drive and the organisation's drive

2. Yes, I'm currently opening &working on the dwg (parent drawing) from my local drive and want to save a copy (clone) to the organisation's drive.

3. (The latter one answered this question I guess)

4. Yes, when I first open the parent drawing I want it to prompt for the clone location where the copy (clone) should be saved.

 

0 Likes

paullimapa
Mentor
Mentor

Thanks for answering my questions. But your answers lead to a major drawing file naming issue. If the drawings saved as copies on the organizations drive all have different names compared to those on the local drive, how do you deal with drawings that are externally referenced into another drawing?


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes

Moshe-A
Mentor
Mentor

@paullimapa 

 

Good point Paul ๐Ÿ˜€  users does not see the whole picture. Bad action of one member office could lead to unusable files for years to come.

 

Moshe

 

 

 

0 Likes

paullimapa
Mentor
Mentor

Yes, it's going to get really messy.

For example:

The original drawing locally is named drawing1.dwg and the clone copy in the organization's drive is named drawing2.dwg.

Then what happens when a new local drawing named drawing2.dwg is created but the clone copy is actually drawing3.dwg but NOT drawing2.dwg.

It becomes a file management nightmare.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes

Vengatesh_Dayalan2
Explorer
Explorer

Hi Paul,

 

Thanks for the observations made on what could potentially happen when one tries to execute the routine.

 

Perhaps, it would be better if I explained a bit more about my profession and how my organisation works and manages projects.

 

The Profession

I work for a glazing contractor and my day-to-day activity (in terms of draughting) includes producing technical drawings for curtain walls and commercial glazed partitions. The drawing that I produce would somewhat look like the attached image ( example image 1.jpg). The document (.dwg) is nowhere externally referred and I understand your concerns, I've noticed how architects/services contractors work with floor plans and other such plans which cross-integrate with one another using the help of the XREF feature in AutoCAD (there could be furthermore used for external references that I'm unaware of). The nature of the job doesn't involve any operations as such. With that being said the external reference issue is not to be worried about.

 

Managing Projects

When it comes to managing different projects, everything is left to the care of the technician working on the project (unless there is an involvement of other team members in the absence of the technician or there needs to be a hand on the deck to help with the deadline). Now, with that being said, all the members of the team save the drawing inside a main folder where all the projects are saved. As I said, this location (main folder) is the central storage point for all the projects that have been taken care of by all the team members. 

 

How I work

Just like the organisation's drive, I work with a folder structure where each drawing is saved in its own project folder with its name - number - revision - and other relevant alphanumeric characters (this is the default naming convention that everyone adheres to such that anyone within the organisation knows which drawing is which. Now, to distinguish between the one that is saved on the local disc and the organisation's drive I follow a simple naming change which is to add my initials at the end of the drawing file. 

 

To explain further the folder structure is as follows,

Organisations Drive/main folder/project name/name - number - revision - and other relevant alphanumeric characters.dwg

Local Drive/main folder/project name/name - number - revision - and other relevant alphanumeric charactersVD.dwg

 

Why do I need this routine?

I use Sheet Set Manager to manage all the projects. It has been a huge time saver and helps me to update the drawing title block without opening a particular layout of the drawing. Unfortunately, the rest of the team doesn't use the sheet set manager and it is super hard to persuade the line manager to get the rest of the team formally trained on how to use it. Also, it is just not about the team itself, there are various departments that use the drawing at different stages as the project completes its milestones.

 

As a workaround, I found this routine which converts all the fields to text. The rest of the team works the way they do and I do the way I want and it's a win-win for all. This is the reason to save the file in two different locations. I get to work with the sheet set manager with all the fields integrated in the drawing and the rest of the team does it as they wish. I simply thought that disclosing this operation would lead to opening another can of worms as this needs to be a separate routine on its own. Also, this being my first routine, I have several other automation listed and want to choose a simple one to start with.

 

The intent is to integrate the FLD2TXT routine somewhere in the script that saves a copy of the file removing all the fields and leaves them as text. 

 

I've written a pseudocode below on how I thought this would work.

 

;New project initiation

;.dwg saved in the local drive (named with the VD initial at the end to distinguish the one saved locally and on the organisation's drive)

;Makes progress/changes in the drawing

;Executes the QQS command (this is what I initially thought I'd name the command as it correlates with quick save and uses only left-hand keys)

(when saved for the first time

(asks for the location  ;This is the organisation's drive

(asks for the name of the drawing ;The name of the drawing always consists of a string that has a VD at the end. I imagine that we could do something with string concatenation as both the values are identical apart from the two alphabets VD

)))

(QQS command)

(add in the FLD2TXT routine to convert all the fields command to text/mtext)

(Memory variable) ;remembers the drawing location for future saving as I don't want the script to ask for the location every single time when I execute the QQS command

 

There might be additional things to be taken care of in the above pseudocode that I'm unaware of (like error handling) and I might have gone wrong on the order of how the code works to complete each task. Please let me know how this can be worked.

 

Thanks,

Venkey

 

 

0 Likes

paullimapa
Mentor
Mentor

Thanks for your detailed explanation. Now I understand that you want to add your initials to your drawing file name to save as a copy. This makes it also easier to identify who actually created the drawing. 

With that see if attached QQS.lsp works for you.

I cannot use the FLD2TXT.vlx file you referred to because it requires a response after the command is executed which LISP cannot fill in.

So I found CFT.lsp which converts Fields to MTexts

Save both CFT.lsp & QQS.lsp to a folder that's in your AutoCAD's Options>File>Support File Search Path & Trusted Locations.

Then load QQS.lsp:

 

(load"QQS")

 

Then run the new command added:

command: QQS

Let me know if it works for you.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes

Vengatesh_Dayalan2
Explorer
Explorer

Many thanks for investing your valuable time in writing the above code for me, @paullimapa

 

I added both the CFT.lsp and QQS.lsp to the startup suite and these are the first observations that I made.

 

  1. The command takes almost 10 to 20 seconds to execute the task that it is meant to do. This is not a problem for now and I would look for ways to speed this up in the future as I gain a bit more knowledge on AutoLisp.
  2. In my drawing, I always have a couple of images attached as .jpg or .png and their external reference paths seem to be missing in the saved file (i.e. the file which is saved onto the organisation's drive with all the fields converted to MTEXT). These images are not constant and it varies from project to project. I now get what you meant by handling the external references in one of your previous replies. You rightly predicted it which eventually happened. 
  3. When prompted for the file saving location for the first time, a wizard appears which is attached (File Path Wizard.png) and I would like to know whether it could have the same pop-up window as it would when a SAVEAS command is executed. This is just to save some time here. I always have the habit of copying and pasting the full folder path onto the File name: window when the SAVEAS command is executed as this would directly take me to the destination where I want the file to be saved rather than clicking each and every folder and its subfolder to choose the destination. 
  4. As far as converting the fields to MTEXT is concerned, the CFT works like a charm. However, the routine doesn't ask me to specify the name of the file that it creates. This in turn creates the .dwg file with the same name (along with my initials at the end) onto the organisation's drive which I don't want to happen.
  5. When executing the QQS command on different drawings, I get a message in the command window as "Unknown command "QQS". I'm loading these .lsp files every single time with the APPLOAD command or have to type (load"QQS") to make it work with that particular drawing. Are there any settings that I need to look after to make this command work on all the drawings?
  6. Could I as an additional feature, have the capacity to change the destination folder where the file needs to be saved? Imagine a scenario where the name of the initially selected folder on the organisation's drive is changed (there have been certain instances when this has happened). In such events, maybe this script could ask the user " Initially selected folder is unavailable, where do you want to save the file?"
  7. Furthermore, Imagine the project has expanded say from 3 layouts (with the fields integrated) to 5 and I want to execute the QQS command to save the local file onto the organisation's drive. Can this routine be designed in such a way that the given file name (without the initials VD that is saved onto the organisation's drive) is captured in a user-defined variable so that when the user executes the QQS command after a considerable expansion of the drawing it gives an option of "This file already exists, do you want to overwrite it" with a Yes and a No rectangular columns next to the crosshair? And of course, this can be only addressed after sorting out the issue on point 4.

That's everything about the QQS command.

 

Over the past few months, I've looked into the Autodesk community forums to look for identical automation or something similar that would suit my needs and then I could make a few tweaks here and there to a code with my little knowledge gained thus far to speed up some of the processes that I do on a daily basis. This is the first case-specific code that I've had an opportunity to look at. The Active X functions always puzzled me (so was the basic lisp code when I started initially) and I always wanted to put my thoughts to wrap my head around it. Now this is a great opportunity for me to do that. I'm about to decode each line of code that you shared over the weekend and get a bit more of an understanding of what's actually happening in the background when each line of code is executed. 

 

Many thanks,

Venkey

 

0 Likes

Sea-Haven
Mentor
Mentor

It is sounding like you need to very much use (getvar 'dwgprefix), the directory path that is returned should match both the local and server drives the only difference would be the drive letter, our server was S: then P: then L: over time. You need to have a custom save lisp, so it saves local 1st with your fields then runs the FLD2MTXT and anything else then does a Saveas to the server, followed by a close or quit as you no longer want the server version as current. If you do want the local version to re-appear then you will need to call a script as the last line in the custom code, which has 1 line in it OPEN Localdwg. So as part of the custom save lisp, it writes the script file every time with current dwg name.

 

I think this would be much better and simpler than trying to do a reactor trapping save close etc.

0 Likes

paullimapa
Mentor
Mentor

1. I'm not sure what is the cause that's adding time to the QQS command execution. But prehaps it could be related to CFTALL function being run which searches through your entire drawing for FIELDS including inside Blocks to convert to MTexts.  You can always try and comment out the following lines in the code which runs the (C:CFTALL) function to see if that's the cause. Current lines of code:

 

;;; chk for CFTALL function
  (if (not c:CFTALL)
   (if (findfile "CFT.lsp")(load "CFT"))
  )  
  (if c:CFTALL
    (c:CFTALL) ; run the field to text conversion function if found
    (alert "CFTALL Function Not Found")
  ) ; if

 

Place semicolons in front of the lines to prevent execution:

 

;;; chk for CFTALL function
;  (if (not c:CFTALL)
;   (if (findfile "CFT.lsp")(load "CFT"))
;  )  
;  (if c:CFTALL
;    (c:CFTALL) ; run the field to text conversion function if found
;    (alert "CFTALL Function Not Found")
;  ) ; if

 

 

2. There are two possible causes for the missing images:

(A) Images are attached using Relative & not Full Paths

If you attached these images using relative paths and the drawing is now saved onto a different location which is the organizations drive then AutoCAD won't be able to locate these images.

(B) Images are stored on your local drive only and not on the organizations drive.

When your coworkers open the drawings on the organization's drive AutoCAD won't be able to locate these images stored only on your local drive. Also if something happens to your local drive then all your images will be lost. The best place to store these images is in the organizations drive where the data is always backed up and can be accessible by your coworkers when the clone drawings are opened. Then you should also be attaching these images from the organization's drive and not from your local drive.

3. These are the two ways I would handle the file saving location:

(A) Embed the folder location in the code here: orgdrivefolder "":

 

 (setq initial "VD" ; set initial as needed
       dwgnameclone (strcat (vl-filename-base (getvar "dwgname")) initial) ; set clone dwgname
       orgdrivefolder "" ; replace "" with orgnanization drive & main folder name to speed up folder selection
 )

 

So for example if the organization drive's main folder is called: M:\Main Folder then the code will change to look like this:

 

 (setq initial "VD" ; set initial as needed
       dwgnameclone (strcat (vl-filename-base (getvar "dwgname")) initial) ; set clone dwgname
       orgdrivefolder "M:\\Main Folder" ; replaced with orgnanization drive & main folder 
 )

 

 

(B) The Express Tool's Folder selection window does NOT provide an option for you to paste a drive & folder name.

So change the following lines of code:

 

    ;;; select org locaiton
    (if (= 1 *aec_acet*) ; chk if express tools
     (setq orglocation (acet-ui-pickdir "Select Folder" orgdrivefolder)) 
     (setq orglocation (aec_get_folder "Select Folder" orgdrivefolder)) 
    )

 

To this which comments out the Express Tool's Folder selection (acet-ui-pickdir) function and now uses the custom (aec_get_folder) function:

 

    ;;; select org locaiton
;    (if (= 1 *aec_acet*) ; chk if express tools
;     (setq orglocation (acet-ui-pickdir "Select Folder" orgdrivefolder)) 
     (setq orglocation (aec_get_folder "Select Folder" orgdrivefolder)) 
;    )

 

This custom window for Folder selection is expandable & has an edit box at the bottom for you to paste that information into:

paullimapa_0-1737159587651.png

4. The CFT.lsp function just does the conversion. Do you also want to change the location to save your drawing whenever you run the (C:CFTALL) function?

You'll have to describe more as to what you're thinking of whenever you run CFTALL that's different than QQS

5. Not sure why you have to manually enter (load"QQS") each time when a drawing is opened since you already placed both CFT.lsp and QQS.lsp to automatically load in Appload's Startup Suite. Perhaps you can do a screenshot of your Appload's Startup Suite and share that here like I did below:

paullimapa_1-1737160122046.png

6. Well I'm not sure why your organization's folder would get moved or changed while you're working in your drawing locally. But if for some reason this happens right after you select your organization's Main Folder location then enter the following at the command prompt:

 

(setq *orglocation* nil)

 

Then run QQS again and you'll be prompted to select a Folder location again.

 

7. What in your mind would be this considerable expansion of time? 1 Month, 2 Months, 3 Months? There could be a routine added into the QQS code so that when the drawing with the initials exists in the organization's drive location it'll compare the date with the date of the drawing locally saved. So based on your considerable expansion of time definition, it'll then prompt for drawing file replacement or perhaps a completely different drawing name copied to the organization's folder location?

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes