Create a LISP to save current drawing with appended revision to the end of the file

Create a LISP to save current drawing with appended revision to the end of the file

logan_ownbey5UKQZ
Contributor Contributor
3,477 Views
31 Replies
Message 1 of 32

Create a LISP to save current drawing with appended revision to the end of the file

logan_ownbey5UKQZ
Contributor
Contributor

Hello,

 

I am trying to create a lisp file that when used will take the current drawing file (xxxx-xxxx) and ask for what revision in a pop-up window. It will then append the revision to the end of the name with -revision. I would like it to save to the current folder. Once saved it will then open the new drawing. 

 

The issue i am running into is, if the file has already been Rev'd i would like for it to replace it with the new revision that is entered in the pop up, overwrite the current revision at the end of the name and save it and open it.

I was using chatgpt to create this and it got pretty close expect for the renaming the -revision with the updated name. not sure how to make that part work.

0 Likes
Accepted solutions (2)
3,478 Views
31 Replies
Replies (31)
Message 2 of 32

logan_ownbey5UKQZ
Contributor
Contributor

This is what i have come up with, however, i get error, char-is-alphanumeric.

 

(defun c:SaveReplaceCharRevisionOpen1 ()
(setq currentFileName (getvar "DWGNAME"))
(setq currentFilePath (getvar "DWGPREFIX"))

; Prompt user for revision using a pop-up window
(setq newRevision (getstring "\nEnter revision: "))

; Check if the drawing name has a character at the end
(if (and (> (strlen currentFileName) 0)
(char-is-alphanumeric (substr currentFileName (- (strlen currentFileName) 1) 1))
)
(progn
; Replace the last character with the new revision
(setq newFileName (vl-string-subst newRevision (- (strlen currentFileName) 1) (- (strlen currentFileName)) currentFileName))
)
; Append "-revision" to the end of the name
(setq newFileName (strcat currentFilePath (vl-filename-base currentFileName) "-" newRevision ".dwg"))
)

; Save current drawing as AutoCAD 2018 file with the new or updated revision
(command "_.SAVEAS" "2018" newFileName)

; Open the new or updated drawing
(command "_.OPEN" newFileName)

(princ "\nCurrent drawing saved with new or updated revision and opened.")
)

(princ)

0 Likes
Message 3 of 32

paullimapa
Mentor
Mentor

First of all Autolisp has no such function:

char-is-alphanumeric

Questions:

1. Is the revision that you want to append a number (-#) or an alphabet (-A through Z)?

2. Would the revision be only single digit (-1) or (-A) or is it possible that it could become double digit (-10) or (-AA)?

3. Would it be possible that your drawing name could also be named with a -# or - alphabet at the end causing confusion as to if this is a revision or the actual name of the drawing?


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 4 of 32

logan_ownbey5UKQZ
Contributor
Contributor

The drawing will start out as a xxxE-xxxx.xxx. Once a revision is required it will go from A-Z. Once the drawing is released the revision will go from 1-99. I do not foresee anything going to AA or more.

What i would like for it to do is once someone revises the drawing to -A it will close the previous revision and open the new one. If the A revision needs to be revd up to a B then it needs to see that it once had a Alpha charc and now needs to change that char to the new revision and save it. then open that file up.

Basically. If i have a file that is Rev A. I would like to run a program that save as the file as the next rev "b" and close the A file and open the B file.

0 Likes
Message 5 of 32

logan_ownbey5UKQZ
Contributor
Contributor

I am also fine with a VBA Macro

0 Likes
Message 6 of 32

paullimapa
Mentor
Mentor

VBA forum is over here:

https://forums.autodesk.com/t5/vba/bd-p/33


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 7 of 32

logan_ownbey5UKQZ
Contributor
Contributor

I understand this is not the area to ask for both but which ever way is easiest to create would be great.

0 Likes
Message 8 of 32

paullimapa
Mentor
Mentor
Accepted solution

Try this...save the contents to SaveAsR.lsp then on AutoCAD's command prompt enter:

(load"SaveAsR")

Or drag+drop SaveAsR.lsp from Windows Explorer onto AutoCAD's drawing area.

Then to run it enter at command prompt:

SaveAsR

; SaveAsR adds revision number or alphabet to drawing name and save to that new name
; OP:
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/create-a-lisp-to-save-current-drawing-with-appended-revision-to/m-p/12509312/highlight/false#M460713
(defun c:SaveAsR (/ currentFileName currentFilePath newFileName newRev newRevision)
 (vl-load-com)
 (setq currentFileName (vl-filename-base (getvar "DWGNAME"))
       currentFileNamelen (strlen currentFileName)
       currentFilePath (getvar "DWGPREFIX")
 )
 (cond
  ((wcmatch currentFileName "*-#") ; has 1 num rev
    (setq currentRev (substr currentFileName currentFileNamelen) ; get #
          newRev (1+ (atoi currentRev))                          ; add 1
          currentFileName (substr currentFileName 1 (- currentFileNamelen 2)) ; filename only
          newRevision (getstring (strcat"\nCurrent revision is [" currentRev "] Enter New revision: <" (itoa newRev) ">: "))
    )
    (if (zerop(strlen newRevision))(setq newRevision (itoa newRev)))
  )
  ((wcmatch currentFileName "*-?") ; has 1 alpha rev 
    (setq currentRev (substr currentFileName currentFileNamelen) ; get alpha
          newRev (1+ (ascii currentRev))                          ; add 1
          currentFileName (substr currentFileName 1 (- currentFileNamelen 2)) ; filename only
          newRevision (getstring (strcat"\nCurrent revision is [" currentRev "] Enter New revision: <" (chr newRev) ">: "))
    )
    (if (zerop(strlen newRevision))(setq newRevision (chr newRev)))
  )
  ((wcmatch currentFileName "*-##") ; has 2 num rev 
   (setq currentRev (substr currentFileName (1- currentFileNamelen)) ; get ##
         newRev (1+ (atoi currentRev))                               ; add 1
         currentFileName (substr currentFileName 1 (- currentFileNamelen 3)) ; filename only
         newRevision (getstring (strcat"\nCurrent revision is [" currentRev "] Enter New revision: <" (itoa newRev) ">: "))
   )
   (if (zerop(strlen newRevision))(setq newRevision (itoa newRev)))
  )
  (t                                ; no rev
   (setq newRevision (getstring "\nNo Current revision...Enter revision <A>: "))
   (if (zerop(strlen newRevision))(setq newRevision "A"))
  )
 )  ; cond

; Replace filename with the new revision
 (setq newFileName (strcat currentFilePath currentFileName "-" newRevision))

; SaveAs current drawing as AutoCAD 2018 file with the new or updated revision
 (vl-cmdf "_.SAVEAS" "2018" newFileName)

 (princ "\nCurrent drawing saved with new or updated revision.")
 (princ)
) ; defun

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 9 of 32

dlbsurveysuk
Collaborator
Collaborator

Thanks, I'll be using this.

0 Likes
Message 10 of 32

paullimapa
Mentor
Mentor

Glad to have helped…cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 11 of 32

logan_ownbey5UKQZ
Contributor
Contributor

Question, So my company wants to do it a little differently. 

Instead of -A they would like a -Axxx, the xxx would start with a 1 as this first iteration of the revision. The xx after would be sequential number of the same iteration (it would go up to 00-99). If a new iteration needs to be created with a -A it would then move to a 2 and follow sequential again starting over with 00

So for an example, my first iteration is a -A100. Then if there are say 10 more saves of that revision before release then it will go as follows; -A101, -A102, etc.

If a major change happened while still on -A it would then change the -A1xx to a -A2xx and restart the sequential numbering.

Once -A has been released then then next would be a -B1xx and continue on.

Also, I was wondering if the revision going from A to B could be driven by the revision in the title block. So once the title block goes to a Rev B, could the lisp or something see that and automatically save it as that rev?

0 Likes
Message 12 of 32

paullimapa
Mentor
Mentor

Share your title block dwg and if the revision A or B is placed in an Attribute then the lisp code can look for the title block BLOCK (if more than one assume they're all the same and just select one) and the ATTRIBUTE TAG name and see if there's an A or B there and then use this as the suffix to the dwg name.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 13 of 32

logan_ownbey5UKQZ
Contributor
Contributor

will be working on updating and redoing the title block so not sure what the attribute name will be, however, if you have a spot that says "place attribute name here" i can fill in the blank with whatever we decide to name it.

0 Likes
Message 14 of 32

paullimapa
Mentor
Mentor

Transition from -Axxx to -Bxxx and etc the code can get that from Title Block's Attribute or will just default to -Axxx if no Block found.

But how will the code know to go from -A1xx to -A2xx and etc:


@logan_ownbey5UKQZ wrote:

If a major change happened while still on -A it would then change the -A1xx to a -A2xx and restart the sequential numbering.





Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 15 of 32

logan_ownbey5UKQZ
Contributor
Contributor
the title block will only specify the A,B,C. as we don't put anything at the end of that on our title block.

We would need a prompt to ask, "Did a major change happen?" If they click, yes then the first number goes from 1 to 2. If they click No. then it keeps the First number, the name and sequential changes the number.

I also just thought of something also while trying to explain how our revision system will work.

Here is the TLDR of it. The higher ups do not see a need for VAULT, which we both know will solve all our troubles, so I am having to come up with something that will be a pseudo vault system.

Basically, when i was talking with someone today explaining how this would work, i found a flaw that i am hoping can be solved.

What i would like to see is, when you open the drawing, Let's use E-000.
Let's say that i have been working on this drawing and it has just gone out for review as a Rev A. I get the drawing back with changes. The process that should happen is.
1. Open drawing.
2. Save as with a E-000-A100.
3. Close the save as drawing.
4. Open the E-000 drawing.

I would like for the script to do all this for the people so there is no one that accidently works on the saved revision of the drawing.

Also, you could say that the first digit used are milestones for the drawing.

Let me know if this is confusing at all.
0 Likes
Message 16 of 32

paullimapa
Mentor
Mentor

Here's the issue with your new setup.

If you're working on E-100.dwg and you've already done a SaveAsR before so you already have files like E-100-A100.dwg & E-100-A101.dwg, how does the lisp code know what your last revision name is since you're working on a file name that no longer have information?


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 17 of 32

logan_ownbey5UKQZ
Contributor
Contributor

The E-100 is the main file. The others are just versions of said file that were saved before edit. This is so that if we ever need to go back a rev we have those files saved.

 

Trying to take the human factor of not saving a rev out of the equation.

 

Basically the E-100 is the main file, like you would see in vault. Then you would have E-100-A100 as a revision of that file. In vault it would show as E-100-A with some kind of digits after so that you know which revision file was when. 

0 Likes
Message 18 of 32

paullimapa
Mentor
Mentor

Try this modified version.

Since the filename now no longer contains reference to revision data, I'm solely dependent on revision files saved & stored in the same drawing folder location. As long as the latest revision file remains there then the revised lisp will find this and will use this revision filename as a basis for the next revision file name. Also since you're now always working on the latest file and not on the latest revision file, there's no need to do a SaveAs. But instead a file copy is used so you never leave the current drawing. Give it a try and let me know what you think.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 19 of 32

logan_ownbey5UKQZ
Contributor
Contributor

That worked great, however, the location it saves the file, I am looking through the code to see if that can change location. The location will always be the same folder inside the folder the drawing is saved in.
The folder will be named 01. Archive

0 Likes
Message 20 of 32

paullimapa
Mentor
Mentor

The attached now includes that subfolder location. Test it out to make sure it works.

Note that at the beginning of the code is where you can make changes in the future if needed to the subfolder name as well as Title Block & Attribute Tag names:

 (setq currentFileNameOnly (vl-filename-base (getvar "DWGNAME"))
       currentFileNamelen (strlen currentFileNameOnly)
       currentFilePath (getvar "DWGPREFIX")
       subfolderName "01"                         ; name of subfolder versions are saved
       titleblockName "MyTitleBlock"              ; change this to user title block name
       attributetagName "MyAttributeRevision"     ; change this to user attribute tag name containing revision letter
       revisionFilePath (strcat currentFilePath subfolderName "\\") ; entire subfolder version path
 )

 


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