Visual LISP, AutoLISP and General Customization

Reply
Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 1 of 17 (338 Views)
Accepted Solution

Batch_DGN2DWG

338 Views, 16 Replies
02-08-2013 07:58 AM

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,

Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 2 of 17 (303 Views)

Re: Batch_DGN2DWG

02-10-2013 06:33 AM in reply to: aqdam1978

34 views and no comment?!

WHY!!

nobody can't help me?

*Expert Elite*
Alfred.NESWADBA
Posts: 8,865
Registered: ‎06-29-2007
Message 3 of 17 (297 Views)

Re: Batch_DGN2DWG

02-10-2013 06:59 AM 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
-------------------------------------------------------------------------
Valued Contributor
dicra
Posts: 63
Registered: ‎02-20-2011
Message 4 of 17 (290 Views)

Re: Batch_DGN2DWG

02-10-2013 07:57 AM 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)

*Expert Elite*
hmsilva
Posts: 2,639
Registered: ‎12-17-2004
Message 5 of 17 (282 Views)

Re: Batch_DGN2DWG

02-10-2013 08:43 AM 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

Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 6 of 17 (265 Views)

Re: Batch_DGN2DWG

02-10-2013 12:05 PM 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,

 

 

*Expert Elite*
hmsilva
Posts: 2,639
Registered: ‎12-17-2004
Message 7 of 17 (259 Views)

Re: Batch_DGN2DWG

02-10-2013 12:36 PM 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

Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 8 of 17 (248 Views)

Re: Batch_DGN2DWG

02-10-2013 03:01 PM 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

 

*Expert Elite*
hmsilva
Posts: 2,639
Registered: ‎12-17-2004
Message 9 of 17 (245 Views)

Re: Batch_DGN2DWG

02-10-2013 03:16 PM in reply to: aqdam1978

Abbas,

you have to add after

 

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

 

 

Henrique

 

*Expert Elite*
hmsilva
Posts: 2,639
Registered: ‎12-17-2004
Message 10 of 17 (236 Views)

Re: Batch_DGN2DWG

02-10-2013 03:54 PM 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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community