pmxcad 发表于 2022-7-5 15:32:32

计数块/按att v选择

大家好,
我用李·麦克的套路做了一个Lisp程序的动作。(按att值选择)
目的是统计模块的数量(相同的块名)。它涉及“EID、ETV和IO”
它部分起作用。如果我有图纸中所有类型的模块,那么就可以了。但是,一旦我删除了一个类型,lisp仍然指示“OPT11”。我做错了什么?
我不知道在该部分中使用什么(如果没有:“,”0“或nil。
我使用了3次Lee的lisp按属性值进行选择。
这里出问题了吗?这个(3x Lee lisp)是否可以被1个例程替换,并且结果存储在变量(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 (entgetatt)
                     )
                     (while
                           (and (= "ATTRIB" (cdr (assoc 0 atx)))
                              (not (wcmatch (strcase (cdr (assoc 1 atx))) str1))
                           )
                           (setq att (entnext att)
                                 atx (entgetatt)
                           )
                     )
                     (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 (entgetatt)
                     )
                     (while
                           (and (= "ATTRIB" (cdr (assoc 0 atx)))
                              (not (wcmatch (strcase (cdr (assoc 1 atx))) str2))
                           )
                           (setq att (entnext att)
                                 atx (entgetatt)
                           )
                     )
                     (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 (entgetatt)
                     )
                     (while
                           (and (= "ATTRIB" (cdr (assoc 0 atx)))
                              (not (wcmatch (strcase (cdr (assoc 1 atx))) str3))
                           )
                           (setq att (entnext att)
                                 atx (entgetatt)
                           )
                     )
                     (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)
)


 
提前谢谢你
 
PmxCAD
平面布置图。图纸

BIGAL 发表于 2022-7-5 15:46:07

只是一点猜测,如果你不检查不存在,添加一个额外的。

(if (= module nil)((setq module "Not found"))

pmxcad 发表于 2022-7-5 16:00:45

嗨,比格尔,
谢谢你的回复,但问题已经解决了。所以它必须是“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 (entgetatt)
                     )
                     (while
                           (and (= "ATTRIB" (cdr (assoc 0 atx)))
                              (not (wcmatch (strcase (cdr (assoc 1 atx))) str1))
                           )
                           (setq att (entnext att)
                                 atx (entgetatt)
                           )
                     )
                     (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 (entgetatt)
                     )
                     (while
                           (and (= "ATTRIB" (cdr (assoc 0 atx)))
                              (not (wcmatch (strcase (cdr (assoc 1 atx))) str2))
                           )
                           (setq att (entnext att)
                                 atx (entgetatt)
                           )
                     )
                     (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 (entgetatt)
                     )
                     (while
                           (and (= "ATTRIB" (cdr (assoc 0 atx)))
                              (not (wcmatch (strcase (cdr (assoc 1 atx))) str3))
                           )
                           (setq att (entnext att)
                                 atx (entgetatt)
                           )
                     )
                     (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

Lee Mac 发表于 2022-7-5 16:03:03

以下是另一种方法(未经测试):
;; 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 (ssnamesel (setq idx (1- idx)))
               att (entnext ent)
               atx (entgetatt)
         )
         (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 (entgetatt)
               )
         )
       )
   )
   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")
         )
       )
   )
)

pmxcad 发表于 2022-7-5 16:18:48

和往常一样,李又是一个很好的明确的常规动作。
结果现在在命令行上,是否也可以存储在变量(setq)中?
 
PmxCAD

Lee Mac 发表于 2022-7-5 16:26:02

 
当然-(setq xxx(cdr(assoc。。。
 
(我相信你本可以解决这个问题)

pmxcad 发表于 2022-7-5 16:39:02

是的。。解决了的。
谢谢李。
 

;; 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 (ssnamesel (setq idx (1- idx)))
               att (entnext ent)
               atx (entgetatt)
         )
         (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 (entgetatt)
               )
         )
       )
   )
   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")
         )
       )
   )
)
)
页: [1]
查看完整版本: 计数块/按att v选择