CODE correction for extract ATTRIBUTE

CODE correction for extract ATTRIBUTE

Anonymous
Not applicable
2,719 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,720 Views
25 Replies
Replies (25)
Message 2 of 26

hak_vz
Advisor
Advisor
(while (/= ctr len)

 

replace with

(while (< ctr len)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 3 of 26

Anonymous
Not applicable

Thanks for your fastest reply,

But i have no luck.

It is still the same model of loading when running the lisp file.

 

The lisp file is working and its complete the process without writing anything in the excel file. If I put the code like the below. 

(while(> ctr len)

 

0 Likes
Message 4 of 26

hak_vz
Advisor
Advisor

OK. Upload  dwg the file with predefined blocks and layers for easier work.

I will check the code to for error.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 5 of 26

hak_vz
Advisor
Advisor

Try this

 

(defun c:ATTX()
	(cond 
		((and (setq f (getfiled "Output .csv file:" (getvar "dwgprefix") "csv" 3)))
			(setq outstring (strcat "\nExtraction is complete. " f " has been created. "))
			(setq xcelfile (open f "w"))
			(setq blocks (ssget "_X" '((0 . "INSERT") (8 . "VALVE"))))
			(setq ctr 0)
			(setq len (sslength blocks))
			(write-line "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 outstring)
		)
	)
	(princ)
)

 

 

 

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 6 of 26

pbejse
Mentor
Mentor
Accepted solution
(defun c:ATTX ( / blocks filePath fileName fileLoc 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 "ValveTag") xcelfile)
	;; 	while ctr valeu is less than len	;;
	;; 	selection sets are 0 index		;;
	(while (< ctr len)
	(setq blk (ssname blocks ctr))
	    (while
	        (and
	            (null valveTag)
	            (setq blk (entnext blk))
	            (= "ATTRIB" (cdr (assoc 0 (setq enx (entget blk)))))
	        )
			(setq valveTag (cdr (assoc 1 (reverse enx))))
	        )
	  (write-line valveTag xcelfile)
	  (setq ctr (1+ ctr) valveTag nil)
	  )
	(close xcelfile)
	(princ (strcat " Extraction is complete. " fileName " has been created. "))
		)
    (princ "\nNo valid block(s) found")
    )
	(princ)
)
Message 7 of 26

_Tharwat
Advisor
Advisor
Accepted solution

Following your way of coding but you need to localize the variables.

 

 

 

(defun c:ATTX ()
  (setq filePath "c:\\temp\\")
  (setq fileName "P&ID List")
  (setq fileLoc (strcat filePath fileName ".csv"))
  (setq xcelfile (open fileLoc "w"))
  (if (setq blocks (ssget "_X" '((0 . "INSERT")(66 . 1)(8 . "VALVE"))))
    (progn
      (setq len (sslength blocks))
      (write-line (strcat "ValveTag") xcelfile)
      (while (setq blk (ssname blocks (setq len (1- len))))
        (setq blks (entget blk))
        (while
          (and (setq blk (entnext blk))
               (/= (cdr (assoc 0 (setq lArea (entget blk)))) "SEQEND")
          )

           (if (= (cdr (assoc 0 lArea)) "ATTRIB")
             (progn
               (setq tagnameArea (cdr (assoc 2 lArea)))

               (if (= tagnameArea "VALVETAG")
                 (setq valveTag (cdr (assoc 1 lArea)))
               )
             )
           )
        )
        (write-line valveTag xcelfile)
      )
      (close xcelfile)
      (princ (strcat " Extraction is complete. "
                     fileName
                     " has been created. "
             )
      )
    )
  )
  (princ)
)

 

 

0 Likes
Message 8 of 26

_Tharwat
Advisor
Advisor

@pbejse wrote:

 

(write-line (strcat "ValveTag") xcelfile)

 


What's the function STRCAT for? 😉

0 Likes
Message 9 of 26

_Tharwat
Advisor
Advisor

Be careful if the ssget function returns nil, then the program will throw an error and keep the file hanging in the sky without closing. 😉

0 Likes
Message 10 of 26

pbejse
Mentor
Mentor

@_Tharwat wrote:


What's the function STRCAT for? 😉


 

 Bad case of copy and paste is what 😀

 

0 Likes
Message 11 of 26

hak_vz
Advisor
Advisor

@_Tharwat wrote:

Be careful if the ssget function returns nil, then the program will throw an error and keep the file hanging in the sky without closing. 😉


I know. But since @Anonymous  is greasing elbows to learn autolisp I would like him to start testing his code and look for potential potholes. That is best way to learn programing, and we all have gone through this.

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 12 of 26

pbejse
Mentor
Mentor

@hak_vz wrote:

That is best way to learn programing, and we all have gone through this.

Though I agree  with you about learning, but that particular error would be a tough one for a beginner to figure out on their own, would you agree?

 

Cmon @hak_vz , be nice 😉

 

Bet money the OP restarted CAD about 5 times by now,

 

0 Likes
Message 13 of 26

hak_vz
Advisor
Advisor

@Anonymous 

Your code is not bad but you have to start using testing.

Instead using IF statements try to use COND.

With selection sets there is a catch. When you receive selection set always test for its existence and number of elements inside it.

(setq ss (ssget))
(cond 
       ((and ss (> (sslenght ss)0)
        ...... code goes here
       )
)

 

Same principle apply when you use function getfiled to select input/output file through system dialog.

If you hit <esc> during file selection file path string will be nill and code will present an error.

 

(cond 
    ((and (setq f (getfiled "Output .csv file:" (getvar "dwgprefix") "csv" 3)))
   .......
  )
)

.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 14 of 26

_Tharwat
Advisor
Advisor

@hak_vz wrote:

@Anonymous 

Your code is not bad but you have to start using testing.

Instead using IF statements try to use COND.

With selection sets there is a catch. When you receive selection set always test for its existence and number of elements inside it.

 

(setq ss (ssget))
(cond 
       ((and ss (> (sslenght ss)0)
        ...... code goes here
       )
)

 


You don't need to check if the sslength of the selection set is more than one as long as the selection set is not equal to nil.

COND can't be BETTER than IF function nor visa versa because that depends on the situation and sometimes on the experience of the user. 

0 Likes
Message 15 of 26

hak_vz
Advisor
Advisor

@_Tharwat wrote:

@hak_vz wrote:

@Anonymous 

 


You don't need to check if the sslength of the selection set is more than one as long as the selection set is not equal to nil.


Yes, but if you want to have your code bulletproof you have to add it in your code. Once selection set equals null your code will break.

 

COND can't be BETTER than IF function nor visa versa because that depends on the situation and sometimes on the experience of the user. 

 

True and false. It is a different story when number of PROGN blocks starts to rise. Cond is than easier to handle.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 16 of 26

_Tharwat
Advisor
Advisor

@hak_vz wrote:

 

Yes, but if you want to have your code bulletproof you have to add it in your code. Once selection set equals null your code will break.

You got it the wrong way, and this is what I meant based on YOUR cond way,

(cond (ss  ;; this is enough without checking if the length is more than zero.

 


@hak_vz wrote:

True and false. It is a different story when number of PROGN blocks starts to rise. Cond is than easier to handle.


Also True and False, the number of times you use PROGN is less than the number of statements you might use with COND although that I did not refer to that at all.

0 Likes
Message 17 of 26

Sea-Haven
Mentor
Mentor

 

hak_z and Tharwat My $0.05 when writing code I do

 

(if (setq ss ....

(progn

hit enter a few times

)

)

 

Then go back in and add code that way know IF is balanced. Same with REPEAT.

 

It is something I should do more often stopping selection errors and admit to that. 

0 Likes
Message 18 of 26

Anonymous
Not applicable

I have tried with the selection set by manual way, its has a proper contents successful.

But only thing the loop is getting issue.

 

Thanks for the suggestion.

0 Likes
Message 19 of 26

Anonymous
Not applicable

1 st Positive response from your code,

Its running and the excel file is creating successfully. 

Thanks for this.

0 Likes
Message 20 of 26

_Tharwat
Advisor
Advisor

@Anonymous wrote:

1 st Positive response from your code,

Its running and the excel file is creating successfully. 

Thanks for this.


You're welcome anytime. 

0 Likes