Author Topic: How to statistics the total number of parts?  (Read 9907 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5218
Re: How to statistics the total number of parts?
« Reply #15 on: January 07, 2010, 12:57:44 PM »
Here is one I just wrote.  Seemed fun, and I need to stretch the grey matter.
Code: [Select]
(defun c:ShowNesting (/ *error* ActDoc BlkCol Sel EntData BlkName NestList)
   
    (defun *error* ( msg )
       
        (vl-bt)
    )
    ;------------------------------
    (defun GetBlockBlocks ( blkObj doc / BlkCol BlkCntList tempList tempName )
       
        (setq BlkCol (vla-get-Blocks doc))
        (vlax-for obj blkObj
            (if (= (vla-get-ObjectName obj) "AcDbBlockReference")
                (setq BlkCntList
                    (if (setq tempList (assoc (setq tempName (vla-get-Name obj)) BlkCntList))
                        (subst (list tempName (1+ (cadr tempList)) (caddr tempList)) tempList BlkCntList)
                        (cons (list tempName 1(GetBlockBlocks (vla-Item BlkCol tempName) doc)) BlkCntList)
                    )
                )
            )
        )
        BlkCntList
    )
    ;-------------------------------------
    (defun PrintList ( lst indent )
       
        (prompt (strcat "\n" indent (car lst) " - " (itoa (cadr lst))))
        (foreach i (caddr lst)
            (PrintList i (strcat "    " indent))
        )
    )
    ;-------------------------------------
    (setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
    (setq BlkCol (vla-get-Blocks ActDoc))
    (if
        (and
            (setq Sel (entsel "\n Select block to get nesting block list: "))
            (setq EntData (entget (car Sel)))
            (= (cdr (assoc 0 EntData)) "INSERT")
            (setq BlkName (cdr (assoc 2 EntData)))
            (setq NestList (GetBlockBlocks (vla-Item BlkCol BlkName) ActDoc))
        )
        (progn
            (prompt (strcat "\nReport for:" BlkName))
            (foreach lst NestList
                (PrintList lst "    ")
            )
        )
    )
    (princ)
)
Returns to the command line
Quote
Report for:testing
    test - 3
        DI-NOTE - 1
        CIR-NOTE - 4
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Lee Mac

  • Seagull
  • Posts: 12253
  • London, England
Re: How to statistics the total number of parts?
« Reply #16 on: January 07, 2010, 01:04:02 PM »
Another bit of code looking at nested blocks, not sure whether it helps?

http://www.theswamp.org/index.php?topic=29988.0

CAB

  • Global Moderator
  • Seagull
  • Posts: 10362
Re: How to statistics the total number of parts?
« Reply #17 on: January 07, 2010, 01:11:55 PM »
Looked good on my test Tim. 8-)
Code: [Select]
Select block to get nesting block list:
Report for:sub-ass2-1
    sub-ass-1 - 2
        part-1 - 1
        part-2 - 1
        part-3 - 1
    sub-ass-2 - 1
        part-3 - 1
        part-2 - 1
        part-1 - 1
    sub-ass-3 - 1
        part-3 - 1
        part-2 - 1
        part-1 - 1
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

T.Willey

  • Needs a day job
  • Posts: 5218
Re: How to statistics the total number of parts?
« Reply #18 on: January 07, 2010, 01:14:12 PM »
Thanks Alan.  I didn't have too many nested blocks, so I just created a simple one.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

xianaihua

  • Newt
  • Posts: 40
Re: How to statistics the total number of parts?
« Reply #19 on: January 07, 2010, 07:13:18 PM »
Thanks Alan,Tim

Thank you for your reply.You spent valuable time



In mechanical design, in each assembly has a bom, the bom sets out the assembly of all the sub-assembly and parts of some information, such as: code, name, quantity, material, weight, etc., but these sub - assembly and parts in the model space is not necessarily a block reference, may be a  line, arc, pline, etc., bom data is entered manually. It is not possible with the search block refer to the number of ways of doing.



The question now is: can be a folder of all the following dwg files of these bom data extraction, and how to process these data?
« Last Edit: January 07, 2010, 07:20:41 PM by xianaihua »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How to statistics the total number of parts?
« Reply #20 on: January 07, 2010, 07:46:31 PM »
xianaihua,

Are you exporting each BOM to a .CSV file ??

if yes ;
is there a seperate .CSV file for each assembly ?
and ; are the CSV files the same name as the assembly ?

How are you generating the lists shown in your first post ??

Regards
Kerry

Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

CAB

  • Global Moderator
  • Seagull
  • Posts: 10362
Re: How to statistics the total number of parts?
« Reply #21 on: January 07, 2010, 07:53:42 PM »
It would help if you supplied a sample DWG file.
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

xianaihua

  • Newt
  • Posts: 40
Re: How to statistics the total number of parts?
« Reply #22 on: January 08, 2010, 01:44:03 AM »
xianaihua,

Are you exporting each BOM to a .CSV file ??

if yes ;
is there a seperate .CSV file for each assembly ?
and ; are the CSV files the same name as the assembly ?

How are you generating the lists shown in your first post ??

Regards
Kerry


Hi,Kerry
I exporting all BOM from assembly's dwg(a folder of all the following dwg files )  to a database (.mdb) file .

xianaihua

  • Newt
  • Posts: 40
Re: How to statistics the total number of parts?
« Reply #23 on: January 08, 2010, 01:50:17 AM »
It would help if you supplied a sample DWG file.

ok,I have supplied a sample DWG file and BOM.txt file.

xianaihua

  • Newt
  • Posts: 40
Re: How to statistics the total number of parts?
« Reply #24 on: November 01, 2011, 11:37:09 PM »
Hi,all
I had already won the way to solve this problem, the following is my practice, everyone please review a review, or give some advice
Code: [Select]
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
;;                                                                               ;;
;;  AUTHOR:  lihuaili, November 2011.                                            ;;
;;...............................................................................;;
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
;;;In the schedule list, statistics a part in code number
;;;Statistical method: first find out the parts in the assembly code father quantity, to find the father in the more higher level assembly father assemble number... ,
;;;And so on, until inquires the top to assembly, the number of these factors, and that of the parts of the total number.
;;;I through trial and error, finally using a recursive method solved the difficult problem.
;;;Parameters:
;;;Bom_lst: bom all list; 【 list 】
;;;Assemble_No: father assembly code; 【 str 】
;;;Part_No_index: spare parts list list adept in code in the list of the index number (note: the first listed as 0) 【 INT 】
;;;Assemble_No_index: assembly code in the list belong to the list of the index number list adept 【 INT 】
;;;Num_index: in its parts assembly (father assembly) of the quantity, understood in the list of the index number list adept 【 INT 】
;;;return:
;;;Total number 【INT】

(defun SumPartCounts (bom_lst       assemble_No     part_No_index
      assemble_No_index       Num_index
      /       tol_num
     )
  (or tol_num (setq tol_num 1))
  (foreach x bom_lst
    (if (= assemble_No (nth part_No_index x))
      (setq assemble_No (nth assemble_No_index x)
    tol_num (* tol_num
   (atoi (nth Num_index x))
   (SumPartCounts
     bom_lst        assemble_No
     part_No_index     assemble_No_index
     Num_index
    ) ;_ 结束SumPartCounts
 ;_ 结束SumPartCounts
) ;_ 结束*
      ) ;_ 结束setq
    ) ;_ 结束if

  ) ;_ 结束foreach
  tol_num
) ;_ 结束defun

;;;writie list data  to the text file
(defun WriteDataFile (file_name data_lst / _tostring file)
;by:Lee Mac
  (defun _tostring (x / dimzin)
    (cond
      ((eq 'INT (type x))
       (itoa x)
      )
      ((eq 'REAL (type x))
       (setq dimzin (getvar 'DIMZIN))
       (setvar 'DIMZIN 0)
       (setq x (rtos x 2 8))
       (setvar 'DIMZIN dimzin)
       x
      )
      ((vl-prin1-to-string x))
    ) ;_ 结束cond
  ) ;_ 结束defun

  (if (setq file (open file_name "w"))
    (progn
      (foreach x data_lst (write-line (_tostring x) file))
      (setq file (close file))
      t
    ) ;_ 结束progn
  ) ;_ 结束if
) ;_ 结束defun

;;;bomlst:Is the way to get through some parts list (list specific methods in this not discussed)
;;;Among them the first listed as parts of each line of code
;;;The second listed as parts assembly code belongs
;;;The third listed as in its parts assembly of quantity

(setq bomlst '(
       ("sub-assembly1-1" "assembly" "2")
       ("sub-assembly1-2" "assembly" "3")
       ("sub-assembly1-3" "assembly" "1")
       ("pat1-1" "assembly" "3")
       ("pat1-2" "assembly" "2")
       ("pat1-3" "assembly" "5")
       ("sub-assembly2-1" "sub-assembly1-1" "2")
       ("sub-assembly2-2" "sub-assembly1-1" "2")
       ("sub-assembly2-3" "sub-assembly1-1" "4")
       ("pat2-1" "sub-assembly1-1" "3")
       ("pat2-2" "sub-assembly1-1" "4")
       ("pat1-3" "sub-assembly1-1" "2")
       ("pat3-1" "sub-assembly2-1" "2")
       ("pat3-2" "sub-assembly2-1" "3")
       ("pat2-1" "sub-assembly2-1" "1")
      )
) ;_ 结束setq
(defun c:test (/ partnumlst Ap_No p_num partnumlst fname)
  (setq partnumlst '())
  (foreach n (reverse bomlst)
    (setq Ap_No      (nth 1 n)
  p_num      (atoi (nth 2 n))
  partnumlst
     (cons
       (append n
       (list (itoa (* (SumPartCounts bomlst Ap_No 0 1 2) p_num)))
       ) ;_ 结束append
       partnumlst
     ) ;_ 结束cons
    ) ;_ 结束setq
  ) ;_ 结束foreach

  (setq
    fname (getfiled "save list data input filename";

    (getvar "dwgprefix")
    "txt"
    3
  ) ;_ 结束getfiled
  ) ;_ 结束setq
  (if fname
    (progn
      (WriteDataFile fname partnumlst)
      (alert "The current data has been preserved!");
    ) ;_ 结束progn
  ) ;_ 结束if
) ;_ 结束defun
« Last Edit: December 04, 2011, 07:51:56 PM by xianaihua »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1540
  • Moscow (Russia)
Re: How to statistics the total number of parts?
« Reply #25 on: November 21, 2011, 03:38:09 PM »
ElpanovEvgeniy
Good weekend!
If you have time, please optimization or simplify my program In the following posts。
Thanks agan!
http://www.theswamp.org/index.php?topic=31524.msg452218#msg452218

:)

Hi,all
I had already won the way to solve this problem, the following is my practice, everyone please review a review, or give some advice
Code: [Select]
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
;;                                                                               ;;
;;  AUTHOR:  lihuaili, November 2011.                                            ;;
;;...............................................................................;;
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
;;;In the schedule list, statistics a part in code number
;;;Statistical method: first find out the parts in the assembly code father quantity, to find the father in the more higher level assembly father assemble number... ,
;;;And so on, until inquires the top to assembly, the number of these factors, and that of the parts of the total number.
;;;I through trial and error, finally using a recursive method solved the difficult problem.
;;;Parameters:
;;;Bom_lst: bom all list; 【 list 】
;;;Assemble_No: father assembly code; 【 str 】
;;;Part_No_index: spare parts list list adept in code in the list of the index number (note: the first listed as 0) 【 INT 】
;;;Assemble_No_index: assembly code in the list belong to the list of the index number list adept 【 INT 】
;;;Num_index: in its parts assembly (father assembly) of the quantity, understood in the list of the index number list adept 【 INT 】
;;;return:
;;;Total number 【INT】

(defun SumPartCounts (bom_lst       assemble_No     part_No_index
      assemble_No_index       Num_index
      /       tol_num
     )
  (or tol_num (setq tol_num 1))
  (foreach x bom_lst
    (if (= assemble_No (nth part_No_index x))
      (setq assemble_No (nth assemble_No_index x)
    tol_num (* tol_num
   (atoi (nth Num_index x))
   (SumPartCounts
     bom_lst        assemble_No
     part_No_index     assemble_No_index
     Num_index
    ) ;_ 结束SumPartCounts
 ;_ 结束SumPartCounts
) ;_ 结束*
      ) ;_ 结束setq
    ) ;_ 结束if

  ) ;_ 结束foreach
  tol_num
) ;_ 结束defun

;;;writie list data  to the text file
(defun WriteDataFile (file_name data_lst / _tostring file)
  (defun _tostring (x / dimzin)
    (cond
      ((eq 'INT (type x))
       (itoa x)
      )
      ((eq 'REAL (type x))
       (setq dimzin (getvar 'DIMZIN))
       (setvar 'DIMZIN 0)
       (setq x (rtos x 2 8))
       (setvar 'DIMZIN dimzin)
       x
      )
      ((vl-prin1-to-string x))
    ) ;_ 结束cond
  ) ;_ 结束defun

  (if (setq file (open file_name "w"))
    (progn
      (foreach x data_lst (write-line (_tostring x) file))
      (setq file (close file))
      t
    ) ;_ 结束progn
  ) ;_ 结束if
) ;_ 结束defun

;;;bomlst:Is the way to get through some parts list (list specific methods in this not discussed)
;;;Among them the first listed as parts of each line of code
;;;The second listed as parts assembly code belongs
;;;The third listed as in its parts assembly of quantity

(setq bomlst '(
       ("sub-assembly1-1" "assembly" "2")
       ("sub-assembly1-2" "assembly" "3")
       ("sub-assembly1-3" "assembly" "1")
       ("pat1-1" "assembly" "3")
       ("pat1-2" "assembly" "2")
       ("pat1-3" "assembly" "5")
       ("sub-assembly2-1" "sub-assembly1-1" "2")
       ("sub-assembly2-2" "sub-assembly1-1" "2")
       ("sub-assembly2-3" "sub-assembly1-1" "4")
       ("pat2-1" "sub-assembly1-1" "3")
       ("pat2-2" "sub-assembly1-1" "4")
       ("pat1-3" "sub-assembly1-1" "2")
       ("pat3-1" "sub-assembly2-1" "2")
       ("pat3-2" "sub-assembly2-1" "3")
       ("pat2-1" "sub-assembly2-1" "1")
      )
) ;_ 结束setq
(defun c:test (/ partnumlst Ap_No p_num partnumlst fname)
  (setq partnumlst '())
  (foreach n (reverse bomlst)
    (setq Ap_No      (nth 1 n)
  p_num      (atoi (nth 2 n))
  partnumlst
     (cons
       (append n
       (list (itoa (* (SumPartCounts bomlst Ap_No 0 1 2) p_num)))
       ) ;_ 结束append
       partnumlst
     ) ;_ 结束cons
    ) ;_ 结束setq
  ) ;_ 结束foreach

  (setq
    fname (getfiled "save list data input filename";

    (getvar "dwgprefix")
    "txt"
    3
  ) ;_ 结束getfiled
  ) ;_ 结束setq
  (if fname
    (progn
      (WriteDataFile fname partnumlst)
      (alert "The current data has been preserved!");
    ) ;_ 结束progn
  ) ;_ 结束if
) ;_ 结束defun

my variant:
Code: [Select]
(defun test (l / f)
  (defun f (a l) (if a (* (atoi (caddr a)) (f (assoc (cadr a) l) l)) 1))
  (mapcar (function (lambda (a) (append a (list (itoa (f a l)))))) l)
)

check:
Code: [Select]
(setq l '(("sub-assembly1-1" "assembly" "2")
               ("sub-assembly1-2" "assembly" "3")
               ("sub-assembly1-3" "assembly" "1")
               ("pat1-1" "assembly" "3")
               ("pat1-2" "assembly" "2")
               ("pat1-3" "assembly" "5")
               ("sub-assembly2-1" "sub-assembly1-1" "2")
               ("sub-assembly2-2" "sub-assembly1-1" "2")
               ("sub-assembly2-3" "sub-assembly1-1" "4")
               ("pat2-1" "sub-assembly1-1" "3")
               ("pat2-2" "sub-assembly1-1" "4")
               ("pat1-3" "sub-assembly1-1" "2")
               ("pat3-1" "sub-assembly2-1" "2")
               ("pat3-2" "sub-assembly2-1" "3")
               ("pat2-1" "sub-assembly2-1" "1")
              )
)
Code: [Select]
(test l)
>>
(("sub-assembly1-1" "assembly" "2" "2")
  ("sub-assembly1-2" "assembly" "3" "3")
  ("sub-assembly1-3" "assembly" "1" "1")
  ("pat1-1" "assembly" "3" "3")
  ("pat1-2" "assembly" "2" "2")
  ("pat1-3" "assembly" "5" "5")
  ("sub-assembly2-1" "sub-assembly1-1" "2" "4")
  ("sub-assembly2-2" "sub-assembly1-1" "2" "4")
  ("sub-assembly2-3" "sub-assembly1-1" "4" "8")
  ("pat2-1" "sub-assembly1-1" "3" "6")
  ("pat2-2" "sub-assembly1-1" "4" "8")
  ("pat1-3" "sub-assembly1-1" "2" "4")
  ("pat3-1" "sub-assembly2-1" "2" "8")
  ("pat3-2" "sub-assembly2-1" "3" "12")
  ("pat2-1" "sub-assembly2-1" "1" "4")
)
蝸牛そろそろ登れ富士の山 /Kobayashi Issa/

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1540
  • Moscow (Russia)
Re: How to statistics the total number of parts?
« Reply #26 on: November 21, 2011, 03:53:38 PM »
Hi,  xianaihua! :)

I'm not sure that is true to understand the purpose of the program.
I have done the same result with your code...

蝸牛そろそろ登れ富士の山 /Kobayashi Issa/

xianaihua

  • Newt
  • Posts: 40
Re: How to statistics the total number of parts?
« Reply #27 on: November 21, 2011, 07:04:17 PM »
Hi,  xianaihua! :)

I'm not sure that is true to understand the purpose of the program.
I have done the same result with your code...

Hi,Evgeniy
excellent!!!
Thanks you help me!
This is what I wanted the results.You is a true recursion experts, you of the recursive use of magic.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1540
  • Moscow (Russia)
Re: How to statistics the total number of parts?
« Reply #28 on: November 22, 2011, 02:35:22 AM »
Hi,Evgeniy
excellent!!!
Thanks you help me!
This is what I wanted the results.You is a true recursion experts, you of the recursive use of magic.


In 2006, I published an article "Lessons from the creation of recursive functions."
You may be interested in reading this series of lessons...
 :-)
Recursion in AutoLISP Lesson 1
Recursion in AutoLISP Lesson 2
Recursion in AutoLISP Lesson 3
Recursion in AutoLISP Lesson 4
Recursion in AutoLISP Lesson 5
Recursion in AutoLISP Lesson 6
Recursion in AutoLISP Lesson 7
Recursion in AutoLISP Lesson 8
Recursion in AutoLISP Lesson 9
Recursion in AutoLISP Lesson 10
蝸牛そろそろ登れ富士の山 /Kobayashi Issa/

nivuahc

  • Guest
Re: How to statistics the total number of parts?
« Reply #29 on: November 22, 2011, 09:16:13 AM »
ElpanovEvgeniy,

What are the chances that you'd be willing to put that together in a single PDF, available for download?