Block attribute sort and export

Block attribute sort and export

smallƑish
Advocate Advocate
1,150 Views
8 Replies
Message 1 of 9

Block attribute sort and export

smallƑish
Advocate
Advocate

I have a request for a lisp routine. which may look very complicated. Filter and choose only a "Count" named blocks from a big selection. Group them concerning one of the tag names called "NAME" .Each group sorts them concerning its serial number "SL.NO".Export all those block attributes to a CSV file, with the above-mentioned order. Sample file attached for Easy Understanding.

0 Likes
Accepted solutions (1)
1,151 Views
8 Replies
Replies (8)
Message 2 of 9

EnM4st3r
Advocate
Advocate

learn to write some lisps yourself. You are on this forum for a while now, you cant keep solely relying on others on this forum. 

Try something, if it doesnt work, you can ask

Message 3 of 9

smallƑish
Advocate
Advocate

Thank you for your advice @EnM4st3r , I will do this for sure. 

0 Likes
Message 4 of 9

Sea-Haven
Mentor
Mentor

Ok some hints as you have asked elsewhere as well.

 

Get all blocks

get attributes from the blocks and put in a list

VL-sort but on 1st 2 items Roomname and SL NO

Then save into a csv or write direct to Excel as asked in other post.

 

Can lisp export attribute datas to a specific cell of excel? - Autodesk Community - AutoCAD

 

Google Alan excel.lsp it has the Excel functions you need in it.

 

0 Likes
Message 5 of 9

komondormrex
Mentor
Mentor
Accepted solution

check the following

 

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

(defun concat_separator (str_list separator_char)
  	(substr (apply 'strcat (mapcar '(lambda (str) (strcat separator_char str)) str_list)) 2)
)

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

(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 "Enter name of CSV file" csv_name_selected "csv" "" 1))
			(setq csv_filename (getfiled        "Enter name of CSV file" csv_name_selected "csv"    1))
	)
	(if (= "" csv_filename) nil csv_filename)
)

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

(defun c:atts_to_csv (/ insert_sset att_data_order separator att_data_list c_att_data_order name_list att_data_name_parsed_list csv_filename_full csv_id)
	(if (setq insert_sset (ssget "_x" '((0 . "insert"))))
			(progn
	  			(setq att_data_order '(("SL.NO") ("NAME") ("TABLE") ("CHAIR"))
	  				  separator ";"
					  att_data_list (mapcar '(lambda (att_list) (last (mapcar '(lambda (attribute) (if (assoc (vla-get-tagstring attribute) c_att_data_order)
	  				  		                      															(setq c_att_data_order (subst (cons (vla-get-tagstring attribute)
																																				(vla-get-textstring attribute)
																																		  )
	  				  		                      																		                  (assoc (vla-get-tagstring attribute) c_att_data_order)
	  				  		                      																		                  c_att_data_order
	  				  		                      																		            )
	  				  		                      															)
	  				  		                  													   )
	  				  		                								   )
	  				  		                     							   (progn (setq c_att_data_order att_data_order) att_list)
	  				  		                 						  )
	  				  		                		 			)
	  				  		      			 )
	  				  		      			 (mapcar '(lambda (insert) (vlax-invoke insert 'getattributes))
											 		  (vl-remove-if-not '(lambda (insert) (= "COUNT" (strcase (vla-get-effectivename insert)))) 
				   														 (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex insert_sset))))
													  )
	  				  			  			 )
	  				  				)
				)
				(foreach room_data att_data_list
					 (if (not (member (cdr (assoc "NAME" room_data)) name_list)) (setq name_list (append name_list (list (cdr (assoc "NAME" room_data))))))
				)
				(setq name_list (vl-sort name_list '(lambda (name_1 name_2) (< (atoi (substr name_1 (+ 2 (vl-string-position (ascii "-") name_1))))
																			   (atoi (substr name_2 (+ 2 (vl-string-position (ascii "-") name_2))))
																			)
													)
								)
					  att_data_name_parsed_list (mapcar '(lambda (name) (vl-remove-if-not '(lambda (room_data) (= name (cdr (assoc "NAME" room_data)))) att_data_list)) name_list)
					  att_data_name_parsed_list (mapcar '(lambda (name_data) (vl-sort name_data '(lambda (att_data_1 att_data_2) (< (atoi (cdr (assoc "SL.NO" att_data_1)))
					  																												(atoi (cdr (assoc "SL.NO" att_data_2)))
																																 )
																								 )
																			 )
														 )
														 att_data_name_parsed_list
												)
				 	  csv_filename_full (get_csv_filename)
				)
				(if csv_filename_full
					(progn
						(setq csv_id (open csv_filename_full "w"))
	  					(princ (strcat "sep=" separator "\n") csv_id)
	  					(princ (strcat (concat_separator (apply 'append att_data_order) separator) "\n") csv_id)
						(foreach name_data att_data_name_parsed_list
							(foreach data name_data
		    					(princ (strcat (concat_separator (mapcar 'cdr data) separator) "\n") csv_id)
							)
						)
						(close csv_id)
						(princ (strcat "File \"" csv_filename_full "\" has been written"))
					)
				)
			)
	)
	(princ)
)

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

 

 

Message 6 of 9

smallƑish
Advocate
Advocate

I don't have words to describe my thanks. I can makeup the code as per proper requirement, which can make my works accurate and faster. As always here the additional feature of ordering the columns and removing unwanted details. Everything is really your own proposed features. It's really make the process much faster.

Thank you so much 🙏

0 Likes
Message 7 of 9

komondormrex
Mentor
Mentor

yep, but make sure the blocks selected are static.

Message 8 of 9

smallƑish
Advocate
Advocate

Hi,

 

I have made minor modifications to the given code as per my requirement, basically renaming only and add the list a little bigger. but the issue is the "NAME" (have renamed it to  DBNAME) I can't able to use a number without a hyphen. please help me to fix this. a revised block is also attached. 

0 Likes
Message 9 of 9

smallƑish
Advocate
Advocate

in addition, I got one of your code from the internet that makes a copy of an AutoCAD file to a specific folder with a time stamp, Would that be possible to consider the same logic here to make the saving csv file process automatically?

 

(defun c:sav ( / cpy sub )
   (setq sub "LOAD CALCULATION TABLES") ;; Subdirectory
   (cond
       (   (zerop (getvar 'dwgtitled))
           (princ "\nCurrent drawing is unsaved.")
       )
       (   (not (or (findfile (setq sub (strcat (getvar 'dwgprefix) sub))) (vl-mkdir sub)))
           (princ (strcat "\nUnable to create directory " sub))
       )
       (   (progn
               (princ "\nSaving current drawing...")
               (command "_.qsave")
               (not 
                   (vl-file-copy (strcat (getvar 'dwgprefix) (getvar 'dwgname))
                       (setq cpy 
                           (strcat sub "\\"
                               (vl-filename-base (getvar 'dwgname)) "-"
                               (menucmd "m=$(edtime,0,DD-MON-YY   HH.MMAM/PM)") 
                               (vl-filename-extension (getvar 'dwgname))
                           )
                       )
                   )
               )
           )
           (princ (strcat "\nUnable to copy drawing to " cpy))
       )
       (   (princ (strcat "\nDrawing copied to " cpy)))
   )
   (princ)
)
0 Likes