Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Batch_DGN2DWG

16 REPLIES 16
SOLVED
Reply
Message 1 of 17
aqdam1978
1395 Views, 16 Replies

Batch_DGN2DWG

Hi,

 

I want to convert my DGN files to DWG files.

So I write this code in LISP:

 

(defun C:Batch_DGN2DWG (/path dgnfile SDI_Mode)
 (setq 
   path "C:\\DGN\\"   ;;Default path for DGN files location.
   DGNIMPORTMODE 0    ;;Imports the DGN file in a new drawing file.
   SDI_Mode SDI       ;;Save Current SDI MODE 
   SDI 1              ;;SET Single Drawing Mode for AutoCAD.
 );;setq

 (foreach dgnfile (vl-directory-files path "*.DGN" 1)
  (progn
    (command "-dgnimport" (strcat path dgnfile) "" "" "" "y" (strcat path dgnfile ".DWG"))
    (delay 100)
   );;progn
 );;foreach

 (setq SDI SDI_Mode) ;;restrore SDI Mode to previous value.

)

 but I can't finish it! does anybody can help me please?

I attached some DGN files for test purpose.

 

Thanks,

16 REPLIES 16
Message 2 of 17
aqdam1978
in reply to: aqdam1978

34 views and no comment?!

WHY!!

nobody can't help me?

Message 3 of 17
Alfred.NESWADBA
in reply to: aqdam1978

Hi,

 

There is no description of what happens (not) on your system. Also there is no AutoCAD-version given (and if you are using a vertical product like Mech or P3D or ...), that is important as DGN-import may vary then how it works.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 4 of 17
dicra
in reply to: aqdam1978


@aqdam1978 wrote:

Hi,

 

I want to convert my DGN files to DWG files.

So I write this code in LISP:

 

(defun C:Batch_DGN2DWG (/path dgnfile SDI_Mode)

 


I have never used any of .dgn files, so don't know a lot about it,

but you suppose to put space between slash "/" and path in the list of local variables...

(/ path dgnfile SDI_Mode)

Message 5 of 17
hmsilva
in reply to: aqdam1978

aqdam1978,
I think you can not set a system variable with "setq" you need to use "setvar" or "command", so I think you should change the start of your code, the part where you make the collection of the current variables and setting the new values, the end part where restores the original variables.
perhaps something like

 

(defun c:Batch_DGN2DWG (/ path old_dgnIPM dgnfile SDI_Mode)
(setq path	 "C:\\DGN\\" ;;Default path for DGN files location.
      old_dgnIPM (getvar "DGNIMPORTMODE");; save the current DGNIMPORTMODE
      SDI_Mode	 (getvar "SDI");;Save Current SDI MODE 
)
(setvar "DGNIMPORTMODE" 0);;Imports the DGN file in a new drawing file.
(setvar "SDI" 1);;SET Single Drawing Mode for AutoCAD

(foreach ...

...)

 (setvar "DGNIMPORTMODE" old_dgnIPM) ;;restrore DGNIMPORTMODE to previous value.
 (setvar "SDI" SDI_Mode) ;;restrore SDI Mode to previous value.
)

 

as to the main part of your code, I can not be much help, unfortunately the AutoCAD I have at home is a version that I can not test some variables, is old like me, but I think when you sets SDI to 1 the code should stop at the first dng to be imported, it's just a hunch, I have no way to test, and I've never worked with DGN's.

 

 

EDITED

 

Perhaps using a script would be easier...

 

Henrique

EESignature

Message 6 of 17
aqdam1978
in reply to: Alfred.NESWADBA

Hi alfred.neswadba,

 

Thank you for your comment,

I updated my code due to "hmsilva" comments, so the final code is:

 

(defun C:Batch_DGN2DWG (/ path dgnfile DGN_Mode SDI_Mode)
 (setq 
   path "C:\\DGN\\"                 ;;Default path for DGN files location.
   DGN_Mode (getvar "DGNIMPORTMODE");;Imports the DGN file in a new drawing file.
   SDI_Mode (getvar "SDI")          ;;Save Current SDI MODE 
 );;setq

 (setvar "Filedia" 0)               ;;No visual dialogue box
 (setvar "DGNIMPORTMODE" 0)         ;;Imports the DGN file in a new drawing file.
 (setvar "SDI" 0)                   ;;SET Multi Drawing Mode for AutoCAD 

 (foreach dgnfile (vl-directory-files path "*.DGN" 1)
  (progn
    (command "-dgnimport" (strcat path dgnfile) "" "" "")
    (command "save" (strcat path dgnfile ".DWG") "y")
  ;;(command "close" "y")
   );;progn
 );;foreach

 (setvar "DGNIMPORTMODE" DGN_Mode) ;;restrore DGNIMPORTMODE to previous value. 
 (setvar "SDI" SDI_Mode)           ;;restrore SDI Mode to previous value.
 (setvar "Filedia" 1)
)

 this code works but there is a problem:

when autocad opens a new drawing to import next dgn file, LISP stopping, because this LISP program does not loaded in new drawing, so I should click on the first autocad icon on the taskbar to switch and view the first autocad that has loaded lisp program, and then Lisp program continues and imported next dgn file.

so for each dgn file, I should click on the first autocad icon on the taskbar to complete all dgn files converted.

 

and the 2nd problem is: there are so many opened autocad icons on the taskbar and I should close them!

 

I think there is no solution via LISP programming, and I should prepare a script file and load it in autocad

I attached some DGN files in my first post, if you like you can test it!

sorry for my poor english.

my system is: Autocad 2013 32bit on Windows 8 32bit

 

THANKS,

 

 

Message 7 of 17
hmsilva
in reply to: aqdam1978

aqdam1978 wrote:

...

and the 2nd problem is: there are so many opened autocad icons on the taskbar and I should close them!

...

 

try replacing

 

(command "close" "y")

 

by

 

(command "Quit")

 

Henrique

EESignature

Message 8 of 17
aqdam1978
in reply to: hmsilva

hi hmsilva,

 

I decided to use scrip for implementing my code as of your first suggestion to me!

so, I prepare this code:

 

;; DGN to DWG Batch conversion ;;
(defun C:Batch_DGN2DWG (/ path dgnfile DGN_Mode SDI_Mode des tmp)
(vl-load-com)
 (setq 
   path "C:\\DGN\\"                 ;;Default path for DGN files location.
   DGN_Mode (getvar "DGNIMPORTMODE");;Save Current DGNIMPORTMODE value
   SDI_Mode (getvar "SDI")          ;;Save Current SDI value 
 );;setq

(setq des (open (strcat path "t.scr") "w" ))
(write-line "filedia" des)
(write-line "0" des)
(write-line "DGNIMPORTMODE" des)
(write-line "0" des)
(write-line "SDI" des)
(write-line "1" des)

 (foreach dgnfile (vl-directory-files path "*.DGN" 1)
  (progn
   (write-line "-dgnimport" des)
   (write-line (strcat path dgnfile) des)
   (write-line "" des)
   (write-line "" des)
   (write-line "" des)
   (write-line "qsave" des)
   (setq tmp (strcat path dgnfile ".DWG"))
   (write-line tmp des)
   (if (findfile tmp) (write-line "y" des))    ;;overwrite? Y
   (write-line "delay" des)
   (write-line "100" des)
   );;progn
 );;foreach

(write-line "filedia" des)
(write-line "1" des)
(write-line "DGNIMPORTMODE" des);;restore DGNIMPORTMODE value
(write-line (rtos DGN_Mode 2 0) des)
(write-line "SDI" des)          ;;restore SDI value
(write-line (rtos SDI_Mode 2 0) des)
(close des)                     ;;closed script files
(command "script" (strcat path "t.scr"))
;;(vl-file-delete (strcat "t.scr"))

)

 it works good, excellent!,

BUT!, when prosess is done, I can't close and exit AutoCAD and I should "End process" acad.exe!

 

So, can you guess what is the problem?

 

THANKS

 

Abbas

 

Message 9 of 17
hmsilva
in reply to: aqdam1978

Abbas,

you have to add after

 

(write-line "100" des)
(write-line "quit" des)
);;progn

 

 

Henrique

 

EESignature

Message 10 of 17
hmsilva
in reply to: aqdam1978

Abbas wrote:

...

BUT!, when prosess is done, I can't close and exit AutoCAD and I should "End process" acad.exe!

...


Just a different approach, do you really need to set SDI to 1?

If not, I think it worked better if just add a "close" in the end of the "foreach", and a "quit" after you restore the DGNIMPORTMODE value.

 

Henrique

EESignature

Message 11 of 17
aqdam1978
in reply to: hmsilva

Hi Henrique,

 

Thanks for your ideas,

YES, I should use SDI=1 because in multi drawing mode if i use "quit" or "close" or "exit"... script running is stop!

 

I updated my code but the problems is still...

 

;; DGN to DWG Batch conversion ;;
(defun C:Batch_DGN2DWG (/ path dgnfile DGN_Mode SDI_Mode des tmp)
(vl-load-com)
 (setq 
   path "C:\\DGN\\"                 ;;Default path for DGN files location.
   DGN_Mode (getvar "DGNIMPORTMODE");;Save Current DGNIMPORTMODE value
   SDI_Mode (getvar "SDI")          ;;Save Current SDI value 
 );;setq
(if (/= (getvar "DBMOD") 0) (command "qsave" "~"))  ;;does current drawing is saved?

(setq des (open (strcat path "t.scr") "w" ))
(write-line "filedia" des)
(write-line "0" des)
(write-line "DGNIMPORTMODE" des)
(write-line "0" des)
(write-line "SDI" des)
(write-line "1" des)

 (foreach dgnfile (vl-directory-files path "*.DGN" 1)
  (progn
   (write-line "-dgnimport" des)
   (write-line (strcat path dgnfile) des)
   (write-line "" des)
   (write-line "" des)
   (write-line "" des)
   (write-line "qsave" des)
   (setq tmp (strcat path dgnfile ".DWG"))
   (write-line tmp des)
   (if (findfile tmp) (write-line "y" des))    ;;overwrite? Y
   (write-line "delay" des) 
   (write-line "100" des)   
   );;progn
 );;foreach

(write-line "filedia" des)
(write-line "1" des)
(write-line "DGNIMPORTMODE" des);;restore DGNIMPORTMODE value
(write-line (rtos DGN_Mode 2 0) des)
(write-line "SDI" des)          ;;restore SDI value
(write-line (rtos SDI_Mode 2 0) des)
(write-line "(alert \"All files converted successfully.\")" des)
(write-line (strcat "(vl-file-delete \"" path "t.scr\")") des) ;;delete created scr file
(close des)
(command "script" (strcat path "t.scr"))
)

 

I 'm wondering why autocad does not response and needs to "End Process"!

 

 

 

 

 

Message 12 of 17
hmsilva
in reply to: aqdam1978

 aqdam,
with these small changes AutoCAD closes at the end of the script

 

;; DGN to DWG Batch conversion ;;
(defun C:Batch_DGN2DWG (/ path dgnfile DGN_Mode SDI_Mode des tmp)
(vl-load-com)
 (setq 
   path "C:\\DGN\\"                 ;;Default path for DGN files location.
   DGN_Mode (getvar "DGNIMPORTMODE");;Save Current DGNIMPORTMODE value
   SDI_Mode (getvar "SDI")          ;;Save Current SDI value 
 );;setq
(if (/= (getvar "DBMOD") 0) (command "qsave" "~"))  ;;does current drawing is saved?

(setq des (open (strcat path "t.scr") "w" ))
(write-line "filedia" des)
(write-line "0" des)
(write-line "DGNIMPORTMODE" des)
(write-line "0" des)
(write-line "SDI" des)
(write-line "1" des)

 (foreach dgnfile (vl-directory-files path "*.DGN" 1)
  (progn
   (write-line "-dgnimport" des)
   (write-line (strcat path dgnfile) des)
   (write-line "" des)
   (write-line "" des)
   (write-line "" des)
   (write-line "qsave" des)
   (setq tmp (strcat path dgnfile ".DWG"))
   (write-line tmp des)
   (if (findfile tmp) (write-line "y" des))    ;;overwrite? Y
   (write-line "delay" des) 
   (write-line "100" des)   
   );;progn
 );;foreach

(write-line "filedia" des)
(write-line "1" des)
(write-line "DGNIMPORTMODE" des);;restore DGNIMPORTMODE value
(write-line (rtos DGN_Mode 2 0) des)
(write-line "SDI" des)          ;;restore SDI value
(write-line (rtos SDI_Mode 2 0) des)
(write-line "(alert \"All files converted successfully.\")" des)
(write-line "Quit" des);; added
(write-line "n" des);; added
;;(write-line (strcat "(vl-file-delete \"" path "t.scr\")") des) ;;delete created scr file - commented - I think it is not possible to delete script
(close des)
(command "script" (strcat path "t.scr"))
)

 

the dwg's files are renamed xxxx.dgn.dwg, if your interest is to becoming xxxx.dwg, you can use something like

 

 

(setq dgnfile (vl-string-translate ".dgn" ".dwg" dgnfile))
(setq tmp (strcat path dgnfile))

 

Cheers
Henrique

 

EESignature

Message 13 of 17
aqdam1978
in reply to: hmsilva

Hi henrique,

 

Thanks for your tips...

YES, AutoCAD closes normally!

 

but! I have two more question!:

 

1- how can I delete the created scr file (t.scr)?

2- how can I show some text when program loaded in autocad?

please see the last lines:

 

;; DGN to DWG Batch conversion ;;
;; written by abbas aqdam ( aqdam1978@yahoo.com )
;; special thanks to hmsilva


(defun C:Batch_DGN2DWG (/ path dgnfile DGN_Mode SDI_Mode des tmp)
(vl-load-com)
 (setq 
   path "C:\\DGN\\"                 ;;Default path for DGN files location.
   DGN_Mode (getvar "DGNIMPORTMODE");;Save Current DGNIMPORTMODE value
   SDI_Mode (getvar "SDI")          ;;Save Current SDI value 
 );;setq
(if (/= (getvar "DBMOD") 0) (command "qsave" "~"))  ;;does current drawing is saved?

(setq des (open (strcat path "t.scr") "w" ))
(write-line "filedia" des)
(write-line "0" des)
(write-line "DGNIMPORTMODE" des)
(write-line "0" des)
(write-line "SDI" des)
(write-line "1" des)

 (foreach dgnfile (vl-directory-files path "*.DGN" 1)
  (progn
   (write-line "-dgnimport" des)
   (write-line (strcat path dgnfile) des)
   (write-line "" des)
   (write-line "" des)
   (write-line "" des)
   (write-line "qsave" des)
   (setq dgnfile (vl-string-translate ".dgn" ".dwg" dgnfile))
   (setq tmp (strcat path dgnfile))  
   (write-line tmp des)
   (if (findfile tmp) (write-line "y" des))    ;;overwrite? Y
   (write-line "delay" des) 
   (write-line "100" des)   
   );;progn
 );;foreach

(write-line "filedia" des)
(write-line "1" des)
(write-line "DGNIMPORTMODE" des);;restore DGNIMPORTMODE value
(write-line (rtos DGN_Mode 2 0) des)
(write-line "SDI" des)          ;;restore SDI value
(write-line (rtos SDI_Mode 2 0) des)
(write-line "(alert \"All files converted successfully.\")" des)
(write-line "Quit" des);; added
(write-line "n" des);; added
(write-line (strcat "(vl-file-delete \"" path "t.scr\")") des) ;;how can I delete the created scr file?
(close des)
;;(command "script" (strcat path "t.scr"));; <== I ignored now because of test of other issues...

(princ "\n:: Batch_DGN2DWG.lsp | Version 0.1 |  ::") ;;how can I show these two texts in command prompt window after loading?
(princ "\n:: Type \"Batch_DGN2DWG\" to Invoke ::")
(princ)
)

 

Message 14 of 17
hmsilva
in reply to: aqdam1978

aqdam wrote:
...

1- how can I delete the created scr file (t.scr)?

2- how can I show some text when program loaded in autocad?

...

1 - I have tried to delete the script, but without success, I'll keep trying...
2 -

 

;; DGN to DWG Batch conversion ;;
;; written by abbas aqdam ( aqdam1978@yahoo.com )
;; special thanks to hmsilva


(defun C:Batch_DGN2DWG (/ path dgnfile DGN_Mode SDI_Mode des tmp)
(vl-load-com)
 (setq 
   path "C:\\DGN\\"                 ;;Default path for DGN files location.
   DGN_Mode (getvar "DGNIMPORTMODE");;Save Current DGNIMPORTMODE value
   SDI_Mode (getvar "SDI")          ;;Save Current SDI value 
 );;setq
(if (/= (getvar "DBMOD") 0) (command "qsave" "~"))  ;;does current drawing is saved?

(setq des (open (strcat path "t.scr") "w" ))
(write-line "filedia" des)
(write-line "0" des)
(write-line "DGNIMPORTMODE" des)
(write-line "0" des)
(write-line "SDI" des)
(write-line "1" des)

 (foreach dgnfile (vl-directory-files path "*.DGN" 1)
  (progn
   (write-line "-dgnimport" des)
   (write-line (strcat path dgnfile) des)
   (write-line "" des)
   (write-line "" des)
   (write-line "" des)
   (write-line "qsave" des)
   (setq dgnfile (vl-string-translate ".dgn" ".dwg" dgnfile))
   (setq tmp (strcat path dgnfile))  
   (write-line tmp des)
   (if (findfile tmp) (write-line "y" des))    ;;overwrite? Y
   (write-line "delay" des) 
   (write-line "100" des)   
   );;progn
 );;foreach

(write-line "filedia" des)
(write-line "1" des)
(write-line "DGNIMPORTMODE" des);;restore DGNIMPORTMODE value
(write-line (rtos DGN_Mode 2 0) des)
(write-line "SDI" des)          ;;restore SDI value
(write-line (rtos SDI_Mode 2 0) des)
(write-line "(alert \"All files converted successfully.\")" des)
(write-line "Quit" des);; added
(write-line "n" des);; added
(write-line (strcat "(vl-file-delete \"" path "t.scr\")") des) ;;how can I delete the created scr file?
(close des)
;;(command "script" (strcat path "t.scr"));; <== I ignored now because of test of other issues...
(princ)
)
(princ "\n:: Batch_DGN2DWG.lsp | Version 0.1 |  ::") ;;how can I show these two texts in command prompt window after loading?
(princ "\n:: Type \"Batch_DGN2DWG\" to Invoke ::")

 

Glad I could help

Henrique

EESignature

Message 15 of 17
hmsilva
in reply to: hmsilva

aqdam,
maybe someone has a more correct way to eliminate the script file, but so far, I only can erase the scr using a work around...
I created a second lisp and you must save it in a support directory. I named the file delscr.lsp
I made some changes in the batch to not close the AutoCAD and to load the lisp and run it.
Until now is the only way I could delete the script...

 

;; Delscr.lsp
(defun delscr (/ scrpath)
  (setq scrpath (getvar "DWGPREFIX"))
  (if (findfile (strcat scrpath "t.scr"))
    (progn
      (command "delay" 100)
      (vl-file-delete (strcat scrpath "t.scr"))
      (command "_Quit" "_N")
    )
  )
  (princ)
)



;; DGN to DWG Batch conversion ;;
;; written by abbas aqdam ( aqdam1978@yahoo.com )
;; special thanks to hmsilva


(defun C:Batch_DGN2DWG (/ path dgnfile DGN_Mode SDI_Mode des tmp)
(vl-load-com)
 (setq 
   path "C:\\DGN\\"                 ;;Default path for DGN files location.
   DGN_Mode (getvar "DGNIMPORTMODE");;Save Current DGNIMPORTMODE value
   SDI_Mode (getvar "SDI")          ;;Save Current SDI value 
 );;setq
(if (/= (getvar "DBMOD") 0) (command "qsave" "~"))  ;;does current drawing is saved?

(setq des (open (strcat path "t.scr") "w" ))
(write-line "filedia" des)
(write-line "0" des)
(write-line "DGNIMPORTMODE" des)
(write-line "0" des)
(write-line "SDI" des)
(write-line "1" des)

 (foreach dgnfile (vl-directory-files path "*.DGN" 1)
  (progn
   (write-line "-dgnimport" des)
   (write-line (strcat path dgnfile) des)
   (write-line "" des)
   (write-line "" des)
   (write-line "" des)
   (write-line "qsave" des)
   (setq dgnfile (vl-string-translate ".dgn" ".dwg" dgnfile))
   (setq tmp (strcat path dgnfile))  
   (write-line tmp des)
   (if (findfile tmp) (write-line "y" des))    ;;overwrite? Y
   (write-line "delay" des) 
   (write-line "100" des)   
   );;progn
 );;foreach

(write-line "filedia" des)
(write-line "1" des)
(write-line "DGNIMPORTMODE" des);;restore DGNIMPORTMODE value
(write-line (rtos DGN_Mode 2 0) des)
(write-line "SDI" des)          ;;restore SDI value
(write-line (rtos SDI_Mode 2 0) des)
(write-line "(alert \"All files converted successfully.\")" des)
(write-line "(load \"delscr\")" des) ;;loads delscr
(write-line "(delscr)" des);run delscr
(close des)
(command "script" (strcat path "t.scr"));; <== I ignored now because of test of other issues...
(princ)
)
(princ "\n:: Batch_DGN2DWG.lsp | Version 0.1 |  ::") 
(princ "\n:: Type \"Batch_DGN2DWG\" to Invoke ::")

 

 

Hope that helps

Henrique

EESignature

Message 16 of 17
aqdam1978
in reply to: hmsilva

Hi Henrique.,

 

Thank you so much.

 

 

 

Message 17 of 17
hmsilva
in reply to: aqdam1978

You're welcome,aqdam

glad I could help

 

Henrique

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost