Author Topic: Count blocks on Layer  (Read 1382 times)

0 Members and 1 Guest are viewing this topic.

MSTG007

  • Gator
  • Posts: 2603
  • I can't remeber what I already asked! I need help!
Count blocks on Layer
« on: July 21, 2015, 08:03:09 AM »
I found this code today. Looks pretty awesome. Again if this not possibly not the end of the world. Just want to know how to swap from blocks to circles, and the last piece of the code usually pops open a text file with the count. I did not know if that could insert that text into a mtext that is stacked.

Here is where the code was at:
http://forums.augi.com/showthread.php?23952-Looking-for-Block-Counter/page2

Code: [Select]
; This routine will create a list of all blocks on a selected layer
; Written By: Peter Jamtgaard P.E. copr 2005

(defun C:CBOL (/ lstEntity lstSelection strLayer)
 (if
  (and (setq lstSelection (entsel "\nSelect block on desired layer: "))
       (setq lstEntity    (entget (car lstSelection)))
       (setq strLayer     (cdr (assoc 8 lstEntity)))
  )
  (countBlocksOnLayer (vla-get-activedocument (vlax-get-acad-object)) strLayer)
 )
)

; General Function for counting blocks on a layer
(defun CountBlocksOnLayer (objDocument strLayer / filTextFile lstBlocks)
 (vlax-for objLayout (vla-get-layouts objDocument)
  (vlax-for objBlock (vla-get-block objLayout)
   (if (and (wcmatch (vla-get-objectname objBlock) "AcDbBlockReference,AcDbMInsertBlock")
            (= (strcase strLayer)(strcase (vla-get-layer objBlock)))
       )
    (progn
     (setq strBlockName (vla-get-name objBlock))
     (if (/= (substr strBlockName 1 1) "*")
      (if lstBlocks       
       (if (assoc strBlockName lstBlocks)
        (setq lstBlocks (subst (cons strBlockName
                                     (1+ (cdr (assoc strBlockName lstBlocks)))
                               )
                               (assoc strBlockName lstBlocks)
                               lstBlocks
                        )   
        )   
        (setq lstBlocks (cons (cons strBlockName 1) lstBlocks))
       )     
       (setq lstBlocks (list (cons strBlockName 1)))   
      )
     )
    ) 
   )
  )
 )
 (print lstBlocks)
 (setq lstBlocks (sortlistofsublistsbyitem lstBlocks 0))
 (print lstBlocks)
 (setq filTextFile (open "temp.txt" "w"))
 (foreach dprPair lstBlocks
  (write-line (strcat (itoa (cdr dprPair))
                      " "
                      (car dprPair)                     
              )
              filTextFile
  )
 )
 (close filTextFile)
 (getstring "\nPress Enter: ")
 (command "notepad" "temp.txt")
)

; General Function for sorting a list of sublists in ascending order of a
; specified item in the sublists:

(defun sortListofSublistsbyItem (lstOfSublists intItem)
 (vl-sort lstOfSublists '(lambda (X Y) (< (nth intItem X) (nth intItem Y))))
)
(princ "\nCountBlocksOnLayer Loaded: ")
(prin1)
Civil3D 2020

ChrisCarlson

  • Guest
Re: Count blocks on Layer
« Reply #1 on: July 21, 2015, 08:17:39 AM »
Derp, misunderstood
« Last Edit: July 21, 2015, 08:46:58 AM by ChrisCarlson »

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Count blocks on Layer
« Reply #2 on: July 21, 2015, 08:45:32 AM »
Here's a start:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:ccol ( / e s ) ;; Count Circles On Layer
  2.     (if (setq e (car (entsel "\nSelect an object on the layer to count: ")))
  3.         (if (setq s (ssget "_X" (list '(0 . "CIRCLE") (assoc 8 (entget e)))))
  4.             (princ (strcat "\nFound " (itoa (sslength s)) " circles."))
  5.             (princ "\nNo circles found.")
  6.         )
  7.     )
  8.     (princ)
  9. )

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Count blocks on Layer
« Reply #3 on: July 21, 2015, 09:11:24 AM »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC