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

#11

Buzzard
Join Date:
12-25-2007
I tested it without the changes and get the same thing.
The error does not allow the code to load at all.
 
Buzzard
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 (= "JACK-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 (= "JACK-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

#12

Bennett
Join Date:
07-26-2007
Put a "(" before your defun line. Like (defun c:AddQtys etc.
Somehow it got left out when you copied from the code window. It should work now.
Program to add up BOM quantities

#13

Buzzard
Join Date:
12-25-2007
Thanks Bennett all your time effort.
 
It works great.
 
Buzzard
Program to add up BOM quantities

#14

JPS
Join Date:
10-06-2007
I find these lines very interesting:
 
(if (= (atoi number) (atof number))
(setq number (atoi number))
(setq number (atof number))
)
 
Nice code to return a real or integer from a "string" number . I like it.
 
JPS
Program to add up BOM quantities

#15

Darren
Join Date:
10-25-2007
Guys,
This is a great thread! I'm already using the QTY's part, but at my company we also have a LENGTH attribute in our BOM block. Is it possible to get a total of the each QTY x LENGTH attribute? Our BOM block has attributes for ITEMNUM, PARTNUM, QTY, PARTDESC, LENGTH, and WEIGHT. Which brings up another idea. Can it also get the total of the weights?
Darren
Program to add up BOM quantities

#16

J Briggs
Join Date:
07-25-2007
Darren,
Here's a slight modification to the AddQtys function that should work for you.
QtyxLens is for quantities x lengths.
Code:
(defun c:QtyxLens (/ elist ename etype index lenghts quantity selset subtotal total)
  (princ "\nSelect blocks for total of quantities x lengths.")
  (if (setq selset (ssget (list '(66 . 1))))
    (progn
      (setq total 0)
      (setq index 0)
      (repeat (sslength selset)
        (setq subtotal 0)
        (setq quantity nil)
        (setq lenghts nil)
        (setq ename (ssname selset index))
        (setq elist (entget ename))
        (while (/= (cdr (assoc 0 elist)) "SEQEND")
          (setq elist (entget ename))
          (setq etype (cdr (assoc 2 elist)))
          (if (= "QTY" etype);Change attribute tag name here
            (setq quantity (atoi (cdr (assoc 1 elist))))
          )
          (if (= "LENGTH" etype);Change attribute tag name here
            (setq lenghts (distof (cdr (assoc 1 elist)) 3))
          )
          (if (and quantity lenghts)
            (progn
              (setq subtotal (* lenghts quantity))
              (setq total (+ total subtotal))
              (princ "\n(")(princ quantity)(princ " x ")(princ lenghts)(princ " = ")
              (princ subtotal)(princ ") total = ")(princ total)
              (setq quantity nil)
              (setq lenghts nil)
            )
          )
          (setq ename (entnext ename))
        )
        (setq index (1+ index))
      )
    )
  )
  (princ)
)
Program to add up BOM quantities

#17

Arch_Eric
Join Date:
04-02-2008
Sorry, I misread the original post. Didn't see the text was in attributes.
 
Quote:
I've got a function called DAREA that I wrote for dynamically updating text with the area of polylines. You can link
polylines to text objects (for the area) and also link text objects to other text objects (for the sum of areas). The links
are stored in an XData dictionary so they're saved with the drawing. 
  
You could use the sum function for adding up your BOM and update it without having to select the text objects
everytime.
  
Load the darea.lsp in AutoCAD. Run DAREA, type PA for ParentAdd, select your total text object, type S for Sum,
then select your text objects to add up. When you're done, it totals them and updates the total text. To update the
sums at any time, type DREC 
  
(The vlax-ldata.lsp is a library of functions that simplifies XData management and is loaded automatically by
darea.lsp)

Attachments  darea.lsp    vlax-ldata.lsp  
Program to add up BOM quantities

#18

Darren
Join Date:
10-25-2007
Thanks for all your help. Nice program Arch!
Darren
Page 2 of 2
1 2