harilalmn 发表于 2022-7-6 08:57:17

搜索列表中的项目

我想在字符串列表中搜索一个字符串。这怎么可能?
 
在将另一项添加到列表之前,我必须验证它是否已添加到列表中。到目前为止,我在这里。。
 
(defun checklist (blst bname)
(setq h (length blst))
(repeat h
   (if
   (eq h (nth j blst))
 
如果相等,则函数应返回1。
否则,如果bname不在列表中,则函数应返回0。
这是怎么做到的?

pBe 发表于 2022-7-6 09:03:05

使用“成员”
 
(if(成员itm lst)
(然后)(否则))
)

harilalmn 发表于 2022-7-6 09:07:15

太棒了pBe,谢谢你。。。!!!

ketxu 发表于 2022-7-6 09:08:47

vl位置为什么不?请注意,它返回列表中的第一项ok。
Ex(vl位置4’(1 2 3 4 1 2 3 4))=>3

harilalmn 发表于 2022-7-6 09:13:38


谁能告诉我下面的代码哪里出错了?写入它是为了提示每个块的计数。我知道BCOUNT命令。学习编码也是一种类似的尝试。
 
(vl-load-com)
(defun c:cntblk(/ allblocks n i blklst blkobj blkobjname)
(setq allblocks (ssget "_X" (List (cons 0 "INSERT"))))
(setq n (sslength allblocks))
(setq i 1)
(setq blklst (ssadd))
(repeat n
   (setq blkobj (ssname allblocks i))
   (setq blkobjname (getEffectiveName blkobj))

   (if (not (eq (member blkobjname blklst) nil))
    (ssadd blkobj blklst)
   )

   (setq i (1+ i))
)
)
(setq j (length blklst))
(setq i 0)
(textscr)
(repeat j
(setq ss(ssget "_X" (list (cons 0 "INSERT")(cons 2 (nth i blklst)))))
(setq cnt (sslength ss))
(prompt (strcat "\n" (nth i blklst) "-----------------" (itoa cnt)))
(setq i (1+ i))
)

(defun getEffectiveName (ent)
(setq obj (vlax-ename->vla-object ent))
(vlax-get obj "EffectiveName")
)

harilalmn 发表于 2022-7-6 09:16:03

好啊我知道我在之前的代码中犯了一个错误;
 
(setq blklst (ssadd))
 
通过ssadd尝试创建空白列表
 
但是有人能纠正下面的问题吗?
 
 
(vl-load-com)
(defun c:cntblk(/ allblocks n i blklst blkobj blkobjname)
(setq allblocks (ssget "_X" (List (cons 0 "INSERT"))))
(setq n (sslength allblocks))
(setq i 1)
(repeat n
   (setq blkobj (ssname allblocks i))
   (setq blkobjname (getEffectiveName blkobj))

   (if
(eq (member blkobjname blklst) nil)
       (if
(not (eq blklst nil))
(setq blklst (list blklst blkobjname))
(setq blklst (list blkobjname))
)
   )

   (setq i (1+ i))
)
)
(setq j (length blklst))
(setq i 0)
(textscr)
(repeat j
(setq ss(ssget "_X" (list (cons 0 "INSERT")(cons 2 (nth i blklst)))))
(setq cnt (sslength ss))
(prompt (strcat "\n" (nth i blklst) "-----------------" (itoa cnt)))
(setq i (1+ i))
)

(defun getEffectiveName (ent)
(setq obj (vlax-ename->vla-object ent))
(vlax-get obj "EffectiveName")
)

pBe 发表于 2022-7-6 09:19:12

blklst变量不是列表
 
哎呀。。你又发了一条。。坚持
 
您正在尝试创建选择集或块名列表吗?
 
(defun c:cntblk(/ allblocks n i blklst blkobj blkobjname)
(setq allblocks (ssget "_X" (List (cons 0 "INSERT"))))
(setq n (sslength allblocks))
(setq i 0)
(repeat n
   (setq blkobj (ssname allblocks i))
   (setq blkobjname (getEffectiveName blkobj))
   (if (not (member blkobjname blklst))
   (setq blklst (cons blkobjname blklst)))
(setq i (1+ i))
)
blklst   
)
 
 
使用ssadd可以创建选择集,而不是列表
使用cons/append/list//vl list*可以创建一个列表

harilalmn 发表于 2022-7-6 09:21:26

pBe,
然而,这只是将列表转储到命令窗口上。我试图计算block的实例,并给出如下提示:;
 
区块1-------------------10
区块2-------------------15
区块3-------------------8
区块4-------------------6
区块5-------------------12

BlackBox 发表于 2022-7-6 09:24:13

查看李的区块计数器例程:眨眼:

Lee Mac 发表于 2022-7-6 09:28:18

以下是代码的快速修复:
 
(vl-load-com)

(defun c:cntblk ( / allblocks blklst blkobj blkobjname i n ss )

    (if (setq allblocks (ssget "_X" (list (cons 0 "INSERT"))))
      (progn
         (setq n (sslength allblocks))
         (setq i 0)
         (repeat n
               (setq blkobj (ssname allblocks i))
               (setq blkobjname (getEffectiveName blkobj))
               (if (not (member blkobjname blklst))
                   (setq blklst (cons blkobjname blklst))
               )
               (setq i (1+ i))
         )
         (textscr)
            (foreach blk blklst
               (if (setq ss (ssget "_X" (list (cons 0 "INSERT") (cons 2 blk))))
                   (prompt (strcat "\n" blk "-----------------" (itoa (sslength ss))))
               )
         )
      )
   )
   (princ)
)

(defun getEffectiveName ( ent / obj )
(setq obj (vlax-ename->vla-object ent))
(vlax-get obj "EffectiveName")
)
 
但这不是一种很好的计数方法,因为在收集选择集时,将排除已更改可见性状态(因此变得匿名)的动态块。此外,通过这种方式,您可以有效地对块进行两次计数,因为您已经遍历了选择集以获得块列表。
 
考虑以下代码:
 
(defun c:cntblk ( / allblocks blockentity blocklist blockname counter item )

   (if (setq AllBlocks (ssget "_X" '((0 . "INSERT"))))
       (progn
         (setq counter 0)
         (repeat (sslength AllBlocks)
               (setq blockentity (ssname AllBlocks counter)
                     blockname   (GetEffectiveName (vlax-ename->vla-object blockentity))
               )
               (if (setq item (assoc blockname blocklist))
                   (setq blocklist (subst (cons blockname (1+ (cdr item))) item blocklist))
                   (setq blocklist (cons(cons blockname 1) blocklist))
               )
               (setq counter (1+ counter))
         )
         (foreach blk blocklist
               (princ (strcat "\n" (car blk) " ----------------- " (itoa (cdr blk))))
         )
       )
   )
   (princ)
)


(defun GetEffectiveName ( obj )
   (if (vlax-property-available-p obj 'effectivename)
       (vla-get-effectivename obj)
       (vla-get-name obj)
   )
)
 
我试图使程序的结构尽可能接近您的原始代码,以便您更容易学习。我强烈建议您仔细研究上述两种解决方案,并询问有关我使用的代码的任何问题。
页: [1] 2
查看完整版本: 搜索列表中的项目