(изменено: Nike, 19 июля 2010г. 11:59:52)

Тема: LISP. Подсчет блоков по значению атрибутов

Замечательная программулина, альтернатива автокадовскому "Извлечению атрибутов", позволяет несколькими кликами подсчитать в чертеже как просто одноименные блоки, так и одноименные блоки с различными значениями атрибутов.  Вывод в текстовое окно AutoCAD или в файл txt.
Взято отсюда http://www.cadtutor.net/forum/attach... … 1272375444
Комментарии в программе на хранцузском языке.
Offtop: Знатоки хранцузского! Помогите перевести комменты!

;;;=================================================================
;;;
;;; LSTATT.LSP V4.01
;;;
;;; Dйcompte des blocs
;;;
;;; Copyright (C) Patrick_35
;;;
;;;=================================================================
 
(defun c:lstatt(/ choix i js ent fic fil lst n nb nm nombl InputBox liste_att rechercher_nom sel tbl trier txt)
 
  (defun nombl(bl)
    (if (vlax-property-available-p bl 'effectivename)
      (vla-get-effectivename bl)
      (vla-get-name bl)
    )
  )
 
  (defun choix(/ bl js lst nom sel)
    (princ "\nSйlectionnez le(s) bloc(s) а dйnombrer : ")
    (and (ssget (list (cons 0 "ins ert")))
      (progn
    (vlax-for bl (setq sel (vla-get-activeselectionset (vla-get-activedocument (vlax-get-acad-object))))
      (or (member (setq nom (nombl bl)) lst)
        (setq lst (cons nom lst))
      )
      (redraw (vlax-vla-object->ename bl) 4)
    )
    (foreach nom lst
      (if js
        (setq js (strcat js "," nom))
        (setq js nom)
      )
    )
    (vla-delete sel)
      )
    )
    js
  )
 
  (defun InputBox (Titre js / ch dcl fil res tmp txt)
    (setq tmp (vl-filename-mktemp "lstatt" nil ".dcl")
      fil (open tmp "w")
      fic "0"
    )
    (foreach txt '(    "lstatt : dialog {"
            "  key = \"titre\";"
            "  alignment = centered;"
            "  is_cancel = true;"
            "  allow_accept = true;"
            "  width = 30;"
            "  : boxed_column {"
            "    label = \"Veuillez donner un nom de bloc ou * pour tous\";"
            "    : row {"
            "      : edit_box {key = \"filtre\";width = 45;}"
            "      : button {key = \"choix\"; label = \">>\";}"
            "    }"
            "    spacer;"
            "  }"
            "  : boxed_column {"
            "    label = \"Nombre d'attributs а prendre en compte\"; "
            "    : edit_box {key= \"att\";}"
            "    spacer;"
            "  }"
            "  spacer;"
            "  : toggle {key = \"fic\"; label = \"Ecrire les rйsultats dans un fichier\";}"
            "  spacer;"
            "  ok_cancel;"
            "}"
         )
      (write-line txt fil)
    )
    (close fil)
    (setq dcl (load_dialog tmp))
    (while (not (member res '(0 1)))
      (new_dialog "lstatt" dcl "")
      (set_tile "titre" titre)
      (set_tile "filtre" js)
      (set_tile "att" nb)
      (set_tile "fic" fic)
      (action_tile "filtre" "(setq js $value)")
      (action_tile "choix"  "(done_dialog 2)")
      (action_tile "att"    "(setq nb $value)")
      (action_tile "fic"    "(setq fic $value)")
      (action_tile "accept" "(done_dialog 1)")
      (action_tile "cancel" "(done_dialog 0)")
      (setq res (start_dialog))
      (and (eq res 2)
       (setq ch (choix))
       (setq js ch)
      )
    )
    (unload_dialog dcl)
    (vl-file-delete tmp)
    (if (member res '(1 2))
      js
      ""
    )
  )
 
  (defun liste_att(att / n lst val)
    (if (< (atoi nb) (length att))
      (progn
    (setq n 0)
    (while (and (< n (atoi nb)) (setq val (nth n att)))
      (setq lst (cons (vla-get-textstring (nth n att)) lst)
        n (1+ n)
      )
    )
    (reverse lst)
      )
      (mapcar 'vla-get-textstring att)
    )
  )
 
  (defun rechercher_nom(val / att nom tbl)
    (setq nom (nombl val))
    (if (eq (vla-get-hasattributes val) :vlax-true)
      (if (member (setq att (vlax-invoke val 'getattributes)) '(nil))
    (list nom)
    (cons nom (liste_att att))
      )
      (list nom)
    )
  )
 
  (defun trier(a b / c n s)
    (setq c 0)
    (while (and (not s) (nth c a))
      (if (eq (nth c a) (nth c b))
    (setq c (1+ c))
    (setq s T)
      )
    )
    (or (nth c a) (setq c 0))
    (< (strcase (nth c a)) (strcase (nth c b)))
  )
 
  (vl-load-com)
  (or (setq nb (getenv "Patrick_35_nb_att"))
    (setq nb "1")
  )
  (if (not (eq (setq nm (InputBox "Dйcompte de blocs V4.01" "*")) ""))
    (progn
      (setq js (strcat "`**," nm))
      (if (ssget (list (cons 0 "INS ERT") (cons 2 js)))
    (progn
      (setenv "Patrick_35_nb_att" nb)
      (vlax-map-collection    (setq sel (vla-get-activeselectionset (vla-get-activedocument (vlax-get-acad-object))))
                '(lambda (x)
                  (if (wcmatch (strcase (car (setq js (rechercher_nom x)))) (strcase nm))
                    (if (eq (vla-get-objectname x) "AcDbMInsertBlock")
                      (repeat (* (vla-get-columns x) (vla-get-rows x))
                    (setq tbl (cons js tbl))
                      )
                      (setq tbl (cons js tbl))
                    )
                  )
                )
      )
      (vla-delete sel)
      (while tbl    
        (setq n   (length tbl)
          js  (car tbl)
          tbl (vl-remove js tbl)
          lst (cons (cons (itoa (- n (length tbl))) js) lst)
        )
      )
      (if lst
        (progn
          (and (eq fic "1")
        (setq fil (open (setq txt (strcat (getvar "dwgprefix") (vl-filename-base (getvar "dwgname")) ".txt")) "w"))
          )
          (foreach n (vl-sort lst '(lambda (a b) (trier (cdr a) (cdr b))))
        (if (eq fic "1")
          (princ (strcat (car n) (chr 9) (cadr n)) fil)
          (princ (strcat "\n"
                 (substr "     " 1 (- 5 (strlen (car n))))
                 (car n)
                 " "
                 (cadr n)
             )
          )
        )
        (setq i 2)
        (while (setq val (nth i n))
          (if (eq fic "1")
            (princ (strcat (chr 9) val) fil)
            (princ (strcat "..." val))
          )
          (setq i (1+ i))
        )
        (and (eq fic "1")
          (write-line "" fil)
        )
          )
          (and (eq fic "1")
        (princ (strcat "\nFichier \"" txt "\" crйй."))
        (close fil)
          )
        )
        (princ "\nPas de bloc а dйnombrer.")
      )
    )
      )
    )
  )
  (princ)
)
 
(se tq nom_lisp "LSTATT")
(if (/= app nil)
  (if (= (strcase (substr app (1+ (- (strlen app) (strlen nom_lisp))) (strlen nom_lisp))) nom_lisp)
    (princ (strcat "..." nom_lisp " chargй."))
    (princ (strcat "\n" nom_lisp ".LSP Chargй.....Tapez " nom_lisp " pour l'йxecuter.")))
  (princ (strcat "\n" nom_lisp ".LSP Chargй......Tapez " nom_lisp " pour l'йxecuter.")))
(se tq nom_lisp nil)
(princ)

Re: LISP. Подсчет блоков по значению атрибутов

C хранцузским напряжёнка. По первому атрибуту считает блки. Клик по блоку.
(defun c:bbya( / blc sp_bl imja filtr nameset nm_at_o 
                 sp_at_o tx_at_o nm_bl nm_at sp_at tx_at
                 kol setlength i namrun spirun l_run )
(setq blc (car (entsel)))
(setq sp_bl (entget blc)) ; spisok
(setq imja (assoc 2 sp_bl)) ;p imja
(setq nm_at_o  (entnext blc)); imya attributa
(setq sp_at_o (entget nm_at_o)) ; spisok attributa
(setq tx_at_o  (cdr(assoc 1 sp_at_o))); atribut 1
(setq filtr (list '( 0 . "INS ERT") imja))
(setq nameset (ssget "X" filtr)) ;imya nabora
(setq setlength (sslength nameset)) ;dlina nabora
(setq kol 0)
(setq i -1)
(repeat setlength
  (setq i (1+ i))
  (setq nm_bl (ssname nameset i)) ;imya i
  (setq nm_at  (entnext nm_bl)); imya attributa
  (setq sp_at (entget nm_at)) ; spisok attributa
  (setq tx_at  (cdr(assoc 1 sp_at))); atribut 1
  (if(= tx_at_o tx_at)(se tq kol (1+ kol)))
)
(princ "\nKol-vo blokov s atributon ")
(princ tx_at_o)
(princ " = ")
(princ kol)
(princ)
)

Re: LISP. Подсчет блоков по значению атрибутов

Считает блоки по выбранному атрибуту:

(defun C:TT ()
  (setq e1 (nentsel "\nSelect attribute to filter: "))
   (setvar "cmdecho" 0)
   (setq eget (entget (car e1)))
   (setq EX_STR (cdr (assoc 1 EGET)))   ;EXISTING TEXTSTRING
   (setq ex_tag (cdr (assoc 2 EGET)))   ;EXISTING tag
  (SETQ PT1 (CADR E1))
  (SETQ SS0 (SSGET PT1))
  (SETQ BLKNAME (CDR (ASSOC 2 (ENTGET (SSNAME SS0 0)))))
  (prompt (strcat "\n Block: " blkname "   Attribute tag: " ex_tag "   >: " ex_str)) 
;______________ SELECTING BLOCKS "BLKNAME" _________________
  (SETQ LST1 (LIST '(0 . "INS ERT") (CONS 2 BLKNAME)) )
  (SETQ SS1 (SSGET "_X" LST1))
; (SETQ SS1 (SSGET LST1))
; (IF (NULL SS1) (SETQ SS1 (SSGET "_X" LST1)) )
  (setq SSM (SSADD))
  (setq len1 (sslength ss1) n1 0 ssx (ssadd))
  (WHILE (< n1 len1) ;WHILE 1
    (setq ename1 (ssname ss1 n1) eget1 (entget ename1) CTRL1 nil COUNTER 0 str1 "")
    (SETQ en1 ename1)
    ;____ Find Tag Level
    (while (and (null ctrl1) (/= (CDR (ASSOC 0 (ENTGET (setq en1 (ENTNEXT en1))))) "SEQEND"))
           (setq tag1 (CDR (ASSOC 2 (ENTGET en1))))
           (if (= tag1 ex_tag) (setq str1 (CDR (ASSOC 1 (ENTGET en1))) ctrl1 T))
           (setq counter (1+ counter))
    ) ;end while2
    ;_____
    ;(if (= str1 ex_str) (princ str1))
    (if (= (STRCASE str1) (STRCASE ex_str)) (setq ssx (ssadd ename1 ssx)))
    (setq n1 (1+ n1))
  ) ; end WHILE1
  (setq lenx (sslength ssx)) 
  (command "_.select" ssx "")
  (PROMPT (strcat "\n Match found : [" (itoa lenx) "].   Selected objects are stored in Previous Selection."))
  (se tvar "cmdecho" 1)
  (princ)
)
;_____________________________________________________________
(prompt "\n Start command with [TT]  - by Raymond Rizkallah -  April 06. ")
(PRINC)