Visual LISP, AutoLISP and General Customization

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

Re: Batch_DGN2DWG

02-10-2013 06:13 PM 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"!

 

 

 

 

 

*Expert Elite*
hmsilva
Posts: 2,648
Registered: ‎12-17-2004
Message 12 of 17 (179 Views)

Re: Batch_DGN2DWG

02-11-2013 02:09 AM 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

 

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

Re: Batch_DGN2DWG

02-11-2013 07:24 AM 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)
)

 

*Expert Elite*
hmsilva
Posts: 2,648
Registered: ‎12-17-2004
Message 14 of 17 (165 Views)

Re: Batch_DGN2DWG

02-11-2013 07:38 AM 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

*Expert Elite*
hmsilva
Posts: 2,648
Registered: ‎12-17-2004
Message 15 of 17 (155 Views)

Re: Batch_DGN2DWG

02-11-2013 09:18 AM 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

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

Re: Batch_DGN2DWG

02-11-2013 11:45 AM in reply to: hmsilva

Hi Henrique.,

 

Thank you so much.

 

 

 

*Expert Elite*
hmsilva
Posts: 2,648
Registered: ‎12-17-2004
Message 17 of 17 (143 Views)

Re: Batch_DGN2DWG

02-11-2013 12:16 PM in reply to: aqdam1978

You're welcome,aqdam

glad I could help

 

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