CODE correction for extract ATTRIBUTE

CODE correction for extract ATTRIBUTE

Anonymous
Not applicable
2,723 Views
25 Replies
Message 1 of 26

CODE correction for extract ATTRIBUTE

Anonymous
Not applicable

Dear All,

 

I am writing a code to extract block ATTRIBUTE into excel file like below. I can load the lisp without any issue. While running in CAD the loop is not completed. And i put something wrong somewhere. but i could not correct that. Could you please anyone correct me and teach me where i put wrong. ?

 

I have attached my Lisp and CAD block file here.

 

(defun c:ATTX()
	(setq filePath "c:\\temp\\")
	(setq fileName "P&ID List")
	(setq fileLoc (strcat filePath fileName ".csv"))
	(setq xcelfile(open fileLoc "w"))
	(setq blocks (ssget "_X" '((0 . "INSERT") (8 . "VALVE"))))
	(setq ctr 0)
	(setq len (sslength blocks))
	(write-line (strcat "ValveTag") xcelfile)
	(while (/= ctr len)
		
		(setq blk (ssname blocks ctr))
		(setq blks (entget blk))
		
		(setq flag T)
		(while flag
			(setq lArea (entget(entnext(cdr(assoc -1 blks)))))
			
			(setq attrib1 (cdr(assoc 0 lArea)))
			
			(if (= attrib1 "ATTRIB")
				(progn
					(setq tagnameArea (cdr(assoc 2 lArea)))
					
					(if (= tagnameArea "VALVETAG")
						(setq valveTag (cdr (assoc 1 lArea)))
						
						
					)
				)
				(setq flag nil)
			)
		)
		(write-line (valveTag) xcelfile)
		(setq ctr (1+ ctr))
	)
	(close xcelfile)
	(princ (strcat " Extraction is complete. " fileName " has been created. "))
	(princ)
)

 

0 Likes
Accepted solutions (2)
2,724 Views
25 Replies
Replies (25)
Message 21 of 26

_Tharwat
Advisor
Advisor

Come on Alan, please try to change your way of contributing in threads all over forums, copy and paste will keep you in the same level and would embarrass you from time to time with the wrong inputs, try to write routines from scratch all the time to go forward and to enhance your abilities in this domain.

Understanding the mechanism and the return values of codes is a must to write successful codes all the time.

 

0 Likes
Message 22 of 26

Anonymous
Not applicable

Sorry to disturb you again,

I made an addition in that code you gave. But i have some issue in the output.

This is the code i modified.

(defun c:ATTX ()
	(if
		(and
			;;	ensure that there are block objects on this layer	;;;
			(setq blocks (ssget "_X" '((0 . "INSERT") (8 . "VALVE"))))
			;; Checks if directory exist			;;
			(findfile (setq filePath "c:\\temp"))			
			(setq fileName "P&ID List")
			(setq fileLoc (strcat filePath "\\" fileName ".csv"))
			(setq xcelfile(open fileLoc "w"))
			(setq ctr 0)
			(setq len (sslength blocks))
		)
		(progn
			(write-line (strcat "VALVE NAME,VALVE TAG") xcelfile)
			;; 	while ctr valeu is less than len	;;
			;; 	selection sets are 0 index		;;
			(while (< ctr len)
				(setq blk (ssname blocks ctr))
					(while
						(and
							(null valveList)
							(setq blk (entnext blk))
							(= "ATTRIB" (cdr (assoc 0 (setq enx (entget blk)))))
							(= "ATTRIB" (cdr (assoc 0 (setq enx1 (entget(entnext(cdr (assoc -1 enx))))))))
							(setq vName (entget blk))
						)
						(setq valveTag (cdr (assoc 1 (reverse enx))))
						(setq valveArea (cdr (assoc 1 (reverse enx1))))
						(setq valveList (strcat valveTag " " valveArea))
						(setq valveName (cdr (assoc 2 vName)))
					)
				(write-line (strcat valveName "," valveList) xcelfile)
				(setq ctr (1+ ctr) valveList nil)
			)
			(close xcelfile)
			(princ (strcat " Extraction is complete. " fileName " has been created. "))
		)
		(princ "\nNo valid block(s) found")
	)
(princ)
)

 

The output i am getting is like the below.

 

Capture.JPG

 

 

 

 

 

 

But the correct output should be,

 

Capture1.JPG

 

 

 

 

 

 

Do you have any idea why am getting the wrong info,

I have attached my CAD file also.

0 Likes
Message 23 of 26

pbejse
Mentor
Mentor

@Anonymous wrote:

Sorry to disturb you again,

I made an addition in that code you gave. But i have some issue in the output...

 


From the looks of it, you are wanting to get the block name and the tag value(s) [ 2 ]

 

(defun c:ATTX ( / blocks filePath fileName fileLoc bname lst xcelfile ctr len valvetag blk )
  (if
    (and
        ;;	ensure that there are block objects on this layer	;;;
       (setq blocks (ssget "_X" '((0 . "INSERT")(66 . 1)(8 . "VALVE"))))
       	;; Checks if directory exist			;;
	(findfile (setq filePath "c:\\temp"))			
	(setq fileName "P&ID List"
		fileLoc (strcat filePath "\\" fileName ".csv"))
	(setq xcelfile(open fileLoc "w"))	
	(setq ctr 0 len (sslength blocks))
	)
       (progn
	 
	(write-line (strcat "VALVE NAME,VALVE TAG") xcelfile)
	;; 	while ctr valeu is less than len	;;
	;; 	selection sets are 0 index		;;
	(while (< ctr len)
	(setq blk (ssname blocks ctr))
	  
	(setq bname (getpropertyvalue blk "BlockTableRecord/Name"))
	    (while (and (setq blk (entnext blk)) (= "ATTRIB" (cdr (assoc 0 (setq enx (entget blk))))))
	        (setq lst
	            (cons
	                (cdr (assoc 1 (reverse enx)))
		                lst
		            )
		        )
		    )
  	  (setq lst (reverse lst))
	  (write-line (strcat bname "," (car lst) " " (cadr lst)) xcelfile)
	  (setq ctr (1+ ctr) lst nil)
	  )
	(close xcelfile)
	(princ (strcat " Extraction is complete. " fileName " has been created. "))
		)
    (princ "\nNo valid block(s) found")
    )
	(princ)
)

 

0 Likes
Message 24 of 26

Anonymous
Not applicable

Thanks again,

I am getting an error (; error: ADS request error) in the below line

 

(setq bname (getpropertyvalue blk "BlockTableRecord/Name"))

 

0 Likes
Message 25 of 26

pbejse
Mentor
Mentor

@Anonymous wrote:

Thanks again,

I am getting an error (; error: ADS request error) in the below line

 

(setq bname (getpropertyvalue blk "BlockTableRecord/Name"))

 


That is strange, what Autocad version are you using anyway?

Anyway. change that line to and tell me how it goes.

 

 

 

(setq bname (cdr (assoc 2 (entget blk ))))

 

 

 

0 Likes
Message 26 of 26

Anonymous
Not applicable

Using AutoCAD 2013,

And the new line is working good,

 

Thanks you 

0 Likes