Author Topic: Relative paths  (Read 4534 times)

0 Members and 1 Guest are viewing this topic.

PKENEWELL

  • Bull Frog
  • Posts: 317
Re: Relative paths
« Reply #15 on: October 20, 2020, 03:41:12 PM »
Here is a new version of the batch import routine, using some ideas from kpblc and adding my own.

I added in a check at the beginning to make sure the user is in a new, blank drawing. I also made a choice at the end to close AutoCAD completely, or just close the open drawing.

Code - Auto/Visual Lisp: [Select]
  1. (defun C:MicroStationToAutoCAD (/ acd doc *error* fil fn pth oecho odgn resp tx1 tx2 tx3 tx4 tx5)
  2.    
  3.    (defun *error* (msg)
  4.       (if (not (wcmatch (strcase msg T) "*break*,*cancel*,*quit*,*exit*"))
  5.          (princ (strcat "\nError: " msg "\n"))
  6.          (princ "\nProgram Aborted.\n")
  7.       )
  8.       (setvar "cmdecho" oecho)
  9.       (setvar "dgnimportmode" odgn)
  10.       (setvar "nomutt" omut)
  11.    )
  12.    
  13.    (setq fn "list.txt"
  14.          tx1 (findfile fn)
  15.          tx4 "Default"
  16.          oecho (getvar "cmdecho")
  17.          odgn (getvar "dgnimportmode")
  18.          omut (getvar "nomutt")
  19.          acd (vlax-get-acad-object)
  20.          doc (vla-get-activedocument acd)
  21.    )
  22.    (setvar "cmdecho" 0)
  23.    (setvar "dgnimportmode" 1)
  24.    
  25.    (if (not tx1)
  26.       (princ "\nList file not found! Check the support paths. ")
  27.       (progn
  28.          (setq pth (vl-filename-directory tx1)
  29.                tx2 (strcat pth "\\in\\")
  30.                tx3 (strcat pth "\\out\\")
  31.          )
  32.          (initget "Yes No")
  33.          (if (= (setq resp (getkword (strcat "\nFile List \"" fn "\" found. You should be in a new drawing to begin conversion. Proceed [Yes/No] <Yes>: "))) "No")
  34.             (setq pth nil)
  35.          )
  36.       )
  37.    )
  38.    (if pth
  39.       (progn
  40.          (setq fil (open tx1 "r")
  41.                tx5 (read-line fil)
  42.          )
  43.          (princ (strcat "\nConverting Files in " fn "> ="))
  44.          (setvar "nomutt" 1)
  45.          (while tx5
  46.             (if (and (wcmatch (strcase tx5) "*`.DGN")
  47.                      (findfile (strcat tx2 tx5))
  48.                 )
  49.                (progn
  50.                   (setq tx5 (vl-filename-base tx5))
  51.                   (command "._UNDO" "_MARK"
  52.                            "._-DGNIMPORT" (strcat tx2 tx5) tx4
  53.                   )
  54.                   (while (= (logand (getvar "cmdactive") 1) 1) (command ""))
  55.                   (vla-zoomall acd)
  56.                   (vla-saveas doc (strcat tx3 tx5 ".dwg") ac2013_dwg)
  57.                   (command "._UNDO" "_BACK")
  58.                   (if (= (logand (getvar "cmdactive") 1) 1)(Alert "Something Did not process correctly in this drawing.\nVerify the command sequence."))
  59.                   (princ "=")
  60.                )
  61.                (princ (strcat "\n file \"" tx5 "\" not found. File does not exist or filename is incorrect in \"" fn "\". Skipping file."))
  62.             )
  63.             (setq tx5 (read-line fil))
  64.          )
  65.          (close fil)
  66.          (princ " Conversion Completed! ")
  67.          (setvar "nomutt" omut)
  68.          (initget "Yes No")
  69.          (if (or (= (setq resp (getkword "\nDo you want to quit AutoCAD? [Yes/No] <Yes>: ")) "Yes")(= resp nil))
  70.            (command "._QUIT" "_Y")
  71.            (command "._CLOSE" "_y")
  72.          )
  73.       )
  74.    )
  75.    (setvar "cmdecho" oecho)
  76.    (setvar "dgnimportmode" odgn)
  77.    (princ)
  78. )
  79.  

EDIT: Added even more checks in the program, including testing if the file exists and is in fact a DGN file.
EDIT2: Changed response test to getkword from incorrect "" to correct nil.
« Last Edit: October 23, 2020, 01:52:33 PM by PKENEWELL »
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

like_citrus

  • Newt
  • Posts: 114
Re: Relative paths
« Reply #16 on: October 21, 2020, 12:03:36 AM »
Thanks very much PKENEWELL, this works now.
I think your previous shorter version works as well.

Long story, but the issue was with the text file, as I discovered having issues with the initial code, once working, then not. It took me a while to figure it out. I was using Excel to dump the drawing list data into the text file and AutoCAD didn't seem to accept it.
Initially I tried the latest code, that did not work, then after working out the issue, your code did work and the previous shorter version seems to work as well.

Sorry if this wasted your time, but it may be useful for someone else.

PKENEWELL

  • Bull Frog
  • Posts: 317
Re: Relative paths
« Reply #17 on: October 21, 2020, 09:22:36 AM »
Thanks very much PKENEWELL, this works now.
I think your previous shorter version works as well.

Long story, but the issue was with the text file, as I discovered having issues with the initial code, once working, then not. It took me a while to figure it out. I was using Excel to dump the drawing list data into the text file and AutoCAD didn't seem to accept it.
Initially I tried the latest code, that did not work, then after working out the issue, your code did work and the previous shorter version seems to work as well.

Sorry if this wasted your time, but it may be useful for someone else.

No Problem Like_Citrus.  :-D I enjoyed playing with the program and trying to make it better! There is still more that could be done, but I like how this works.

Examples of improvements:
  • Store the name of the file list and the paths in the registry.
  • Add a DCL configuration interface to change saved values for above.
  • Create folders if they don't exist.
  • Eliminate the file list and just use (vl-directory-files) to get the list of files to import.

I might look back on these and add features if I have time.
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

PKENEWELL

  • Bull Frog
  • Posts: 317
Re: Relative paths
« Reply #18 on: October 28, 2020, 11:28:49 AM »
Here is an alternative I just wrote that does not rely on a text file to list the files. It just searches the import path for DGN files and processes them.

Upon the first time starting, the program will ask for the Import and Output folders using a "Browse for Folder" dialog (thanks to Lee Mac's function). Then it saves them to the registry for future use. You can afterward use the "Configure" option at the "...Proceed? " prompt to change them if needed.

Before proceeding with the conversion, Make sure the Import folder you selected has the DGN files in it that you want to convert, and that you are currently in a NEW BLANK drawing. Then select "Yes" to proceed with the conversion. This uses all the default options of the DGNIMPORT comment so just update the command string if you need different options.

Any other Guru's out there - feel free to critique and note any improvements or issues with the code. There are still improvements that could be made, such as a DCL interface for the "Configure" option and the first start up. Note: I don't normally deal with Microstation files at all, so someone with a better understanding of the DGNIMPORT options could chime in and give some advisement.
« Last Edit: October 28, 2020, 11:34:15 AM by PKENEWELL »
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

like_citrus

  • Newt
  • Posts: 114
Re: Relative paths
« Reply #19 on: October 28, 2020, 09:33:16 PM »
Thanks for your efforts again. The code seems quite sophisticated. It does work as described and seems to have been thought through.

PKENEWELL

  • Bull Frog
  • Posts: 317
Re: Relative paths
« Reply #20 on: October 29, 2020, 02:09:06 PM »
Thanks Like_Citrus. It's better than the old method used before we had the benefit of Visual LISP added to AutoLISP. The original AutoLISP language did not have any methods for querying and manipulating files outside of the (findfile) and text file functions, nor had any ActiveX / COM access like it does now. There were some creative ways around those problems, but now it's much easier.
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt