Jump to content
dilan

Function write-line. Align lines.

Recommended Posts

dilan

Hello everyone!

I can’t understand how can I align lines when I write to a file using a function write-line.

For example, here is the program:

(defun write_example ( / pt lst fl_name count )
;-----------------------------
(while 
  (setq pt (getpoint "\nClik ->>"))
  (setq lst (cons pt lst))
) ;_while
;-----------------------------
(if (and
     lst
    (setq fl_name (acet-ui-getfile "--< Save as... >--" (getvar "DWGPREFIX") "txt" "" 1))
	)
(progn
(setq count 1)
(setq fl(open fl_name "w"))
(write-line "         №              X              Y               Z" fl)

(foreach itm lst
  (write-line (strcat "         " (rtos count 2 0)
                      "              " (rtos (car itm)) 
					  "              " (rtos (cadr itm))
					  "              " (rtos (caddr itm))) fl)
(setq count (+ count 1))
); foreach
(close fl)
); end progn
); end if
(princ)
); end defun

In the end, this is what I get in the file:

         №              X              Y               Z
         1              1129.795              2571.649              0.000
         2              1125.115              2576.517              0.000
         3              1126.632              2583.535              0.000
         4              1133.969              2587.707              0.000
         5              1145.100              2590.615              0.000
         6              1143.899              2583.408              0.000
         7              1140.547              2578.477              0.000
         8              1135.993              2577.655              0.000
         9              1126.696              2584.609              0.000
         10              1125.241              2590.173              0.000
         11              1127.012              2596.053              0.000
         12              1136.689              2591.184              0.000
         13              1147.946              2582.460              0.000
         14              1148.389              2575.189              0.000
         15              1146.239              2572.028              0.000
         16              1140.800              2573.103              0.000
         17              1136.309              2579.678              0.000
         18              1136.309              2585.747              0.000

But I want to get like this:

         №              X                     Y                     Z
         1              1129.795              2571.649              0.000
         2              1125.115              2576.517              0.000
         3              1126.632              2583.535              0.000
         4              1133.969              2587.707              0.000
         5              1145.100              2590.615              0.000
         6              1143.899              2583.408              0.000
         7              1140.547              2578.477              0.000
         8              1135.993              2577.655              0.000
         9              1126.696              2584.609              0.000
        10              1125.241              2590.173              0.000
        11              1127.012              2596.053              0.000
        12              1136.689              2591.184              0.000
        13              1147.946              2582.460              0.000
        14              1148.389              2575.189              0.000
        15              1146.239              2572.028              0.000
        16              1140.800              2573.103              0.000
        17              1136.309              2579.678              0.000
        18              1136.309              2585.747              0.000

Tell me please. How can I do that?

thank

Share this post


Link to post
Share on other sites
BIGAL
Posted (edited)

Try using say tabs (strcat "x" (chr xx) "y" (chr xx) etc I will have to look up what a tab is. need my ascii table. found it 9 (chr 9)

Edited by BIGAL
  • Like 1

Share this post


Link to post
Share on other sites
rlx

Other way than \t could be


(defun write_example (/ pt lst fl_name count l)
  (while (setq pt (getpoint "\nClik ->>")) (setq lst (cons pt lst)))
  (if (and lst (setq fl_name (acet-ui-getfile "--< Save as... >--" (getvar "DWGPREFIX") "txt" "" 1)))
    (progn
      (setq count 1 l 15 fl (open fl_name "w"))
      (write-line (strcat (fus "No" l)(fus "X" l)(fus "Y" l)(fus "Z" l)) fl)
      (foreach itm lst
        (write-line (strcat (fus (itoa count) l) (fus (rtos (car itm)) l)
                            (fus (rtos (cadr itm)) l) (fus (rtos (caddr itm)) l)) fl)
        (setq count (+ count 1))
      )
      (close fl)
    )
  )
  (princ)
)

; fill up string
(defun fus (s i) (while (> i (strlen s))(setq s (strcat s " "))) s)

 

  • Like 1

Share this post


Link to post
Share on other sites
dilan
Posted (edited)
54 minutes ago, rlx said:

Other way than \t could be

 


(defun write_example (/ pt lst fl_name count l)
  (while (setq pt (getpoint "\nClik ->>")) (setq lst (cons pt lst)))
  (if (and lst (setq fl_name (acet-ui-getfile "--< Save as... >--" (getvar "DWGPREFIX") "txt" "" 1)))
    (progn
      (setq count 1 l 15 fl (open fl_name "w"))
      (write-line (strcat (fus "No" l)(fus "X" l)(fus "Y" l)(fus "Z" l)) fl)
      (foreach itm lst
        (write-line (strcat (fus (itoa count) l) (fus (rtos (car itm)) l)
                            (fus (rtos (cadr itm)) l) (fus (rtos (caddr itm)) l)) fl)
        (setq count (+ count 1))
      )
      (close fl)
    )
  )
  (princ)
)

; fill up string
(defun fus (s i) (while (> i (strlen s))(setq s (strcat s " "))) s)

 

 

Thanks, it's cool, but I get it like that:

No             X              Y              Z              
1              992.774        979.172        0.000          
2              993.442        980.264        0.000          
3              996.053        984.089        0.000          
4              996.479        986.518        0.000          
5              997.268        989.735        0.000          
6              995.446        992.467        0.000          
7              993.077        994.532        0.000          
8              992.409        997.264        0.000          
9              993.381        1000.360       0.000          
10             995.446        1004.913       0.000          
11             993.503        1007.706       0.000          
12             989.190        1007.706       0.000          
13             986.275        1005.581       0.000          
14             979.412        1005.703       0.000          
15             979.473        1009.467       0.000          
16             977.408        1011.592       0.000          
17             973.885        1012.381       0.000          
18             967.690        1010.742       0.000          
19             963.621        1006.249       0.000          
20             960.038        1009.527       0.000          

And I need like this:

 No             X              Y              Z              
 1              992.774        979.172        0.000          
 2              993.442        980.264        0.000          
 3              996.053        984.089        0.000          
 4              996.479        986.518        0.000          
 5              997.268        989.735        0.000          
 6              995.446        992.467        0.000          
 7              993.077        994.532        0.000          
 8              992.409        997.264        0.000          
 9              993.381        1000.360       0.000          
10              995.446        1004.913       0.000          
11              993.503        1007.706       0.000          
12              989.190        1007.706       0.000          
13              986.275        1005.581       0.000          
14              979.412        1005.703       0.000          
15              979.473        1009.467       0.000          
16              977.408        1011.592       0.000          
17              973.885        1012.381       0.000          
18              967.690        1010.742       0.000          
19              963.621        1006.249       0.000          
20              960.038        1009.527       0.000          

I need the number to shift to the left

Now the number is shifting to the right

Edited by dilan

Share this post


Link to post
Share on other sites
rlx


(defun write_example (/ pt lst fl fl_name count l)
  (while (setq pt (getpoint "\nClik ->>")) (setq lst (cons pt lst)))
  (if (and lst (setq fl_name (acet-ui-getfile "--< Save as... >--" (getvar "DWGPREFIX") "txt" "" 1)))
    (progn
      (setq count 1 l 15 fl (open fl_name "w"))
      (write-line (strcat (flus "No" 6)(flus "" 9)(fus "X" l)(fus "Y" l)(fus "Z" l)) fl)
      (foreach itm lst
        (write-line (strcat (flus (itoa count) 6) (flus "" 9) (fus (rtos (car itm)) l)
                            (fus (rtos (cadr itm)) l) (fus (rtos (caddr itm)) l)) fl)
        (setq count (+ count 1))
      )
      (close fl)
    )
  )
  (princ)
)

; fill up string
(defun fus (s i) (while (> i (strlen s))(setq s (strcat s " "))) s)
; fill up string
(defun flus (s i) (while (> i (strlen s))(setq s (strcat " " s))) s)

 

Share this post


Link to post
Share on other sites
dilan
34 minutes ago, rlx said:

 


(defun write_example (/ pt lst fl fl_name count l)
  (while (setq pt (getpoint "\nClik ->>")) (setq lst (cons pt lst)))
  (if (and lst (setq fl_name (acet-ui-getfile "--< Save as... >--" (getvar "DWGPREFIX") "txt" "" 1)))
    (progn
      (setq count 1 l 15 fl (open fl_name "w"))
      (write-line (strcat (flus "No" 6)(flus "" 9)(fus "X" l)(fus "Y" l)(fus "Z" l)) fl)
      (foreach itm lst
        (write-line (strcat (flus (itoa count) 6) (flus "" 9) (fus (rtos (car itm)) l)
                            (fus (rtos (cadr itm)) l) (fus (rtos (caddr itm)) l)) fl)
        (setq count (+ count 1))
      )
      (close fl)
    )
  )
  (princ)
)

; fill up string
(defun fus (s i) (while (> i (strlen s))(setq s (strcat s " "))) s)
; fill up string
(defun flus (s i) (while (> i (strlen s))(setq s (strcat " " s))) s)

 

 

Thank you very much rlx

Share this post


Link to post
Share on other sites
rlx

you're welcome. Note that Bigal's suggestion to use tabs ("\t") is working too for the x/y/z part of your table if all number are roughly the same length , so you would only need to right align your counter and the rest could be done with tabs. Doubt in real life you would be able to measure the difference in speed but theoretically tabs should be a little faster.

:beer:

  • Like 1

Share this post


Link to post
Share on other sites
dilan
5 minutes ago, rlx said:

you're welcome. Note that Bigal's suggestion to use tabs ("\t") is working too for the x/y/z part of your table if all number are roughly the same length , so you would only need to right align your counter and the rest could be done with tabs. Doubt in real life you would be able to measure the difference in speed but theoretically tabs should be a little faster.

:beer:

thanks again

Share this post


Link to post
Share on other sites
BIGAL
Posted (edited)

The less than 10 is pretty common this is another simple example of padding. 

 

Rlx nice idea "\t"

 


(if (< (car dwgnum) 10.0) 
      (setq newstr2 (strcat dwgname "-D0"  (rtos sheetnum 2 0)))
      (setq newstr2 (strcat dwgname "-D"  (rtos sheetnum 2 0)))
)

Edited by BIGAL
  • Like 1

Share this post


Link to post
Share on other sites
dilan
10 hours ago, BIGAL said:

The less than 10 is pretty common this is another simple example of padding. 

 

Rlx nice idea "\t"

 


(if (< (car dwgnum) 10.0) 
      (setq newstr2 (strcat dwgname "-D0"  (rtos sheetnum 2 0)))
      (setq newstr2 (strcat dwgname "-D"  (rtos sheetnum 2 0)))
)

thanks BIGAL

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...