Lisp to Update Attributes

Lisp to Update Attributes

timhNP6SK
Explorer Explorer
871 Views
5 Replies
Message 1 of 6

Lisp to Update Attributes

timhNP6SK
Explorer
Explorer

Hello

 

I am trying to create a lisp that can update a table based on data from a csv file. Currently, our method of updating a "table" in AutoCAD involves manually transferring the data which can lead to mistakes and be cumbersome.

 

The first part of my "routine" is to pull from a template file that has the block attributes within the table. This was simple enough to make. 

 

Once the table is in, I want to run a "routine" to update the table with data from the csv.

 

I was able to get it to do 1 update but have given up on the lisp because I think it was all wrong anyways.

 

I will provide the lisp for bringing the table into the drawing, the csv file, and the table template for reference.

 

Can someone help me out with creating a lisp that will read the csv file and then insert the data into its appropriate spot?

 

Thanks a ton! I enjoy learning to so feel free to school me! 

0 Likes
Accepted solutions (1)
872 Views
5 Replies
Replies (5)
Message 2 of 6

paullimapa
Mentor
Mentor

FYI, if you have Express Tools, you can actually use ATTOUT to create the CSV file from AutoCAD.

Changes to the items in the CSV file then can be brought back in with ATTIN command.

AutoCAD 2022 Help | ATTOUT (Express Tool) | Autodesk


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 6

komondormrex
Mentor
Mentor
Accepted solution

hi,

check this for updating the table with data from csv.

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

(defun get_string_number (string)
	(atoi (vl-list->string (vl-remove-if-not 
								'(lambda (list_element) (member list_element '(48 49 50 51 52 53 54 55 56 57))) 
								 (vl-string->list string)
						   )
		  )
	) 
)

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

(defun get_string_without_number (string)
	(vl-list->string (vl-remove-if '(lambda (list_element) (member list_element '(48 49 50 51 52 53 54 55 56 57))) 
									(vl-string->list string)
					 )
	) 
)

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

(defun get_csv_filename (/ csv_name_selected)
		(setq csv_name_selected (strcat (vlax-get (vla-get-activedocument (vlax-get-acad-object)) 'Path)
						   		 		 "\\"
										 (if (and
										 			csv_filename
													(findfile csv_filename)
											 )
												(vl-filename-base csv_filename)
												""
										 )
						  		)
		)
		(if (type acet-ui-getfile)
				(setq csv_filename (acet-ui-getfile "Select block name/description CSV file"
													csv_name_selected
													"csv"
													""
													2
								   )
				)
				(setq csv_filename (getfiled "Select block name/description CSV file"
											  csv_name_selected
											 "csv"
											 2
								   )
				)
		)
		(if (= "" csv_filename)
			nil
			csv_filename
		)
	)

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

(defun read_csv_to_list (csv_filename / csv_file_id raw_index data_line subdata data_line data_list data_sublist)
	(setq csv_file_id (open csv_filename "r")
		  raw_index 0
	)
	(while (setq data_line (read-line csv_file_id))
		(setq data_sublist nil)
		(while (setq comma_pos (vl-string-position (ascii ",") data_line)) 
			(setq subdata (substr data_line 1 comma_pos)
				  data_line	 (substr data_line (+ 2 comma_pos))
				  data_sublist (append data_sublist (list subdata))
			)
		)
		(setq data_sublist (append data_sublist (list data_line)) 
			  data_list (append data_list (list (list (setq raw_index (1+ raw_index)) data_sublist)))
		)
	)
	(close csv_file_id)
	data_list
)

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

(defun c:fill_in_attributes_csv (/ table_block_object attributes_list row_index_list parsed_attributes_list
								   attribute_sequence attribute_index
								)
	(setq table_block_object (vlax-ename->vla-object (car (entsel "\nPick table block:")))
		  attributes_list (vl-sort
								(mapcar '(lambda (attribute) (list (get_string_number (vla-get-tagstring attribute)) 
																   (get_string_without_number (vla-get-tagstring attribute)) 
																   attribute
															 )
										 )  
			 					     	 (vlax-invoke table_block_object 'getattributes)
						    	)
								'(lambda (attribute_1 attribute_2) (< (car attribute_1) (car attribute_2)))
						  )
		  row_index_list (vl-sort (mapcar 'car attributes_list) '<)
	)
	(foreach row row_index_list
		(setq parsed_attributes_list (append parsed_attributes_list 
											(list
												(list row 
													(mapcar 'cdr 
														  	(vl-remove-if-not '(lambda (attribute) (= row (car attribute))) 
														  					   attributes_list
														    )
												    )
												)
											)
									 )
		)
	)
	(setq attribute_sequence '("FOOTING_NO." "LOCATION" "COMP" "UP" "LAT" "MO" "STU" "PO" "FO" "RR")
		  csv_row_attributes_values_list  (read_csv_to_list (get_csv_filename))
	)
	(foreach attribute_row csv_row_attributes_values_list 
		(setq attribute_index -1)
		(foreach attribute_str (cadr attribute_row)
			(vla-put-textstring (cadr (assoc (nth (setq attribute_index (1+ attribute_index)) attribute_sequence) 
											 (cadr (assoc (car attribute_row) parsed_attributes_list))
									  )
								) 
								attribute_str
			)  
		)
	)
	(princ)
)

;***************************************************************************************************************
0 Likes
Message 4 of 6

timhNP6SK
Explorer
Explorer

I originally tried to do this but I could not get the .txt file to be formatted properly I exported the attributes. It was definitely a user error - haha.

 

If you know of any great tutorials for doing what you've suggested, please forward them!

 

Thanks

0 Likes
Message 5 of 6

paullimapa
Mentor
Mentor

Do a quick search online for videos on this topic and you’ll get links like this

https://m.youtube.com/watch?v=rFUvJMuMyo8


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 6 of 6

timhNP6SK
Explorer
Explorer

This worked perfectly. I appreciate the help

 

Thanks so much!

 

-Tim

0 Likes