Visual LISP, AutoLISP and General Customization

Reply
Contributor
deandavis
Posts: 17
Registered: ‎08-30-2007
Message 1 of 7 (376 Views)

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

376 Views, 6 Replies
05-02-2012 10:27 AM

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?

*Expert Elite*
Kent1Cooper
Posts: 5,231
Registered: ‎09-13-2004
Message 2 of 7 (372 Views)

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

05-02-2012 11:25 AM in reply to: deandavis

deandavis 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
*Expert Elite*
dgorsman
Posts: 5,035
Registered: ‎10-12-2006
Message 3 of 7 (369 Views)

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

05-02-2012 11:26 AM 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.
Adopt. Adapt. Overcome. Or be overcome.
A good question will be halfway to a good answer.


Contributor
deandavis
Posts: 17
Registered: ‎08-30-2007
Message 4 of 7 (362 Views)

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

05-02-2012 12:17 PM 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!

*Expert Elite*
dbroad
Posts: 1,475
Registered: ‎08-16-2007
Message 5 of 7 (361 Views)

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

05-02-2012 12:23 PM 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.

*Expert Elite*
Kent1Cooper
Posts: 5,231
Registered: ‎09-13-2004
Message 6 of 7 (357 Views)

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

05-02-2012 12:50 PM in reply to: deandavis

deandavis 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
Contributor
deandavis
Posts: 17
Registered: ‎08-30-2007
Message 7 of 7 (350 Views)

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

05-02-2012 01:50 PM 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.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community