【问题标题】:Draftsight LISP, problems building a list of x coordinates with the (cons ) functionDraftsight LISP,使用 (cons) 函数构建 x 坐标列表的问题
【发布时间】:2021-04-21 16:04:49
【问题描述】:

我正在尝试遍历一些矩形并将 x 和 y 坐标分别放在一个列表中。但是我要填写的列表看起来很糟糕,代码:

(setq sspnls (ssget '((0 . "LWPOLYLINE"))))
    (while (= 1 (getvar 'cmdactive))
            (command  pause)
            )
    (setq pnlslength (sslength sspnls))
    (setq xlist (list))
    (setq ylist (list)) 
    (setq pnlname(ssname sspnls 0))
    (setq sssort (ssadd))
    (setq tllr 0)
    (while (< tllr pnlslength)
        (setq pnlname(ssname sspnls tllr))
        (Command "hatch" "S" pnlname "")
        (setq xs (cadr (assoc 10 (entget pnlname)))); y cordinate
        (setq ys (caddr (assoc 10 (entget pnlname)))); x cordinate
        (setq xlist (cons xlist xs))
        (setq ylist (cons ylist ys))
        (setq tllr (+ tllr 1))
        
    );while end

xlist 或 y 列表如下:

((((nil . 12057.63625954) . 12057.63625954) . 10345.63625954) . 10345.63625954)

我需要它看起来像:

(12057.63625954 12057.63625954 10345.63625954 10345.63625954)

我做错了什么?请注意,我使用的是 Draftsight,我也使用了 append 函数,但 append 函数根本不起作用?

非常感谢您的帮助!

【问题讨论】:

    标签: list autolisp


    【解决方案1】:

    主要问题是您使用 cons 函数创建列表和 X 和 Y 坐标值的点对,而不是将 X 和 Y 坐标值推到现有列表的头部。

    例如,您的代码正在这样做:

    _$ (cons '(1 2 3 4) 5)
    ((1 2 3 4) . 5)
    

    而不是这个:

    _$ (cons 5 '(1 2 3 4))
    (5 1 2 3 4)
    

    一般来说,您的代码可以简化为以下内容:

    (if (setq sel (ssget '((0 . "LWPOLYLINE"))))
        (repeat (setq idx (sslength sel))
            (setq idx (1- idx)
                  ent (ssname sel idx)
                  enx (entget ent)
                  xls (cons (cadr  (assoc 10 enx)) xls)
                  yls (cons (caddr (assoc 10 enx)) yls)
            )
            (command "_.hatch" "_S" ent "")
        )
    )
    

    另请注意,您只是检索第一个折线顶点的坐标 - 我不确定这是否是故意的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-28
      相关资源
      最近更新 更多