1

我在 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


    )
4

2 回答 2

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)
    

如果您遇到任何其他问题,请告诉我。顺便说一句:我也是初学者:)

于 2014-09-02T06:57:08.680 回答
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 (list-of-strings))

于 2017-12-05T18:02:22.463 回答