Modifying Archive Lisp

Modifying Archive Lisp

ME42L
Enthusiast Enthusiast
176 Views
7 Replies
Message 1 of 8

Modifying Archive Lisp

ME42L
Enthusiast
Enthusiast

I've been using a customized version of Lonnie's archive_and_save.lsp to archive my current DWG files. My version includes a few key modifications to better suit my workflow:

 

  1. Creates an "Archive" folder with a subfolder named after the current drawing.
  2. Copies only the current drawing into this folder, appending a timestamp to the filename.
  3. Verifies that the archive was successfully created before proceeding.

However, I’ve run into an issue with unsaved drawings. If the drawing hasn’t been saved yet, I’d like the lsp to display a dialog box notifying the user and then exit without executing the rest of the code.

 

I used AI to assist with some of the edits, so if anything looks off or could be improved, let me know.

 

(defun C:ACOPY (/ dwgDir dwgName currentDate year month day hour min sec formattedDateTime archiveDir subFolder fullPath srcFile dstFile newName ext)

  ;; Get the directory and filename of the current drawing
  (setq dwgDir (getvar "dwgprefix"))
  (setq dwgName (vl-filename-base (getvar "dwgname")))
  (setq ext (vl-filename-extension (getvar "dwgname")))

  ;; Get and format the current date and time
  (setq currentDate (rtos (getvar "cdate") 2 8)) ;; yyyymmdd.hhmmss
  (setq year (substr currentDate 1 4))
  (setq month (substr currentDate 5 2))
  (setq day (substr currentDate 7 2))
  (setq hour (substr currentDate 10 2))
  (setq min (substr currentDate 12 2))
  (setq sec (substr currentDate 14 2))
  (setq formattedDateTime (strcat year "-" month "-" day "_" hour "-" min "-" sec))

  ;; Define archive folder and subfolder named after DWG file
  (setq archiveDir (strcat dwgDir "archive\\"))
  (setq subFolder (strcat archiveDir dwgName "\\"))
  (vl-mkdir archiveDir)
  (vl-mkdir subFolder)

  ;; Construct full path of the current DWG file
  (setq srcFile (strcat dwgDir dwgName ext))
  ;; Construct destination file name with timestamp
  (setq newName (strcat dwgName "_" formattedDateTime ext))
  (setq dstFile (strcat subFolder newName))

  ;; Copy the current drawing file
  (vl-file-copy srcFile dstFile)

  ;; Check if the file was actually created and display appropriate message
  (if (findfile dstFile)
    (alert (strcat "Current DWG file archived successfully to:\n\n" dstFile))
    (alert "ERROR: Archive unsuccessful. File was not created.")
  )

  (princ)
)

Thanks in advance!

0 Likes
Accepted solutions (3)
177 Views
7 Replies
Replies (7)
Message 2 of 8

BlackBox_
Advisor
Advisor
Accepted solution
(vl-load-com)

(defun c:ACOPY (/ dwgprefix dwgname source date archive archiveDir
               subFolder)
  (setq dwgprefix (getvar 'dwgprefix))
  (setq dwgname (getvar 'dwgname))
  (setq source (strcat dwgprefix dwgname))
  
  (if (not (findfile source))
    (progn
      (alert "Please save the drawing first. ")
      (exit)
      )
    )

  (setq date (menucmd "M=$(edtime,$(getvar,date),YYYY-MO-DD_HH-MM-SS)"))  
  (setq archive
         (strcat (vl-filename-base dwgname)
                 "_"
                 date
                 (vl-filename-extension dwgname)
         )
  )
  (setq archiveDir (strcat dwgprefix "archive\\"))
  (setq subFolder
         (strcat archiveDir (vl-filename-base (getvar 'dwgname)))
  )
  (foreach folder (list archiveDir subFolder)
    (vl-mkdir folder)
  )
  (alert
    (if (vl-file-copy source (setq archive (strcat subFolder "\\" archive)))
      (strcat "Current DWG file archived successfully to:\n\n" archive)
      "ERROR: Archive unsuccessful. File was not created."
    )
  )
  (princ)
)

 

[Edit] - Simplified the date variable to a single line with a menucmd call.


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

Message 3 of 8

ME42L
Enthusiast
Enthusiast

Thank you very much! Would it be possible to add a dialogue box at the start that informs the user "The Archive Tool is NOT meant for use with ProjectWise, ACC Docs, or other cloud-based services" Proceed? with a Yes/No option? No being to exit the routine. I've been tinkering with .dcl and that too isn't in my alley.

0 Likes
Message 4 of 8

BlackBox_
Advisor
Advisor
Accepted solution

@ME42L wrote:

Thank you very much! Would it be possible to add a dialogue box at the start that informs the user "The Archive Tool is NOT meant for use with ProjectWise, ACC Docs, or other cloud-based services" Proceed? with a Yes/No option? No being to exit the routine. I've been tinkering with .dcl and that too isn't in my alley.


Too easy: 

 

(vl-load-com)

(defun c:ACOPY (/ dwgprefix dwgname source date archive archiveDir
               subFolder)

  ;; demand load express tools
  (if (not acet-ui-message)
    (progn
      (c:ExpressTools)
      ;;(prompt "\nEXPRESSTOOLS loaded. ") ; uncomment if needed
    )
  )

  (if
    (=
      7
      (acet-ui-message
        "The Archive Tool is NOT meant for use with ProjectWise, ACC Docs, or other cloud-based services... Proceed?"
        "Archive Warning"
        52
      )
    )
     (exit)
  )
  
  (setq dwgprefix (getvar 'dwgprefix))
  (setq dwgname (getvar 'dwgname))
  (setq source (strcat dwgprefix dwgname))
  
  (if (not (findfile source))
    (progn
      (alert "Please save the drawing first. ")
      (exit)
      )
    )

  (setq date (menucmd "M=$(edtime,$(getvar,date),YYYY-MO-DD_HH-MM-SS)"))  
  (setq archive
         (strcat (vl-filename-base dwgname)
                 "_"
                 date
                 (vl-filename-extension dwgname)
         )
  )
  (setq archiveDir (strcat dwgprefix "archive\\"))
  (setq subFolder
         (strcat archiveDir (vl-filename-base (getvar 'dwgname)))
  )
  (foreach folder (list archiveDir subFolder)
    (vl-mkdir folder)
  )
  (alert
    (if (vl-file-copy source (setq archive (strcat subFolder "\\" archive)))
      (strcat "Current DWG file archived successfully to:\n\n" archive)
      "ERROR: Archive unsuccessful. File was not created."
    )
  )
  (princ)
)

 

Cheers

 

[Edit] - added demand load for express tools in 2025+


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

Message 5 of 8

ME42L
Enthusiast
Enthusiast

Thanks!!

 

0 Likes
Message 6 of 8

ME42L
Enthusiast
Enthusiast

I seem to be doing something wrong, the lsp routine was initially working but now I'm getting this error

 

error: no function definition: ACET-UI-MESSAGE

0 Likes
Message 7 of 8

BlackBox_
Advisor
Advisor
Accepted solution

@ME42L wrote:

I seem to be doing something wrong, the lsp routine was initially working but now I'm getting this error

 

error: no function definition: ACET-UI-MESSAGE


You didn't specify, but it sounds like you're using 2025+... this might be the cause:

 

https://www.autodesk.com/support/technical/article/caas/sfdcarticles/sfdcarticles/AutoCAD-2025-Produ...

 

Does the code above work after running (c:EXPRESSTOOLS) ?


 

I've revised the code above to include a demand load for express tools.

 


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes
Message 8 of 8

ME42L
Enthusiast
Enthusiast

Yes, I'm using 2025! Thanks again!