LISP to save the current (active) drawing as DWG-2018 in project directory structure

LISP to save the current (active) drawing as DWG-2018 in project directory structure

bigcarl5000kg
Advisor Advisor
828 Views
10 Replies
Message 1 of 11

LISP to save the current (active) drawing as DWG-2018 in project directory structure

bigcarl5000kg
Advisor
Advisor

Dear community,

 

please, can someone create a LISP for saving current DWG in DWG-2018 format to a Project subdirectory (automatic, without path selection). I work always with the same Project directory structure (eg. Project1, Project2) locally or on the server. Each Project directory have always the same subdirectories structure:

 

Project1

 - BOM

 - DWG2018

 - Mechanical

 - PDF

 

Project2

 - BOM

 - DWG2018

 - Mechanical

 - PDF

 

bigcarl5000kg_0-1687435355663.png

My DWG (source file) for each Project directory is located in the corresponding subdirectory "Mechanical". The DWG-2018 must be saved in the related subdirectory "DWG2018" (Project directory). It may be that subdirectory "DWG2018" has not been created yet, the LISP should check that and possibly create a new one, before saving as DWG-2018.

 

Many thanks for your help in advance

 

 

 

 

+++ impossible immediately and miracles within 3 days +++
+++ the only constant is the change +++ stay tuned for more +++
+++ since 03/2023 is Advance Steel in maintenance mode, no further development +++
0 Likes
Accepted solutions (1)
829 Views
10 Replies
Replies (10)
Message 2 of 11

Moshe-A
Mentor
Mentor

@bigcarl5000kg  hi,

 

We do not really understand why you are in need of such a command but this is my version called DWG18

the goal here is to save the current to another folder but stay in the current drawing - yes?!

 

the obvious is to use the SAVE command but it has no option to choose the format like SAVEAS and i could not find a way to change the dwg format so i choose to use -EXPORTTOAUTOCAD command. this command is meant to help vertical  product of AutoCAD (like architectural\mechanical) to save their drawing as standard vanila version. i do not have much experience with it and one drawback i already see, it does not save a thumbnail preview. so you need to be aware for other drawback if exist 😀

 

the program maintain AutoCAD backup policy.

first save, a dwg is created. next save, the dwg become a bak and a new dwg saved. next save, the bak is deleted and so on...

 

enjoy

Moshe

 

 

(defun c:dwg18 (/ root_folder _dwgfile _bakfile _savefile  ;|local functions|;  rf)

 (defun root_folder (/ dwgprefix)
  (setq dwgprefix (getvar "dwgprefix")) 
  (substr dwgPrefix 1 (setq p (vl-string-position (ascii "\\") dwgprefix 4)))
 ); root_folder

 (setq _dwgfile  (lambda () (strcat rf "\\dwg2018\\" (getvar "dwgname")))) 
 (setq _bakfile  (lambda () (strcat rf "\\dwg2018\\" (vl-filename-base (getvar "dwgname")) ".bak")))
 (setq _savefile (lambda () (command "-exporttoautocad" "_format" "_2018" "" (_dwgfile)))) 
  
 ; here start c:dwg18
 (if (not (vl-some (function (lambda (fld) (eq (strcase fld) "DWG2018"))) (vl-directory-files (setq rf (root_folder)))))
  (mkdir (strcat rf "\\DWG2018"))
 )

 (cond
  ((findfile (_dwgfile))
   (cond 
    ((findfile (_bakfile))
     (vl-file-delete (_bakfile))
     (vl-file-rename (_dwgfile) (_bakfile))
     (_savefile)
    ); case
    ( t
     (vl-file-rename (_dwgfile) (_bakfile))
     (_savefile)
    ); case
   ); cond
  ); case
  ( t
   (_savefile)
  )
 ); cond

 (princ)
); c:dwg18

 

 

0 Likes
Message 3 of 11

bigcarl5000kg
Advisor
Advisor

Hi @Moshe-A,

 

thank you for your response. Yes, I really only need to do the "saveas" while staying in the current drawing. I use AutoCAD Mechanical 2024 (German version). Only for customer purposes, I need to save the data as Autocad 2018. I can get it semi-automatically eg. via command macro with the command "_+saveas", where is the option for the format (dwg, dwt, ...):

bigcarl5000kg_0-1687511148551.png

 

and for each format there is a version:

bigcarl5000kg_1-1687511195856.png

 

Naturally, I always have to specify the path to a Project structure (subdirectory) where it should be stored AND I would have to open the original DWG again. I don't know how to do it through the command macro, so I'll try it through LISP 👍. Nice work 😉 !

 

I really appreciate your help, have a nice day and weekend

 

 

+++ impossible immediately and miracles within 3 days +++
+++ the only constant is the change +++ stay tuned for more +++
+++ since 03/2023 is Advance Steel in maintenance mode, no further development +++
0 Likes
Message 4 of 11

bigcarl5000kg
Advisor
Advisor

I get an error: 

bigcarl5000kg_0-1687512834794.png

 

+++ impossible immediately and miracles within 3 days +++
+++ the only constant is the change +++ stay tuned for more +++
+++ since 03/2023 is Advance Steel in maintenance mode, no further development +++
0 Likes
Message 5 of 11

bigcarl5000kg
Advisor
Advisor

Isn't it a Linux command?

+++ impossible immediately and miracles within 3 days +++
+++ the only constant is the change +++ stay tuned for more +++
+++ since 03/2023 is Advance Steel in maintenance mode, no further development +++
0 Likes
Message 6 of 11

bigcarl5000kg
Advisor
Advisor

Hi @Moshe-A,

 

I have many problem with the LISP:

- I think MKDIR is VL-MKDIR (I found so much in the LISP manual)

- the directory DWG2018 will be created one level higher than it should be (one level higher as Mechanical directory with source DWG) ... this should be the same level

- unfortunately, no data is saved in DWG2018

+++ impossible immediately and miracles within 3 days +++
+++ the only constant is the change +++ stay tuned for more +++
+++ since 03/2023 is Advance Steel in maintenance mode, no further development +++
0 Likes
Message 7 of 11

Moshe-A
Mentor
Mentor

@bigcarl5000kg ,

 


@bigcarl5000kg wrote:

Hi @Moshe-A,

 

I have many problem with the LISP:

- I think MKDIR is VL-MKDIR (I found so much in the LISP manual)

- the directory DWG2018 will be created one level higher than it should be (one level higher as Mechanical directory with source DWG) ... this should be the same level

- unfortunately, no data is saved in DWG2018


about (mkdir) you are right, sorry my mistake. change it to (vl-mkdir)

the DWG2018 folder is created at the same level as Mechanical exactly as you wanted in your picture.

 

Moshe

 

 

 

 

0 Likes
Message 8 of 11

komondormrex
Mentor
Mentor
Accepted solution

hey Carl,

check the following

(defun c:save_dwg2018_dir (/ dwg_2018_directory_path)
	(if (null (findfile (setq dwg_2018_directory_path 
	  								(strcat (vl-filename-directory (vla-get-path (vla-get-activedocument (vlax-get-acad-object)))) 
											"\\DWG2018"
								 	)
				  		)
			  )
		)
		(acet-file-mkdir dwg_2018_directory_path)
	)
	(command "_.saveas"
			 ""
			 (strcat dwg_2018_directory_path
					"\\"
					(vl-filename-base (vla-get-name (vla-get-activedocument (vlax-get-acad-object))))
					".dwg"
			 )
	)
	(princ (vla-get-fullname (vla-get-activedocument (vlax-get-acad-object))))
	(princ)
)
Message 9 of 11

bigcarl5000kg
Advisor
Advisor

Hi @komondormrex,

 

many thanks for your solution, it works nice. Please can you still implement closing the new DWG 2018 file (after save as) and opening the source file from which DWG2018 was created?

 

Thank you in advance

+++ impossible immediately and miracles within 3 days +++
+++ the only constant is the change +++ stay tuned for more +++
+++ since 03/2023 is Advance Steel in maintenance mode, no further development +++
0 Likes
Message 10 of 11

Moshe-A
Mentor
Mentor

@bigcarl5000kg,

 

Good morning 😀

 

i made a minor fix but it did not change the essence of the command, it works exactly as you wanted.

 

Moshe

 

 

 

 

0 Likes
Message 11 of 11

komondormrex
Mentor
Mentor

you just need to save source file as dwg in dwg2018 folder but stay in source file?

 

 

 

(defun c:save_dwg2018_dir (/ dwg_2018_directory_path active_document_parent_folder saved_as_dwg active_document_fullname)
	(setq active_document_fullname (vla-get-fullname (vla-get-activedocument (vlax-get-acad-object))))
  	(if (null (findfile (setq dwg_2018_directory_path 
				(strcat (setq active_document_parent_folder (vl-filename-directory (vla-get-path (vla-get-activedocument (vlax-get-acad-object))))) 
					(if (= ":" (substr active_document_parent_folder (strlen active_document_parent_folder))) "\\" "")
					"DWG2018\\"
			 	)
	  		)
		  )
		)
		(vl-mkdir dwg_2018_directory_path)
	)
  	(vla-saveas (vla-get-activedocument (vlax-get-acad-object))
		    (setq saved_as_dwg (strcat dwg_2018_directory_path
				               (vl-filename-base (vla-get-name (vla-get-activedocument (vlax-get-acad-object))))
				               ".dwg"
	 	                       )
		    )
	  	    ac2018_dwg
	)
	(alert (strcat "Current document saved as \"" saved_as_dwg "\""))
  	(vla-open (vla-get-documents (vlax-get-acad-object)) active_document_fullname)
  	(command "_.close")
	(princ)
)

 

 

0 Likes