Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Summing values from attributes

  1. #1
    Member
    Join Date
    2011-09
    Posts
    5
    Login to Give a bone
    0

    Default 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.

  2. #2
    All AUGI, all the time
    Join Date
    2010-06
    Posts
    962
    Login to Give a bone
    0

    Default 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

  3. #3
    Member
    Join Date
    2011-09
    Posts
    5
    Login to Give a bone
    0

    Default 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

  4. #4
    All AUGI, all the time
    Join Date
    2010-06
    Posts
    962
    Login to Give a bone
    0

    Default Re: Summing values from attributes

    No worries mate .

    Can you please upload a piece of drawing contains some of these attributes ?

    Tharwat

  5. #5
    Member
    Join Date
    2011-09
    Posts
    5
    Login to Give a bone
    0

    Default Re: Summing values from attributes

    Quote Originally Posted by tharwat View Post
    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.

  6. #6
    All AUGI, all the time
    Join Date
    2010-06
    Posts
    962
    Login to Give a bone
    0

    Default 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

  7. #7
    I could stop if I wanted to
    Join Date
    2009-03
    Location
    London, England
    Posts
    304
    Login to Give a bone
    0

    Default 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)
    )
    Last edited by Lee Mac; 2011-09-29 at 12:59 PM.

  8. #8
    Member
    Join Date
    2011-09
    Posts
    5
    Login to Give a bone
    0

    Default 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

  9. #9
    I could stop if I wanted to
    Join Date
    2009-03
    Location
    London, England
    Posts
    304
    Login to Give a bone
    0

    Default 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:
    Code:
    "%lu6%qf1"
    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:
    Code:
    "%lu2%pr1"
    These formatting codes can be found using the Field Dialog.

  10. #10
    Member
    Join Date
    2011-09
    Posts
    5
    Login to Give a bone
    0

    Default 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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Block Count via attributes values
    By cadconcepts in forum AutoLISP
    Replies: 21
    Last Post: 2015-02-10, 06:35 PM
  2. Getting Block Attributes/Values (for dummies)
    By stusic in forum AutoLISP
    Replies: 7
    Last Post: 2012-09-22, 03:56 PM
  3. Assign Attributes & Block to Values
    By asma.waqar in forum AutoCAD General
    Replies: 0
    Last Post: 2011-02-15, 06:21 PM
  4. Replies: 13
    Last Post: 2009-12-14, 12:39 AM
  5. Add values to attributes
    By todd.mackay in forum AutoLISP
    Replies: 11
    Last Post: 2008-05-30, 01:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •