2014-05-06 60 views
1

我正在努力與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 


    ) 
+0

列表中沒有方向,既不是水平方向,也不是垂直方向。 – ceving

+0

我在想我正在使用錯誤的列表。我可以只寫pnt字符串到一個txt文件也許???我可以承認我很困惑。如果你可以幫忙... – Tony

+1

你的問題是關於一個特定的解決方案,而不告訴我們問題是什麼,爲什麼你認爲這是正確的解決方案。請通過描述您嘗試解決的基本問題來開始您的問題。我懷疑該解決方案與格式化列表無關。 –

回答

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) 
    

讓我知道,如果你面對任何其他問題。 BTW:我也是個初學者:)

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表達將返回到你點轉換字符串列表。如果您需要將此列表轉換爲字符串,請使用函數(應用'strcat(字符串列表))