Export Specific attribute named block X, Y coordinates to Excel

Export Specific attribute named block X, Y coordinates to Excel

Yamishon_Noah
Enthusiast Enthusiast
4,228 Views
11 Replies
Message 1 of 12

Export Specific attribute named block X, Y coordinates to Excel

Yamishon_Noah
Enthusiast
Enthusiast

Hi experts,

 

I have many blocks but its attribute  Tag is "TYPE" same for all blocks.

 

So, I want to extract only these block attributed Tag "TYPE" X & Y coordinates to Excel.

 

Sample drawing is attached.

 

Appreciate your helps

 

 

Thanks


Yamishon Noah

4,229 Views
11 Replies
Replies (11)
Message 2 of 12

pbejse
Mentor
Mentor

@Yamishon_Noah wrote:

Hi experts,

So, I want to extract only these block attributed Tag "TYPE" X & Y coordinates to Excel.

 

Appreciate your helps

 


With Data Extraction you can this result...
sample.png

 

 

 

 

It is not the attribute definitions coordinates but the blocks' insertion point

Is that what you're after? or something else?

 

 

 

Message 3 of 12

pbejse
Mentor
Mentor
(defun c:demo ( / LM:open ss i vl_e ins_point valid_tag All_data filetowrite)
  
;; pBe May 2018
  
;; Open  -  Lee Mac
;; A wrapper for the 'Open' method of the Shell Object
;; target - [int/str] File, folder or ShellSpecialFolderConstants enum
  
(defun LM:open ( target / rtn shl )
    (if (and (or (= 'int (type target)) (setq target (findfile target)))
             (setq shl (vla-getinterfaceobject (vlax-get-acad-object) "shell.application"))
        )
        (progn
            (setq rtn (vl-catch-all-apply 'vlax-invoke (list shl 'open target)))
            (vlax-release-object shl)
            (if (vl-catch-all-error-p rtn)
                (prompt (vl-catch-all-error-message rtn))
                t
            )
        )
    )
)  
    (and
    	(setq ss (ssget "_X" '((0 . "INSERT")(66 . 1)(410 . "Model"))))
    	(setq csvfile (getfiled "Enter CSV file name" (getvar 'dwgprefix) "csv" 1))
	(repeat (setq i (sslength ss))
		(setq vl_e (vlax-ename->vla-object (ssname ss (Setq i (1- i)))))
		(setq ins_point (vlax-get vl_e 'Insertionpoint))
		(if (setq valid_tag
			   (Cdr (assoc "TYPE"
			       (mapcar '(lambda	(at)
					  (list (vla-get-tagstring at) (vla-get-textstring at))
					)
				       (vlax-invoke vl_e 'GetAttributes)
				       )
				     )
				   )
				)	  		
			  (setq All_data (cons (list (vla-get-EffectiveName vl_e)
						     (strcat (Car valid_tag) ","
							     (rtos (car ins_point) 2) ","
							     (rtos (cadr ins_point) 2)
							     )
						     ) All_data))
		  )
	All_data
	)
	(setq filetowrite (open csvfile "w"))
        (foreach itm (vl-sort All_data '(lambda (a b)(< (Car a)(car b))))
	    	(write-line (strcat (Car itm) "," (Cadr itm)) filetowrite))
	(not (close filetowrite))
	(LM:OPEN csvfile)
	)
  (princ)
  )
Message 4 of 12

Yamishon_Noah
Enthusiast
Enthusiast
Hello pBe,


Appreciate your reply and thanks a lot.

I don’t want to use Data extraction method.

Let me check your Lisp (posted) and will let you know.

Thanks again. 🙂
0 Likes
Message 5 of 12

Yamishon_Noah
Enthusiast
Enthusiast

Hi pBe,

 

your code works perfect.. awesome..

 

is it possible to save file as  "file_name".xlsx instead of user to enter name & CSV file.

 

Thanks a lot

 

🙂

0 Likes
Message 6 of 12

pbejse
Mentor
Mentor

well...

(defun c:demo ( /  ss i vl_e ins_point valid_tag All_data filetowrite xlCells Excel_exist col row)
  
;; pBe May 2018
    
  (if
    	(and
	  (setq All_data nil
	        ss (ssget "_X" '((0 . "INSERT")(66 . 1)(410 . "Model"))))
	  (repeat (setq i (sslength ss))
				(setq vl_e (vlax-ename->vla-object (ssname ss (Setq i (1- i)))))
				(setq ins_point (vlax-get vl_e 'Insertionpoint))
				(if (setq valid_tag
					   (Cdr (assoc "TYPE"
					       (mapcar '(lambda	(at)
							  (list (vla-get-tagstring at) (vla-get-textstring at))
							)
						       (vlax-invoke vl_e 'GetAttributes)
						       )
						     )
						   )
						)	  		
					  (setq All_data (cons (list (vla-get-EffectiveName vl_e)
								     (Car valid_tag) (rtos (car ins_point) 2) (rtos (cadr ins_point) 2)
											     )
								     All_data))
				  )
	  		All_data
			)
	  )
	(cond
	  	(	(null All_data)		)
		(	(setq Excel_exist (vlax-get-or-create-object "Excel.Application"))
		 	(setq col 1 row 1)
		 	(setq  xlCells  (vlax-get-property
                                     (vlax-get-property
                                       (vlax-get-property
                                         (vlax-invoke-method
                                           (vlax-get-property Excel_exist "Workbooks")
                                             "Add"
                                         )
                                         "Sheets"
                                       )
                                       "Item" 1
                                     )
                                     "Cells"
                                   )
                   	)
			((lambda (n)
				(foreach val '("Block Name" "Value" "X" "Y" )
				  (vlax-put-property xlCells 'Item row (setq n (1+ n)) val)
					 )
				   )
				  0
				)
		 
		 	(vlax-put-property xlCells 'ColumnWidth 15.00)		 
			(foreach itm (vl-sort All_data '(lambda (a b)(< (Car a)(car b))))
			  (setq row (1+ row))
			  	((lambda (n)
					(foreach val itm
					  (vlax-put-property xlCells 'Item row (setq n (1+ n)) val)
						 )
					   )
					  0
					)
			  )
		 	(vla-put-visible Excel_exist :vlax-true)
			(mapcar (function (lambda (obj) 
					(and obj (eq 'VLA-OBJECT (type obj)) (not (vlax-object-released-p obj))
					      (not
					        (vl-catch-all-error-p
					          (vl-catch-all-apply
					            (function vlax-release-object) (list obj)
						    )
					          )
					        )
					      )
				    	)	
					)
			(list Excel_exist xlCells))
		 	(setq Excel_exist nil
			      xlCells nil)
		 		)	
		(	(setq csvfile (getfiled "Enter CSV file name"
				(strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)))   "CSV" 1))
		 
			(setq filetowrite (open csvfile "w"))
			(foreach itm (vl-sort All_data '(lambda (a b)(< (Car a)(car b))))
			(write-line (strcat (Car itm) "," (Cadr itm)) filetowrite))
			(close filetowrite)
		 	(startapp "notepad")
		 	)
		)
	  )
  (princ)
	)

 

You'll be able to save the excel file to whatever name and location after the program ends.

HTH

 

 

 

 

Message 7 of 12

Yamishon_Noah
Enthusiast
Enthusiast

Hi HTH,

 

 

Thanks very much for your support on this, really appreciate it.

 

After command execution it opened excel (*.xlsx), where as i want to save as "Exported" name and same path of cad drawing kept and after msg box "exported"

 

Is it possible to update the code please?

 

 

Thanks

 

YN

 

 

0 Likes
Message 8 of 12

Yamishon_Noah
Enthusiast
Enthusiast

Hi pBe,

 

 

Is it possible to do this step for multiple drawings at a time?

 

ex: I have 25 drawings in one folder

 

when I execute command it will ask to select folder and then export block coordinates as *.csv to the same folder (file name same as cad dwg).

 

I really appreciate your help on this.

 

thanks

Message 9 of 12

Yamishon_Noah
Enthusiast
Enthusiast

Any experts can help on this please?...

 

 

Thanks

0 Likes
Message 10 of 12

Anonymous
Not applicable

I added one more tag in the attribute block.

 

I want to export all of tags and the contents

 

plz modify the codes once more. Thank you very much.

0 Likes
Message 11 of 12

Sea-Haven
Mentor
Mentor

I am sure if a donation is made the please change will occur faster. 

 

There should be a answer block to excel lisp did you google ? Lots of youtube etc.

0 Likes
Message 12 of 12

pbejse
Mentor
Mentor

@Anonymous wrote:

I added one more tag in the attribute block.

plz modify the codes once more. Thank you very much.


Are you Yamishon_Noah?  The way you ask for modification is as if you are the original poster. 

 

0 Likes