export a part of a drawing to dxf

export a part of a drawing to dxf

Hans_Knol
Advocate Advocate
4,587 Views
20 Replies
Message 1 of 21

export a part of a drawing to dxf

Hans_Knol
Advocate
Advocate

Hello, does somebody know how or have a lisproutine that give the posibility to export a part of the drawing (by selection and set the 0,0 point) to a dxf file, it will be used for a CNC machine so it must be an old 2004 dxf format file (if possible)

 

Hope te find it soon.

Hans Knol
0 Likes
Accepted solutions (2)
4,588 Views
20 Replies
Replies (20)
Message 2 of 21

Kent1Cooper
Consultant
Consultant

WBLOCK command.

Pick your base point and object(s), and the browse ... button:

Kent1Cooper_1-1675793432166.png

and in there, give it a name after you pull down the Files of type: list for the variety you want [it will supply the .dxf in the File name: slot]:

Kent1Cooper_0-1675793299580.png

 

Kent Cooper, AIA
Message 3 of 21

komondormrex
Mentor
Mentor

If programming the approach may be as following.

User select entities he wants to be exported in dxf format, routine calculates rectangular boundary of selected entities and draw a rectangle to visualize selection made. User then point origin point he wants to be an origin point of CS of created dwg document to which routine copies selected entities. Then newly created document saved and opened in another autocad document window to be saved as dxf version 2004. Down side is opening of another window. Otherwise it will do target goal just fine.

0 Likes
Message 4 of 21

Kent1Cooper
Consultant
Consultant

@komondormrex wrote:

.... Then newly created document saved and opened in another autocad document window to be saved as dxf version 2004. Down side is opening of another window. ....


It's not necessary to open the resulting drawing file to save it to DXF format, when WBLOCK can create it directly in that format.  See Message 2.

Kent Cooper, AIA
0 Likes
Message 5 of 21

komondormrex
Mentor
Mentor

i saw your message and i am pretty sure it would work... but it is much clicky imho and not as transparent to the user, so suggest just one another approach).  still the user would not have to open the drawing himself, the routine would do it for him.

and additionally wblock will retain source origin of CS, while TS wants it to be especially set.

0 Likes
Message 6 of 21

Kent1Cooper
Consultant
Consultant

@komondormrex wrote:

.... wblock will retain source origin of CS, while TS wants it to be especially set.


WBLOCK via the dialog box lets you pick the origin/base/insertion point.  And overall it doesn't seem any more complicated than your described process -- pick a base point, select objects, choose a format, give it a name [and those can be in any order], done.

Kent Cooper, AIA
0 Likes
Message 7 of 21

komondormrex
Mentor
Mentor


WBLOCK via the dialog box lets you pick the origin/base/insertion point.  


am i missed smth? wblock's generated dxf will be somehow a block and i am aware when inserted it will be inserted with that base point. but opened independently it will retain the source  origin of CS. correct me if i went wrong.

 

komondormrex_0-1676039525206.png

 

0 Likes
Message 8 of 21

Kent1Cooper
Consultant
Consultant

@komondormrex wrote:


... wblock's generated dxf will be somehow a block and i am aware when inserted it will be inserted with that base point. but opened independently it will retain the source  origin of CS. correct me if i went wrong.


It won't be a Block, but a drawing interchange file.  It will constitute or become the definition of a Block only if and when it is Inserted in some other drawing, in which case its internal Coordinate System will be irrelevant, so does it matter?

 

Yes, the Coordinate System 0,0 will remain where it was in relation to the exported pieces.  But back to your description:  "User then point origin point he wants to be an origin point of CS of created dwg document to which routine copies selected entities."  I don't see how that would change it, either.  By what methods, exactly, are you picturing the User would pick the origin point and the routine would copy the things to a new drawing?

 

Also, "Then newly created document saved and opened in another autocad document window to be saved as dxf version 2004."  If you are picturing that as being done within the same routine that exported things to a new drawing, that won't be possible, because an AutoLisp routine cannot start in one drawing and continue in another.  It would need to be done manually by the User, or a follow-up separate routine would need to be written, to be called for when in the new drawing.

 

@Hans_Knol :  If it's necessary to actually change the 0,0 origin in the exported drawing [does that matter to the CNC machine or something?], I suspect it will require either:

A.  Moving the selected objects from the desired origin to 0,0 in the source drawing before exporting them; or

B.  Defining the selected objects into a Block first, using the desired origin location as the base point, and then WBLOCKing out that Block definition [not the selected objects independent of it] to a new drawing/dxf file, in which case in the resulting new drawing the 0,0 origin will be where the selected base point was in the source drawing.

Kent Cooper, AIA
0 Likes
Message 9 of 21

hmsilva
Mentor
Mentor

@Hans_Knol wrote:

Hello, does somebody know how or have a lisproutine that give the posibility to export a part of the drawing (by selection and set the 0,0 point) to a dxf file, it will be used for a CNC machine so it must be an old 2004 dxf format file (if possible)

 

Hope te find it soon.


@Hans_Knol 

just as a demo.
Does not test for the existence of a file with the same name, and the dwf will be created in the same directory with the same dwg name...

(defun c:demo (/ *error* ss)
    (defun *error* (msg)
	(if ofs
	    (setenv "DefaultFormatForSave" ofs)
	)
	(cond ((not msg))
	      ((member msg '("Function cancelled" "quit / exit abort")))
	      ((princ (strcat "\n** Error: " msg " ** ")))
	)
	(princ)
    )
    (if	(setq ss (ssget))
	(progn
	    (setq ofs (getenv "DefaultFormatForSave"))
	    (setenv "DefaultFormatForSave" "25");; AutoCAD 2004 DXF
	    (command "_.wblock" (strcat (getvar "Dwgprefix") (vl-filename-base (getvar "dwgname"))".dxf") "" "" "0,0,0" SS "" "_.oops")
	    (setenv "DefaultFormatForSave" ofs)
	)
    )
    (princ)
)

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 10 of 21

CADaSchtroumpf
Advisor
Advisor

Or with DXFOUT

Quickly exemple

((lambda ( / ss pt)
	(setq ss (ssget))
	(setq pt (getpoint "\nBase point: "))
	(command "_.undo" "_mark")
	(command "_.move" ss "" pt '(0.0 0.0 0.0))
	(setvar "INSBASE" '(0.0 0.0 0.0))
	(command "_.DXFOUT" "" "_Objects" ss "" "_version" "LT2004" "16")
	(command "_.undo" "_back")
))
0 Likes
Message 11 of 21

Hans_Knol
Advocate
Advocate

is it possible to select the starting point in instead of using 0,0,0 in the lisp file?

 

Hans Knol
0 Likes
Message 12 of 21

Hans_Knol
Advocate
Advocate

is it possible to select the starting point in instead of using 0,0,0 in the lisp file?

Hans Knol
0 Likes
Message 13 of 21

hmsilva
Mentor
Mentor

@Hans_Knol wrote:

is it possible to select the starting point in instead of using 0,0,0 in the lisp file?


@CADaSchtroumpf code in  message #10 already do that.

 

Henrique

EESignature

0 Likes
Message 14 of 21

komondormrex
Mentor
Mentor
Accepted solution

@Kent1Cooper wrote:

If you are picturing that as being done within the same routine that exported things to a new drawing, that won't be possible, because an AutoLisp routine cannot start in one drawing and continue in another. 

sure i picturing namely that. won't want to reassure you about that, but autolisp for sure thing could and would have done it. wanna bet?

almost forgot. wblock command stands for write Block, no matter what format it will be saved to. you easily may insert either format.

0 Likes
Message 15 of 21

komondormrex
Mentor
Mentor
Accepted solution

Hey Hans,

here it goes. no checking on target files are opened in autocad thou.

 

 

;*******************************************************************************************************************************************

;	komondormrex, feb 2023

;*******************************************************************************************************************************************

(defun get_filename (extension / output_name filename)
	(setq output_name (strcat (vlax-get (vla-get-activedocument (vlax-get-acad-object)) 'path) "\\"
							  (vl-filename-base (vlax-get (vla-get-activedocument (vlax-get-acad-object)) 'name))
					  )
	)
	(if (type acet-ui-getfile)
			(setq filename (acet-ui-getfile (strcat "Enter " (strcase extension) " filename") output_name extension "" 1))
			(setq filename (getfiled "Enter CSV filename" output_name extension 1))
	)
	(if (= "" filename) nil filename)
)

;*******************************************************************************************************************************************

(defun create_dummy_dwg_file (/ dummy_dwg_filename acad_dbx_object)
	(setq dummy_dwg_filename (strcat (vla-get-tempfilepath (vla-get-files (vla-get-preferences (vlax-get-acad-object)))) "Dummy.dwg"))
	(if (findfile dummy_dwg_filename) (vl-file-delete dummy_dwg_filename))
	(setq acad_dbx_object (vlax-create-object (strcat "objectdbx.axdbdocument." (substr (getvar 'acadver) 1 2))))
	(vl-catch-all-apply 'vla-open (list acad_dbx_object dummy_dwg_filename))
	(vla-saveas acad_dbx_object dummy_dwg_filename)
	(vlax-release-object acad_dbx_object)
	dummy_dwg_filename
)

;*******************************************************************************************************************************************

(defun c:partial_dxf_export (/ odbx_to_objects_list llc_list ruc_list min_x min_y max_x max_y boundary_rectangle
							   selection_origin_point dxf_full_name dummy_full_name acad_dbx_object dxf_document
							   screen_size boundary_center vp_center aspect_ratio
							)
	(if (minusp (vlax-get (vla-get-system (vla-get-preferences (vlax-get-acad-object))) 'singledocumentmode))
			(vla-set-singledocumentmode (vla-get-system (vla-get-preferences (vlax-get-acad-object))) :vlax-false)
	)
	(setq odbx_to_objects_list (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget))))))
	(foreach object odbx_to_objects_list
		(vla-getboundingbox object 'llc 'ruc)
		(setq llc_list (append llc_list (list (vlax-safearray->list llc)))
			  ruc_list (append ruc_list (list (vlax-safearray->list ruc)))
		)
	)
	(setq min_x (apply 'min (mapcar 'car llc_list))
		  min_y (apply 'min (mapcar 'cadr llc_list))
		  max_x (apply 'max (mapcar 'car ruc_list))
		  max_y (apply 'max (mapcar 'cadr ruc_list))
		  boundary_rectangle (vlax-invoke (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
										  'addlightweightpolyline
										  (list min_x min_y min_x max_y max_x max_y max_x min_y min_x min_y)
							 )
		  boundary_center (vla-addpoint (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
									    (vlax-3d-point (list (* 0.5 (+ min_x max_x)) (* 0.5 (+ min_y max_y))))
						  )
	)
	(vla-put-color boundary_rectangle 1)
	(vla-put-color boundary_center 1)
	(setq selection_origin_point (getpoint "\nPick selection origin point: "))
	(vla-move boundary_center (vlax-3d-point selection_origin_point) (vlax-3d-point '(0 0 0)))
	(setq vp_center (reverse (cdr (reverse (vlax-get boundary_center 'coordinates)))))
	(vla-erase boundary_rectangle)
	(vla-erase boundary_center)
	(setq dxf_full_name (get_filename "dxf")
		  dummy_full_name (create_dummy_dwg_file)
		  acad_dbx_object (vlax-create-object (strcat "objectdbx.axdbdocument." (substr (getvar 'acadver) 1 2)))
	)
	(vla-open acad_dbx_object dummy_full_name)
	(setq odbx_objects_list
		  (vlax-invoke (vla-get-database (vla-get-activedocument (vlax-get-acad-object)))
		  			  'copyobjects
		  			   odbx_to_objects_list
		  			   (vla-get-modelspace acad_dbx_object)
		  )
	)
	(foreach object odbx_objects_list
		(vla-move object (vlax-3d-point selection_origin_point) (vlax-3d-point '(0 0 0)))
	)
	(setq screen_size (getvar 'screensize)
		  aspect_ratio (/ (car screen_size) (cadr screen_size))
	)
	(if (< (/ (abs (- max_x min_x)) (abs (- max_y min_y))) aspect_ratio)
		(progn
			(vla-put-height (vla-item (vla-get-viewports acad_dbx_object) 0) (abs (- max_y min_y)))
			(vla-put-width (vla-item (vla-get-viewports acad_dbx_object) 0) (* (abs (- max_y min_y)) aspect_ratio))
		)
		(progn
			(vla-put-width (vla-item (vla-get-viewports acad_dbx_object) 0) (abs (- max_x min_x)))
			(vla-put-height (vla-item (vla-get-viewports acad_dbx_object) 0) (/ (abs (- max_x min_x)) aspect_ratio))
		)
	)
	(vla-put-center (vla-item (vla-get-viewports acad_dbx_object) 0)
					(vlax-safearray-fill (vlax-make-safearray vlax-vbdouble '(0 . 1))
										 vp_center
					)
	)
	(vla-saveas acad_dbx_object dummy_full_name)
	(vlax-release-object acad_dbx_object)
	(setq dxf_document (vla-open (vla-get-documents (vlax-get-acad-object)) dummy_full_name))
	(vla-saveas dxf_document dxf_full_name ac2004_dxf)
	(vla-close dxf_document)
	(vlax-release-object dxf_document)
	(princ (strcat "\nSelected enames have been written as \"" dxf_full_name "\" in 2004 file format"))
	(princ)
)

;*******************************************************************************************************************************************

 

 

Message 16 of 21

Hans_Knol
Advocate
Advocate

Hi, thanks for your time to create the lisp, it looks well but after selecting the part or items from the drawing and select the o,o point, he said "incorrect type - nil"

so something went wrong...

Hans Knol
0 Likes
Message 17 of 21

komondormrex
Mentor
Mentor

you are right, there was a bug. try updated version. still need to fine tune dxf vp size...

0 Likes
Message 18 of 21

Hans_Knol
Advocate
Advocate
do you have updated it? stil have the same issue
Hans Knol
0 Likes
Message 19 of 21

komondormrex
Mentor
Mentor

seemed to be yes. try to close a doc where the error came and reopen it to test lisp one more time.

0 Likes
Message 20 of 21

Hans_Knol
Advocate
Advocate
not jet, maybe it needs more time to refresch.... also closed my AutoCAD....
Hans Knol
0 Likes