|
-
Mar 9th, 2008, 03:19 PM
#1
Thread Starter
New Member
New member query
Hello there everyone. I'm a new user to VB although I am fairly conversant with QBasic (lol i know..a long time ago). I used QBasic for printer operations and have never really got to grips with importing, manipulating and outputing files.
I would like to import a file (usually opened with notepad), make adjustments, and ooutput the new file.
As a cnc programmer, I would like to renumber my program file and also manipulate numbers.
The format is as follows...(example only)
N120 G0 X:123.123 Y:123.123 A:-123.123 etc etc
To renumber, as far as I understand, I would need to import the file, find the first line (N) make it 10, then find the next line and delete the number there, and rewrite to N+10 and repeat for all lines.
Also, I would like to be able to recalculate axis positions....for exaple ...add 180 to all A axis values.
Any advice on where to start would be greatly appreciated.
-
Mar 9th, 2008, 03:51 PM
#2
Re: New member query
you can write a small vb program just to do the conversions, opening the code file as a text file, once it is converted you can copy and paste it into your new project as required
note line numbers are optional in vb, not required, i would believe that most people do not use them
there are many examples here for opening and reading textfiles and manipulating strings and calculating with values of strings
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 9th, 2008, 04:04 PM
#3
Re: New member query
I think your task can be done in a single loop. Are you familiiar with reading/writing files? If not, recommend spending a little time on the home page's FAQ section, VB6 & Earlier. The logic below is described in the Files faq.
Don't know how big the files are; you have 2 basic options, read entire file into memory & manipulate it, then output. Or use 2 files, open one for reading and the other for writing.
pseudo code looks a little like this, using 2 file method. Split() & Join() are VB commands.
Code:
Open File for reading
Open File for writing
Begin loop
Read line of input
Does it begin with N?
If not keep reading until it is found or end of file occurs
once found ...
Replace N with new N
' to replace a token in the line....
Split() line on delimiter -- appears to be spaces
Change value as needed
Join() split line using same delimiter
Write modified line to file
Increment N
Continue Loop
Close both files
Delete old file, rename new file if needed
done
-
Mar 9th, 2008, 04:23 PM
#4
Thread Starter
New Member
Re: New member query
Thank you guys, I'll have a play and a look around the FAQ's. Just to note, files could be a few thousand lines long.
Really appreciate the start you have both given me, thanks again.
-
Jul 14th, 2008, 03:20 AM
#5
Thread Starter
New Member
Re: New member query
Been a while, but I got there . I used someones "delete word in file" program I found on the net and modified it to suit.
Boolean logic and the code I'm ok with, but I'm still struggling with subs, modules etc and how they fit in to VB code (learning fast tho )
So anyway, my main code is as follows and works a treat .....
Do While Not EOF(infile)
Line Input #infile, one_line
If Left$(one_line, 1) = "N" Then
oldn = Left$(one_line, 1)
chrcount = 1
While Asc(Right$(oldn, 1)) <> 32
oldn = Left$(one_line, chrcount)
chrcount = chrcount + 1
Wend
one_line = Replace(one_line, oldn, ("N" + CStr(newn) + " "))
newn = newn + step
End If
Print #outfile, one_line
Loop
I have another query tho, I would like to re-write the file but starting from the bottom line and working backwards (or starting reading at the top line and copying this to the bottom of the new file).
When I have used the "do while not EOF", is there a way I can get the line pointer back to the top of the file ?
An example of part of a file ....
N210 X: -158.501 Y: 858.88 A: -88.7084 B: 88.0005
N220 X: -158.488 Y: 858.746 Z: -109.293 A: -86.0202 B: 88.0048
N230 X: -158.464 Y: 858.629 Z: -109.294 A: -83.1159 B: 88.0144
N240 X: -158.432 Y: 858.53 Z: -109.295 A: -79.9853 B: 88.0304
N250 X: -158.397 Y: 858.449 Z: -109.296 A: -76.6225 B: 88.0542
I would like to read and copy to a new filename and swap the order as follows .....
N250 X: -158.397 Y: 858.449 Z: -109.296 A: -76.6225 B: 88.0542
N240 X: -158.432 Y: 858.53 Z: -109.295 A: -79.9853 B: 88.0304
N230 X: -158.464 Y: 858.629 Z: -109.294 A: -83.1159 B: 88.0144
N220 X: -158.488 Y: 858.746 Z: -109.293 A: -86.0202 B: 88.0048
N210 X: -158.501 Y: 858.88 A: -88.7084 B: 88.0005
The main problem for me is that once I have read and copied over the last line, I need to start to read again from the top of the file I am taking from (or move my line pointer EOF-n)
Thanks in advance.
-
Jul 14th, 2008, 10:45 AM
#6
Re: New member query
The simple way to write the lines in a file to a new file in reverse order is to save each line to a string array when reading the file in.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|