Author Topic: Challenge: Rename Drawing  (Read 6602 times)

0 Members and 1 Guest are viewing this topic.

M-dub

  • Guest
Challenge: Rename Drawing
« on: April 19, 2006, 02:06:59 PM »
One I've been thinking about for a long time, but have never done anything about it...
In addition to Save and Saveas, it would be nice to have a Rename function which would basically SaveAs, then delete the original... with an 'Are you sure' message, of course.

/idea

Well, Kelie was nice enough to take this one on and sent me the following.  I thought that since we had one person interested in it, that I'd open it up to everyone.  (With Kelie's permission, of course)

Again, Thanks a lot! :)

Code: [Select]
;;;COMMAND: RENDWG
;;;FUNCTION: Save current drawing as a new one with user specified name and
;;;          delete original drawing if successful.
;;;NOTES: Current drawing should have been saved prior to using this command.
;;;By: Kelie Feng, April 2006.
;;;   
(defun C:RENDWG (/ *ERROR* app saveAsType doc oPath oDwgName dir nDwgName nPath)
  (setq app    (vlax-get-acad-object)
saveAsType (vla-get-saveastype
     (vla-get-opensave
       (vla-get-preferences app)
     )
   )
doc    (vla-get-activedocument app)
oPath    (vla-get-fullname doc)
oDwgName   (vla-get-name doc)
dir    (getvar "DWGPREFIX")
  )
  (setq nDwgName (getstring 1 "\nType new drawing name w/o extension: ")
nPath (strcat dir nDwgName ".dwg")
  )
  (if (or (not (findfile nPath))
  (KF:Yes? nil
   (strcat "File \"" nDwgName ".dwg\"" " exists. Overwrite?")
  )
      )
    (if (not
  (vl-catch-all-error-p
    (vl-catch-all-apply
      (function
(lambda ()
  (vla-saveas doc nPath saveAsType)
  (vl-file-delete oPath)
)
      )
    )
  )
)
      (prompt "\nDone.")
      (prompt "\nRename/delete failed.")
    )
  )
  (princ)
)

(defun KF:Yes? (default msg / input)
  (initget "Yes No")
  (setq input (getkword (strcat "\n"
msg
" [Yes/No] <"
(if default
  "Y"
  "N"
)
">: "
)
      )
  )
  (cond
    ((= input "Yes") t)
    ((= input "No") nil)
    (t default)
  )
)

(princ)

hudster

  • Gator
  • Posts: 2848
Re: Challenge: Rename Drawing
« Reply #1 on: April 20, 2006, 03:30:40 AM »
Here's one I wrote, very dirty and very simple but does what I need it to.

Code: [Select]
;;;
;;; TITLE:moddwg.lsp
;;;
;;; Copyright (C) 2005 by Andy Hudson
;;;
;;; Permission to use, copy, modify, and distribute this
;;; software and its documentation for any purpose and without
;;; fee is hereby granted
;;;
;;; THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR
;;; IMPLIED WARRANTY. ALL IMPLIED WARRANTIES OF FITNESS FOR ANY
;;; PARTICULAR PURPOSE AND OF MERCHANTABILITY ARE HEREBY
;;; DISCLAIMED.
;;;
;;; Andy Hudson
;;; April 2005
;;;
;;;-------------------------------------------------------------
;;; Description:
;;;
;;; Routine to make modifying a drawing comply with company
;;; Standards the routine Moves the file to a Mods folder to
;;; ensure only current drawings remain in the current folder
;;;
;;;
;;;-------------------------------------------------------------
;;; COMMAND
;;; moddwg
;;;-------------------------------------------------------------
;;;
;;error checker from www.afralisp.com
;;;
(LOAD "R:/DWG/aids/blocks/rybka_blocks/lisps/ERROR.LSP")
;;
(defun c:moddwg (/ oldname olddir oldrev newrev newname newdir origfile combined)
 ;;change filename to show new revision
  (setq oldname (getvar "dwgname")
olddir (getvar "dwgprefix")
oldrev (getstring T "\nEnter Drawing number and current revision: ")
newrev (Getstring T "\nEnter drawing number and New Revision: ")
newname (vl-string-subst newrev oldrev oldname)
newdir (strcat olddir "Mods\\")
origfile (STRCAT olddir oldname))
  ;;;COMBINE INFORMATION
  (SETQ combined (STRCAT newdir newname))
  ;;MAKE MODS FOLDER
  (if (not (member "Mods" (vl-directory-files (getvar "dwgprefix") nil -1)))
    (vl-mkdir (strcat (getvar "dwgprefix") "Mods")))
  ;;check if file exists
  (if (findfile combined)
    (alert (strcat "File " combined  " already exists In Mods Folder! "))
    (progn);progn
    );if
  ;;SAVE DRAWING
  (COMMAND "_.SAVEAS" "" combined)
  ;;DELETE THE OLD FILE
  (VL-FILE-DELETE ORIGFILE)
  (princ)
  )
(prompt "\nModify Drawing Lisp Loaded, enter moddwg to run.")
(princ)
Revit BDS 2017, 2016, 2015, 2014, AutoCAD 2017, 2016, Navisworks 2017, 2016, BIM360 Glue

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Challenge: Rename Drawing
« Reply #2 on: April 20, 2006, 04:12:14 AM »
Slightly different functionality .. saves a copy of the current drawing to an archive, appending the date and time to the fileName
Code: [Select]
(DEFUN C:copy_backup_as
       (/ *error* activedoc docfullname backuppath archivename)
   ;; codehimbelonga kwb@theSwamp
   ;; requires the DosLib Library.
   ;;
   ;; IAcadDocument Object
   (SETQ activedoc (VLA-GET-ACTIVEDOCUMENT (VLAX-GET-ACAD-OBJECT)))
   ;;
   ;;------ Set Error Trap --------------------------
   ;;
   (DEFUN *error* (msg /) (kb:on-error msg))
   (VLA-ENDUNDOMARK activedoc)                    ; end any open undo group
   (VLA-STARTUNDOMARK activedoc)                  ; start new group
   ;;
   ;;----- Main --------------------------
   ;;
   (VLA-SAVE activedoc)
   (IF
      (AND
         (NOT (= 0 (STRLEN (SETQ docfullname (VLA-GET-FULLNAME activedoc))))
         )
         (SETQ backuppath (DOS_MKDIR
                             (STRCAT (VL-FILENAME-DIRECTORY docfullname)
                                     "\\DRAWINGArchive"
                             )
                          )
         )
         (SETQ archivename
                 (STRCAT backuppath
                         (VL-FILENAME-BASE (VLA-GET-NAME activedoc))
                         "-"
                         (RTOS (* (GETVAR "cdate") 1000000) 2 0)
                         ".dwg"
                 )
         )
      )
      (VL-FILE-COPY docfullname archivename)
      ;; else
      (ALERT "Ooooops. Unable to establish ArchiveName.")
   )
   (PROMPT (STRCAT "\n Archived file : " archivename))
   (*error* nil)
   (PRINC)
)

The kb:on-error library routine is posted elsewhere on the forun, but can easily be replaced.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

CraigP

  • Mosquito
  • Posts: 12
Re: Challenge: Rename Drawing
« Reply #3 on: December 19, 2023, 12:34:05 AM »
Hi,

I have found this code from Kelie very useful, thanks Kelie   :-).
Is there a way of having a pop-up with the current filename already populated ready for editing?
Most of the time I only need to alter 1 or 2 characters, and this would save me a lot of retyping long filenames and reduce the risk of errors.

regards

One I've been thinking about for a long time, but have never done anything about it...
In addition to Save and Saveas, it would be nice to have a Rename function which would basically SaveAs, then delete the original... with an 'Are you sure' message, of course.

/idea

Well, Kelie was nice enough to take this one on and sent me the following.  I thought that since we had one person interested in it, that I'd open it up to everyone.  (With Kelie's permission, of course)

Again, Thanks a lot! :)

Code: [Select]
;;;COMMAND: RENDWG
;;;FUNCTION: Save current drawing as a new one with user specified name and
;;;          delete original drawing if successful.
;;;NOTES: Current drawing should have been saved prior to using this command.
;;;By: Kelie Feng, April 2006.
;;;   
(defun C:RENDWG (/ *ERROR* app saveAsType doc oPath oDwgName dir nDwgName nPath)
  (setq app    (vlax-get-acad-object)
saveAsType (vla-get-saveastype
     (vla-get-opensave
       (vla-get-preferences app)
     )
   )
doc    (vla-get-activedocument app)
oPath    (vla-get-fullname doc)
oDwgName   (vla-get-name doc)
dir    (getvar "DWGPREFIX")
  )
  (setq nDwgName (getstring 1 "\nType new drawing name w/o extension: ")
nPath (strcat dir nDwgName ".dwg")
  )
  (if (or (not (findfile nPath))
  (KF:Yes? nil
   (strcat "File \"" nDwgName ".dwg\"" " exists. Overwrite?")
  )
      )
    (if (not
  (vl-catch-all-error-p
    (vl-catch-all-apply
      (function
(lambda ()
  (vla-saveas doc nPath saveAsType)
  (vl-file-delete oPath)
)
      )
    )
  )
)
      (prompt "\nDone.")
      (prompt "\nRename/delete failed.")
    )
  )
  (princ)
)

(defun KF:Yes? (default msg / input)
  (initget "Yes No")
  (setq input (getkword (strcat "\n"
msg
" [Yes/No] <"
(if default
  "Y"
  "N"
)
">: "
)
      )
  )
  (cond
    ((= input "Yes") t)
    ((= input "No") nil)
    (t default)
  )
)

(princ)

JohnK

  • Administrator
  • Seagull
  • Posts: 10655
Re: Challenge: Rename Drawing
« Reply #4 on: December 19, 2023, 11:03:22 AM »
You can use a simple dialog box to get the string.

Something like:
Code - Auto/Visual Lisp: [Select]
  1. (defun GetString-dlg (str / dcl_id fn fo)
  2.   (setq fn (vl-filename-mktemp "" "" ".dcl"))
  3.   (setq fo (open fn "w"))
  4.   (setq ValueStr (strcat "value = \"" str "\";"))
  5.   (write-line "stringdlg : dialog {
  6.              label = \"Enter String\";" fo)
  7.   (write-line ": edit_box {
  8.              label = \"\";
  9.              edit_width = 30;
  10.              key = \"stringdlg\";
  11.              is_default = true; " fo)
  12.   (write-line ValueStr fo)
  13.   (write-line "}" fo)
  14.   (write-line ": row {
  15.              alignment   = centered;
  16.              fixed_width = true;
  17.               : button {
  18.               label      = \"OK\";
  19.               key        = \"dcl_accept\";
  20.               width      = 10;
  21.               allow_accept = true;
  22.             }
  23.           }
  24.         }" fo)
  25.    (close fo)
  26.    (setq dcl_id (load_dialog fn))
  27.    (new_dialog "stringdlg" dcl_id)
  28.    (action_tile "stringdlg" "(setq str $value)(done_dialog)")
  29.    (unload_dialog dcl_id)
  30.   str
  31.  )
  32.  
  33. ;; Use the above like this:
  34. (setq userstring (getstring-dlg "Test String"))
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

BIGAL

  • Swamp Rat
  • Posts: 1423
  • 40 + years of using Autocad
Re: Challenge: Rename Drawing
« Reply #5 on: December 19, 2023, 06:00:06 PM »
I am sure I have seen a command that lets you do a edit string with a couple of lines of code trying to remember what it was. May be one of the acet commands. Will have a look around.

Just a comment, I use my library function Multi getvals.lsp as it can be used in any code. The number of values to enter is controlled by the list input, 1-about 20.

Code: [Select]
(if (not AH:getvalsm)(load "Multi Getvals.lsp"))
(setq ans (AH:getvalsm (list "Enter value " "Length " 25 24 (getvar 'dwgname) )))
« Last Edit: December 19, 2023, 06:12:09 PM by BIGAL »
A man who never made a mistake never made anything

CraigP

  • Mosquito
  • Posts: 12
Re: Challenge: Rename Drawing
« Reply #6 on: December 19, 2023, 06:24:52 PM »
Hi John,

Thanks for the reply.
I tried saving this as a dcl and calling it from the lisp, but I have never done this before, and something I have done is not working.
I receive the following error message
"no function definition <GETSTRING-DLG> ; expected FUNCTION at [eval]"

Not sure if it makes any difference but I am using BricsCAD V23

regards



You can use a simple dialog box to get the string.

Something like:
Code - Auto/Visual Lisp: [Select]
  1. (defun GetString-dlg (str / dcl_id fn fo)
  2.   (setq fn (vl-filename-mktemp "" "" ".dcl"))
  3.   (setq fo (open fn "w"))
  4.   (setq ValueStr (strcat "value = \"" str "\";"))
  5.   (write-line "stringdlg : dialog {
  6.              label = \"Enter String\";" fo)
  7.   (write-line ": edit_box {
  8.              label = \"\";
  9.              edit_width = 30;
  10.              key = \"stringdlg\";
  11.              is_default = true; " fo)
  12.   (write-line ValueStr fo)
  13.   (write-line "}" fo)
  14.   (write-line ": row {
  15.              alignment   = centered;
  16.              fixed_width = true;
  17.               : button {
  18.               label      = \"OK\";
  19.               key        = \"dcl_accept\";
  20.               width      = 10;
  21.               allow_accept = true;
  22.             }
  23.           }
  24.         }" fo)
  25.    (close fo)
  26.    (setq dcl_id (load_dialog fn))
  27.    (new_dialog "stringdlg" dcl_id)
  28.    (action_tile "stringdlg" "(setq str $value)(done_dialog)")
  29.    (unload_dialog dcl_id)
  30.   str
  31.  )
  32.  
  33. ;; Use the above like this:
  34. (setq userstring (getstring-dlg "Test String"))

CraigP

  • Mosquito
  • Posts: 12
Re: Challenge: Rename Drawing
« Reply #7 on: December 19, 2023, 06:29:33 PM »
Thanks Bigal,

This looks simple but I have no idea how to use it.

regards

I am sure I have seen a command that lets you do a edit string with a couple of lines of code trying to remember what it was. May be one of the acet commands. Will have a look around.

Just a comment, I use my library function Multi getvals.lsp as it can be used in any code. The number of values to enter is controlled by the list input, 1-about 20.

Code: [Select]
(if (not AH:getvalsm)(load "Multi Getvals.lsp"))
(setq ans (AH:getvalsm (list "Enter value " "Length " 25 24 (getvar 'dwgname) )))


CraigP

  • Mosquito
  • Posts: 12
Re: Challenge: Rename Drawing
« Reply #8 on: December 20, 2023, 12:12:23 AM »
After some searching I managed to come up with this, but now have a drawing with the filename ".dwg.dwg"

Here is what I see on the command line, I also have to hit the enter key after accepting the dialog

: (LOAD "R:/Support/BricsCAD-KMI/lisp/New folder/RenameDrawing1.lsp")
: RENDWG1
(230048-004234-001_1x)


Code: [Select]
;;;COMMAND: RENDWG
;;;FUNCTION: Save current drawing as a new one with user specified name and
;;;          delete original drawing if successful.
;;;NOTES: Current drawing should have been saved prior to using this command.
;;;By: Kelie Feng, April 2006.
;;;   
(defun C:RENDWG (/ *ERROR* app saveAsType doc oPath oDwgName dir nDwgName nPath)
  (setq app (vlax-get-acad-object)
        saveAsType (vla-get-saveastype
                     (vla-get-opensave
                       (vla-get-preferences app)
                     )
                   )
        doc (vla-get-activedocument app)
        oPath (vla-get-fullname doc)
        oDwgName (vla-get-name doc)
        dir (getvar "DWGPREFIX")
  )
  (if (not AH:getvalsm) (load "Multi Getvals.lsp"))
; (setq nDwgName (getstring 1 "\nType new drawing name w/o extension: ")
  (setq nDwgName (getstring 1 (AH:getvalsm (list "Enter Drawing Number " "Number " 25 24 (strcase (acet-filename-ext-remove (getvar 'DWGNAME))))))
        nPath (strcat dir nDwgName ".dwg")
  )
  (if (or (not (findfile nPath))
          (KF:Yes? nil
                   (strcat "File \"" nDwgName ".dwg\"" " exists. Overwrite?")
          )
      )
    (if (not
          (vl-catch-all-error-p
            (vl-catch-all-apply
              (function
                (lambda ()
                        (vla-saveas doc nPath saveAsType)
                        (vl-file-delete oPath)
                )
              )
            )
          )
        )
      (prompt "\nDone.")
      (prompt "\nRename/delete failed.")
    )
  )
  (princ)
)

(defun KF:Yes? (default msg / input)
  (initget "Yes No")
  (setq input (getkword (strcat "\n"
                                msg
                                " [Yes/No] <"
                          (if default
                            "Y"
                            "N"
                          )
                                ">: "
                        )
              )
  )
  (cond
    ((= input "Yes") t)
    ((= input "No") nil)
    (t default)
  )
)

(princ)

Thanks Bigal,

This looks simple but I have no idea how to use it.

regards

I am sure I have seen a command that lets you do a edit string with a couple of lines of code trying to remember what it was. May be one of the acet commands. Will have a look around.

Just a comment, I use my library function Multi getvals.lsp as it can be used in any code. The number of values to enter is controlled by the list input, 1-about 20.

Code: [Select]
(if (not AH:getvalsm)(load "Multi Getvals.lsp"))
(setq ans (AH:getvalsm (list "Enter value " "Length " 25 24 (getvar 'dwgname) )))


BIGAL

  • Swamp Rat
  • Posts: 1423
  • 40 + years of using Autocad
Re: Challenge: Rename Drawing
« Reply #9 on: December 20, 2023, 06:59:04 PM »
I ran John's code on Bricscad with no problems, Just copy and paste to notepad, save as say Rendwg.lsp, then use appload and it should run.

Here is Multi getvals.lsp, you have sample code.
A man who never made a mistake never made anything

CraigP

  • Mosquito
  • Posts: 12
Re: Challenge: Rename Drawing
« Reply #10 on: December 20, 2023, 08:43:39 PM »
I am so lost now...

I managed to incorporate Johns code and have the pop-up populated with the current filename but something is still not right.
I type the new filename in the dialog, hit enter. The new filename appears on the command line, and I must hit enter again.
Then I end up with a file called ".dwg.dwg"

Can you please help

Code: [Select]
;;;COMMAND: RENDWG
;;;FUNCTION: Save current drawing as a new one with user specified name and
;;;          delete original drawing if successful.
;;;NOTES: Current drawing should have been saved prior to using this command.
;;;By: Kelie Feng, April 2006.
;;;Revised C.Potter December 2023. (https://www.theswamp.org/index.php?topic=9696.0)
;;;   
(defun GetString-dlg (str / dcl_id fn fo)
  (setq fn (vl-filename-mktemp "" "" ".dcl"))
  (setq fo (open fn "w"))
  (setq ValueStr (strcat "value = \"" str "\";"))
  (write-line "stringdlg : dialog {
                            label = \"Enter New Filename\";" fo
  )

  (write-line ": edit_box {
              label = \"\";
              edit_width = 30;
              key = \"stringdlg\";
                            is_default = true; " fo
  )

  (write-line ValueStr fo)
  (write-line "}" fo)
  (write-line ": row {
              alignment   = centered;
              fixed_width = true;
               : button {
               label      = \"OK\";
               key        = \"dcl_accept\";
               width      = 10;
               allow_accept = true;
             }
           }
                    }" fo
  )

  (close fo)
  (setq dcl_id (load_dialog fn))
  (new_dialog "stringdlg" dcl_id)
  (action_tile "stringdlg" "(setq str $value)(done_dialog)")
  (start_dialog)
  (unload_dialog dcl_id)
  str
)
(defun C:RENDWG1 (/ *ERROR* app saveAsType doc oPath oDwgName dir nDwgName nPath)
  (setq app (vlax-get-acad-object)
        saveAsType (vla-get-saveastype
                     (vla-get-opensave
                       (vla-get-preferences app)
                     )
                   )
        doc (vla-get-activedocument app)
        oPath (vla-get-fullname doc)
        oDwgName (vla-get-name doc)
        dir (getvar "DWGPREFIX")
  )
  (setq nDwgName (getstring 1 (getstring-dlg (strcase (acet-filename-ext-remove (getvar 'DWGNAME)))))
        nPath (strcat dir nDwgName ".dwg")
  )
  (if (or (not (findfile nPath))
          (KF:Yes? nil
                   (strcat "File \"" nDwgName ".dwg\"" " exists. Overwrite?")
          )
      )
    (if (not
          (vl-catch-all-error-p
            (vl-catch-all-apply
              (function
                (lambda ()
                        (vla-saveas doc nPath saveAsType)
                        (vl-file-delete oPath)
                )
              )
            )
          )
        )
      (prompt "\nDone.")
      (prompt "\nRename/delete failed.")
    )
  )
  (princ)
)

(defun KF:Yes? (default msg / input)
  (initget "Yes No")
  (setq input (getkword (strcat "\n"
                                msg
                                " [Yes/No] <"
                          (if default
                            "Y"
                            "N"
                          )
                                ">: "
                        )
              )
  )
  (cond
    ((= input "Yes") t)
    ((= input "No") nil)
    (t default)
  )
)

(princ)

stevej

  • Newt
  • Posts: 30
Re: Challenge: Rename Drawing
« Reply #11 on: January 01, 2024, 11:19:39 PM »
@CraigP
Is there a solution yet to this latest version of the program using a DCL?
This would be a useful tool and I have tried a few things, but my meager efforts are not up to the task, and the drawing is not renamed.

Steve

dexus

  • Bull Frog
  • Posts: 210
Re: Challenge: Rename Drawing
« Reply #12 on: January 02, 2024, 06:08:09 AM »
Why not use getfiled function to get the new location?
Something like this:
Code - Auto/Visual Lisp: [Select]
  1.     "Rename drawing" ; Title
  2.     (strcat (getvar 'dwgprefix) (getvar 'dwgname)) ; Get default file from current drawing
  3.     (substr (last (fnsplitl (getvar 'dwgname))) 2) ; Get extension from current drawing
  4.     1 ; Flag as new file
  5. )

dexus

  • Bull Frog
  • Posts: 210
Re: Challenge: Rename Drawing
« Reply #13 on: January 02, 2024, 09:09:30 AM »
Code - Auto/Visual Lisp: [Select]
  1. ;;;COMMAND: RENDWG
  2. ;; FUNCTION: Save current drawing as a new one with user specified name and
  3. ;;           delete original drawing if successful.
  4. ;; NOTES: Current drawing should have been saved prior to using this command.
  5. ;; By: Kelie Feng, April 2006.
  6. ;; Revised C.Potter December 2023. (https://www.theswamp.org/index.php?topic=9696.0)
  7. ;; Revised dexus January 2024, select file using getfiled
  8. (defun c:rendwg (/ app saveAsType doc oPath nPath)
  9.         doc (vla-get-activedocument app)
  10.         oPath (vla-get-fullname doc)
  11.         nPath (getfiled "Rename drawing" oPath (substr (last (fnsplitl oPath)) 2) 1 ))
  12.   (cond
  13.     ((not nPath) (princ "\nNo file selected."))
  14.     ((not (vl-catch-all-error-p (vl-catch-all-apply
  15.         (function (lambda nil
  16.           (vla-saveas doc nPath saveAsType)
  17.           (vl-file-delete oPath)
  18.         ))
  19.       )))
  20.       (princ
  21.         (strcat
  22.           "\nFile renamed"
  23.           "\n From: \"" (apply 'strcat (cdr (fnsplitl oPath))) "\""
  24.           "\n To:   \"" (apply 'strcat (cdr (fnsplitl nPath))) "\""
  25.         )
  26.       )
  27.     )
  28.     ((princ "\nRename/delete failed."))
  29.   )
  30.   (princ)
  31. )
« Last Edit: January 03, 2024, 04:35:15 PM by dexus »

stevej

  • Newt
  • Posts: 30
Re: Challenge: Rename Drawing
« Reply #14 on: January 03, 2024, 07:08:54 PM »
I am so lost now...

I managed to incorporate Johns code and have the pop-up populated with the current filename but something is still not right.
I type the new filename in the dialog, hit enter. The new filename appears on the command line, and I must hit enter again.
Then I end up with a file called ".dwg.dwg"

As you found, the program works until the new filename text appears on the command line.
I found that only text appended to what appears on the command line becomes the new drawing name.
So it kinda works, but not exactly as you wanted: the DCL file name entry not being passed back to the program as the new file name.

dexus' version works, but it opens a small folder list window that is small (6 lines) and cannot be resized in any direction.

Steve
« Last Edit: January 03, 2024, 07:13:49 PM by stevej »

JohnK

  • Administrator
  • Seagull
  • Posts: 10655
Re: Challenge: Rename Drawing
« Reply #15 on: January 04, 2024, 08:07:21 AM »
I am so lost now...

I managed to incorporate Johns code and have the pop-up populated with the current filename but something is still not right.
I type the new filename in the dialog, hit enter. The new filename appears on the command line, and I must hit enter again.
Then I end up with a file called ".dwg.dwg"

Can you please help
--->%

You are very close. The line you had wrong was:
  (setq nDwgName (getstring 1 (getstring-dlg (strcase (acet-filename-ext-remove (getvar 'DWGNAME)))))
it should be:
  (setq nDwgName (getstring-dlg (strcase (acet-filename-ext-remove (getvar 'DWGNAME))))

Here is a complete code that should work. Although, there are some other areas of the code which I think should be corrected but I do not have the time at the moment, and this should work fine for now.

Code - Auto/Visual Lisp: [Select]
  1. ;;;COMMAND: RENDWG
  2. ;;;FUNCTION: Save current drawing as a new one with user specified name and
  3. ;;;          delete original drawing if successful.
  4. ;;;NOTES: Current drawing should have been saved prior to using this command.
  5. ;;;By: Kelie Feng, April 2006.
  6. ;;;Revised C.Potter December 2023. (https://www.theswamp.org/index.php?topic=9696.0)
  7. ;;;  
  8. (defun GetString-dlg (str / dcl_id fn fo)
  9.   (setq fn (vl-filename-mktemp "" "" ".dcl"))
  10.   (setq fo (open fn "w"))
  11.   (setq ValueStr (strcat "value = \"" str "\";"))
  12.   (write-line "stringdlg : dialog {
  13.                            label = \"Enter New Filename\";" fo
  14.   )
  15.  
  16.   (write-line ": edit_box {
  17.              label = \"\";
  18.              edit_width = 30;
  19.              key = \"stringdlg\";
  20.                            is_default = true; " fo
  21.   )
  22.  
  23.   (write-line ValueStr fo)
  24.   (write-line "}" fo)
  25.   (write-line ": row {
  26.              alignment   = centered;
  27.              fixed_width = true;
  28.               : button {
  29.               label      = \"OK\";
  30.               key        = \"dcl_accept\";
  31.               width      = 10;
  32.               allow_accept = true;
  33.             }
  34.           }
  35.                    }" fo
  36.   )
  37.  
  38.   (close fo)
  39.   (setq dcl_id (load_dialog fn))
  40.   (new_dialog "stringdlg" dcl_id)
  41.   (action_tile "stringdlg" "(setq str $value)(done_dialog)")
  42.   (unload_dialog dcl_id)
  43.   str
  44. )
  45. (defun C:RENDWG1 (/ *ERROR* app saveAsType doc oPath oDwgName dir nDwgName nPath)
  46.         saveAsType (vla-get-saveastype
  47.                      (vla-get-opensave
  48.                        (vla-get-preferences app)
  49.                        )
  50.                      )
  51.         doc (vla-get-activedocument app)
  52.         oPath (vla-get-fullname doc)
  53.         oDwgName (vla-get-name doc)
  54.         dir (getvar "DWGPREFIX")
  55.         )
  56.   (setq nDwgName (getstring-dlg (strcase (acet-filename-ext-remove (getvar 'DWGNAME))))
  57.         nPath (strcat dir nDwgName ".dwg")
  58.         )
  59.   (if (or (not (findfile nPath))
  60.           (KF:Yes? nil
  61.                    (strcat "File \"" nDwgName ".dwg\"" " exists. Overwrite?")
  62.                    )
  63.           )
  64.     (if (not
  65.           (vl-catch-all-error-p
  66.             (vl-catch-all-apply
  67.               (function
  68.                 (lambda ()
  69.                   (vla-saveas doc nPath saveAsType)
  70.                   (vl-file-delete oPath)
  71.                   )
  72.                 )
  73.               )
  74.             )
  75.           )
  76.       (prompt "\nDone.")
  77.       (prompt "\nRename/delete failed.")
  78.       )
  79.     )
  80.   (princ)
  81.   )
  82.  
  83. (defun KF:Yes? (default msg / input)
  84.   (initget "Yes No")
  85.   (setq input (getkword (strcat "\n"
  86.                                 msg
  87.                                 " [Yes/No] <"
  88.                           (if default
  89.                             "Y"
  90.                             "N"
  91.                           )
  92.                                 ">: "
  93.                         )
  94.               )
  95.   )
  96.   (cond
  97.     ((= input "Yes") t)
  98.     ((= input "No") nil)
  99.     (t default)
  100.   )
  101. )
  102.  

TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

dexus

  • Bull Frog
  • Posts: 210
Re: Challenge: Rename Drawing
« Reply #16 on: January 05, 2024, 06:19:20 AM »
Here is a version using powershell to create a saveFileDialog.
It falls back on getfiled when something doesn't work.
I should have spent less time on this, but I got a bit obsessed with getting a resizable file dialog.
Code - Auto/Visual Lisp: [Select]
  1. ;; COMMAND: RENDWG
  2. ;; FUNCTION: Save current drawing as a new one with user specified name and
  3. ;;           delete original drawing if successful.
  4. ;; NOTES: Current drawing should have been saved prior to using this command.
  5. ;; By: Kelie Feng, April 2006.
  6. ;; Revised C.Potter December 2023. (https://www.theswamp.org/index.php?topic=9696.0)
  7. ;; Revised dexus Januari 2024, using filePicker
  8. (defun c:rendwg (/ acobj saveAsType oPath sPath nPath)
  9.         saveAsType (vla-get-saveastype (vla-get-opensave (vla-get-preferences acobj)))
  10.         oPath (vla-get-fullname (vla-get-activedocument acobj))
  11.         sPath (fnsplitl oPath)
  12.         nPath (filePicker (car sPath) (cadr sPath) (substr (caddr sPath) 2)))
  13.   (cond
  14.     ((not nPath) (princ "\nNo file selected."))
  15.     ((not (vl-catch-all-error-p (vl-catch-all-apply
  16.         (function (lambda nil
  17.           (vla-saveas (vla-get-activedocument acobj) nPath saveAsType)
  18.           (vl-file-delete oPath)
  19.         ))
  20.       )))
  21.       (princ
  22.         (strcat
  23.           "\nFile renamed"
  24.           "\n From: \"" (apply 'strcat (cdr (fnsplitl oPath))) "\""
  25.           "\n To:   \"" (apply 'strcat (cdr (fnsplitl nPath))) "\""
  26.         )
  27.       )
  28.     )
  29.     ((princ "\nRename/delete failed."))
  30.   )
  31.   (princ)
  32. )
  33.  
  34. (defun filePicker (directory filename extension / appHandle dch des pickedFile rtn shell tmp tmpopen tmpoutput)
  35.   ; FilePicker by dexus
  36.   ; Create filePicker using a .bat file based on https://stackoverflow.com/a/15885133/4301351
  37.   (cond
  38.     ((and
  39.         (setq tmp (vl-filename-mktemp nil nil ".bat")) ; Create .bat that executes a html file selector
  40.         (setq des (open tmp "w"))
  41.         (write-line
  42.           (strcat
  43.             "<# : chooser.bat \n"
  44.             ":: launches a File... Open sort of file chooser and outputs choice(s) to the console\n"
  45.             ":: https://stackoverflow.com/a/15885133/4301351\n"
  46.             "@echo off\n"
  47.             "setlocal\n"
  48.             "for /f \"delims=\" %%I in ('powershell -noprofile \"iex (${%~f0} | out-string)\"') do (\n"
  49.             "  echo You chose %%~I\n"
  50.             ")\n"
  51.             "goto :EOF\n"
  52.             ": end Batch portion / begin PowerShell hybrid chimera #>\n"
  53.             "Add-Type -AssemblyName System.Windows.Forms\n"
  54.             "$f = new-object Windows.Forms.SaveFileDialog\n"
  55.             "$f.InitialDirectory = \"" directory "\"\n"
  56.             "$f.FileName = \"" filename "\"\n"
  57.             "$f.Filter = \""
  58.               (cond
  59.                 ((= "dwg" (strcase extension t)) "AutoCAD Drawing (*.dwg)|*.dwg")
  60.                 ((= "dxf" (strcase extension t)) "AutoCAD DXF (*.dxf)|*.dxf")
  61.                 ("All Files (*.*)|*.*") ; Default
  62.               )
  63.             "\"\n$f.FilterIndex = 1\n"
  64.             "$f.DefaultExt = \"" extension "\";"
  65.             "$f.ShowHelp = $true\n"
  66.             "[void]$f.ShowDialog()\n"
  67.             "$f.FileName"
  68.           )
  69.           des
  70.         )
  71.         (not (close des))
  72.         (setq tmpoutput (vl-filename-mktemp nil nil ".txt")) ; Create txt file for the return value
  73.         (setq shell (vla-getinterfaceobject (vlax-get-acad-object) "WScript.shell")) ; Shell to run the bat
  74.         (setq appHandle (vl-catch-all-apply 'vlax-invoke (list shell 'Run (strcat "cmd /c " tmp ">" tmpoutput) 0 :vlax-false))) ; Execute hidden
  75.         (vlax-release-object shell) ; Clean up shell object
  76.         (not (vl-catch-all-error-p appHandle)) ; If no errors were found
  77.         (setq tmpopen (open tmpoutput "r")) ; Open txt file
  78.         (setq pickedFile (read-line tmpopen)) ; Read line
  79.         (not (close tmpopen)) ; Close txt file
  80.       )
  81.     )
  82.     ((setq pickedFile (getfiled "" (strcat directory filename) extension 1)) ; Fallback
  83.       (setq pickedFile (strcat "You chose " pickedFile))
  84.     )
  85.   )
  86.   (if (and tmp (setq tmp (findfile tmp))) (vl-file-delete tmp)) ; Cleanup .bat file
  87.   (if (and tmpoutput (setq tmpoutput (findfile tmpoutput))) (vl-file-delete tmpoutput)) ; Cleanup .txt file
  88.   (if
  89.     (and
  90.       pickedFile
  91.       (wcmatch pickedFile "You chose *")
  92.       (/= (setq pickedFile (substr pickedFile 11)) filename)
  93.       (/= pickedFile (strcat directory filename "." extension))
  94.     )
  95.     pickedFile ; Return path if path was picked
  96.   )
  97. )
Edit: Removed dependencies
« Last Edit: January 08, 2024, 02:21:25 AM by dexus »

stevej

  • Newt
  • Posts: 30
Re: Challenge: Rename Drawing
« Reply #17 on: January 06, 2024, 09:35:15 PM »
Here is a version using powershell to create a saveFileDialog.
It falls back on getfiled when something doesn't work.
I should have spent less time on this, but I got a bit obsessed with getting a resizable file dialog.

Was not able to find copies of LM:acobj or LM:acdoc called in your program.
Can you share these?

Steve

stevej

  • Newt
  • Posts: 30
Re: Challenge: Rename Drawing
« Reply #18 on: January 06, 2024, 09:39:00 PM »
Here is a complete code that should work. Although, there are some other areas of the code which I think should be corrected but I do not have the time at the moment, and this should work fine for now.

This works well. Thank you.

Steve

CraigP

  • Mosquito
  • Posts: 12
Re: Challenge: Rename Drawing
« Reply #19 on: January 07, 2024, 09:21:26 PM »
Thanks John, this is exactly what I was trying to achieve.  :-)
Sorry for the delayed reply, I have only just returned from leave.

I am so lost now...

I managed to incorporate Johns code and have the pop-up populated with the current filename but something is still not right.
I type the new filename in the dialog, hit enter. The new filename appears on the command line, and I must hit enter again.
Then I end up with a file called ".dwg.dwg"

Can you please help
--->%

You are very close. The line you had wrong was:
  (setq nDwgName (getstring 1 (getstring-dlg (strcase (acet-filename-ext-remove (getvar 'DWGNAME)))))
it should be:
  (setq nDwgName (getstring-dlg (strcase (acet-filename-ext-remove (getvar 'DWGNAME))))

Here is a complete code that should work. Although, there are some other areas of the code which I think should be corrected but I do not have the time at the moment, and this should work fine for now.

Code - Auto/Visual Lisp: [Select]
  1. ;;;COMMAND: RENDWG
  2. ;;;FUNCTION: Save current drawing as a new one with user specified name and
  3. ;;;          delete original drawing if successful.
  4. ;;;NOTES: Current drawing should have been saved prior to using this command.
  5. ;;;By: Kelie Feng, April 2006.
  6. ;;;Revised C.Potter December 2023. (https://www.theswamp.org/index.php?topic=9696.0)
  7. ;;;  
  8. (defun GetString-dlg (str / dcl_id fn fo)
  9.   (setq fn (vl-filename-mktemp "" "" ".dcl"))
  10.   (setq fo (open fn "w"))
  11.   (setq ValueStr (strcat "value = \"" str "\";"))
  12.   (write-line "stringdlg : dialog {
  13.                            label = \"Enter New Filename\";" fo
  14.   )
  15.  
  16.   (write-line ": edit_box {
  17.              label = \"\";
  18.              edit_width = 30;
  19.              key = \"stringdlg\";
  20.                            is_default = true; " fo
  21.   )
  22.  
  23.   (write-line ValueStr fo)
  24.   (write-line "}" fo)
  25.   (write-line ": row {
  26.              alignment   = centered;
  27.              fixed_width = true;
  28.               : button {
  29.               label      = \"OK\";
  30.               key        = \"dcl_accept\";
  31.               width      = 10;
  32.               allow_accept = true;
  33.             }
  34.           }
  35.                    }" fo
  36.   )
  37.  
  38.   (close fo)
  39.   (setq dcl_id (load_dialog fn))
  40.   (new_dialog "stringdlg" dcl_id)
  41.   (action_tile "stringdlg" "(setq str $value)(done_dialog)")
  42.   (unload_dialog dcl_id)
  43.   str
  44. )
  45. (defun C:RENDWG1 (/ *ERROR* app saveAsType doc oPath oDwgName dir nDwgName nPath)
  46.         saveAsType (vla-get-saveastype
  47.                      (vla-get-opensave
  48.                        (vla-get-preferences app)
  49.                        )
  50.                      )
  51.         doc (vla-get-activedocument app)
  52.         oPath (vla-get-fullname doc)
  53.         oDwgName (vla-get-name doc)
  54.         dir (getvar "DWGPREFIX")
  55.         )
  56.   (setq nDwgName (getstring-dlg (strcase (acet-filename-ext-remove (getvar 'DWGNAME))))
  57.         nPath (strcat dir nDwgName ".dwg")
  58.         )
  59.   (if (or (not (findfile nPath))
  60.           (KF:Yes? nil
  61.                    (strcat "File \"" nDwgName ".dwg\"" " exists. Overwrite?")
  62.                    )
  63.           )
  64.     (if (not
  65.           (vl-catch-all-error-p
  66.             (vl-catch-all-apply
  67.               (function
  68.                 (lambda ()
  69.                   (vla-saveas doc nPath saveAsType)
  70.                   (vl-file-delete oPath)
  71.                   )
  72.                 )
  73.               )
  74.             )
  75.           )
  76.       (prompt "\nDone.")
  77.       (prompt "\nRename/delete failed.")
  78.       )
  79.     )
  80.   (princ)
  81.   )
  82.  
  83. (defun KF:Yes? (default msg / input)
  84.   (initget "Yes No")
  85.   (setq input (getkword (strcat "\n"
  86.                                 msg
  87.                                 " [Yes/No] <"
  88.                           (if default
  89.                             "Y"
  90.                             "N"
  91.                           )
  92.                                 ">: "
  93.                         )
  94.               )
  95.   )
  96.   (cond
  97.     ((= input "Yes") t)
  98.     ((= input "No") nil)
  99.     (t default)
  100.   )
  101. )
  102.  

dexus

  • Bull Frog
  • Posts: 210
Re: Challenge: Rename Drawing
« Reply #20 on: January 08, 2024, 02:22:27 AM »
Here is a version using powershell to create a saveFileDialog.
It falls back on getfiled when something doesn't work.
I should have spent less time on this, but I got a bit obsessed with getting a resizable file dialog.

Was not able to find copies of LM:acobj or LM:acdoc called in your program.
Can you share these?

Steve
Oops, I removed the dependencies. Could you give it another try?

CraigP

  • Mosquito
  • Posts: 12
Re: Challenge: Rename Drawing
« Reply #21 on: January 08, 2024, 05:51:19 PM »
Hi Dexus,

I get 2 dialogs when I run this version?
If I cancel the 2nd dialog then enter the new filename everything works as expected.



Here is a version using powershell to create a saveFileDialog.
It falls back on getfiled when something doesn't work.
I should have spent less time on this, but I got a bit obsessed with getting a resizable file dialog.

Was not able to find copies of LM:acobj or LM:acdoc called in your program.
Can you share these?

Steve
Oops, I removed the dependencies. Could you give it another try?

dexus

  • Bull Frog
  • Posts: 210
Re: Challenge: Rename Drawing
« Reply #22 on: January 12, 2024, 02:48:30 AM »
CraigP, looks like something works differently in Bricscad.
One of the checks will return false where Autocad returns true.
I'll have to do some testing, but I don't have it installed on this computer.