Generate dxf file for single part ProNest nesting.

hi_mustofa
Explorer
Explorer

Generate dxf file for single part ProNest nesting.

hi_mustofa
Explorer
Explorer

Hi, my name is Mustofa, and I found AutoLisp that really helpful.

 

here is  The Post 

 

I tried to do some adjustments, but I got an error. I tried to use ChatGPT, but I'm facing some errors.

 

so the adjustment that i want to make is before generate the dxf file.

AutoCAD will prompt you to select 
PART_NAME

MATERIAL

GRADE

THICKNESS

QUANTITY

 

AND WILL GENERATE (EXAMPLE) -FOR PRONEST to read the data

PART_NAME=WPS-1-2

MATERIAL=S335

GRADE=KT-40

THICKNESS=50

QUANTITY=10

 

hi_mustofa_1-1725424345133.png

 

 

The text on the left is generated after selecting the text with magenta color.

 

the green color auto-move CUT Layer; if the layer does not exist, then the script will automatically create it.

magenta color should be moved on layer PLOT

grey color Bill of material text should be moved to layer BOM.

and then purge  all layer 

only left PLOT BOM and CUT layer.

 

0 Likes
Reply
549 Views
5 Replies
Replies (5)

hi_mustofa
Explorer
Explorer

Hi, @komondormrex, can you please help? 

0 Likes

komondormrex
Advisor
Advisor

hey there,

so you just want to place some requested text(s) to some point  on the dwg and do partial export then selected entities to a dxf or maybe dwg?

0 Likes

hi_mustofa
Explorer
Explorer

yes. that's all. tried some modification but the text for BOM ( bill of quantity) and the layer of BOM is not exported.

and the text of BOM is too large, while my requirement is that the text is on the right side inside the object. for ProNest to detect the material and etc.

 

here is the script after my modification

 

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

;	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 (strcat "Enter " (strcase extension) " 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 (vlax-get (vla-get-activedocument (vlax-get-acad-object)) 'path) "\\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-saveas (list acad_dbx_object dummy_dwg_filename))
  (vlax-release-object acad_dbx_object)
  dummy_dwg_filename
)

(defun check-or-create-layer (layer-name color-code)
  (if (null (tblsearch "layer" layer-name))
    (progn
      (command "._LAYER" "New" layer-name "")
      (command "._LAYER" "Color" (itoa color-code) layer-name "")
    )
  )
)

(defun get-bounding-box-center (object)
  (if (and object (vlax-method-applicable-p object 'getboundingbox))
    (progn
      (vla-getboundingbox object 'llc 'ruc)
      (setq llc (vlax-safearray->list llc))
      (setq ruc (vlax-safearray->list ruc))
      (mapcar '(lambda (a b) (/ (+ a b) 2.0)) llc ruc)
    )
  )
)

(defun c:partial_dxf_export (/ odbx_to_objects_list object-center part_name material grade thickness quantity
                               new_text text-list obj-scale text-insertion-point text-height)

  ;; Check and create necessary layers
  (check-or-create-layer "CUT" 3)  ;; Green
  (check-or-create-layer "PLOT" 4) ;; Cyan
  (check-or-create-layer "BOM" 8)  ;; Grey

  ;; Set the drawing object to layer CUT except for text, which goes to layer PLOT
  (setq odbx_to_objects_list (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget))))))
  (foreach object odbx_to_objects_list
    (if (and object
             (or (eq (vla-get-objectname object) "AcDbText")
                 (eq (vla-get-objectname object) "AcDbMText")))
      (vla-put-layer object "PLOT")
      (vla-put-layer object "CUT")
    )
  )

  ;; Prompt user to select text objects for metadata
  (setq part_name (car (entsel "\nSelect text for PART_NAME: ")))
  (setq material (car (entsel "\nSelect text for MATERIAL: ")))
  (setq grade (car (entsel "\nSelect text for GRADE: ")))
  (setq thickness (car (entsel "\nSelect text for THICKNESS: ")))
  (setq quantity (car (entsel "\nSelect text for QUANTITY: ")))

  ;; Validate selections
  (if (or (null part_name) (null material) (null grade) (null thickness) (null quantity))
    (progn
      (princ "\nError: One or more text selections were invalid.")
      (exit)
    )
  )

  ;; Get the text content from selected objects
  (setq part_name (cdr (assoc 1 (entget part_name))))
  (setq material (cdr (assoc 1 (entget material))))
  (setq grade (cdr (assoc 1 (entget grade))))
  (setq thickness (cdr (assoc 1 (entget thickness))))
  (setq quantity (cdr (assoc 1 (entget quantity))))

  ;; Calculate the center of the selected objects
  (setq object-center (get-bounding-box-center (car odbx_to_objects_list)))

  ;; Ensure object-center is valid
  (if (not object-center)
    (progn
      (princ "\nError: Unable to calculate object bounding box center.")
      (exit)
    )
  )

  ;; Define insertion point for the new text relative to the object center
  (setq obj-scale (/ (distance (list (car object-center) (cadr object-center)) '(0 0)) 2))
  (setq text-height (/ obj-scale 4)) ;; Adjust text height to be 1/4 of object size
  (setq text-insertion-point (list (car object-center) (- (cadr object-center) (* text-height 1.5))))

  ;; Create each line of the BOM as individual text objects
  (setq text-list (list (strcat "PART_NAME=" part_name)
                        (strcat "MATERIAL=" material)
                        (strcat "GRADE=" grade)
                        (strcat "THICKNESS=" thickness)
                        (strcat "QUANTITY=" quantity)))

  (foreach txt text-list
    (entmake (list (cons 0 "TEXT")
                   (cons 10 text-insertion-point)
                   (cons 40 text-height)
                   (cons 1 txt)
                   (cons 7 "Standard")
                   (cons 8 "BOM")
                   (cons 62 8) ;; Set color to grey
            ))
    ;; Move insertion point down for the next line of text
    (setq text-insertion-point (list (car text-insertion-point) (- (cadr text-insertion-point) (* text-height 1.5))))
  )

  ;; Export DXF process continues as before...

  (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 llc_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)
)

0 Likes

komondormrex
Advisor
Advisor

could you please illustrate what exactly you want to have in the dxf exported and in what positions? the latter mainly concerns the magenta and gray text. before/after is preferred. i mean, should the magenta texts be inside the detail? should the gray text be right on the right to the detail? should  the both texts be texts primitives, not mtexts?

0 Likes

hi_mustofa
Explorer
Explorer

Hi, "Thanks for your prompt reply and for taking the time to address this."

I attached files and explained in those files that the dxf file is the result file I want to achieve from __PROJECT__.dwg file

 

Please check the files.

 

 

0 Likes