Changing Layer Status and saving part of the drawings as DXF format

Changing Layer Status and saving part of the drawings as DXF format

kadir.oezen
Explorer Explorer
245 Views
2 Replies
Message 1 of 3

Changing Layer Status and saving part of the drawings as DXF format

kadir.oezen
Explorer
Explorer

Hello Everyone


At the moment we are using this workflow as some part of daily usage.

I would like to improve and automate as much as possible.

The sequence is as follows.

We work with 40 layers. Most of them are measurements and such things.

We export the Layouts as PDF.

We change predefined Layerstatus and only enable 8 layers for the CNC.

We manually select the necessary part of the drawing and write it as Block (WBLOCK) as DXF2000 and R12 separately on 2 different folders on the server.

 

If possible i would like to optimize this layerstatus change, selection, naming, and saving this 2 times in 2 different folders with 2 different formats.

Would it be possible to achieve such a task?


For example,
File name 45001_002_ko.dwg
Drawings saved as PDF 45001_002.pdf in PDF Folder
We made selection and identical drawing saved as 2 different formats under 2 different orders
45001002.dxf (R12) in R12 folder
45001002.dxf (2000) in 2000 folder

 

 

Thank you. Best Regards

Kadir (AutoCAD 2024 LT)

0 Likes
246 Views
2 Replies
Replies (2)
Message 2 of 3

Sea-Haven
Mentor
Mentor

You have answered your own question, yes is the answer set all the layers etc then run plot code, then runa save code, then run save2 code. There is lots of plot code out there, this is just one example.

 

(COMMAND "-PLOT"  "Y"  "" "DWG To PDF"
	       "Iso full bleed A3 (420.00 x 297.00 MM)" "m" "LANDSCAPE"  "N"   "W"  "-6,-6" "807,560" "1=2"  "C"
	       "y" "Acad.ctb" "Y"	"n" "n" "n" pdfName "N" "y"
    )

 It is hard coded to suit our title block in a layout, the pdf filename is pdfname.

 

; check that pdf directory exists
(setq dwgpre (strcat (getvar "dwgprefix") "\pdf"))
(if (= (vl-file-directory-p dwgpre) nil)
(vl-mkdir dwgpre)
)

 

0 Likes
Message 3 of 3

kadir.oezen
Explorer
Explorer

I manage to write a code for my requirement with the help of the internet. You could find it and use it as template or improve upon it.  

;;;;;This AutoLisp is written by Kadir Özen. 08.08.2023
(defun c:R12 ( / *error* msg dwg_name dxf_name folder_r12 folder_2000 selected_entities mlist allelayers keyword layerName becken:ans ans selected_entitiesr12 selected_entities2000)
	(vl-load-com)
  
  (defun *error* ( msg )
  (setvar "filedia" 1)
  (setvar "cmdecho" oldcmdecho)
  (vlax-for allelayers (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
			(vla-put-layeron allelayers :vlax-true)
	)
        (and msg (not (wcmatch (strcase msg) "*CANCEL*,*QUIT*,*BREAK*"))
        (princ (strcat "\nError: " msg))
        )
  (princ)
  )
  (setq oldcmdecho (getvar "cmdecho") )
	(setq dwg_name (getvar "dwgname")) ; Get the current DWG file name

	(setq base_name (vl-filename-base dwg_name)) ; Extract the base name without the extension

	(setq dxf_name (substr base_name 1 5)) ; Extract the first 5 characters from the base name
  
	(setq dxf_name (strcat dxf_name (substr base_name 7 3))) ; Append characters 7, 8, and 9 from the base name
  
	(setq folder_r12 "C:\\Users\\kadir.oezen\\Desktop\\AutoLISP\\R12\\") ; Set the R12 format destination folder path
	
	(setq folder_2000 "C:\\Users\\kadir.oezen\\Desktop\\AutoLISP\\2000\\") ; Set the 2000 format destination folder path
	(setvar "cmdecho" 1)
  (if (null becken:ans)
    (setq becken:ans "Nein"))
	(setq mlist '("CNC"
"SEQ"
"CUT"
"DRILL"
"POLISH"
"WJ"
"Ausschnitt"
"Bemassung Maschine"
"Nachschneiden"
"Granit"
)
); set CNC Maschine Readable Layer names and keywords,
	
	(if (and (vl-file-directory-p folder_r12) (vl-file-directory-p folder_2000)) ;if both folders are present continue otherwise go below
		(if (and (or 	(not(findfile (strcat folder_r12 dxf_name ".dxf"))) ;;no oldfile found
				(vl-file-delete (strcat folder_r12 dxf_name ".dxf"))) ;;old file found and deleted
				(or 	(not(findfile (strcat folder_2000 dxf_name ".dxf")))  ;;no oldfile found
				(vl-file-delete (strcat folder_2000 dxf_name ".dxf"))) ;;old file found and deleted
			);Folders are present and they either have no DXF or DXF with the same name inside

			(progn
        (vlax-for allelayers (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
        (vla-put-layeron allelayers :vlax-false);vlax command takes all layers and turns them off one by one
        (setq layerName (vla-get-name allelayers));set layername to the all layers
          ; Check if any of the keywords are present in the layer name
          (foreach keyword mlist
            (if (vl-string-search keyword layerName)
            (vla-put-layeron allelayers :vlax-true) ; if the name is on the list Turn on the layer there is no else command.
            )
          )
        )
            (initget "Ja Nein")
            (if (setq ans (cond ((getkword (strcat "\nHaben Sie SteinBecken/Segmenten in die Zeichnung (SIE DÜRFEN NICHT IN 2000 GESPEICHERT WERDEN? [Ja/Nein] <" becken:ans ">"))) (becken:ans)));cond testes if first getkword has value, if not goes to second option which is global ans
                        (progn
                        (setq becken:ans ans)
                          (if (= ans "Ja")
                            (progn
                              (alert "Wählen Sie nur R12-Daten (alles)")
                              (setq selected_entitiesr12 (ssget)) ;ask user to make selections
                                  (if selected_entitiesr12
                                    (progn
                                      (setvar "filedia" 0)
                                          ; Save the DXF file with DXF R12 format
                                          (command "SICHALS" "d" "v" "r12" "o" selected_entitiesr12 "" "16" (strcat folder_r12 dxf_name ".dxf"))
                                          (princ (strcat dxf_name ".dxf als R12 ist erfolgreich gespeichert."))
                                    );if selr12 else
                                      (progn
                                        (alert "Programm gestoppt, Auswahl ist nicht gültig, versuchen Sie es erneut")
                                        (*error* " nicht gültig auswahl r12")
                                      )
                                  );--if selr12 ende
                                (alert "Wählen Sie nur 2000-Daten (OHNE SEGMENT BECKEN etc.)")
                                (setq selected_entities2000 (ssget)) ;ask user to make selections
                                  (if selected_entities2000
                                    (progn
                                          ; Save the DXF file with DXF 2000 format
                                          (command "SICHALS" "d" "v" "2000" "o" selected_entities2000 "" "16" (strcat folder_2000 dxf_name ".dxf"))
                                          (princ (strcat dxf_name ".dxf als 2000 ist erfolgreich gespeichert."))
                                    );if sel2000 else
                                    (progn
                                      (alert "Programm gestoppt, Auswahl ist nicht gültig, versuchen Sie es erneut")
                                      (*error* " nicht gültig auswahl 2000")
                                    )
                                   );--if sel2000 ende
                                (command "SICHALS" "2013" "" "j")
                                ;;(alert (strcat dxf_name ".dxf als R12 - 2000 ist erfolgreich gespeichert."))
                                (princ (strcat dxf_name ".dxf als R12 - 2000 ist erfolgreich gespeichert."))                          
                          );if becken ans nein go with the below code
                            (progn
                              (setq selected_entities (ssget)) ;ask user to make selections
                              (if selected_entities
                                (progn
                                  (setvar "filedia" 0)
                                  ; Save the DXF file with DXF R12 format
                                  (command "SICHALS" "d" "v" "r12" "o" selected_entities "" "16" (strcat folder_r12 dxf_name ".dxf"))
                                      
                                  ; Save the DXF file with DXF 2000 format
                                  (command "SICHALS" "d" "v" "2000" "o" selected_entities "" "16" (strcat folder_2000 dxf_name ".dxf"))
                                  (command "SICHALS" "2013" "" "j")
                                  ;;(alert (strcat dxf_name ".dxf als R12 - 2000 ist erfolgreich gespeichert."))
                                  (princ (strcat dxf_name ".dxf als R12 - 2000 ist erfolgreich gespeichert."))
                                ) ; if ss nicht gültig else
                                (progn
                                  (alert "Programm gestoppt, Auswahl ist nicht gültig, versuchen Sie es erneut")
                                  (*error* " nicht gültig auswahl r12-2000")
                                )
                              );--if ss nicht gültig ende 
                            )
                          )            
                          )
                      );if beckebans ende
      );if else command can not overwrite files it gives this error
      (progn
        (alert "Programm gestoppt, Datei kann nicht überschrieben werden. Schließen Sie die geöffneten Dateien und versuchen Sie es erneut")
        (*error* " kann nicht überschreiben")
      )
		);--second if else command goes to the false state, if there are no folders with the right name present on the filepath
		(progn
      (alert "Sie haben keinen Zugriff auf den Server. Stellen Sie sicher, dass Sie die Dateien auf T: lesen können.")
      (*error* " kein zugriff")
		)
	)
	;first if end
	(progn
	(*error* nil)
	)
)
0 Likes