Jump to content

count blocks / select by att value


pmxcad

Recommended Posts

Hello all,

I made a lisp with a routine from Lee Mac. (select by att value)

The intention is to count the number of modules (same block name). It concerns "EIDS, ETV and IO"

It works partially. If I have all the type of modules in the drawing, then it will be fine. But as soon as I remove a type, then the lisp still indicates "OPT11". What am I doing wrong?

I do not know what to use in the section (if (and ..... if none: "", "0" or nil.

I used 3 times Lee's lisp to select by attribute value.

Is it going wrong here? Can this (3x Lee lisp) be replaced by 1 routine and that outcomes are stored in the variables (setq) "EIDS", "ETV" "IO" ?.

 

 


;	Module counting     "MOD-COUNT" to run



;------------- Count EIDS -------------

;; Select Blocks by Attribute Value  -  Lee Mac
;; Selects all attributed blocks in the current layout which contain a specified attribute value.

(defun c:EIDS ( / att atx ent idx sel1 str1 )
   (if (/= "" (setq str1 "EIDS"))
       (if (and
               (setq sel1
                   (ssget "_X"
                       (list '(0 . "INSERT") '(66 . 1)
                           (if (= 1 (getvar 'cvport))
                               (cons 410 (getvar 'ctab))
                              '(410 . "Model")
                           )
                       )
                   )
               )
               (progn
                   (repeat (setq idx (sslength sel1))
                       (setq ent (ssname sel1 (setq idx (1- idx)))
                             att (entnext ent)
                             atx (entget  att)
                       )
                       (while
                           (and (= "ATTRIB" (cdr (assoc 0 atx)))
                                (not (wcmatch (strcase (cdr (assoc 1 atx))) str1))
                           )
                           (setq att (entnext att)
                                 atx (entget  att)
                           )
                       )
                       (if (= "SEQEND" (cdr (assoc 0 atx)))
                           (ssdel ent sel1)
                       )
                   )
                   (< 0 (sslength sel1))
               )
           )
           (sssetfirst nil sel1)
           (princ (strcat "\nNo blocks found with attribute value matching \"" str1 "\"."))
       )
   )
   (princ)
(progn (setq n (sslength sel1))
(setq EIDS (itoa n))
)
)


;------------- Count ETV -------------

;; Select Blocks by Attribute Value  -  Lee Mac
;; Selects all attributed blocks in the current layout which contain a specified attribute value.

(defun c:ETV ( / att atx ent idx sel2 str2 )
   (if (/= "" (setq str2 "ETV"))
       (if (and
               (setq sel2
                   (ssget "_X"
                       (list '(0 . "INSERT") '(66 . 1)
                           (if (= 1 (getvar 'cvport))
                               (cons 410 (getvar 'ctab))
                              '(410 . "Model")
                           )
                       )
                   )
               )
               (progn
                   (repeat (setq idx (sslength sel2))
                       (setq ent (ssname sel2 (setq idx (1- idx)))
                             att (entnext ent)
                             atx (entget  att)
                       )
                       (while
                           (and (= "ATTRIB" (cdr (assoc 0 atx)))
                                (not (wcmatch (strcase (cdr (assoc 1 atx))) str2))
                           )
                           (setq att (entnext att)
                                 atx (entget  att)
                           )
                       )
                       (if (= "SEQEND" (cdr (assoc 0 atx)))
                           (ssdel ent sel2)
                       )
                   )
                   (< 0 (sslength sel2))
               )
           )
           (sssetfirst nil sel2)
           (princ (strcat "\nNo blocks found with attribute value matching \"" str2 "\"."))
       )
   )
   (princ)
(progn (setq n (sslength sel2))
(setq ETV (itoa n))
)
)


;------------- Count IO -------------

;; Select Blocks by Attribute Value  -  Lee Mac
;; Selects all attributed blocks in the current layout which contain a specified attribute value.

(defun c:IO ( / att atx ent idx sel3 str3 )
   (if (/= "" (setq str3 "I/O"))
       (if (and
               (setq sel3
                   (ssget "_X"
                       (list '(0 . "INSERT") '(66 . 1)
                           (if (= 1 (getvar 'cvport))
                               (cons 410 (getvar 'ctab))
                              '(410 . "Model")
                           )
                       )
                   )
               )
               (progn
                   (repeat (setq idx (sslength sel3))
                       (setq ent (ssname sel3 (setq idx (1- idx)))
                             att (entnext ent)
                             atx (entget  att)
                       )
                       (while
                           (and (= "ATTRIB" (cdr (assoc 0 atx)))
                                (not (wcmatch (strcase (cdr (assoc 1 atx))) str3))
                           )
                           (setq att (entnext att)
                                 atx (entget  att)
                           )
                       )
                       (if (= "SEQEND" (cdr (assoc 0 atx)))
                           (ssdel ent sel3)
                       )
                   )
                   (< 0 (sslength sel3))
               )
           )
           (sssetfirst nil sel3)
           (princ (strcat "\nNo blocks found with attribute value matching \"" str3 "\"."))
       )
   )
   (princ)
(progn (setq n (sslength sel3))
(setq IO (itoa n))
)
)


; ------------- Set Amount -------------

(defun c:amount ()

(if (and (= EIDS nil)	(= ETV "1")	(= IO nil))	(setq Module "OPT1"))
(if (and (= EIDS "1")	(= ETV nil)	(= IO nil))	(setq Module "OPT2"))
(if (and (= EIDS "1")	(= ETV nil)	(= IO "1"))	(setq Module "OPT3"))
(if (and (= EIDS "1")	(= ETV nil)	(= IO "2"))	(setq Module "OPT4"))
(if (and (= EIDS "1")	(= ETV nil)	(= IO "3"))	(setq Module "OPT5"))
(if (and (= EIDS "1")	(= ETV nil)	(= IO "4"))	(setq Module "OPT6"))
(if (and (= EIDS "1")	(= ETV nil)	(= IO "5"))	(setq Module "OPT7"))
(if (and (= EIDS "1")	(= ETV "1")	(= IO nil))	(setq Module "OPT8"))
(if (and (= EIDS "1")	(= ETV "1")	(= IO "1"))	(setq Module "OPT9"))
(if (and (= EIDS "1")	(= ETV "1")	(= IO "2"))	(setq Module "OPT10"))
(if (and (= EIDS "1") 	(= ETV "1")	(= IO "3"))	(setq Module "OPT11"))

;(setenv 'USER1' ")
;(setenv'USER1' Module)
;(setq USl (getenv'USER1'))
(alert module); to test
)


i------------- Main / Run -------------

(defun c:test ()

;(setq EIDS nil)
;(setq ETV nil)
;(setq IO nil)
;(setq amount nil)

(c:EIDS)
(c:ETV)
(c:IO)
(c:amount)
)

 

thank you in advance

 

PmxCAD

floor-plan.dwg

Link to comment
Share on other sites

Hi BIGAL,

thanks you for your response, but it has been solved. So it had to be "0".

 

;    Module counting     "MOD-COUNT" to run



;------------- Count EIDS -------------

;; Select Blocks by Attribute Value  -  Lee Mac
;; Selects all attributed blocks in the current layout which contain a specified attribute value.

(defun c:EIDS ( / att atx ent idx sel1 str1 )
   (if (/= "" (setq str1 "EIDS"))
       (if (and
               (setq sel1
                   (ssget "_X"
                       (list '(0 . "INSERT") '(66 . 1)
                           (if (= 1 (getvar 'cvport))
                               (cons 410 (getvar 'ctab))
                              '(410 . "Model")
                           )
                       )
                   )
               )
               (progn
                   (repeat (setq idx (sslength sel1))
                       (setq ent (ssname sel1 (setq idx (1- idx)))
                             att (entnext ent)
                             atx (entget  att)
                       )
                       (while
                           (and (= "ATTRIB" (cdr (assoc 0 atx)))
                                (not (wcmatch (strcase (cdr (assoc 1 atx))) str1))
                           )
                           (setq att (entnext att)
                                 atx (entget  att)
                           )
                       )
                       (if (= "SEQEND" (cdr (assoc 0 atx)))
                           (ssdel ent sel1)
                       )
                   )
                   (< 0 (sslength sel1))
               )
           )
           (sssetfirst nil sel1)
           (princ (strcat "\nNo blocks found with attribute value matching \"" str1 "\"."))
       )
   )
   (princ)
(progn (setq n1 (sslength sel1))
(setq EIDS (itoa n1))
)
;(alert eids)
)


;------------- Count ETV -------------

;; Select Blocks by Attribute Value  -  Lee Mac
;; Selects all attributed blocks in the current layout which contain a specified attribute value.

(defun c:ETV ( / att atx ent idx sel2 str2 )
   (if (/= "" (setq str2 "ETV"))
       (if (and
               (setq sel2
                   (ssget "_X"
                       (list '(0 . "INSERT") '(66 . 1)
                           (if (= 1 (getvar 'cvport))
                               (cons 410 (getvar 'ctab))
                              '(410 . "Model")
                           )
                       )
                   )
               )
               (progn
                   (repeat (setq idx (sslength sel2))
                       (setq ent (ssname sel2 (setq idx (1- idx)))
                             att (entnext ent)
                             atx (entget  att)
                       )
                       (while
                           (and (= "ATTRIB" (cdr (assoc 0 atx)))
                                (not (wcmatch (strcase (cdr (assoc 1 atx))) str2))
                           )
                           (setq att (entnext att)
                                 atx (entget  att)
                           )
                       )
                       (if (= "SEQEND" (cdr (assoc 0 atx)))
                           (ssdel ent sel2)
                       )
                   )
                   (< 0 (sslength sel2))
               )
           )
           (sssetfirst nil sel2)
           (princ (strcat "\nNo blocks found with attribute value matching \"" str2 "\"."))
       )
   )
   (princ)
(progn (setq n2 (sslength sel2))
(setq ETV (itoa n2))
)
;(alert etv)
)


;------------- Count IO -------------

;; Select Blocks by Attribute Value  -  Lee Mac
;; Selects all attributed blocks in the current layout which contain a specified attribute value.

(defun c:IO ( / att atx ent idx sel3 str3 )
   (if (/= "" (setq str3 "I/O"))
       (if (and
               (setq sel3
                   (ssget "_X"
                       (list '(0 . "INSERT") '(66 . 1)
                           (if (= 1 (getvar 'cvport))
                               (cons 410 (getvar 'ctab))
                              '(410 . "Model")
                           )
                       )
                   )
               )
               (progn
                   (repeat (setq idx (sslength sel3))
                       (setq ent (ssname sel3 (setq idx (1- idx)))
                             att (entnext ent)
                             atx (entget  att)
                       )
                       (while
                           (and (= "ATTRIB" (cdr (assoc 0 atx)))
                                (not (wcmatch (strcase (cdr (assoc 1 atx))) str3))
                           )
                           (setq att (entnext att)
                                 atx (entget  att)
                           )
                       )
                       (if (= "SEQEND" (cdr (assoc 0 atx)))
                           (ssdel ent sel3)
                       )
                   )
                   (< 0 (sslength sel3))
               )
           )
           (sssetfirst nil sel3)
;            (princ (strcat "\nNo blocks found with attribute value matching \"" str3 "\"."))
       )
   )
   (princ)
(progn (setq n3 (sslength sel3))
(setq IO (itoa n3))
)
;(alert io)
)


; ------------- Set Amount -------------

(defun c:amount ()

(if (and (= EIDS "0")    (= ETV "1")    (= IO "0"))    (setq Module "OPT1"))
(if (and (= EIDS "1")    (= ETV "0")    (= IO "0"))    (setq Module "OPT2"))
(if (and (= EIDS "1")    (= ETV "0")    (= IO "1"))    (setq Module "OPT3"))
(if (and (= EIDS "1")    (= ETV "0")    (= IO "2"))    (setq Module "OPT4"))
(if (and (= EIDS "1")    (= ETV "0")    (= IO "3"))    (setq Module "OPT5"))
(if (and (= EIDS "1")    (= ETV "0")    (= IO "4"))    (setq Module "OPT6"))
(if (and (= EIDS "1")    (= ETV "0")    (= IO "5"))    (setq Module "OPT7"))
(if (and (= EIDS "1")    (= ETV "1")    (= IO "0"))    (setq Module "OPT8"))
(if (and (= EIDS "1")    (= ETV "1")    (= IO "1"))    (setq Module "OPT9"))
(if (and (= EIDS "1")    (= ETV "1")    (= IO "2"))    (setq Module "OPT10"))
(if (and (= EIDS "1")     (= ETV "1")    (= IO "3"))    (setq Module "OPT11"))

;(setenv 'USER1' ")
;(setenv'USER1' Module)
;(setq USl (getenv'USER1'))

(alert module)
)


i------------- Main / Run -------------

(defun c:MOD-COUNT ()
(c:EIDS)
(c:ETV)
(c:IO)
(c:amount)
)

 

PmxCAD

Link to comment
Share on other sites

Here's another way to approach it (untested):

;; Count Blocks by Attribute Value  -  Lee Mac

(defun CountBlocksByAttribValue ( lst / att atx ent idx itm key rtn sel )
   (if (setq lst (mapcar 'strcase lst)
             sel (ssget "_X" (list '(0 . "INSERT") '(66 . 1) (if (= 1 (getvar 'cvport)) (cons 410 (getvar 'ctab)) '(410 . "Model"))))
       )
       (repeat (setq idx (sslength sel))
           (setq ent (ssname  sel (setq idx (1- idx)))
                 att (entnext ent)
                 atx (entget  att)
           )
           (while (= "ATTRIB" (cdr (assoc 0 atx)))
               (cond
                   (   (not (member (setq key (strcase (cdr (assoc 1 atx)))) lst)))
                   (   (setq itm (assoc key rtn))
                       (setq rtn (subst (cons key (1+ (cdr itm))) itm rtn))
                   )
                   (   (setq rtn (cons (cons key 1) rtn)))
               )
               (setq att (entnext att)
                     atx (entget  att)
               )
           )
       )
   )
   rtn
)

(defun c:test ( / key lst )
   (setq key '("EIDS" "ETV" "I/O")
         lst  (CountBlocksByAttribValue key)
   )
   (cdr
       (assoc (mapcar '(lambda ( k ) (cond ((cdr (assoc k lst))) (0))) key)
          '(
               ((0 1 0) . "OPT1")
               ((1 0 0) . "OPT2")
               ((1 0 1) . "OPT3")
               ((1 0 2) . "OPT4")
               ((1 0 3) . "OPT5")
               ((1 0 4) . "OPT6")
               ((1 0 5) . "OPT7")
               ((1 1 0) . "OPT8")
               ((1 1 1) . "OPT9")
               ((1 1 2) . "OPT10")
               ((1 1 3) . "OPT11")
           )
       )
   )
)

Link to comment
Share on other sites

It is again a nice clear routine Lee, as usual.

The result is now on command line, can it also be stored in a variable (setq)?

 

Certainly - (setq xxx (cdr (assoc ...

 

(I'm sure you could have figured this one out ;) )

Link to comment
Share on other sites

Yeb..solved.

Thanks Lee.

 

;; Count Blocks by Attribute Value  -  Lee Mac

(defun CountBlocksByAttribValue ( lst / att atx ent idx itm key rtn sel )
   (if (setq lst (mapcar 'strcase lst)
             sel (ssget "_X" (list '(0 . "INSERT") '(66 . 1) (if (= 1 (getvar 'cvport)) (cons 410 (getvar 'ctab)) '(410 . 

"Model"))))
       )
       (repeat (setq idx (sslength sel))
           (setq ent (ssname  sel (setq idx (1- idx)))
                 att (entnext ent)
                 atx (entget  att)
           )
           (while (= "ATTRIB" (cdr (assoc 0 atx)))
               (cond
                   (   (not (member (setq key (strcase (cdr (assoc 1 atx)))) lst)))
                   (   (setq itm (assoc key rtn))
                       (setq rtn (subst (cons key (1+ (cdr itm))) itm rtn))
                   )
                   (   (setq rtn (cons (cons key 1) rtn)))
               )
               (setq att (entnext att)
                     atx (entget  att)
               )
           )
       )
   )
   rtn
)





(defun c:test ( / key lst )
   (setq key '("EIDS" "ETV" "I/O")
         lst  (CountBlocksByAttribValue key)
   )
   (setq result (cdr
       (assoc (mapcar '(lambda ( k ) (cond ((cdr (assoc k lst))) (0))) key)
          '(
               ((0 1 0) . "OPT1")
               ((1 0 0) . "OPT2")
               ((1 0 1) . "OPT3")
               ((1 0 2) . "OPT4")
               ((1 0 3) . "OPT5")
               ((1 0 4) . "OPT6")
               ((1 0 5) . "OPT7")
               ((1 1 0) . "OPT8")
               ((1 1 1) . "OPT9")
               ((1 1 2) . "OPT10")
               ((1 1 3) . "OPT11")
           )
       )
   )
)
)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...