Write attribute value to .csv

Write attribute value to .csv

andreas7ZYXQ
Advocate Advocate
361 Views
5 Replies
Message 1 of 6

Write attribute value to .csv

andreas7ZYXQ
Advocate
Advocate

Hi all out there.

Im looking for a function or someone that could help me with writing one. I think its quite simple for someone with more knowledge than me. let me describe.

User selects a block, and if there is an attribute inside the block that matches 3 different alternatives of attribute names it whould be written to a .csv file. I would like it to loop until i just press esc or similar.

I have a following code that writes to a lsp file. maybe it could be used in some parts?

 

 

(defun C:frez(/ frzfile esel lname)
  (setq frzfile (open (strcat (getvar 'dwgprefix) "mid.lsp") "a"))
  (while
    (setq esel (nentsel "\nMessage: "))
    (setq lname (cdr (assoc 8 (entget (car esel)))))
    (command "-layer" "f" lname "")
	
	(if(wcmatch lname "*|*")
		(setq lname(strcat "*" (substr lname (+(vl-string-search "|" lname)1) (strlen lname))))
	)
	
    (write-line
      (strcat "(command \"-layer\" \"f\" \"" lname "\" \"\")")
      frzfile
    ); write-line
  ); while
  (close frzfile)
)

 

 

0 Likes
362 Views
5 Replies
Replies (5)
Message 2 of 6

komondormrex
Mentor
Mentor

hello there,

and what are matching conditions for attribute tag, i suppose?

0 Likes
Message 3 of 6

andreas7ZYXQ
Advocate
Advocate

Hi,

my tag names are section, adress and page. Would be nice to have them in separate columns. 

0 Likes
Message 4 of 6

Moshe-A
Mentor
Mentor

@andreas7ZYXQ  hi,

 

Check this MCSV (Make CSV file) command 😀

 

TAG1, TAG2 TAG3 (lines 79-81) represent your 3 attributes respectively (e.g "section" "address" & "page")

make sure to synchronize these tags name with the tags name in the block.

 

command starts with asking you the csv file name to write to than select object(s):  pick the blocks to write out attributes. only blocks that has the 3 attributes match, will be taken.

 

enjoy

Moshe

 

(vl-load-com) ; load activex support

; Make CSV file
(defun c:mcsv (/ match_attribute alternative3 attributes->list LM:lst->str summarize  ; local functions
	         fname ss f ctr ename AcDbBlkRef AcDbAttrib attributes)

 (defun match_attribute (lst tag)
  (vl-some
    (function
      (lambda (attrib)
       (if (eq (strcase (vla-get-tagString attrib)) (strcase tag)) attrib)
      )
    )
    lst
  ); vl-some
 ); match_attribute

  
 ; return list of attributes that match 3 alternative 
 (defun alternative3 (lst / l)
  (if
    (vl-every
      (function
        (lambda (attrib)
         (eq (type attrib) 'VLA-OBJECT)
        )
      ); function
      (setq l (mapcar
      	        (function
                  (lambda (tag)
                    (match_attribute lst tag)
                  )
                ); function
                (list TAG1 TAG2 TAG3)
             ); mapcar
      ); setq
    ); vl-every
    l)
 ); alternative3 


 (defun attributes->list (attributes / lst)
  (foreach attrib attributes
   (setq lst (append lst (list (vla-get-textString attrib))))
  )
  lst 
 ); attributes->list 
  

 ;; List to String  -  Lee Mac
 ;; Concatenates each string in a supplied list, separated by a given delimiter
 ;; lst - [lst] List of strings to concatenate
 ;; del - [str] Delimiter string to separate each item

 (defun LM:lst->str ( lst del )
    (if (cdr lst)
        (strcat (car lst) del (LM:lst->str (cdr lst) del))
        (car lst)
    )
 ); LM:lst->str

 
 (defun summarize ()
  (cond
   ((= ctr 0)
    (vlr-beep-reaction)
    (princ (strcat "\n0 record(s) written to " fname " file."))
   ); case
   ( t
    (princ (strcat "\n" (itoa ctr) " record(s) written to " fname " file."))
   )
  ); cond
 ); summarize 

  
 ; here start c:mcsv

 ; declar 3 attributes to seek for
 (setq TAG1 "section")  ; const
 (setq TAG2 "address")  ; const
 (setq TAG3 "page")     ; const 
  
 (if  (and
       (setq fname (getfiled "Save excel csv file" (strcat (getvar "dwgprefix") (vl-string-right-trim ".dwg" (getvar "dwgname"))) "csv" 9))
       (setq ss (ssget  (list '(0 . "insert") '(66 . 1))))
       (setq f (open fname "w"))
     )
  (progn
   (write-line (LM:lst->str (list TAG1 TAG2 TAG3) ",") f) ; header line
   
   (setq ctr 0)
   (foreach ename (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))) 
    (setq AcDbBlkRef (vlax-ename->vla-object ename))

    (if (setq attributes (alternative3 (vlax-invoke AcDbBlkRef 'GetAttributes)))
     (progn
      (setq ctr (1+ ctr))
      (write-line (LM:lst->str (attributes->list attributes) ",") f)
     ); progn
    ); if

    ; dispose memory
    (foreach AcDbAttrib attributes
     (vlax-release-object AcDbAttrib)
    )
     
    (vlax-release-object AcDbBlkRef)  ; dispose memory
   ); foreach
   (setq f (close f)) ; close file
   
   (summarize)
  ); progn
 ); if

 (princ) 
); c:mcsv 

 

 

 

0 Likes
Message 5 of 6

komondormrex
Mentor
Mentor

hi,

and a CSV file is existed or should be newly created?

0 Likes
Message 6 of 6

komondormrex
Mentor
Mentor

anyway

(defun c:SAP_to_CSV (/ csv_full_name csv_file_id block_entsel_data block_object section_address_page csv_attribute_line)
	(if (findfile (setq csv_full_name (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) "_SAP.csv")))
		(progn
			(write-line (strcat "File \"" csv_full_name "\" has been opened for appending."))
			(setq csv_file_id (open csv_full_name "a"))
		)
		(progn
			(setq csv_file_id (open csv_full_name "a"))
			(write-line (strcat "File \"" csv_full_name "\" has been created for writing."))
			(write-line "Section;Address;Page" csv_file_id) 
		)
	)
	(while (if (and
					(setq block_entsel_data (vl-catch-all-apply 'entsel (list "\nPick an attributed block (ESC to stop command): ")))
			   		(null (vl-catch-all-error-p block_entsel_data))
		   	   )
				(progn
			   		(setq block_object (vlax-ename->vla-object (car block_entsel_data)))
			   		(if (or 
							 (/= "AcDbBlockReference" (vla-get-objectname block_object))
							 (zerop (vlax-get block_object 'hasattributes))
						)
			   				(null (alert "You've picked neither block nor it has no attributes"))
							(progn
								(setq section_address_page '((section . "") (address . "") (page . "")))
								(foreach attribute (vlax-invoke block_object 'getattributes)
									(if	(member (read (vla-get-tagstring attribute)) '(section address page))
										(progn
											(setq section_address_page (subst (cons (read (vla-get-tagstring attribute)) (vla-get-textstring attribute))
																			  (assoc (read (vla-get-tagstring attribute)) section_address_page)
																			  section_address_page
																	   )
											)
										)
									)
								)
								(if (/= ";;" (setq csv_attribute_line (substr (apply 'strcat (mapcar '(lambda (attribute) (strcat ";" (cdr attribute)))
																											section_address_page
															  									)
											   								   )
											   								   2
									   								  )
											 )
									)
										(progn
											(write-line (strcat "\nLine \"" csv_attribute_line "\" has been written to CSV file"))
											(write-line csv_attribute_line csv_file_id)
										)
										(princ "None attributes found to be written to CSV file")
								)
							)
			   		)
					t
				)
				(if (vl-catch-all-error-p block_entsel_data)
					(progn
						(write-line (strcat "File \"" csv_full_name "\" has been closed."))
						(close csv_file_id) 
					)
					t
				)
			)
	)
	(princ)
)
0 Likes