Author Topic: lisp to count number of attributes in block  (Read 4732 times)

0 Members and 1 Guest are viewing this topic.

serge_c

  • Newt
  • Posts: 39
lisp to count number of attributes in block
« on: January 22, 2015, 02:29:06 PM »
is it possible to count the number of attributes in block and prompt it ? This is the easiest question .
and hardest one : is it possible to compare the numbers of attributes between 2 blocks , and prompt if the results is negative ?

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: lisp to count number of attributes in block
« Reply #1 on: January 22, 2015, 03:46:05 PM »
This can be a start:
Code: [Select]
; Function: ALE_Block_GetAttributesFilter
;
; Version 1.00 - 30/11/2005
;
; Description:
;   returns a list of all attributes matching Tag names and Tag values
;
; Arguments:
;   VlaObj: [VLA-OBJECT]
;   TagNms: Tag names  - Wcmatch string > "Date,Scale" or "*" for all
;   TagVls: Tag values - Wcmatch string > "*2005,1:10" or "*" for all
;
; Return Values: LIST
;     tag   value   ObjectID                 vla-object
;  (("Tag1" "Val1" 2130160792 #<VLA-OBJECT IAcadAttributeReference 0abb21e4>) ...)
;
(defun ALE_Block_GetAttributesFilter (VlaObj TagNms TagVls / AttObj TagNam TagVal OutLst)
  (if
    (and
      (= 'VLA-OBJECT (type VlaObj))
      (= (vla-get-hasattributes VlaObj) :vlax-true)
    )
    (foreach ObjFor (vlax-invoke VlaObj 'GetAttributes)
      (if   
        (and
          (wcmatch (setq TagNam (vla-get-TagString  ObjFor))           (strcase TagNms))
          (wcmatch (strcase (setq TagVal (vla-get-TextString ObjFor))) (strcase TagVls))
        )
        (setq OutLst (cons (list TagNam TagVal (vla-get-ObjectID ObjFor) ObjFor) OutLst))
      )
    )
  )
  OutLst
)

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: lisp to count number of attributes in block
« Reply #2 on: January 23, 2015, 05:48:24 AM »
Something like this ?

Code - Auto/Visual Lisp: [Select]
  1. (defun c:Test (/ s ss)
  2. ;;; Tharwat 23.01.2015  ;;;
  3.   (if (and (princ "\n Select FIRST Attributed Block :")
  4.            (setq s (ssget "_+.:S:E" '((0 . "INSERT") (66 . 1))))
  5.            (princ "\n Select the SECOND Attributed Block :")
  6.            (setq ss (ssget "_+.:S:E" '((0 . "INSERT") (66 . 1))))
  7.       )
  8.     (mapcar
  9.       'length
  10.       (mapcar
  11.         '(lambda (a)
  12.            (mapcar
  13.              '(lambda (x) (vla-get-textstring x))
  14.              (vlax-invoke (vlax-ename->vla-object a) 'getattributes)
  15.            )
  16.          )
  17.         (list (ssname s 0) (ssname ss 0))
  18.       )
  19.     )
  20.   )
  21. )

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: lisp to count number of attributes in block
« Reply #3 on: January 23, 2015, 07:19:14 AM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:attcompare ( / a b )
  2.     (if (and (setq a (selectattblock "\nSelect 1st block: "))
  3.              (setq b (selectattblock "\nSelect 2nd block: "))
  4.         )
  5.         (if (apply '/= (mapcar '(lambda ( x ) (length (vlax-invoke (vlax-ename->vla-object x) 'getattributes))) (list a b)))
  6.             (princ "\nNumber of attributes is different.")
  7.             (princ "\nNumber of attributes is identical.")
  8.         )
  9.     )
  10.     (princ)
  11. )
  12. (defun selectattblock ( msg / ent enx )
  13.     (while
  14.         (progn (setvar 'errno 0) (setq ent (car (entsel msg)))
  15.             (cond
  16.                 (   (= 7 (getvar 'errno))
  17.                     (princ "\nMissed, try again.")
  18.                 )
  19.                 (   (null ent) nil)
  20.                 (   (/= "INSERT" (cdr (assoc 0 (setq enx (entget ent)))))
  21.                     (princ "\nSelected object is not a block.")
  22.                 )
  23.                 (   (/= 1 (cdr (assoc 66 enx)))
  24.                     (princ "\nSelected block is not attributed.")
  25.                 )
  26.             )
  27.         )
  28.     )
  29.     ent
  30. )

serge_c

  • Newt
  • Posts: 39
Re: lisp to count number of attributes in block
« Reply #4 on: January 23, 2015, 10:12:26 AM »
Sorry Tharwat and LeeMac , but both lisp dose not work , I try both of them.
I attach a dwg file you can try lisps on it .

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: lisp to count number of attributes in block
« Reply #5 on: January 23, 2015, 10:30:42 AM »
Sorry Tharwat and LeeMac , but both lisp dose not work , I try both of them.
I attach a dwg file you can try lisps on it .
Sorry but all 3 solutions works here:
Code: [Select]
Comando: TEST
 Select FIRST Attributed Block :
 Select the SECOND Attributed Block :
(151 151)

Comando: TEST
 Select FIRST Attributed Block :
Selezionare oggetti:
 Select the SECOND Attributed Block :
Selezionare oggetti:
(297 295)
Code: [Select]
Comando: ATTCOMPARE
Select 1st block:
Select 2nd block:
Number of attributes is different.

Comando:  ATTCOMPARE
Select 1st block:
Select 2nd block:
Number of attributes is identical.
Code: [Select]
(length (ALE_Block_GetAttributesFilter (vlax-ename->vla-object (car (entsel))) "*" "*"))
=> 151
 
(length (ALE_Block_GetAttributesFilter (vlax-ename->vla-object (car (entsel))) "*" "*"))
=> 297
 
(length (ALE_Block_GetAttributesFilter (vlax-ename->vla-object (car (entsel))) "*" "*"))
=> 295

ronjonp

  • Needs a day job
  • Posts: 7531
Re: lisp to count number of attributes in block
« Reply #6 on: January 23, 2015, 10:49:07 AM »
Just tested Lee's here and it worked fine  :?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: lisp to count number of attributes in block
« Reply #7 on: January 23, 2015, 11:09:13 AM »
Me thinks the OP wants a prompt for each att & perhaps update the block.
Just a guess.  ;D
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.

serge_c

  • Newt
  • Posts: 39
Re: lisp to count number of attributes in block
« Reply #8 on: January 23, 2015, 12:23:58 PM »
Sorry my bad , I try it in 2012 version work fine , I am working in 2013 version and few day I try a lot of lisps and problably some of them overlapped ...
Phanks a lot sensei Lee and sensei Tharwat  !!!!
It would be very helpful to my work ..

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: lisp to count number of attributes in block
« Reply #9 on: January 23, 2015, 12:35:56 PM »
Sorry my bad , I try it in 2012 version work fine , I am working in 2013 version and few day I try a lot of lisps and problably some of them overlapped ...
Phanks a lot sensei Lee and sensei Tharwat  !!!!
It would be very helpful to my work ..

Good to hear , you are welcome .