Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using AutoLISP to copy a text string from one file to another

6 REPLIES 6
Reply
Message 1 of 7
deandavis
1734 Views, 6 Replies

Using AutoLISP to copy a text string from one file to another

I have spent the last three days writing a lisp program that will copy a text string from one text file and paste it into another.  I have tried using the write-string, getstring, prin1, princ and print functions with no success.  Does anyone know what I may be doing wrong?

6 REPLIES 6
Message 2 of 7
Kent1Cooper
in reply to: deandavis


@Anonymous wrote:

.... a lisp program that will copy a text string from one text file and paste it into another.  ....


You don't seem to be doing anything with what you read from the first file, either to save it or to put it into the second file.  Try either:

 

....

(setq f (open "c:\\misc\\test.csv" "r"))
(setq temp (read-line f))

(close f); maybe...?
(setq f (open "c:\\dwgs\\beniciafab\\scripttools\\testing.txt" "w"))

(write-line temp f)
(write-line "LOADRASTER" f)

....

 

or perhaps:

 

....

(setq

  e (open "c:\\misc\\test.csv" "r")
  f (open "c:\\dwgs\\beniciafab\\scripttools\\testing.txt" "w")

)

(write-line (read-line e) f)
(close e); maybe...?
(write-line "LOADRASTER" f)

....

Kent Cooper, AIA
Message 3 of 7
dgorsman
in reply to: deandavis

Just a cursory look, it looks like the call to (read-line...) isn't being assigned to a variable, and the source file isn't being closed before the destination file is being opened.  Plus, all the (write-line...) calls are using hard-coded values.

 

It also helps if the variables are both localized and given verbose names e.g. (defun foo ( argument1 argument 2 / file_name file_pointer).

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 4 of 7
deandavis
in reply to: deandavis

Your first suggestion worked.  Now I need to have the program loop through all the text strings in the first file and copy them to the second file until all drawings in the file have been processed.

 

Thanks again for your help!

Message 5 of 7
dbroad
in reply to: deandavis

Sure doesn't look like 3 days of effort. To copy text from one file to another, use 2 file handles, not 1.  Open the source "read" and the destination either "write" or "append".  Spell all functions and commands correctly.

Your code:

 

(defun c:startraster ()
;(prompt "\nHit Enter to continue ")
(command "iinsert") ;mispelled? incomplete command?
(command "zoom" "e");would be ignored if insert command worked.
(princ))

(defun c:runscan2cad ()
(setq f(open "c:\\misc\\test.csv" "r")) ;first file handle "f".
(read-line f) ;value from read-line ignored.
(setq f(open "c:\\dwgs\\beniciafab\\scripttools\\testing.txt" "w"));use same file handle for the second file. First file handle is lost
(write-line "LOADRASTER" f) ;overwrites anything in the file.
(write-line "REMOVESPECKLES" f)
(write-line "8" f)
(write-line "RUNVECTOR" f)
(write-line "SAVEVECTOR" f)
(write-line "EXIT" f)
(close f)
;(startapp "C:/program files/scan2cadv7/scan2cadv7.exe" "C:\\dwgs\\beniciafab\\scripttools\\scan2cadconvertraster.txt")
(princ))

(defun c:cleanraster ()
(Command "idespeckle" "" "x" "8" "" ^c) ;;Is idespeckle a real command?  ^c is interpreted as a symbol, not a cancel.
(princ))

 

Use read-line to read text from a file one line at a time.  Use write-line to write text to a file one line at a time.

This is not copy and paste.  Files must be closed as you have done before actions take effect.

Architect, Registered NC, VA, SC, & GA.
Message 6 of 7
Kent1Cooper
in reply to: deandavis


@Anonymous wrote:

Your first suggestion worked.  Now I need to have the program loop through all the text strings in the first file and copy them to the second file until all drawings in the file have been processed.

....


If you just want to transfer all the lines of text in one file into another, and you're making the new file to be written to rather than appended to, is there any reason not to just copy the file to another of a different name?  And what does "all drawings in the file" mean?  Drawings don't go in files -- they are files.  Are the text strings drawing names or something?

 

If you do mean to transfer all text lines in the first file to the second file, and there is some reason not to just copy the whole file, try something like:

  

(setq

  f (open "c:\\misc\\test.csv" "r")

  g (open "c:\\dwgs\\beniciafab\\scripttools\\testing.txt" "w")

); setq
(while

  (setq temp (read-line f)); will stop when it gets to end of first file

  (write-line temp g)

); while

(close f)

(close g)

Kent Cooper, AIA
Message 7 of 7
deandavis
in reply to: deandavis

Thanks for the info.  Now, to answer your questions.  Iinsert and idespeckle are commands used by Autodesk Raster Design to process scanned images.  It is just one of the programs I use to convert scanned drawings to CAD files.  It is a program that requires heavy interaction with the user to convert scanned images, but still is very useful depending on what I need to do to the scanned drawing.  The code you see in the original attachment above and below the code I am working on; is code I may run when using that program.

 

The other program that I use to convert scanned drawings is Scan2Cad by Softcover International.  It is a program that automatically converts scanned drawings to CAD drawings, (dxf format), which I can then open in AutoCAD.  The code I have been writing is to process scanned drawings via the command line processing built into Scan2Cad.  The first file is the drawing names and file paths of the scanned drawings I want to process.  The second file is the file that will be used by Scan2Cad to carry out the conversions via the command line switches.  That is why I copy one line at a time from the first file.  The looping will allow me to process the drawings listed in the first file until all files have been processed.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost