TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: mhy3sx on March 10, 2023, 12:04:44 PM

Title: Export results to notepad
Post by: mhy3sx on March 10, 2023, 12:04:44 PM
Hi, I want to export some results to notepad

I know that the path and the name given as

Code - Auto/Visual Lisp: [Select]
  1. (setq F (open (setq txt (strcat "c:\\test\\DAT\\myexport.dat")) "w"))
  2.  

I want very time I use the code the export name change as myexport1,myexport2 etc.

How to do that?

Thanks
Title: Re: Export results to notepad
Post by: danAllen on March 10, 2023, 12:50:41 PM
Are you familiar with using variables and strings? You'll use a variable as a counter (1,2,3) and then concatenate the strings to make file name.

Code: [Select]
(setq n 1)
(setq fname (strcat "c:\\test\\DAT\\myexport" (atoi n) ".txt"))
(setq n (1+ n))
;;; use unique global variable to save between use of code
(setq MHY3SX_export_count n)
Check out basics:
https://www.afralisp.net/autolisp/tutorials/set-and-setq.php
https://www.afralisp.net/archive/Tips/code98.htm
https://www.afralisp.net/reference/autolisp-functions.php#A (see function ATOI)
https://www.afralisp.net/archive/lispa/lisp5.htm (for global vs local variable)
Title: Re: Export results to notepad
Post by: 57gmc on March 10, 2023, 03:23:15 PM
Hi Dan, wouldn't you want to use (itoa n)?
Title: Re: Export results to notepad
Post by: mhy3sx on March 10, 2023, 04:13:18 PM
Hi thanks for the reply. I start this post because I have a problem with a code. In the beginning, I believe that the problem was in the name of exit file but now recognize that I have problem in code. Every time I use the code didn't clean the cash and write in the same file under the previous. I need help to fix it.

This code find how many text are in some layers. Is a part of the code . I use  more than two layers

Code - Auto/Visual Lisp: [Select]
  1.          (defun c:test (/ F txt)
  2.          (command "-purge" "a" "*" "n")
  3.          (setq F (open (setq txt (strcat "c:\\test\\DAT\\mytest.dat")) "w"))
  4.          (setq laylst2 '( "BLD" "ROAD"))
  5.          (foreach lay2 laylst2
  6.            (setq SST nil)  ;clear last selection sets
  7.           (if (not (setq SST (ssget "_X" (list '(0 . "TEXT") (cons 8 lay2)))))
  8.            (setq SST (ssadd))
  9.           )
  10.           (if (tblsearch "layer" lay2)
  11.            (setq lstText (cons (list lay2 (rtos (sslength SST) 2 0)) lstText))
  12.           )
  13.          )
  14.          (setq lstText (reverse lstText))
  15.          (write-line "find layers:" F)
  16.          (write-line "---------------------------" F)
  17.          (write-line "" F)
  18.          (foreach line lstText
  19.           (if (not (eq (cadr line) "0"))
  20.            (write-line (strcat "- " (cadr line) " Text find in Layer " (car line)) F)
  21.           )
  22.          )
  23.          (write-line "" F)
  24.          (write-line "//////////////////////////////////////////////////////////////////////" F)
  25.          (write-line "----------------------------------------------------------------------------------" F)
  26.          (write-line "" F)
  27.          (if (=(tblsearch "layer" "BLD") nil)
  28.             (write-line "- BLD " F)
  29.          );end if
  30.          (if (=(tblsearch "layer" "ROAD") nil)
  31.             (write-line "- ROAD " F)
  32.          );end if
  33.          
  34.          (write-line "" F)
  35.          (write-line "" F)
  36.          (close F)
  37.          (startapp "NOTEPAD" txt)
  38.          (princ)
  39. )
  40.  

The problem is like this

fist run the code export

Code: [Select]
- 1 Text find in Layer BLD
- 4 Text find in Layer ROAD

After 4 runs

Code: [Select]
- 1 Text find in Layer BLD
- 4 Text find in Layer ROAD
- 1 Text find in Layer BLD
- 4 Text find in Layer ROAD
- 1 Text find in Layer BLD
- 4 Text find in Layer ROAD
- 1 Text find in Layer BLD
- 4 Text find in Layer ROAD

Didn't  clean the cash and write under again and again. I want every time I run the code to clean and write only the new results.

Thansk
Title: Re: Export results to notepad
Post by: kdub_nz on March 10, 2023, 05:10:57 PM
Looks to me like you have some global variables.
Perhaps add lstText (at least ) to the locals parameters

Code - Auto/Visual Lisp: [Select]
  1.  (defun c:test (/ F txt lstText) ....

Because it is currently global each iteration of your function will be cons'd to the lstText list
Title: Re: Export results to notepad
Post by: mhy3sx on March 10, 2023, 05:34:19 PM
Hi kdub. At the beginning I didn't believe that this was the solution, but I add

Code: [Select]
(defun c:test (/ F txt lstText) ....

And it works !!!!!

Thanks