Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Getting Lisp to total values in attributes

14 REPLIES 14
SOLVED
Reply
Message 1 of 15
rodb
763 Views, 14 Replies

Getting Lisp to total values in attributes

I've never attempted to get Lisp to total up values contained in a block attribute. So a collection of blocks with  variable quantity of attributes. To search through them and total up the intergers in each "watts" attribute tag of each block that has the same interger value in its "location" attribute tag.

 

so

block number 1 has 2 in Location tag and 50 in its watts tag

block number 2 has 2 in Location tag and 40 in its watts tag

block number 3 has 4 in Location tag and 25 in its watts tag

block number 4 has 3 in Location tag and 30 in its watts tag

block number 5 has 4 in Location tag and 66 in its watts tag

etc

 

this produces a txt file with

Location 2 = 90 watts

Location 3 = 30 watts

Location 4 = 91 watts

etc

 

I think this is really tricky with Lisp because you can't add variables together?

 

or could you loop through the selection set of blocks looking for location 1 then adding the watt values together, then repeat the loop through the selection set of all blocks and look for location 2 etc But how would you know when to stop because you may have anything from 1 location to 50 locations. I'm going to try and get some sleep now!!!!

14 REPLIES 14
Message 2 of 15
hmsilva
in reply to: rodb

Hi rodb.

 

A quick and dirty one, and only as a "demo"...

Untested

 

(defun c:demo (/ a attlst b ent fn hnd i lst obj of s1)
  (vl-load-com)
  (if (and (setq fn (getfiled "" (getvar 'DWGPREFIX) "txt" 1))
	   (setq s1 (ssget '((0 . "INSERT") (66 . 1))))
      )
    (progn
      (repeat (setq i (sslength s1))
	(setq hnd    (ssname s1 (setq i (1- i)))
	      ent    (entget hnd)
	      attlst nil
	      obj    (vlax-ename->vla-object hnd)
	      attlst (vlax-invoke obj 'GetAttributes)
	);; setq
	(foreach att attlst
	  (cond	((= (vla-get-TagString att) "Location")
		 (setq a (vla-get-TextString att))
		)
		((= (vla-get-TagString att) "watts")
		 (setq b (atoi (vla-get-TextString att)))
		)
	  );; cond
	);; foreach
	(if (and a b)
	  (progn
	    (if	(assoc a lst)
	      (setq lst	(subst (cons a (+ b (cdr (assoc a lst)))) (assoc a lst) lst))
	      (setq lst (cons (cons a b) lst))
	    );; if
	    (setq a nil b nil)
	  );; progn
	);; if
      );; repeat
      (if lst
	(progn
	  (setq lst (vl-sort lst (function (lambda (a b) (< (car a) (car b))))))
	  (setq of (open fn "w"))
	  (foreach l lst
	    (write-line (strcat "Location " (car l) " = " (itoa (cdr l)) " watts") of)
	  );; foreach
	  (close of)
	);; progn
      );; if
    );; progn
  );; if
  (princ)
);; demo

 

Hope that helps
Henrique

 

EESignature

Message 3 of 15
3wood
in reply to: rodb

Without any lisp, just use ATTOUT in Express Tools to export the attribute informaiton as a txt file, then open it in Excel.

You can sort the data in Excel by "Location tag", as shown below:

attout.png

 

Then just select all cells under the same "Location" and the sum is at bottom-right of Excel window.

attout2.png

Message 4 of 15
rodb
in reply to: hmsilva

Really strange, it seems to work fine no errors reported but the file does not appear to get written. It asks for a location to save the file but when I look there the file does not exist???? Would be amazing if you can get it to write the file?

Message 5 of 15
pbejse
in reply to: rodb

Quick glance, change these items on the code

 

"Location" to "LOCATION"

"watts" to "WATTS"

 

HTH

Message 6 of 15
rodb
in reply to: 3wood

Thank you for your input. I have suggested that for now and I do if fact do that for the circuits in a drawing having written a VBA script for Excel which automatically adds the circuits up and saves a prn file to import onto a grid in Autocad and I suppose I could make another VBA to do that with the watts but would love to find out how to do it in lsp!

Message 7 of 15
hmsilva
in reply to: rodb


@RodB wrote:

Really strange, it seems to work fine no errors reported but the file does not appear to get written. It asks for a location to save the file but when I look there the file does not exist???? Would be amazing if you can get it to write the file?


Hi rodb,

post a sample block with the attributes...

 

Henrique

EESignature

Message 8 of 15
rodb
in reply to: pbejse

That works but this is what I get

 

Location  = 197 watts

 

problem is I need a total for each location as in

 

Location 2 = 10 watts

Location 3 = 23 watts

Location 4 = 50 watts

etc.

 

so how do you carry a value in a variable as in loc2watt then add to it when you find another Location 2 attribute watt value? 

 

Am really trying to avoid going into Excel but will write a VBA if I have to.

Message 9 of 15
rodb
in reply to: rodb

Here is the test dwg

 

Message 10 of 15
hmsilva
in reply to: rodb

rodb,

 

if you don't have the Location filled with a value the code would error...

Try this version

(defun c:demo (/ a attlst b ent fn hnd i lst obj of s1)
  (vl-load-com)
  (if (and (setq fn (getfiled "" (getvar 'DWGPREFIX) "txt" 1))
	   (setq s1 (ssget '((0 . "INSERT") (66 . 1))))
      )
    (progn
      (repeat (setq i (sslength s1))
	(setq hnd    (ssname s1 (setq i (1- i)))
	      ent    (entget hnd)
	      attlst nil
	      obj    (vlax-ename->vla-object hnd)
	      attlst (vlax-invoke obj 'GetAttributes)
	);; setq
	(foreach att attlst
	  (cond	((and (= (strcase (vla-get-TagString att)) "LOCATION")
		      (/= (vla-get-TextString att) ""))
		 (setq a (vla-get-TextString att))
		)
		((and (= (strcase (vla-get-TagString att)) "WATTS")
		      (/= (vla-get-TextString att) ""))
		 (setq b (atoi (vla-get-TextString att)))
		)
	  );; cond
	);; foreach
	(if (and a b)
	  (progn
	    (if	(assoc a lst)
	      (setq lst	(subst (cons a (+ b (cdr (assoc a lst)))) (assoc a lst) lst))
	      (setq lst (cons (cons a b) lst))
	    );; if
	    (setq a nil b nil)
	  );; progn
	);; if
      );; repeat
      (if lst
	(progn
	  (setq lst (vl-sort lst (function (lambda (a b) (< (car a) (car b))))))
	  (setq of (open fn "w"))
	  (foreach l lst
	    (write-line (strcat "Location " (car l) " = " (itoa (cdr l)) " watts") of)
	  );; foreach
	  (close of)
	);; progn
      );; if
    );; progn
  );; if
  (princ)
);; demo

To have a valid txt you'll have to put some values at Location tag...

 

HTH

Henrique

EESignature

Message 11 of 15
pbejse
in reply to: hmsilva

Suggestions

 

Regarding the sequence, only prompt for file to save if and only if there are datas found

Launch notepad to quickly see the result --> (startapp "notepad" fn)

Message 12 of 15
hmsilva
in reply to: pbejse


@pbejse wrote:

Suggestions

 

Regarding the sequence, only prompt for file to save if and only if there are datas found

Launch notepad to quickly see the result --> (startapp "notepad" fn)


Good suggestions, pBe!

 

Cheers

Henrique

EESignature

Message 13 of 15
rodb
in reply to: hmsilva

Amazing it works that is SO clever! and so much easier than going in and out of Excel. Its awesome, thank you!

 

I did get an error message see below when changing some of the wattage values to test it but that went away when I re-ran it.

 

Command: DEMO

Select objects: all
24 found

Select objects:
; error: Exception occurred: 0xC0000005 (Access Violation)
; warning: unwind skipped on exception
; error: Exception occurred: 0xC0000005 (Access Violation)

Command:

Message 14 of 15
rodb
in reply to: pbejse

Thats a great idea too! it always amazes me how its possible to make AutoCAD do just about anything if you have the brains for it!

Message 15 of 15
hmsilva
in reply to: rodb


@RodB wrote:

Amazing it works that is SO clever! and so much easier than going in and out of Excel. Its awesome, thank you!

 

I did get an error message see below when changing some of the wattage values to test it but that went away when I re-ran it.

 

Command: DEMO

Select objects: all
24 found

Select objects:
; error: Exception occurred: 0xC0000005 (Access Violation)
; warning: unwind skipped on exception
; error: Exception occurred: 0xC0000005 (Access Violation)

Command:


You're welcome, rodb!

Regarding the error, I don't see anything that might cause this error in code, try reopen AutoCAD...

 

Henrique

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost