Jump to content

Recommended Posts

Posted

Hello, im new to autocad and lisp. I have been scouring the net for 2 weeks in an attempt to find code to modify a lisp i am using with no luck.

 

I have modified the original BOMtoCSV.lisp code to do almost all of what i need. I am trying to extract text from a table in a drawing, separate the fields by comma's. then add two other text fields to the same row.

 

eg: COL1 COL2 COL3

Row1: XXX XXX XXX

 

the text is dumped as XXX,XXX,XXX but i also want to add two other fields, that i was thinking would need to be added as coordinates. also separated by comma's and copied for each row of the table.

 

such as (ssget "_W" (list 16.8 10.3)(list 18.9 5.0) '((0 . "TEXT"))))

 

I dont usually post because i know how annoying newbie requests are, but i have been unable to find much for modifying the layout of extracted text before it is written.

 

If its possible, any help would be greatly appreciated. Thanks.

 

 

I have added the code below

Posted
 
;; BOM to CSV; A program to create a csv file from a text based table in AutoCAD
;; Author: Doug Barnes
;; Company: Grantek Systems Integration
(defun Cleanup_Text ()
;; find all the text with no content and erase them
(setq nil_text
(ssget "x"
'((-4 . "<and") (0 . "TEXT") (1 . "") (-4 . "and>"))
)
)
;; search for text with no content
(if nil_text
(command "erase" nil_text "")
)
;; erase text with no content
)
;; end cleanup_text defun
;;==========================================================================================
(defun Gather_Text (/ sel2 ct)

(setq Text_SS (ssget "_W" (list 16.8 10.3)(list 18.9 5.0) '((0 . "TEXT"))))
(setq sel2 (ssget "_W" (list 16.8 16.6)(list 25.8 11.0) '((0 . "TEXT"))))
(setq ct 0)
;set counter to zero
(repeat (sslength sel2)
;get the number of items in selection set 2
;and loop that number of times
(ssadd (ssname sel2 ct) Text_SS)
;get the name of the entity from selection set 2
;by using the counter index number and add it to
;selection set 1
(setq ct (1+ ct))
;increment the counter by 1

);end repeat
)
;; create a selection set for all text selected by the user.
;; end Gather_Text defun
;;==========================================================================================
(defun MakeList_X_Y_Text (Text_SS)
;; Make a list of x-coords, y-coords and text
(setq MakeList_cnt 0)
(repeat (sslength Text_SS)
(setq X_cord
(if (= (cdr (assoc 72 (entget (ssname Text_SS MakeList_cnt))))
0
)
;; check for the text's justification
(cadr (assoc 10 (entget (ssname Text_SS MakeList_cnt))))
;; if left justified use first alignment point
(cadr (assoc 11 (entget (ssname Text_SS MakeList_cnt))))
;; else use second alignment point
)
;;end if
)
;;end Setq
(setq y_cord
(if (= (cdr (assoc 72 (entget (ssname Text_SS MakeList_cnt))))
0
)
;; check for the text's justification
(caddr (assoc 10 (entget (ssname Text_SS MakeList_cnt))))
;; if left justified use first alignment point
(caddr (assoc 11 (entget (ssname Text_SS MakeList_cnt))))
;; else use second alignment point
)
;;end if
)
(setq List_X_Y_Text
(append
List_X_Y_Text
(list
(list
(cons "X coord"
X_cord
)
(cons "Y coord" y_cord)
(cons "Text"
(cdr (assoc 1 (entget (ssname Text_SS MakeList_cnt))))
)
)
)
)
)
;;make assocation list of x, y and text dotted pairs
(setq MakeList_cnt (1+ MakeList_cnt))
;; index to next entity
)
;;repeat
)
;;end MakeList_X_Y_Text defun
;;==========================================================================================
(defun Sort_by_X (List_X_Y_Text)
;; re-order list from left to right
(setq X_List
(vl-sort
List_X_Y_Text
(FUNCTION (LAMBDA (E1 E2) (< (cdar E1) (cdar E2))))
)
)
)
;; end Sort_by_X defun
;;==========================================================================================
(defun Sort_by_Y (List_X_Y_Text_Col)
;; re-order list from top to bottom
(setq Y_List
(vl-sort
List_X_Y_Text_Col
(FUNCTION (LAMBDA (E1 E2) (> (cdadr E1) (cdadr E2))))
)
)
)
;; end Sort_by_Y defun
;;==========================================================================================
(defun Get_Columns (X_List)
;; determine the number of columns text
(setq Column_Qty 1
X_List_cnt 0
)
(repeat (length X_List)
;; add a column number to List_X_Y_Text
(setq List_X_Y_Text_Col
(append List_X_Y_Text_Col
(list (list
(assoc "X coord" (nth X_List_cnt X_List))
(assoc "Y coord" (nth X_List_cnt X_List))
(assoc "Text" (nth X_List_cnt X_List))
(cons "Column" Column_Qty)
)
)
)
)
(if (/= (nth (+ X_List_cnt 1) X_List) nil)
;; Check for end of list
(progn
(if
(>=
(- (cdr (car (nth (+ X_List_cnt 1) X_List))) Line_Tolerance)
(cdr (car (nth X_List_cnt X_List)))
)
;; if the X Coord of the next text is out of tolerance with the current text then increase the column count
(setq Column_Qty (1+ Column_Qty))
)
)
)
(setq X_List_cnt (1+ X_List_cnt))
;; index list position
)
;; end repeat
)
;; end Get_Columns defun
;;==========================================================================================
(defun Get_Rows (Y_List)
;; determine the number of rows of text
(setq Row_Qty 1
Y_List_cnt
0
)
(repeat (length Y_List)
;; add a row number to List_X_Y_Text_Col
(setq List_All
(append
List_All
(list (list ;; make a list of x, y, text, column and row
(assoc "X coord" (nth Y_List_cnt Y_List))
(assoc "Y coord" (nth Y_List_cnt Y_List))
(assoc "Text" (nth Y_List_cnt Y_List))
(assoc "Column" (nth Y_List_cnt Y_List))
(cons "Row" Row_Qty)
)
)
)
)
(if (/= (nth (+ Y_List_cnt 1) Y_List) nil)
;; Check for end of list
(progn
(if (<= (cdr (cadr (nth (+ Y_List_cnt 1) Y_List)))
(- (cdr (cadr (nth Y_List_cnt Y_List))) Line_Tolerance)
)
;; if the Y Coord of the next text is out of tolerance with the current text then increase the row count
(setq Row_Qty (1+ Row_Qty))
)
;; end if
)
;;end progn
)
;; end if
(setq Y_List_cnt (1+ Y_List_cnt))
;; index list position
)
;; end repeat
)
;; end Get_Rows defun
;;==========================================================================================
(defun Write_BOM_to_CSV ()
(setq Position 0)
(setq Path (getvar "dwgprefix"))
;; Obtain the file path of the drawing
(setq Dwg_Name (substr (getvar "dwgname")
1
(- (strlen (getvar "dwgname")) 4)
)
)
;; remove the .dwg from the file name
(setq BOM_CSV_File (open (strcat Path Dwg_Name " WELD.txt") "a"))
;; Open a CSV file for ammending
(while (/= (nth Position List_All) nil)
(setq Current_Row (cddr (nth Position List_All)))
;; setup to gather all of the same row text
(setq Next_Row (cddr (nth (+ Position 1) List_All)))
;; setup to check end of row
(while (= (cdr (assoc "Row" Current_Row))
(cdr (assoc "Row" Next_Row))
)
;; loop for all the same row
(setq Text_Column
(append Text_Column
(list (list (cdr (assoc "Text" Current_Row))
(cdr (assoc "Column" Current_Row))
)
)
)
)
;; create a list of text and column only
(setq Position (1+ Position))
;; index position in list
(setq Current_Row (cddr (nth Position List_All)))
(setq Next_Row (cddr (nth (+ Position 1) List_All)))
)
;; end if
(setq Text_Column
(append Text_Column
(list (list (cdr (assoc "Text" Current_Row))
(cdr (assoc "Column" Current_Row))
)
)
)
)
;; append Text_Column with the last text in the current row
(setq Text_Column
(vl-sort
Text_Column
(FUNCTION (LAMBDA (E1 E2) (< (cadr E1) (cadr E2))))
)
)
;; Sort list by Column number (left to right)
(setq Column_Qty_Count
1
Write_Row ""
Column_List_Position
0
)
(repeat Column_Qty
;; repeat for the maximum number of columns
(if (= (cadr (nth Column_List_Position Text_Column))
Column_Qty_Count
)
;; check the position of the current text's postion with the column count
(progn ;; if equal string together the cuurent text and a comma
(setq
Write_Row (strcat
Write_Row
(car (nth Column_List_Position Text_Column))
","
)
)
(setq Column_List_Position (1+ Column_List_Position))
;; index position
)
;; end progn
;; if not equal string together previous text and a comma
(setq Write_Row (strcat Write_Row ","))
)
;; end if
(setq Column_Qty_Count (1+ Column_Qty_Count))
;; index the column count
)
;; end repeat
;;(print Write_Row)
(write-line Write_Row BOM_CSV_File)
;; write the current row to the csv file
(setq Position (1+ Position))
;; index list position
(setq Text_Column nil)
;; reset for next row
)
;; end while
(close BOM_CSV_File)
;; close the csv file
)
;; end Write_BOM_to_CSV defun
;;==========================================================================================
;; Create a CSV file from a text based Bill of Materials in AutoCAD
(defun c:weld (/ X_List Y_List
Text_Column List_X_Y_Text List_X_Y_Text_Col
List_All
)
(Cleanup_Text)
(Gather_Text)
(MakeList_X_Y_Text Text_SS)
(Sort_by_X List_X_Y_Text)
(setq Text_Height (cdr (assoc 40 (entget (ssname Text_SS 0)))))
;; get the text height used
(setq Line_Tolerance (+ Text_Height (* Text_Height 0.1)))
;; set up a line spacing tolerance
(Get_Columns X_List)
(Sort_by_Y List_X_Y_Text_Col)
(Get_Rows Y_List)
(Write_BOM_to_CSV)
)
;; end defun

Posted

I was attempting to add a simple text string at the end of the last entry for each row just before it is printed to the CSV file. but nothing worked.

if there is a way to add a text string or another variable into each row after the last comma, i think it would need to be added into this small piece of the HUGE code.

 

(defun Write_BOM_to_CSV ()
 (setq Position 0)
 (setq Path (getvar "dwgprefix"))
 ;; Obtain the file path of the drawing
 (setq Dwg_Name (substr (getvar "dwgname")
   1
   (- (strlen (getvar "dwgname")) 4)
  )
 )
 ;; remove the .dwg from the file name
 (setq BOM_CSV_File (open (strcat Path Dwg_Name " WELD.txt") "a"))
 ;; Open a CSV file for ammending
 (while (/= (nth Position List_All) nil)
   (setq Current_Row (cddr (nth Position List_All)))
   ;; setup to gather all of the same row text
   (setq Next_Row (cddr (nth (+ Position 1) List_All)))
   ;; setup to check end of row
   (while (= (cdr (assoc "Row" Current_Row))
      (cdr (assoc "Row" Next_Row))
   )
     ;; loop for all the same row
     (setq Text_Column
     (append Text_Column
      (list (list (cdr (assoc "Text" Current_Row))
    (cdr (assoc "Column" Current_Row))
     )
      )
     )
     )
     ;; create a list of text and column only
     (setq Position (1+ Position))
     ;; index position in list
     (setq Current_Row (cddr (nth Position List_All)))
     (setq Next_Row (cddr (nth (+ Position 1) List_All)))
   )
   ;; end if
   (setq Text_Column
   (append Text_Column
    (list (list (cdr (assoc "Text" Current_Row))
         (cdr (assoc "Column" Current_Row))
   )
    )
   )
   )
   ;; append Text_Column with the last text in the current row
   (setq Text_Column
   (vl-sort
     Text_Column
     (FUNCTION (LAMBDA (E1 E2) (< (cadr E1) (cadr E2))))
   )
   )
   ;; Sort list by Column number (left to right)
   (setq Column_Qty_Count
   1
  Write_Row ""
  Column_List_Position
   0
   )
   (repeat Column_Qty
     ;; repeat for the maximum number of columns
     (if (= (cadr (nth Column_List_Position Text_Column))
     Column_Qty_Count
  )
;; check the position of the current text's postion with the column count
(progn ;; if equal string together the cuurent text and a comma
       (setq
  Write_Row (strcat
       Write_Row
       (car (nth Column_List_Position Text_Column))
       ","
     )
       )
       (setq Column_List_Position (1+ Column_List_Position))
       ;; index position
)
;; end progn
;; if not equal string together previous text and a comma
(setq Write_Row (strcat Write_Row ","))
     )
     ;; end if
     (setq Column_Qty_Count (1+ Column_Qty_Count))
     ;; index the column count
   )
   ;; end repeat
   ;;(print Write_Row)
   (write-line Write_Row BOM_CSV_File)
   ;; write the current row to the csv file
   (setq Position (1+ Position))
   ;; index list position
   (setq Text_Column nil)
   ;; reset for next row
 )
 ;; end while
 (close BOM_CSV_File)
 ;; close the csv file
)
;; end Write_BOM_to_CSV defun

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...