首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Autolisp列表操作

Autolisp列表操作
EN

Stack Overflow用户
提问于 2014-05-06 14:07:24
回答 3查看 703关注 0票数 1

我正在与奥托利什斗争,我找不到我正在寻找的答案。

我有一个空的列表,我用点坐标填充它,并把它转换成字符串。所产生的列表如下:

(12.5484,7.6054,0.0000 17.0626,8.1782,0.0000 17.5642,10.7199,0.0000 12.0110,11.4716,0.0000)

是否有可能使列表填充垂直并具有类似于以下内容的输出:

(12.5484 7.6054 0.0000)

17.0626 8.1782,0.0000

17.5642,10.7199,0.0000

12.0110,11.4716,0.0000)

我使用的代码是:

代码语言:javascript
复制
(setq lst()) ;create empty list named lst

    (while
        (setq a (getpoint "\nTick the Point")) ;select points

        (setq x (rtos(car a))) ;get as X the x of a point (as string)
        (setq y (rtos(cadr a))) ;get as Y the y of a point (as string)
        (setq z (rtos(caddr a))) ;get as Z the z of a point (as string)
        (setq pnt (strcat x "," y ","z))

        (setq lst (cons pnt lst)) ;start filling the empty list with the coordinates of the points


    )
EN

回答 3

Stack Overflow用户

发布于 2014-09-02 06:57:08

我认为您所面临的问题仅仅是打印存储在列表中的值。存储的值是绝对好的。

因此,我认为您应该将这些行添加到现有代码中(如果您想要输出,如上面所示):

  1. 写入文本文件: (提示"\n*写入当前绘图目录的文本文件“)(Terpri) (setq fname(getstring "\n输入有效的文件名:”);如果用户不提供文件名,则使用绘图名称(if (= fname ")) (setq fname (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname")) 4));打开文件(setq (打开(strcat (getvar“dwg前缀”) fname ".txt") );循环将数据写入文件(foreach项lst (写行项目txt) );关闭文件(关闭txt) (princ (strcat "\n*文本文件“(getvar”dwg前缀“) fname”已经创建*\n“)
  2. 若要在命令行中打印(根据请求使用起始括号和结束括号): (setq计数器0) (princ“n(”)(而(> (长度lst) (+计数器1)) (progn (strcat (strcat (nth计数器lst) "\n")) (setq计数器(1+计数器) (princ (strcat(最后1 lst)“) (princ) (princ )
  3. 在命令行中打印(没有任何括号): (原第一项(第一项))

如果你面临其他问题,请告诉我。顺便说一句:我也是个初学者

票数 2
EN

Stack Overflow用户

发布于 2017-12-05 18:02:22

代码语言:javascript
复制
(setq ptlist '())
(mapcar '(lambda (pt) 
        (strcat (rtos (car pt)) "," (rtos (cadr pt)) "," (rtos (caddr pt))))
        (reverse (while (setq pttemp(getpoint "Your point:"))
           (if pttemp (cons pttemp ptlist))
        ))
)

mapcar表达式将返回从点转换的字符串列表。如果需要将此列表转换为字符串,请使用该函数(应用'strcat (字符串列表))。

票数 0
EN

Stack Overflow用户

发布于 2022-05-17 19:21:12

我同意戈文达对这个问题的回答。更改代码打印列表的方式可以向您展示您想要实现的目标。

如果您在理解列表方面遇到困难,那么下面的代码可以轻松地阅读列表是如何构建的。代码将打印嵌套列表或原始列表,具体取决于“返回变量”下未注释的列表。

代码语言:javascript
复制
;; Printing Results via Command Line
(defun C:PrintResults(/ lResults DataPoint)
  
  ;; Calling Function
  (setq lResults (CreateList))
  
  ;; Printing List
  (princ "\n \n")
  (princ "List : ")(princ lResults)
  (princ "\n \n")

  ;; Printing List Contents - New line per data point
  (foreach DataPoint lResults
    (princ DataPoint)(terpri)
  );foreach
  (princ)
);defun C:PrintResults


;; Original
(defun CreateList(/ NestedList MasterList lst a x y z)
    (setq NestedList nil)
    (setq MasterList nil)
    (setq lst nil) ;create empty list named lst

    (while 
        (setq a (getpoint "\nTick the Point")) ;select points
        (terpri)

        (setq x (rtos(car a))) ;get as X the x of a point (as string)
        (setq y (rtos(cadr a))) ;get as Y the y of a point (as string)
        (setq z (rtos(caddr a))) ;get as Z the z of a point (as string)

        ;; A nested list setup
        (setq NestedList (list x y z))
        (setq MasterList (append MasterList (list NestedList)))
        (princ "List 1 : ")(princ MasterList)(terpri)

        ;; Original Setup
        (setq pnt (strcat x "," y ","z))
        (setq lst (cons pnt lst)) ;start filling the empty list with the coordinates of the points
        (princ "List 2 : ")(princ lst)(terpri)
    );while 

  ;; Returned variable
  ;MasterList
  (reverse lst); Making the first entry the first item in the list
);defun EmptyList
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23497055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档