Exchange Forum > AutoLISP and Visual LISP
Program to add up BOM quantities
Page 1 of 2
1 2
Program to add up BOM quantities

#1

B Matlock
Join Date:
10-10-2007
Does anyone have a program that adds up Bill of Material quantities? At my last job they had a program that did this. It sure would come in handy at my new job if I had a similar program. I’m not a programmer but all I can tell you is that each BOM line is a separate block. If I explode it the attribute text changes to QTY in that column. The last BOM block is where we have to add up and place the total quantities. I appreciate any help or leads on finding such a program.
Thanks,
Bill
Program to add up BOM quantities

#2

J Briggs
Join Date:
07-25-2007
Here’s an AutoLISP function that I came across in my archives. You may need to tweak it a little for the precision for real numbers. Other than that I think it works for text, mtext and attributes.
James
Code:
(defun c:Addup (/ elist ename epick number text total)
  (princ "\nSelect quantities to add up. Press Enter when finished.\n")
  (setq total 0)
  (while (setq epick (nentsel ""))
    (setq elist (entget (car epick)))
    (if (assoc 1 elist)
      (progn
        (setq number (cdr (assoc 1 elist)))
        (if (= (atoi number) (atof number))
          (setq number (atoi number))
          (setq number (atof number))
        )
        (princ number) (princ " + ")
        (setq total (+ total number))
      )
    )
  )
  (if (> total 0)
    (progn
      (if (= (fix total) (float total))
        (setq total (itoa total))
        (setq total (rtos total 2 2));change precision?
      )
      (setq text (strcat "\nSelect text object to replace with " total ": "))
      (if (setq epick (nentsel text))
        (progn
          (setq ename (car epick))
          (setq elist (entget ename))
          (if (assoc 1 elist)
            (progn
              (entmod (subst (cons 1 total) (assoc 1 elist) elist))
              (entupd ename)
            )
          )
        )
      )
    )
  )
  (princ)
)
Program to add up BOM quantities

#3

B Matlock
Join Date:
10-10-2007
I tested your Addup program and it works great. That’s exactly what I needed.
Thanks for your help.
Program to add up BOM quantities

#4

Buzzard
Join Date:
12-25-2007
I also tested your routine, But is there a way to window the objects all at one time.
This would be helpful with drawings that have alot of objects to add up.
Program to add up BOM quantities

#5

Bennett
Join Date:
07-26-2007
Buzzard,
It might be easier to get a selection using the attribute tag names to add up. Do you know the attribute tag name? If not explode one of the blocks and it will display the tag name.
Program to add up BOM quantities

#6

J Briggs
Join Date:
07-25-2007
Bennett,
This function is similar to AddUp, but allows the user to select a window of blocks to add up the QTY tags. They can change the QTY tag name in the code as needed in two places. The lines end in ";Change tag name here".
 
Code:
(defun c:AddQtys (/ elist ename epick index number ssqtys text total)
  (princ "\nSelect blocks to add up quantities.")
  (if (setq ssqtys (ssget (list '(66 . 1))))
    (progn
      (setq total 0)
      (setq index 0)
      (repeat (sslength ssqtys)
        (setq ename (ssname ssqtys index))
        (setq elist (entget ename))
        (while (/= (cdr (assoc 0 elist)) "SEQEND")
          (setq elist (entget ename))
          (if (= "QTY" (cdr (assoc 2 elist)));Change tag name here
            (progn
              (setq number (cdr (assoc 1 elist)))
              (if (= (atoi number) (atof number))
                (setq number (atoi number))
                (setq number (atof number))
              )
              (princ number) (princ " + ")
              (setq total (+ total number))
            )
          )
          (setq ename (entnext ename))
        )
        (setq index (1+ index))
      )
    )
  )
  (if (> total 0)
    (progn
      (if (= (fix total) (float total))
        (setq total (itoa total))
        (setq total (rtos total 2 2))
      )
      (setq text (strcat "\nSelect block to update quantity with " total ": "))
      (if (setq epick (entsel text))
        (progn
          (setq ename (car epick))
          (setq elist (entget ename))
          (while (/= (cdr (assoc 0 elist)) "SEQEND")
            (setq elist (entget ename))
            (if (= "QTY" (cdr (assoc 2 elist)));Change tag name here
              (progn
                (entmod (subst (cons 1 total) (assoc 1 elist) elist))
                (entupd ename)
              )
            )
            (setq ename (entnext ename))
          )
        )
      )
    )
  )
  (princ)
)
Program to add up BOM quantities

#7

Buzzard
Join Date:
12-25-2007
Thanks for getting back to me Bennett,
 
I am using Telecommunication symbols that represent an outlet location,
and has an attribute tag JACK-QTY. The quantities can vary between
2 and upto 12. The drawings I work with can have from 300 upto 500 of
these symbols. It would not be productive to click each one. I have seen
many applications for this, But yours works well. I just need a way to window all symbols.
 
I would be appreciated if you can help.
Buzzard
Program to add up BOM quantities

#8

J Briggs
Join Date:
07-25-2007
You can select a window for the prompt "Select objects:" or you can type "All" to select all blocks. In the code above just change the tag "QTY" to your tag name "JACK-QTY". It's in the code twice.
Program to add up BOM quantities

#9

Buzzard
Join Date:
12-25-2007
Thanks again Bennett for getting back to me so quick. I changed the QTY variable in the program and tested it on my symbol but I get

error: bad argument type: numberp: nil
 
Any suggestions?
 
Buzzard
Program to add up BOM quantities

#10

Bennett
Join Date:
07-26-2007
In the code from J. Briggs did you change the tag name from "QTY" to "JACK-QTY" in all capitals? Can you attach you revised code here so we can debug it? I'm trying to figure out what would cause the numberp error in his code.
 
Page 1 of 2
1 2