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

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
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: 207
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: 207
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: 207
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.