Summing values from attributes
Hello !
I'm looking for solution how to quickly sum values from my attributes, and put them in another attribute. Now it takes to much time, i got a lot of attribs in my drawing and it takes 7-8 steps for each value, so its a little annoying.
This is how it looks like:
http://img801.imageshack.us/img801/9580/wys3.jpg
best regards!
t.
Re: Summing values from attributes
Hope this would help you with your work ... :)
Code:
(defun c:TesT (/ *error* acdoc n ss p i sset v)
;; ==-- Tharwat 29. Sep. 2011 --==;;
(vl-load-com)
(defun *error* (msg)
(or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
(princ (strcat "\n** Error: " msg " **"))
)
(princ)
)
(setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object)))
(if (and (setq n 0
ss (ssget '((0 . "ATTDEF")))
)
(setq p (getpoint "\n Specify Attributes text location :"))
)
(progn
(repeat (setq i (sslength ss))
(setq sset (ssname ss (setq i (1- i))))
(if
(not (eq (atof (setq v (cdr (assoc 2 (entget sset))))) 0.0))
(setq n (+ n (atoi v)))
)
)
(vla-StartUndoMark acdoc)
(if (not (eq n 0.0))
(entmakex (list '(0 . "TEXT")
(cons 40 (getvar 'textsize))
(cons 10 p)
(cons 1 (rtos n 2))
'(50 . 0.)
)
)
)
)
(princ)
)
(vla-EndUndoMark acdoc)
(princ)
)
Tharwat
Re: Summing values from attributes
>Hope this would help you with your work ... :)
Thank You for fast answer! :)
Unfortunately Your routine does not work. Acad prompts :
0 found, 0 total
i've tried lisp that was summing values from mtext and it was prompting same message :|
Re: Summing values from attributes
No worries mate .
Can you please upload a piece of drawing contains some of these attributes ?
Tharwat
Re: Summing values from attributes
Quote:
Originally Posted by
tharwat
No worries mate .
Can you please upload a piece of drawing contains some of these attributes ?
Tharwat
of course, here it is:
http://wrzucacz.pl/file/6961317292227/sample.dwg
t.
Re: Summing values from attributes
This would select each value and calculate the sum in one text as a result , so you should hit
the attribute value directly to get the value of it and insert it in a text not a field .
Code:
(defun c:TesT (/ *error* acdoc n ss e p h v)
;; ==-- Tharwat 29. Sep. 2011 --==;;
(vl-load-com)
(defun *error* (msg)
(or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
(princ (strcat "\n** Error: " msg " **"))
)
(princ)
)
(setq n 0)
(setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object)))
(while
(if
(and (setq ss (car (nentsel "\n Select attribute value in Block :")))
(eq (cdr (assoc 0 (setq e (entget ss)))) "ATTRIB")
)
(progn
(if
(not (eq (atof (setq v (cdr (assoc 1 e)))) 0.0))
(setq h (assoc 40 e)
n (+ n (atof v))
)
)
)
)
)
(vla-StartUndoMark acdoc)
(if
(and
(not (eq n 0.0))
(setq p (getpoint "\n Specify Attributes text location :"))
)
(entmakex
(list '(0 . "TEXT")
h
(cons 10 p)
(cons 1 (rtos n 2 4))
'(50 . 0.)
)
)
)
(vla-EndUndoMark acdoc)
(princ)
)
Tharwat
Re: Summing values from attributes
I think this would benefit from the use of Fields:
Code:
(defun c:AddAttribs ( / _SelectIf _list->string _objectID attrib doc lst num total )
;; © Lee Mac 2011
(setq format "%lu6%qf1") ;; Field Formatting
(defun _SelectIf ( message predicate / entity ) (setq predicate (eval predicate))
(while
(progn (setvar 'ERRNO 0) (setq entity (car (nentsel message)))
(cond
( (= 7 (getvar 'ERRNO))
(princ "\nMissed, try again.")
)
( (eq 'ENAME (type entity))
(if (and predicate (not (predicate entity)))
(princ "\nInvalid Object.")
)
)
)
)
)
entity
)
(defun _list->string ( lst del )
(if (cdr lst)
(strcat (car lst) del (_list->string (cdr lst) del))
(car lst)
)
)
(defun _objectID ( doc obj )
(if (vl-string-search "64" (getenv "PROCESSOR_ARCHITECTURE"))
(vlax-invoke-method (vla-get-utility doc) 'getobjectidstring obj :vlax-false)
(itoa (vla-get-objectid obj))
)
)
(setq total 0.0)
(setq doc (vla-get-activedocument (vlax-get-acad-object)))
(while
(setq attrib
(_SelectIf "\nSelect Attribute <Done>: "
(function
(lambda ( entity )
(eq "ATTRIB" (cdr (assoc 0 (entget entity))))
)
)
)
)
(if (setq num (distof (cdr (assoc 1 (entget attrib)))))
(progn
(princ
(strcat "\nValue: "
(rtos num) " -<>- Total: "
(rtos (setq total (+ total num)))
)
)
(setq lst
(cons
(strcat "%<\\AcObjProp Object(%<\\_ObjId "
(_objectID doc (vlax-ename->vla-object attrib)) ">%).TextString>%"
)
lst
)
)
)
(princ "\nAttribute does not contain numerical data.")
)
)
(if
(and lst
(setq attrib
(_SelectIf "\nSelect Attribute for Total: "
(function
(lambda ( entity )
(eq "ATTRIB" (cdr (assoc 0 (entget entity))))
)
)
)
)
)
(progn
(vla-startundomark doc)
(vla-put-textstring (vlax-ename->vla-object attrib)
(if (not (cdr lst))
(car lst)
(strcat "%<\\AcExpr " (_list->string lst " + ") " \\f \"" format "\">%")
)
)
(vla-endundomark doc)
(vla-regen doc acactiveviewport)
)
)
(princ)
)
Re: Summing values from attributes
Thank for both of You for helping me solving this "mindless clicking problem". I'm so glad to make all this stuff faster and easier!!!:)
The only thing that i would ask for , is to change code a little to make sum only one place behind point .0 instead of .000000, i was trying to find this in code, but unfortunately its almost black magic for me :cry:
Re: Summing values from attributes
I have updated my code to include a 'field formatting' variable at the top of the code.
The current setting:
Will use Current Units and Precision; however you can change this formatting to suit your requirements.
So, for example, to use the Decimal Units with 1 dp. precision, the code would be:
These formatting codes can be found using the Field Dialog.
Re: Summing values from attributes
Thank You very much, it all works just perfect! :)
Your website is really popular all around the world, also in Poland!
best regards, t.