Autolisp列表操作

时间:2014-05-06 14:07:25

标签: list autolisp

我正在与Autolisp挣扎,我找不到我想要的答案。

我有一个空列表,我用点坐标填充它,我已经变成了字符串。生成的列表类似于:

  

(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)

我使用的代码是:

(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


    )

2 个答案:

答案 0 :(得分:1)

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

所以我认为您应该将这些行添加到现有代码中(如果您希望输出如上所示):

  1. 要写入文本文件:

    (prompt "\n* Text file written to directory of current drawing *")(terpri)
    (setq fname(getstring "\n Enter a valid file name: "))  
    
    ;if the user doesn't provide a filename, use the drawing name
    (if (= fname "")        
        (setq fname (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname")) 4)))
    )
    ;open the file
    (setq txt (open (strcat (getvar "dwgprefix") fname ".txt") "w"))
    
    ;loop to write data to the file
    (foreach item lst
        (write-line item txt)
    )
    
    ;close the file
    (close txt) 
    (princ (strcat "\n* Text file " (getvar "dwgprefix") fname " has been created *\n"))
    
  2. 要在命令行中打印(根据请求使用起始和结束括号):

    (setq counter 0)
    (princ "\n(")
    (while (> (length lst)  (+ counter 1))
        (progn
            (princ (strcat (nth counter lst) "\n"))     
            (setq counter (1+ counter))
        )
    )
    (princ (strcat (last lst) ")"))
    (princ)
    
  3. 要在命令行中打印(不带任何括号):

    (terpri)
    (foreach item lst 
        (progn
            (princ item)
            (terpri)
        )
    )
    (princ)
    
  4. 如果您遇到任何其他问题,请告诉我。 顺便说一句:我也是初学者:)

答案 1 :(得分:0)

(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表达式将返回一个从点转换的字符串列表。如果您需要将此列表转换为字符串,请使用该函数(apply' strcat(string-of-strings))