1

我已经用谷歌搜索了一段时间,但我没有找到解决方案。

我有这个清单:

((-1 . <Nome entità: 7ff5ff905910>) (0 . "DICTIONARY") (5 . "1F9") (102 . "{ACAD_REACTORS") (330 . <Nome entità: 7ff5ff9038c0>) (102 . "}") (330 . <Nome entità: 7ff5ff9038c0>) (100 . "AcDbDictionary") (280 . 0) (281 . 1) (3 . "1") (350 . <Nome entità: 7ff5ff9933c0>) (3 . "2") (350 . <Nome entità: 7ff5ff9fa0d0>) (3 . "3") (350 . <Nome entità: 7ff5ff9fa410>))

我想获得所有 dxf 代码为 350 的实体。使用该指令我只能获得第一个。

(assoc 350 list)

如何获得所有关联?

谢谢,丹尼斯

4

2 回答 2

2

有多种方法可以实现这一点 - 这里有几个例子:

(defun mAssoc1 ( key lst / rtn )
    (foreach x lst
        (if (= key (car x))
            (setq rtn (cons (cdr x) rtn))
        )
    )
    (reverse rtn)
)

(defun mAssoc2 ( key lst )
    (apply 'append
        (mapcar
            (function
                (lambda ( x ) (if (= key (car x)) (list (cdr x))))
            )
            lst
        )
    )
)


(defun mAssoc3 ( key lst )
    (mapcar 'cdr
        (vl-remove-if-not
            (function (lambda ( x ) (= key (car x))))
            lst
        )
    )
)


(defun mAssoc4 ( key lst / item )
    (if (setq item (assoc key lst))
        (cons (cdr item) (mAssoc4 key (cdr (member item lst))))
    )
)


(defun mAssoc5 ( key lst / item rtn )
    (while (setq item (assoc key lst))
        (setq rtn (cons (cdr item) rtn) lst (cdr (member item lst)))
    )
    (reverse rtn)
)


(defun mAssoc6 ( key lst )
    (mapcar 'cdr (acet-list-m-assoc key lst))
)


(defun mAssoc7 ( key lst )
    (if lst
        (if (= key (caar lst))
            (cons (cdar lst) (mAssoc7 key (cdr lst)))
            (mAssoc7 key (cdr lst))
        )
    )
) 

以下是上述功能的快速性能比较:

;;; Benchmarking: Elapsed milliseconds / relative speed for 32768 iteration(s):
;;;
;;;    (MASSOC4 2 L).....1482 / 1.25 <fastest>
;;;    (MASSOC5 2 L).....1482 / 1.25
;;;    (MASSOC6 2 L).....1498 / 1.24
;;;    (MASSOC3 2 L).....1638 / 1.13
;;;    (MASSOC7 2 L).....1747 / 1.06
;;;    (MASSOC1 2 L).....1748 / 1.06
;;;    (MASSOC2 2 L).....1856 / 1 <slowest>
于 2017-02-07T23:45:46.373 回答
0

assoc 只返回第一个,所以只要没有找到就需要在 find 之后循环 all (assoc returns nil)。您应该尝试这样的代码:

(defun DXF:Dump (EntDef code / out)
    (while (setq values(assoc code EntDef))
        (setq out (append out (list (cdr values))))
        (setq EntDef(cdr(member values EntDef)))
    )
    out
)

; (setq dxfs (entget (car (entsel))))

; (DXF:Dump dxfs 350)
于 2016-10-27T20:16:07.547 回答