View Full Version : Attribute Extraction routine
ivan_sosa_b
2006-03-02, 03:54 PM
Hi every one. I have a question: When I use the attribute extract wizard (typing "eattext") on the option "select drawings"; it leaves out 3 of the 9 files I need to process. All these files have the block I need to extract info from. It kind of suggests that the eattext command is limited to a certain number of files to process, the problem is that the files it leaves out are not the last ones but in a random fashion along the list as the Autocad browser shows them. Once the process is done and eattext is applied to the files it left out the first time it processes them with no problems. Any Ideas? I am using Autocad 2005. Looking for a solution:
I found the following lisp routine to do this and its instructions. The problem is that it extracts info from all the blocks with attributes on the drawings processed, it writes a column for the attribute value and another one with the attribute tag to the right of the first one -this may be a matter of interchanging two rows of code-, and it would be nice if it put the tags as titles of columns and the data under each column.
The instructions are:
"To process a few drawings drag the lisp file icon in the drawing area. The program will auto start.
A possible usage for batch processing: open a new drawing, go to TOOLS > AutoLisp > LOAD� and in the "Load/Unload Application" window locate the lisp file and drag it in the Startup Suit. Now this program will be loaded in every drawing. Close the window and go to FILE > OPEN. Select all the files you wish to process (hold shift/Ctrl to select multiple files) and press OPEN. Relax and watch the screen. After the last file opened search the file C:attributes.CSV and open it with Excel. Probable a double-click on the file will make it open in Excel, but that also depends on your settings.
If a dwg contains no blocks with attributes you will see an error message in the AutoCAD command line. Please ignore it.
And don't forget to remove the program from the Startup Suit!
P.S. Without an adequate filter the program will extract all your attributes -including the data in the title data block, date stamp and so on.
The code is:
; Global ATTribute EXtractor
; by Miklos Fuccaro mfuccaro@hotmail.com
;-------------------------November 2004 -------
(defun gattex ()
(setq ss (ssget "X" '((0 . "INSERT") (66 . 1))))
(if (not ss)
(quit)
)
(setq file (open "c:attributes.CSV" "a")
i -1
)
(write-line
(strcat (getvar "DWGPREFIX")
(getvar "DWGNAME")
" -found "
(itoa (sslength ss))
" block(s) with attributes"
)
file
)
(repeat (sslength ss)
(setq l (entget (setq e (ssname ss (setq i (1+ i))))))
(write-line
(strcat "block name:" "," (cdr (assoc 2 l)))
file
)
(while (/= (cdr (assoc 0 l)) "SEQEND")
(if (= (cdr (assoc 0 l)) "ATTRIB")
(write-line
(strcat ",," (cdr (assoc 1 l)) "," (cdr (assoc 2 l)))
file
)
)
(setq l (entget (setq e (entnext e))))
)
)
(close file)
(princ)
)
(gattex)
It seems that the editor takes out the tab and spaces at the begining of each row. I hope it does not make a difference.
I know I may sound too picky but the truth is I don't know any programing or Lisp. Can someone please help?
Thank you for your time.:)
[ Moderator Action = ON ] What are [ CODE ] tags... (http://forums.augi.com/misc.php?do=bbcode#code) [ Moderator Action = OFF ]
Mike.Perry
2006-03-02, 10:22 PM
Hi "ivan_sosa_b" ( Sorry, I do not know your real name )
Please note I have *moved* this thread from the AutoCAD Customization (http://forums.augi.com/forumdisplay.php?f=118) forum to this one, as I believe it will be better served here.
Thanks, Mike
Forum Moderator
ivan_sosa_b
2006-03-03, 01:01 AM
Thank you. You are right. I think I did not see this option.
tyshofner
2006-03-03, 05:13 PM
Hello ivan,
Here is an edited version of the code, what I changed is in red, and where you see "YOURBLOCKNAME" is where you would need to place the name of the block you want to filter for. I also adjusted the writing of the "*.csv" file so that the "tag" is in the first column with its value to the right, and I removed the "blockname" portion. (not sure if thats what you wanted or not, but it's a start).
; Global ATTribute EXtractor
; by Miklos Fuccaro mfuccaro@hotmail.com
;-------------------------November 2004 -------
(defun gattex ()
(setq ss (ssget "X" '((0 . "INSERT") (2 . "YOURBLOCKNAME") (66 . 1))))
(if (not ss)
(quit)
)
(setq file (open "c:attributes.CSV" "a")
i -1
)
(write-line
(strcat (getvar "DWGPREFIX")
(getvar "DWGNAME")
" -found "
(itoa (sslength ss))
" block(s) with attributes"
)
file
)
(repeat (sslength ss)
(setq l (entget (setq e (ssname ss (setq i (1+ i))))))
;;; (write-line
;;; (strcat "block name:" "," (cdr (assoc 2 l)))
;;; file
;;; )
(while (/= (cdr (assoc 0 l)) "SEQEND")
(if (= (cdr (assoc 0 l)) "ATTRIB")
(write-line
(strcat (cdr (assoc 2 l)) "," (cdr (assoc 1 l)))
file
)
)
(setq l (entget (setq e (entnext e))))
)
)
(close file)
(princ)
)
(gattex)
Ty :mrgreen:
ivan_sosa_b
2006-03-03, 06:12 PM
Hi. tyshofner!
It still takes the information from all the blocks with attributes.
when Autocad opens the drawings it shows an error on the message area that reads:
AutoCAD menu utilities loaded.; error: misplaced dot on input
What do you think is wrong? :)
kennet.sjoberg
2006-03-03, 08:39 PM
Try to change
(open "c:attributes.CSV" "a")
to
(open "c:\\attributes.CSV" "a")
or check other lisp that loads at startup
you can drag'n drop lispfiles in to AutoCAD to find the file that cause the error
: ) Happy Computing !
kennet
kennet.sjoberg
2006-03-03, 09:10 PM
. . . or try this
;;; Global ATTribute EXtractor
;;; by Miklos Fuccaro mfuccaro@hotmail.com
;;; ------------------------November 2004 ------- Modified in 2006
(defun gattex (/ SelSet File#1 Counter EntName EntDxf )
(if (setq SelSet (ssget "X" '((0 . "INSERT") (2 . "YOURBLOCKNAME" ) (66 . 1 )))) ;; <-- Change "YOURBLOCKNAME" to appropriate BlockName
(progn
(if (setq File#1 (open "c:\\attributes.CSV" "a" ) )
(progn
(setq Counter -1 )
(write-line (strcat (getvar "DWGPREFIX" ) (getvar "DWGNAME" ) " -found " (itoa (sslength SelSet )) " block(s) with attributes" ) File#1 )
(repeat (sslength SelSet )
(setq EntDxf (entget (setq EntName (ssname SelSet (setq Counter (1+ Counter ))))) )
(while (/= (cdr (assoc 0 EntDxf )) "SEQEND" )
(if (= (cdr (assoc 0 EntDxf )) "ATTRIB" )
(write-line (strcat (cdr (assoc 2 EntDxf )) "," (cdr (assoc 1 EntDxf ))) File#1 ) ; Comma
;; if TAB (write-line (strcat (cdr (assoc 2 EntDxf )) " " (cdr (assoc 1 EntDxf ))) File#1 ) ; Tab
( )
)
(setq EntDxf (entget (setq EntName (entnext EntName ))) )
)
)
(close File#1 )
(princ "Done ! " )
)
(princ ". . file not found " )
)
)
(princ "No legal block found " )
)
(princ)
)
(gattex)
: ) Happy Computing !
kennet
ivan_sosa_b
2006-03-03, 09:13 PM
Hi, kennet.sjoberg!
The change you proposed still takes info from all blocks. It generates a set of the desired data on different columns though; and you can manipulate the data some what.
Since it seems it cannot be told to read only from one block; can we tell it to specifically not read from the other blocks?
T.Willey
2006-03-03, 09:16 PM
What are the names of the blocks you want to read the attributes from? That is the information the people need to help you.
kennet.sjoberg
2006-03-03, 09:29 PM
Hi, kennet.sjoberg!
The change you proposed still takes info from all blocks. It generates a set of the desired data on different columns though; and you can manipulate the data some what.
Since it seems it cannot be told to read only from one block; can we tell it to specifically not read from the other blocks?
Yes, the program is designed to take all block with the same name
Do you want to manually select one of them ?
or a couple ?
or one at a specific coordinate ?
is the block name not necessary ?
: ) Happy Computing !
kennet
kennet.sjoberg
2006-03-03, 09:41 PM
This one let you manually select all kind and numbers of blocks with attributes. . .
;;; Global ATTribute EXtractor
;;; by Miklos Fuccaro mfuccaro@hotmail.com
;;; ------------------------November 2004 ------- Modified in 2006
(defun gattex (/ SelSet File#1 Counter EntName EntDxf )
(princ "\nCommand: Select Block(s) to extract attributes from : " )
(if (setq SelSet (ssget '((0 . "INSERT") (66 . 1 ))))
(progn
(if (setq File#1 (open "c:\\attributes.CSV" "a" ) )
(progn
(setq Counter -1 )
(write-line (strcat (getvar "DWGPREFIX" ) (getvar "DWGNAME" ) " -found " (itoa (sslength SelSet )) " block(s) with attributes" ) File#1 )
(repeat (sslength SelSet )
(setq EntDxf (entget (setq EntName (ssname SelSet (setq Counter (1+ Counter ))))) )
(while (/= (cdr (assoc 0 EntDxf )) "SEQEND" )
(if (= (cdr (assoc 0 EntDxf )) "ATTRIB" )
(write-line (strcat (cdr (assoc 2 EntDxf )) "," (cdr (assoc 1 EntDxf ))) File#1 ) ;; Comma as delimiter
; (write-line (strcat (cdr (assoc 2 EntDxf )) " " (cdr (assoc 1 EntDxf ))) File#1 ) ;; Tab as delimiter
( )
)
(setq EntDxf (entget (setq EntName (entnext EntName ))) )
)
)
(close File#1 )
(princ "Done ! " )
)
(princ ". . file not found or already open " )
)
)
(princ "No block selected " )
)
(princ)
)
(gattex)
: ) Happy Computing !
kennet
ivan_sosa_b
2006-03-03, 11:24 PM
Hi there!
I need to extract info from one block called "I" that exists once in all drawings. All of its tags are included in other blocks called "A", "B", "C", or "D" as in the names of the title block that correspond to the different drawing sizes. On each drawing -of 1 sheet-, only one of the "A", "B". "C" or "D" blocks exists; the drawings of more than one sheet have more than one block "D", "C" etc... The multiple instances and the different names of these blocks are the reasons for which I made the block "I" as a "control block" which by means of the "att-rep" command copies the values of the tags of the "B"."C", "D"... blocks; this mentioned "I" block appears only once even if the drawing consists of more than one sheet. I am attaching a copy of a report I need to generate from the "I" info. and the output I get from the routine.
As you can see the info includes data from the blocks "D", "B" and "C". The columns C and D correspond respectively to "value" and "tag name". The desired result would be to have tag under C column and value under D column. The tags should be column titles.
I know the tags have odd names, this is the way I found things here I began in this job last December. I would be an enormous task to change them all.
To kind of flatten things out, the output I am attaching is generated by the following code:
; Global ATTribute EXtractor
; by Miklos Fuccaro mfuccaro@hotmail.com
;-------------------------November 2004 -------
(defun gattex ()
(setq ss (ssget "X" '((0 . "INSERT") (2 . "I") (66 . 1))))
(if (not ss)
(quit)
)
(setq file (open "c:attributes.CSV" "a")
i -1
)
(write-line
(strcat (getvar "DWGPREFIX")
(getvar "DWGNAME")
" -found "
(itoa (sslength ss))
" block(s) with attributes"
)
file
)
(repeat (sslength ss)
(setq l (entget (setq e (ssname ss (setq i (1+ i))))))
;;; (write-line
;;; (strcat "block name:" "," (cdr (assoc 2 l)))
;;; file
;;; )
(while (/= (cdr (assoc 0 l)) "SEQEND")
(if (= (cdr (assoc 0 l)) "ATTRIB")
(write-line
(strcat (cdr (assoc 2 l)) "," (cdr (assoc 1 l)))
file
)
)
(setq l (entget (setq e (entnext e))))
)
)
(close file)
(princ)
)
(gattex)Thank you very much for your time. :)
[ Moderator Action = ON ] What are [ CODE ] tags... (http://forums.augi.com/misc.php?do=bbcode#code) [ Moderator Action = OFF ]
T.Willey
2006-03-03, 11:36 PM
So you want all the attributes from the block "l"? Do you write any lisp?
(defun GetAttsl (/ ss)
(if (setq ss (ssget "x" '((0 . "INSERT") (66 . 1) (2 . "l"))))
(mapcar
'(lambda (x)
(cons (vla-get-TagString x) (vla-get-TextString x))
)
(vlax-invoke (vlax-ename->vla-object (ssname ss 0)) 'GetAttributes)
)
)
)
This will return a list of all the attributes for the block, in the form of (TagName . Value) or nil if the block doesn't exist. Then you can just print that returned value to you cvs file.
ivan_sosa_b
2006-03-04, 02:31 AM
Hi!
Yes I want the info from the block "I" on each drawing. And no I don't know how to write Autolisp.
My work has ended for the week :) and I did not get a chance to try the last lisp codes that you guys kindly show on this thread. I will try them on Monday and let you know how it went.
Have a good weekend you all!
Thank you.
ivan_sosa_b
2006-03-10, 05:37 PM
Hi you all!
Okay, some one helped me change code and this is the one that gave me the results I wanted.
On the first row where "name" appears you type the block name from which to extract attributes. On the second row where "name" appears you list the tag names to be extracted from the blocks. In both cases you have to type in the names exactly as they are named in the block names and tag names -capitals, dashes and so on-.
Always remember to remove the program form the start suite because if you don't you are going to generate *.csv files everywhere you open a drawing from since the program writes the .csv file inside the directory the drawing is at. The file generated can be opened and edited in excel. The data is very manageable in the sense of taking off what you don't want and end up with a file with named columns and their values lined up under each column.
I am attaching a file of instructions and the actual code.
Thank you. :)
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.