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

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

13 REPLIES 13
Reply
Message 1 of 14
deandavis
2557 Views, 13 Replies

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

Sorry folks, but I'm a newcomer to AutoLISP and still struggling with the code I was trying to write last week.  I do appreciate the help that all of you have given me so far.  I will try to better explain what I am trying to do.  I am trying to automate the conversion of scanned drawings to full CAD drawings.  The program I am using to convert the scanned drawings is titled Scan2Cad.  The program that I am writing generates a text file that can be used in a command prompt when Scan2Cad launches.  The file that I want the program to read from is test.csv.  This file has 13 lines in it.  Each line states the filename and directory path to the scanned file I want to convert.  The program will read the file path of the image and insert it in the appropriate line of the text file that will be called from the command prompt.  The program also writes additional lines specifying the options I want Scan2Cad to use for the conversion.  This generated text file is named testing.txt.

 

The program as written, will generate the first text file for the first image, launch Scan2Cad and save the dxf file.  What I need it to do, (and this is where I'm having difficulty), is for the program to continue to loop (using WHILE) through the remaining files listed in test.csv, overwriting the testing.txt file with the new information for the next image until all files listed in test.csv have been processed.  The code is shown below.  I have removed my previous while statement, as I could not get it to work.

 

Thanks again for your help!

 

 

(defun c:runscan2cad ()
  (setq a (open "c:\\misc\\test.csv" "r"))
  (setq temp (read-line a))
  (setq b (open "c:\\dwgs\\beniciafab\\scripttools\\testing.txt" "w"))
  (write-line "LOADRASTER" b)
  (write-line temp b)
  (write-line "REMOVESPECKLES" b)
  (write-line "8" b)
  (write-line "RUNVECTOR" b)
  (write-line "SAVEVECTOR" b)
  (write-line "EXIT" b)
  (close b)
  (startapp "C:/program files/scan2cadv7/scan2cadv7.exe"
	    "C:\\dwgs\\beniciafab\\scripttools\\testing.txt"
  )
  (princ)
)

 

13 REPLIES 13
Message 2 of 14
hgasty1001
in reply to: deandavis

Hi, Not sure about the workflow you are trying to implement, but something  like this may help.

 

(while (setq temp (read-line a))

   (do your stuff...)

)

(close a)

 

Obs: I think you need to create a different "testing.txt" in each pass of the loop, usinga index like

 

(setq idx 0)

(setq path "MyPath")

(while (setq temp (read-line a))

 (setq testingFileName (strcat path  "testing" (itoa idx) ".txt"))

(setq b (open testingFileName "w"))

(write...stuff)

(close b)

(startapp...)

(setq idx (+ idx 1))

)

(close a)

 

 

 

Message 3 of 14
balisteor
in reply to: deandavis

if you could attach the csv file that has all of the filenames that would be helpful, because it is a csv file you may need to remove commas, or other information from each line, but from what your doing it seems that every line in the csv file is the exact path to the drawing on each line and not really a csv file. If this is the case I have made some changes to your program and it should work. if not  then hopefully it helps you finish it.

 

(defun c:runscan2cad ( / a b temp)
(setq a (open "c:\\misc\\test.csv" "r"))
(while (setq temp (read-line a))
(setq b (open "c:\\dwgs\\beniciafab\\scripttools\\testing.txt" "w"))
(foreach line (list "LOADRASTER" temp "REMOVESPECKLES" "8" "RUNVECTOR" "SAVEVECTOR" "EXIT")
(write-line line b)
);fe
(close b)
(startapp "C:/program files/scan2cadv7/scan2cadv7.exe" "C:\\dwgs\\beniciafab\\scripttools\\testing.txt")
);w

(close a)
(princ))

 
Also i have never used this program "Scan2Cad", so just wanted to let you know that this will start the application up for every line in the csv file. and it may or may not shut itself down every time.  but autolisp will not wait for it to do so.

 

Message 4 of 14
deandavis
in reply to: balisteor

Thank you for your help. I will try the program and see how it does. I may need to find some way to pause AutoLISP to allow Scan2Cad to exit before AutoLISP starts processing the next line. I have attached the csv file as you requested, but you are correct, each line just shows the filename and path to the file. Dean Davis CAD Administrator Capital Projects - Drafting Chevron Richmond Refinery
Message 5 of 14
balisteor
in reply to: deandavis

If waiting for the program to complete is an issue I'm sure there are a few solutions, I don't know if you are familiar with doslib, but that is what I use for these types of things.

Message 6 of 14
deandavis
in reply to: balisteor

I ran your code and it does like you say. The Scan2Cad program launches, but AutoLISP continues to run the rest of the program code before Scan2Cad finishes the first line. Scan2CAD displays the message "Only a single instance of Scan2Cad is allowed". I have seen another lisp programmer use the DELAY statement in an AutoLISP program he wrote, but I don't fully follow his code to see if there is anything else he has put in that allows it to work.
Message 7 of 14
balisteor
in reply to: deandavis

So, my next question would be does scan2cad shut down when it is finished? or do we also need to shut it down, or it may also accept sending a different command to it to scan another drawing while its open, see if you can figure this stuff out, If i get a change ill download the trial and see what i can find out.  we can always have autolisp delay and then continue but even then if scan2cad wont accept another drawing and wont shut down we have to fix that first.

I also wonder even if scan2cad can read a file with more lines, so we just send it all info at once.

so file will look something like the following and be accepted by scan2cad

 

"LOADRASTER"
**filetoread**
"REMOVESPECKLES"
"8"
"RUNVECTOR"
"SAVEVECTOR"
"EXIT" <<<-- is this supposed to exit program (if so we remove it for next files)

"LOADRASTER"
**filetoread**
"REMOVESPECKLES"
"8"
"RUNVECTOR"
"SAVEVECTOR"
"EXIT"

"LOADRASTER"
**filetoread**
"REMOVESPECKLES"
"8"
"RUNVECTOR"
"SAVEVECTOR"
"EXIT"

"LOADRASTER"
**filetoread**
"REMOVESPECKLES"
"8"
"RUNVECTOR"
"SAVEVECTOR"
"EXIT"

 

 

 

Send me a link if you have one on the scan2cad documentation, otherwise i will try to find this out when i can.

Message 8 of 14
balisteor
in reply to: deandavis

I was digging around with the documentation i could find online, keep in mind this is all guesswork but If you have version 8.3 this may work well, see if you can read through what im doing here so you can see if i have the right idea.

 

(defun c:runscan2cad ( / filetoread filetowrite scanfilepath)
(setq filetoread (open "c:\\misc\\test.csv" "r")
      filetowrite (open "c:\\dwgs\\beniciafab\\scripttools\\testing.txt" "w")
)
(while (setq scanfilepath (read-line filetoread))
(foreach S2C_COMMAND (list "LOADRASTER" scanfilepath "REMOVESPECKLES" "8" "RUNVECTOR" "SAVEVECTOR" "CLOSERASTER")
(write-line S2C_COMMAND filetowrite)
);fe
);w

(close filetoread)
(close filetowrite)
(startapp "C:/program files/scan2cadv7/scan2cadv7.exe" "C:\\dwgs\\beniciafab\\scripttools\\testing.txt")
(princ))

 

Message 9 of 14
deandavis
in reply to: balisteor

LOADRASTER G:\SCANNED\MHA Scanned Files\Delivery 06\D-89429-0.TIF REMOVESPECKLES 8 RUNVECTOR SAVEVECTOR EXIT
Message 10 of 14
deandavis
in reply to: balisteor

LOADRASTER G:\SCANNED\MHA Scanned Files\Delivery 06\D-304021-4.TIF REMOVESPECKLES 8 RUNVECTOR SAVEVECTOR EXIT LOADRASTER G:\SCANNED\MHA Scanned Files\Delivery 06\D-304022-4.TIF REMOVESPECKLES 8 RUNVECTOR SAVEVECTOR EXIT LOADRASTER G:\SCANNED\MHA Scanned Files\Delivery 06\D-304023-4.TIF REMOVESPECKLES 8 RUNVECTOR SAVEVECTOR EXIT LOADRASTER G:\SCANNED\MHA Scanned Files\Delivery 06\D-304024-4.TIF REMOVESPECKLES 8 RUNVECTOR SAVEVECTOR EXIT LOADRASTER G:\SCANNED\MHA Scanned Files\Delivery 06\D-304025-4.TIF REMOVESPECKLES 8 RUNVECTOR SAVEVECTOR EXIT LOADRASTER G:\SCANNED\MHA Scanned Files\Delivery 06\D-304026-2.TIF REMOVESPECKLES 8 RUNVECTOR SAVEVECTOR EXIT LOADRASTER G:\SCANNED\MHA Scanned Files\Delivery 03\D-308245-0.TIF REMOVESPECKLES 8 RUNVECTOR SAVEVECTOR EXIT LOADRASTER G:\SCANNED\MHA Scanned Files\Delivery 06\B-311559-0-1.TIF REMOVESPECKLES 8 RUNVECTOR SAVEVECTOR EXIT LOADRASTER G:\SCANNED\MHA Scanned Files\Delivery 06\D-312422-0.TIF REMOVESPECKLES 8 RUNVECTOR SAVEVECTOR EXIT LOADRASTER G:\SCANNED\MHA Scanned Files\Delivery 06\D-312423-0.TIF REMOVESPECKLES 8 RUNVECTOR SAVEVECTOR EXIT LOADRASTER G:\SCANNED\MHA Scanned Files\Delivery 06\D-312424-0.TIF REMOVESPECKLES 8 RUNVECTOR SAVEVECTOR EXIT LOADRASTER G:\SCANNED\MHA Scanned Files\Delivery 02\B-86761-1.TIF REMOVESPECKLES 8 RUNVECTOR SAVEVECTOR EXIT LOADRASTER G:\SCANNED\MHA Scanned Files\Delivery 06\D-89429-0.TIF REMOVESPECKLES 8 RUNVECTOR SAVEVECTOR EXIT
Message 11 of 14
deandavis
in reply to: balisteor

OK, so I'm almost there with the code.  I have been able to delay AutoLISP to allow Scan2Cad to process the command line file and repeat the process for all of the filenames and paths in the CSV file except for the very first one.  In other words, there are 13 lines that need to be processed.  The program runs 13 instances launching Scan2Cad 13 times but when the program finishes, only 12 dxf files were generated.  The very first file is always dropped.  In this case, D-304021-4.tif.  I will attach the CSV file to this post.  You can open it with Microsoft Excel.

 

 Clarifying info: Only one instance of Scan2Cad can be run at a time.  The delay built into the lisp routine allows Scan2Cad to process the file and exit, before the next command line file is sent to it.  I have attached the code as it stands now for you to review.  I commented the FOREACH line out in favor of direct code for the lines that need to be included in the txt file to see if there would be a difference.  (There wasn't any).

 

Thanks again for your help!

 

(defun c:runscan2cad ( / a b temp)
(progn
(setq a (open "c:\\misc\\test.csv" "r"))
(while (setq temp (read-line a))
(setq b (open "c:\\dwgs\\beniciafab\\scripttools\\testing.txt" "w"))
(write-line "LOADRASTER" b)
(write-line temp b)
(write-line "REMOVESPECKLES" b)
(write-line "8" b)
(write-line "RUNVECTOR" b)
(write-line "SAVEVECTOR" b)
(write-line "EXIT" b)
;(foreach line (list "LOADRASTER" temp "REMOVESPECKLES" "8" "RUNVECTOR" "SAVEVECTOR" "EXIT") (write-line line b) )
(close b)
(command "delay" 20000)
(startapp "C:/program files/scan2cadv7/scan2cadv7.exe" "C:\\dwgs\\beniciafab\\scripttools\\testing.txt") )
(close a))
(princ))

 

Message 12 of 14
pbejse
in reply to: deandavis

A shot in the dark, Is it possible theres nothing worng with your code and the problem is the TIF file?

or perhaps misspelled filename on your CSV file. 

 

Message 13 of 14
deandavis
in reply to: pbejse

I resolved it yesterday afternoon. It had to do with the placement of the delay command in the routine. Once I placed the delay command behind the call to Scan2Cad, the routine worked and all 13 drawings were processed.
Message 14 of 14
balisteor
in reply to: deandavis


deandavis wrote: I commented the FOREACH line out in favor of direct code for the lines that need to be included in the txt file to see if there would be a difference.  (There wasn't any).

There should be no difference, Just the way i like to write things

 


deandavis wrote: Once I placed the delay command behind the call to Scan2Cad, the routine worked and all 13 drawings were processed.

good work finding your own mistakes too! I'm glad to seee that you have it working now.

 

 

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

Post to forums  

Autodesk Design & Make Report

”Boost