Home > Archive > Visual Basic > August 2005 > How to speed up this process
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to speed up this process
|
|
| SteveC 2005-08-24, 9:55 pm |
| This code I've managed to cobble together from reading/searching this
group and some help from regulars,Rick and Ken especially.Thanks.
It runs fine but really slows down on files over 2500 lines.There must
be some way to improve its speed.Any pointers or samples.?
SteveC
Dim uSpacedFile As String
Dim Ff As Integer
Ff = FreeFile
frmView.RichTextBox1.Text = vbNullString
uSpacedFileName = CommonDialog1.FileName
Open uSpacedFileName For Input As #Ff
Do While Not EOF(Ff)
Line Input #Ff, Lotxt
AddSpaces Lotxt
SpacedFile = SpacedFile & LTrim$(Lotxt) & vbNewLine
Loop
Close Ff
frmView.RichTextBox1.Text = SpacedFile
Sub AddSpaces(ByRef Lotxt)
Dim i As Integer
Dim X As Long
Dim Chars() As String
Chars = Split("X Y Z G F S I J K M P L D H")
For X = 0 To UBound(Chars)
If InStr(Lotxt, "(") <> 0 Then Exit For
Lotxt = Replace$(Lotxt, Chars(X), " " & Chars(X))
Next
End Sub
| |
| Karl E. Peterson 2005-08-24, 9:55 pm |
| Hi Steve --
Two things are killing you:
* reading the file line by line
* concatenation
Recommend reading the entire file in one gulp:
Public Function ReadFile(ByVal FileName As String) As String
Dim hFile As Long
On Error GoTo Hell
hFile = FreeFile
Open FileName For Binary As #hFile
ReadFile = Space$(LOF(hFile))
Get #hFile, , ReadFile
Close #hFile
Hell:
End Function
Then running that entire string through your spacing routine. You may need to run
one more Replace(filetext, vbNewLine & " ", vbNewLine) just to make up for the loss
of LTrim, too?
Also, it's unclear to me what the test for a "(" is there for, but wouldn't it make
more sense to pull that out front of the loop rather than check on each iteration?
Later... Karl
--
Working Without a .NET?
http://classicvb.org/petition
SteveC wrote:
> This code I've managed to cobble together from reading/searching this
> group and some help from regulars,Rick and Ken especially.Thanks.
> It runs fine but really slows down on files over 2500 lines.There must
> be some way to improve its speed.Any pointers or samples.?
> SteveC
>
>
> Dim uSpacedFile As String
> Dim Ff As Integer
> Ff = FreeFile
>
> frmView.RichTextBox1.Text = vbNullString
> uSpacedFileName = CommonDialog1.FileName
> Open uSpacedFileName For Input As #Ff
> Do While Not EOF(Ff)
> Line Input #Ff, Lotxt
> AddSpaces Lotxt
> SpacedFile = SpacedFile & LTrim$(Lotxt) & vbNewLine
> Loop
> Close Ff
>
> frmView.RichTextBox1.Text = SpacedFile
>
> Sub AddSpaces(ByRef Lotxt)
> Dim i As Integer
> Dim X As Long
> Dim Chars() As String
> Chars = Split("X Y Z G F S I J K M P L D H")
>
> For X = 0 To UBound(Chars)
> If InStr(Lotxt, "(") <> 0 Then Exit For
> Lotxt = Replace$(Lotxt, Chars(X), " " & Chars(X))
> Next
> End Sub
| |
| SteveC 2005-08-24, 9:55 pm |
| Hi Karl,
I figured that reading the whole file at once was needed , but I
couldn't figure out how to run that through the addspace after wards
and then display the results all at once in the texbox
SteveC
| |
| Karl E. Peterson 2005-08-24, 9:55 pm |
| SteveC wrote:
> I figured that reading the whole file at once was needed , but I
> couldn't figure out how to run that through the addspace after wards
> and then display the results all at once in the texbox
Pretty much exactly as you're doing it, except all at once rather than one line at a
time. Not sure I fully appreciate the purpose of that routine, though. If I did, I
might have other ideas how to optimize.
Later... Karl
--
Working Without a .NET?
http://classicvb.org/petition
| |
| Rick Rothstein [MVP - Visual Basic] 2005-08-25, 3:55 am |
| > > I figured that reading the whole file at once was
>
> Pretty much exactly as you're doing it, except all
> at once rather than one line at a time.
Not necessarily. I think that "(" you asked about might affect this.
Since the OP was reading the file line-by-line, it appears he only wants
to add spaces to the text in front of the "(" and not after it (the Exit
For seems to indicate this).
> Not sure I fully appreciate the purpose of that routine,
> though. If I did, I might have other ideas how to optimize.
Agreed. If he would tell us what he is attempting to do, we might be
able to make better comments on how to proceed.
Rick
| |
| Ken Halter 2005-08-25, 3:55 am |
| "Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net>
wrote in message news:O13TucSqFHA.3524@tk2msftngp13.phx.gbl...
>
> Agreed. If he would tell us what he is attempting to do, we might be
> able to make better comments on how to proceed.
>
> Rick
>
iirc, he's creating a utility that adds spaces between "words" in a CNC
program. Some CNC controls "swallow" all of the white space during upload so
when you make changes to the program out in the shop and download the
modified version back to the PC, it's all crammed together and hard to read.
Newer controls have parameters you can set to prevent this type of thing but
with the oldies, you get what you get <g> (completely off topic but....)
fwiw, the first CNC I ever programmed couldn't even swing an arc! You had to
"point to point" a full circle... of course, a good finish meant a huge
program (many, many points). Another one of my favorite "death machines" was
a Brown & Sharp. It required (and I do mean "required") leading zeros before
each and every number. The axis positions were 6 digits. 000000 to 999999.
(last 2 were decimals) If you entered 9 by itself, the control interpreted
that as 9000.00. Now, the machine right next to that was an OKK and was the
exact opposite. If you enter 9 by itself, it interpreted that as .0009. FAR
safer but switching back and forth was a nightmare waiting to happen.
Imagine thinking that you're moving the part .0009 and it starts taking off
into space, heading for that 9000.00 inch mark.... did I mention that these
ran on mylar tape? The fun never stops.
I picked up the sample CNC code below from the web to give y'all an idea of
what it's supposed to look like when it's "cleaned up"
O0001 (Program number)
N005 T01 M06 (Place 3/4 end mill in spindle)
N010 G54 G90 S500 M03 T02 (Select coordinate system, abs mode, start
spindle, get next tool ready)
N015 G00 X-0.5 Y-0.5 (Rapid to approach position)
N020 G43 H01 Z0.1 (Instate tool length compensation)
N025 Z-0.6 M08 (Rapid to work surface, turn on ant)
N030 G41 D31 X0 (Instate cutter radius comp)
N035 G01 Y0.7395 (Mill left end)
N040 X1.75 (Mill top to beginning of left radius)
N045 G03 X2.4134 Y1.0 J0.975 (Mill top left radius)
N050 G01 X6.5236 (Mill top to beginning of top right radius)
N055 G03 X7.187 Y0.7395 I0.6634 J0.7145 (Mill top right radius)
N060 G01 X8.0 (Mill to right end)
N065 Y0 (Mill right end)
N070 X7.1843 (Mill to beginning of bottom right radius)
N075 G03 X6.375 Y0.355 I-0.8093 J-0.745 (Mill bottom right radius)
N080 G01 X2.375 (Mill bottom to beginning of lower left radius)
N085 G03 X1.5657 Y0 J-1.1 (Mill lower left radius)
N090 G01 X-0.2 (Mill final portion of bottom)
N095 G00 Z0.1 (Retract in Z)
N100 G40 M09 (Cancel cutter comp, turn off ant)
N105 G91 G28 Z0 M19 (Return to tool change position)
N110 M01 (Optional stop)
--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
| |
| Rick Rothstein [MVP - Visual Basic] 2005-08-25, 3:55 am |
| Okay, this all is starting to make sense now. Since Steve doesn't
attempt to put a space in front of the left parenthesis, I presume there
is a space automatically placed there. Given this and your explanation,
I probably would do what Steve wants this way (warning, top of the head
code follows)....
Dim X As Long
Dim Y As Long
Dim FileNum As Long
Dim TotalFile As String
Dim Lines() As String
Dim Fields() As String
Dim Chars() As String
Chars = Split("X Y Z G F S I J K M P L D H")
FileNum = FreeFile
Open "c:\SomeDircectory\TextFile.txt" For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum, , TotalFile
Close #FileNum
Lines = Split(TotalFile, vbNewLine)
For X = 0 To UBound(Lines)
Fields = Split(Lines(X), "(")
For Y = 0 To UBound(Chars)
Fields(0) = Replace(Fields(0), Chars(Y), " " & Chars(Y))
Next
Lines(X) = Join(Fields, "(")
Next
frmView.RichTextBox1.Text = Join(Lines, vbNewLine)
I think I covered everything.
Rick
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:%23r2qhsSqFHA.272@TK2MSFTNGP15.phx.gbl...
> "Rick Rothstein [MVP - Visual Basic]"
<rickNOSPAMnews@NOSPAMcomcast.net>
> wrote in message news:O13TucSqFHA.3524@tk2msftngp13.phx.gbl...
>
> iirc, he's creating a utility that adds spaces between "words" in a
CNC
> program. Some CNC controls "swallow" all of the white space during
upload so
> when you make changes to the program out in the shop and download the
> modified version back to the PC, it's all crammed together and hard to
read.
> Newer controls have parameters you can set to prevent this type of
thing but
> with the oldies, you get what you get <g> (completely off topic
but....)
> fwiw, the first CNC I ever programmed couldn't even swing an arc! You
had to
> "point to point" a full circle... of course, a good finish meant a
huge
> program (many, many points). Another one of my favorite "death
machines" was
> a Brown & Sharp. It required (and I do mean "required") leading zeros
before
> each and every number. The axis positions were 6 digits. 000000 to
999999.
> (last 2 were decimals) If you entered 9 by itself, the control
interpreted
> that as 9000.00. Now, the machine right next to that was an OKK and
was the
> exact opposite. If you enter 9 by itself, it interpreted that as
..0009. FAR
> safer but switching back and forth was a nightmare waiting to happen.
> Imagine thinking that you're moving the part .0009 and it starts
taking off
> into space, heading for that 9000.00 inch mark.... did I mention that
these
> ran on mylar tape? The fun never stops.
>
> I picked up the sample CNC code below from the web to give y'all an
idea of
> what it's supposed to look like when it's "cleaned up"
>
> O0001 (Program number)
> N005 T01 M06 (Place 3/4 end mill in spindle)
> N010 G54 G90 S500 M03 T02 (Select coordinate system, abs mode, start
> spindle, get next tool ready)
> N015 G00 X-0.5 Y-0.5 (Rapid to approach position)
> N020 G43 H01 Z0.1 (Instate tool length compensation)
> N025 Z-0.6 M08 (Rapid to work surface, turn on ant)
> N030 G41 D31 X0 (Instate cutter radius comp)
> N035 G01 Y0.7395 (Mill left end)
> N040 X1.75 (Mill top to beginning of left radius)
> N045 G03 X2.4134 Y1.0 J0.975 (Mill top left radius)
> N050 G01 X6.5236 (Mill top to beginning of top right radius)
> N055 G03 X7.187 Y0.7395 I0.6634 J0.7145 (Mill top right radius)
> N060 G01 X8.0 (Mill to right end)
> N065 Y0 (Mill right end)
> N070 X7.1843 (Mill to beginning of bottom right radius)
> N075 G03 X6.375 Y0.355 I-0.8093 J-0.745 (Mill bottom right radius)
> N080 G01 X2.375 (Mill bottom to beginning of lower left radius)
> N085 G03 X1.5657 Y0 J-1.1 (Mill lower left radius)
> N090 G01 X-0.2 (Mill final portion of bottom)
> N095 G00 Z0.1 (Retract in Z)
> N100 G40 M09 (Cancel cutter comp, turn off ant)
> N105 G91 G28 Z0 M19 (Return to tool change position)
> N110 M01 (Optional stop)
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Please keep all discussions in the groups..
>
>
| |
| Larry Serflaten 2005-08-25, 7:56 am |
|
"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net> wrote
> Okay, this all is starting to make sense now. Since Steve doesn't
> attempt to put a space in front of the left parenthesis, I presume there
> is a space automatically placed there. Given this and your explanation,
> I probably would do what Steve wants this way (warning, top of the head
> code follows)....
I would have attacked it more as a copy and add process, like:
LFS
Dim src As Long
Dim dst As Long
Dim char As Long
Dim FileNum As Long
Dim TotalFile As String
Dim NewFile As String
Dim skip As Boolean
Const Targets = "XYZGFSIJKMPLDH"
FileNum = FreeFile
Open "c:\SomeDircectory\TextFile.txt" For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
NewFile = TotalFile & TotalFile
Get #FileNum, , TotalFile
Close #FileNum
dst = 1
For src = 1 To Len(TotalFile)
char = Asc(Mid(TotalFile, src, 1))
If char = 10 Then skip = False
If char = 40 Then skip = True
If Not skip Then
If InStr(Targets, Chr$(char)) > 0 Then
dst = dst + 1
End If
End If
Mid(NewFile, dst, 1) = Mid$(TotalFile, src, 1)
Next
NewFile = RTrim$(NewFile)
| |
| Rick Rothstein [MVP - Visual Basic] 2005-08-25, 6:55 pm |
| > I would have attacked it more as a copy and add process, like:
>
> LFS
>
> Dim src As Long
> Dim dst As Long
> Dim char As Long
> Dim FileNum As Long
> Dim TotalFile As String
> Dim NewFile As String
> Dim skip As Boolean
>
> Const Targets = "XYZGFSIJKMPLDH"
>
> FileNum = FreeFile
> Open "c:\SomeDircectory\TextFile.txt" For Binary As #FileNum
> TotalFile = Space(LOF(FileNum))
> NewFile = TotalFile & TotalFile
> Get #FileNum, , TotalFile
> Close #FileNum
>
> dst = 1
> For src = 1 To Len(TotalFile)
> char = Asc(Mid(TotalFile, src, 1))
> If char = 10 Then skip = False
> If char = 40 Then skip = True
> If Not skip Then
> If InStr(Targets, Chr$(char)) > 0 Then
> dst = dst + 1
> End If
> End If
> Mid(NewFile, dst, 1) = Mid$(TotalFile, src, 1)
> Next
> NewFile = RTrim$(NewFile)
Yes, that is another approach and I would guess a faster one than mine
(too lazy to check that out though<g> ).
Two questions...
1) I've only done a quick read of your code, but aren't you
missing a dst=dst+1 statement in your loop (probably
just before the Next statement)?
2) Why is char a Long rather than a String? You could
store Mid(TotalFile, src, 1) in it and test char against
Chr$(10) and "(" in the two If statements) and then use
it directly within the InStr statement. I'm not sure I see
the benefit in working with char as a Long.
Rick
| |
| Larry Serflaten 2005-08-25, 6:55 pm |
|
"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net> wrote
[color=darkred]
> Two questions...
>
> 1) I've only done a quick read of your code, but aren't you
> missing a dst=dst+1 statement in your loop (probably
> just before the Next statement)?
Yep, somehow it didn't make it there after a bout of testing... (ooops)
>
> 2) Why is char a Long rather than a String? You could
> store Mid(TotalFile, src, 1) in it and test char against
> Chr$(10) and "(" in the two If statements) and then use
> it directly within the InStr statement. I'm not sure I see
> the benefit in working with char as a Long.
Probably not a big concern, but since the two char tests were
executed in every loop, I'd want them comparing numbers instead
of strings. The Instr function is only called if its not skippng over
the comments (which usually seemed to be longer than the commands)
so it used the more 'expensive' conversion....
LFS
| |
| SteveC 2005-08-25, 6:55 pm |
| Thanks Larry,I fiddled with this and noticed I needed to add another
dst=dst+1 right above the next.Ken is right it is a cnc utility,I can
write nc code of the top of my head but VB is still a struggle.The only
other function I'm going to try to add is to change the feed
value,Fxx.xx, find, and change the value either to a specific value or
a percent change, verify before change.I'm looking at the Search code
that comes with the VB mdi notepad example.maybe I can tweak it.Any
pointers or samples will you're at very much appreciated.Thanks again
gentlemen
SteveC
| |
| Rick Rothstein [MVP - Visual Basic] 2005-08-25, 6:55 pm |
| > Thanks Larry,I fiddled with this and noticed I needed to add another
> dst=dst+1 right above the next.Ken is right it is a cnc utility,I can
> write nc code of the top of my head but VB is still a struggle.The
only
> other function I'm going to try to add is to change the feed
> value,Fxx.xx, find, and change the value either to a specific value or
> a percent change, verify before change.I'm looking at the Search code
> that comes with the VB mdi notepad example.maybe I can tweak it.Any
> pointers or samples will you're at very much appreciated.Thanks again
> gentlemen
Not being familiar with a "cnc utility", I'm not sure how accurately you
know the feed value that you want to search for. In other words, do you
know the exact value you are searching for or only that you are trying
to locate an "F" field wherever it may be? Depending on what you know
about it, you might find the Find method of the RichTextBox useful in
performing that search. If you provide more detail as to how this search
is to work, perhaps we can suggest some coding approaches to use.
Rick
| |
| SteveC 2005-08-25, 6:55 pm |
| I would search for an "F", which denotes the feed in inches or mm per
minute. eg F12.50 then I would like to change the value to say F15.75
or maybe a 10% increase from F12.50 to F13.75, so basically
all numbers to the right of the "F" to the next space,I'd do this after
I "space" the file.Since this is an in house utility I write my cnc
programme,run it on the machine and make adjustments to the programme
feeds, with a override switch on the machine, if needed.Save the
programme back to the computer,Now I want to make the feed changes so
next time I run it the feeds are right.A lot of machining is,these are
the theoretical feeds now we make real world adjustments.
SteveC
| |
| Rick Rothstein [MVP - Visual Basic] 2005-08-25, 6:55 pm |
| > I would search for an "F", which denotes the feed in inches or mm per
> minute. eg F12.50 then I would like to change the value to say F15.75
> or maybe a 10% increase from F12.50 to F13.75, so basically
> all numbers to the right of the "F" to the next space,I'd do this
after
> I "space" the file.Since this is an in house utility I write my cnc
> programme,run it on the machine and make adjustments to the programme
> feeds, with a override switch on the machine, if needed.Save the
> programme back to the computer,Now I want to make the feed changes so
> next time I run it the feeds are right.A lot of machining is,these are
> the theoretical feeds now we make real world adjustments.
When do you want to make these changes, before or after the text is
placed in the RichTextBox? Also, is the "F" always upper case? And, does
every line always have an "F" field?
Rick
| |
| Ken Halter 2005-08-25, 6:55 pm |
| "Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net>
wrote in message news:ucm0lmZqFHA.620@TK2MSFTNGP15.phx.gbl...
>
> When do you want to make these changes, before or after the text is
> placed in the RichTextBox? Also, is the "F" always upper case? And, does
> every line always have an "F" field?
>
> Rick
>
F is always Upper Case and it's required on a line only if it needs to
change.
It's really too bad I left the source behind where I used to work. I had a
great utility going. Did things like... Shift Coordinates X/Y/Z, Rotate
Coordinates X/Y/Z, Showed how long a program would take to run, both before
and after changing speeds/feeds, reversed the CNC program back to it's cad
source, etc, etc ,etc
--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
| |
| Rick Rothstein [MVP - Visual Basic] 2005-08-25, 6:55 pm |
| > I would search for an "F", which denotes the feed in inches or mm per
> minute. eg F12.50 then I would like to change the value to say F15.75
> or maybe a 10% increase from F12.50 to F13.75, so basically
> all numbers to the right of the "F" to the next space,I'd do this
after
> I "space" the file.Since this is an in house utility I write my cnc
> programme,run it on the machine and make adjustments to the programme
> feeds, with a override switch on the machine, if needed.Save the
> programme back to the computer,Now I want to make the feed changes so
> next time I run it the feeds are right.A lot of machining is,these are
> the theoretical feeds now we make real world adjustments.
Based on Ken's last response to me, below is some code that should do
what you want. Notice that it is based on the code I posted earlier to
add the spaces to the original file. If you used that method in your
program, you would already have the Total file in a String variable and,
assuming you want to make the corrections prior to putting the corrected
file into the RichTextBox, you code merge the code below with that
previously posted routine. If you want to correct the data after it is
in the RichTextBox, then use the code as shown sort of as is. If you
plan to do more than one modification, either do all of them at once in
the area designated for you calculations or let the Lines array persist
and operate on that directly each time (rather than always splitting the
text in the RichTextBox with every calculation or modification you do).
What you do to optimize this code depends on what it is your program
needs to do and how it needs to do it.
Rick
Dim TotalFile As String
' Here I've used a CommandButton to kick off the
' routine, move the code to whatever event it needs
' to be executed from.
Private Sub Command1_Click()
Dim X As Long
Dim ActualValue As Double
Dim FNumber() As String
Dim FSplit() As String
Dim Lines() As String
TotalFile = RichTextBox1.Text
Lines = Split(TotalFile, vbNewLine)
For X = 0 To UBound(Lines)
If InStr(Left$(Lines(X), InStr(Lines(X), "(")), "F") Then
FSplit = Split(Lines(X), "F")
FNumber = Split(FSplit(1))
ActualValue = CDbl(FNumber(0))
'
' Perform your actual modifcation
' to the "F" value here. Below, I'm
' increasing it by 10%.
'
ActualValue = 1.1 * ActualValue
FNumber(0) = CStr(ActualValue)
FSplit(1) = Join(FNumber, " ")
Lines(X) = Join(FSplit, "F")
End If
Next
TotalFile = Join(Lines, vbNewLine)
RichTextBox1.Text = TotalFile
End Sub
| |
| Karl E. Peterson 2005-08-25, 6:55 pm |
| Rick Rothstein [MVP - Visual Basic] wrote:
>
> Not necessarily. I think that "(" you asked about might affect this.
> Since the OP was reading the file line-by-line, it appears he only
> wants to add spaces to the text in front of the "(" and not after it
> (the Exit For seems to indicate this).
It looked to me like he didn't want to do any replaces if "(" was in the line.
Ahhhh... <lightbulb!> Yeah, that presents a problem, don't it? <g>
Okay, amend recommendation to continue reading the entire file in one gulp, but
running Replace on a Split array. Better?
>
> Agreed. If he would tell us what he is attempting to do, we might be
> able to make better comments on how to proceed.
Yep.
--
Working Without a .NET?
http://classicvb.org/petition
| |
| Rick Rothstein [MVP - Visual Basic] 2005-08-25, 6:55 pm |
| > Okay, amend recommendation to continue reading the entire
> file in one gulp, but running Replace on a Split array. Better?
Much better, especially since I posted such a solution about 3h16m
ago.<g>
Rick
| |
| Karl E. Peterson 2005-08-25, 6:55 pm |
| Rick Rothstein [MVP - Visual Basic] wrote:
>
> Much better, especially since I posted such a solution about 3h16m
> ago.<g>
Yeah, and since I hadn't read that, I'll agree it's much better.
--
Working Without a .NET?
http://classicvb.org/petition
| |
| SteveC 2005-08-25, 9:55 pm |
| Thanks Rick, I played with it a bit and found I had to change this
> If InStr(Left$(Lines(X), InStr(Lines(X), "(")), "F") Then
to If InStr(Left$(Lines(X), InStr(Lines(X), "F")), "") Then
If I read it right ,the the line will only read an "F" after a left
bracket.
on cnc programmes brackets are use to enclose comments and the machine
doesn't read them so I want to ignore everything between brackets. Any
way it mostly works although on some test programme files I get an type
mismatch error at this point,
ActualValue = CDbl(FNumber(0))
SteveC
| |
| Rick Rothstein [MVP - Visual Basic] 2005-08-26, 3:55 am |
| > Thanks Rick, I played with it a bit and found I had to change this
> to If InStr(Left$(Lines(X), InStr(Lines(X), "F")), "") Then
Why?
> If I read it right ,the the line will only read an "F" after a left
> bracket.
What my statement does is looks for an "F" in the text on the left side
of the "(". The line worked fine in the test I did against the sample
file data that Ken posted.
> on cnc programmes brackets are use to enclose comments and the machine
> doesn't read them so I want to ignore everything between brackets. Any
> way it mostly works although on some test programme files I get an
type
> mismatch error at this point,
> ActualValue = CDbl(FNumber(0))
I think the may be happening because of the change you made to my posted
code. Did you try the code exactly as I had posted it? If so, and if you
are getting an error, can you post a sample of the line in the file that
is producing the error?
Rick
| |
| SteveC 2005-08-26, 6:55 pm |
| Rick Rothstein [MVP - Visual Basic] Aug 25, 9:24 pm show options
Newsgroups: microsoft.public.vb.general.discussion
From: "Rick Rothstein [MVP - Visual Basic]"
<rickNOSPAMn...@NOSPAMcomcast.net> - Find messages by this author
Date: Fri, 26 Aug 2005 00:24:06 -0400
Local: Thurs, Aug 25 2005 9:24 pm
Subject: Re: How to speed up this process
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse
> Thanks Rick, I played with it a bit and found I had to change this
> to If InStr(Left$(Lines(X), InStr(Lines(X), "F")), "") Then
[color=darkred]
It didn't work otherwise.
this is a bit of the file that causes the type mismatch error.
G90 G0 G28 G40 G49 Z0 M19
M00(T1M6_#FDrill) <<<<<<<<<<< causes error,if lcase it's OK
(G54/55=H1,G56/57=H11)
M3 S2244
G54 G90 X-2.338 Y0.750 Z0.250 G43 H1 M8
G82 Z-0.125 F15.0 R0.010 P125
I see now the your statement is suposed to skip over "(" but it seems
to skip everything.With my change everything works so long as I have no
"F"s inbetween my statement brackets.I'll keep poking at it to see if I
can make it work,
SteveC
| |
| Rick Rothstein [MVP - Visual Basic] 2005-08-26, 6:55 pm |
| > > Thanks Rick, I played with it a bit and found I had to change this
>
> It didn't work otherwise.
>
> this is a bit of the file that causes the type mismatch error.
> G90 G0 G28 G40 G49 Z0 M19
> M00(T1M6_#FDrill) <<<<<<<<<<< causes error,if lcase it's OK
> (G54/55=H1,G56/57=H11)
> M3 S2244
> G54 G90 X-2.338 Y0.750 Z0.250 G43 H1 M8
> G82 Z-0.125 F15.0 R0.010 P125
> I see now the your statement is suposed to skip over "(" but it seems
> to skip everything.With my change everything works so long as I have
no
> "F"s inbetween my statement brackets.I'll keep poking at it to see if
I
> can make it work,
I retried my code using the data you included in your post and I do NOT
get a type mismatch error on that first line... I get NO error... there
is no "F" or "(" in the line and it is skipped (as it should be).
However, the last line with the "F15.0" in it doesn't get processed.
That is because (being unfamiliar with a CNC data) I assumed every line
contained a comment in parentheses; but your last line doesn't. Below is
some changed code that corrects for this problem. Since I can't
duplicate your first problem, I'm not sure how to account for it. Are
all of the data lines you showed (6 of them) exactly as you have them in
your file? Please try my new code below on your file data again, just to
be sure.
Rick
Dim TotalFile As String
' Here I've used a CommandButton to kick off the
' routine, move the code to whatever event it needs
' to be executed from.
Private Sub Command1_Click()
Dim X As Long
Dim ActualValue As Double
Dim FNumber() As String
Dim FSplit() As String
Dim Lines() As String
TotalFile = RichTextBox1.Text
Lines = Split(TotalFile, vbNewLine)
For X = 0 To UBound(Lines)
If InStr(Left$(Lines(X), InStr(Lines(X) & "(", "(")), "F") Then
FSplit = Split(Lines(X), "F")
FNumber = Split(FSplit(1))
ActualValue = CDbl(FNumber(0))
'
' Perform your actual modifcation
' to the "F" value here. Below, I'm
' increasing it by 10%.
'
ActualValue = 1.1 * ActualValue
FNumber(0) = CStr(ActualValue)
FSplit(1) = Join(FNumber, " ")
Lines(X) = Join(FSplit, "F")
End If
Next
TotalFile = Join(Lines, vbNewLine)
RichTextBox1.Text = TotalFile
End Sub
| |
| SteveC 2005-08-26, 6:55 pm |
| thanks again Rick,I don't know why your code works for you but not for
me,I copied it directly to a new VB project with only 2 command buttons
and a Richtextbox.Any way this new bit seems to be working,now I will
have to figure out how to stop it at an "F" to give me a choice of
changing the value or skipping it.
FYI on cnc programme files everything enclosed between brackets in
ignored by the machine controller and depending the post processor,part
of a cam programme that generates the cnc file, or the individual
programmer comments can be either on a separate line or at the begining
or end of a line.I use comments for setup info,usually at a tool
change.T? M6.
SteveC
Rick Rothstein [MVP - Visual Basic] wrote:
> no
> I
>
> I retried my code using the data you included in your post and I do NOT
> get a type mismatch error on that first line... I get NO error... there
> is no "F" or "(" in the line and it is skipped (as it should be).
> However, the last line with the "F15.0" in it doesn't get processed.
> That is because (being unfamiliar with a CNC data) I assumed every line
> contained a comment in parentheses; but your last line doesn't. Below is
> some changed code that corrects for this problem. Since I can't
> duplicate your first problem, I'm not sure how to account for it. Are
> all of the data lines you showed (6 of them) exactly as you have them in
> your file? Please try my new code below on your file data again, just to
> be sure.
>
> Rick
>
> Dim TotalFile As String
>
> ' Here I've used a CommandButton to kick off the
> ' routine, move the code to whatever event it needs
> ' to be executed from.
> Private Sub Command1_Click()
> Dim X As Long
> Dim ActualValue As Double
> Dim FNumber() As String
> Dim FSplit() As String
> Dim Lines() As String
> TotalFile = RichTextBox1.Text
> Lines = Split(TotalFile, vbNewLine)
> For X = 0 To UBound(Lines)
> If InStr(Left$(Lines(X), InStr(Lines(X) & "(", "(")), "F") Then
> FSplit = Split(Lines(X), "F")
> FNumber = Split(FSplit(1))
>
> ActualValue = CDbl(FNumber(0))
> '
> ' Perform your actual modifcation
> ' to the "F" value here. Below, I'm
> ' increasing it by 10%.
> '
> ActualValue = 1.1 * ActualValue
>
> FNumber(0) = CStr(ActualValue)
> FSplit(1) = Join(FNumber, " ")
> Lines(X) = Join(FSplit, "F")
> End If
> Next
> TotalFile = Join(Lines, vbNewLine)
> RichTextBox1.Text = TotalFile
> End Sub
| |
| Rick Rothstein [MVP - Visual Basic] 2005-08-26, 6:55 pm |
| > thanks again Rick,I don't know why your code works for you but not for
> me,I copied it directly to a new VB project with only 2 command
buttons
> and a Richtextbox.
I have no idea either.
> Any way this new bit seems to be working,
Good!
> now I will have to figure out how to stop it at an
> "F" to give me a choice of changing the value or skipping it.
Below is some modified code to show one approach to this. Note, however,
that the actual changes are delayed until all "F" values have been
queried. If you want an interactive question and text update upon
approval, you will have to modify how the loop functions.
Dim TotalFile As String
' Here I've used a CommandButton to kick off the
' routine, move the code to whatever event it needs
' to be executed from.
Private Sub Command1_Click()
Dim X As Long
Dim Answer As Long
Dim ActualValue As Double
Dim FNumber() As String
Dim FSplit() As String
Dim Lines() As String
TotalFile = RichTextBox1.Text
Lines = Split(TotalFile, vbNewLine)
For X = 0 To UBound(Lines)
If InStr(Left$(Lines(X), InStr(Lines(X) & "(", "(")), "F") Then
FSplit = Split(Lines(X), "F")
FNumber = Split(FSplit(1))
Answer = MsgBox("F-Value on Line #" & CStr(X + 1) & _
" equals " & FNumber(0) & vbNewLine & vbNewLine & _
"Do you want to change it?", vbYesNo)
If Answer = vbYes Then
ActualValue = CDbl(FNumber(0))
'
' Perform your actual modifcation
' to the "F" value here. Below, I'm
' increasing it by 10%.
'
ActualValue = 1.1 * ActualValue
FNumber(0) = CStr(ActualValue)
FSplit(1) = Join(FNumber, " ")
Lines(X) = Join(FSplit, "F")
End If
End If
Next
TotalFile = Join(Lines, vbNewLine)
RichTextBox1.Text = TotalFile
End Sub
> FYI on cnc programme files everything enclosed between brackets in
> ignored by the machine controller and depending the post
processor,part
> of a cam programme that generates the cnc file, or the individual
> programmer comments can be either on a separate line or at the
begining
> or end of a line.I use comments for setup info,usually at a tool
> change.T? M6.
Are you saying that comments (text surrounded by brackets, what I call
parentheses) can come first on a line and the values, including "F"
values can follow it? If so, the code I posted won't process that type
of line correctly.
Rick
| |
| SteveC 2005-08-27, 3:55 am |
|
Rick Rothstein [MVP - Visual Basic] wrote:
Yes ,sometimes coments are at the begining of the line,not often but
sometimes
SteveC
> Are you saying that comments (text surrounded by brackets, what I call
> parentheses) can come first on a line and the values, including "F"
> values can follow it? If so, the code I posted won't process that type
> of line correctly.
>
>
> Rick
| |
| Rick Rothstein [MVP - Visual Basic] 2005-08-27, 3:55 am |
| > > Are you saying that comments (text surrounded by brackets, what I
call
type[color=darkred]
>
> Yes ,sometimes coments are at the begining of the line,not often but
> sometimes
In that case, try the code below (I think it will solve your other
problem too).
Rick
Dim TotalFile As String
' Here I've used a CommandButton to kick off the
' routine, move the code to whatever event it needs
' to be executed from.
Private Sub Command1_Click()
Dim X As Long
Dim LeftParen As Long
Dim Rightparen As Long
Dim Answer As Long
Dim ActualValue As Double
Dim Comment As String
Dim FNumber() As String
Dim FSplit() As String
Dim Lines() As String
TotalFile = RichTextBox1.Text
Lines = Split(TotalFile, vbNewLine)
For X = 0 To UBound(Lines)
LeftParen = InStr(Lines(X), "(")
If LeftParen > 0 Then
Rightparen = InStr(Lines(X), ")")
Comment = Mid$(Lines(X), LeftParen, Rightparen - LeftParen + 1)
Lines(X) = Replace$(Lines(X), Comment, Chr$(1))
End If
If InStr(Lines(X), "F") Then
FSplit = Split(Lines(X), "F")
FNumber = Split(FSplit(1))
Answer = MsgBox("F-Value on Line #" & CStr(X + 1) & _
" equals " & FNumber(0) & vbNewLine & vbNewLine & _
"Do you want to change it?", vbYesNo)
If Answer = vbYes Then
ActualValue = CDbl(FNumber(0))
'
' Perform your actual modifcation
' to the "F" value here. Below, I'm
' increasing it by 10%.
'
ActualValue = 1.1 * ActualValue
FNumber(0) = CStr(ActualValue)
FSplit(1) = Join(FNumber, " ")
Lines(X) = Join(FSplit, "F")
End If
End If
If LeftParen > 0 Then
Lines(X) = Replace$(Lines(X), Chr$(1), Comment)
End If
Next
TotalFile = Join(Lines, vbNewLine)
RichTextBox1.Text = TotalFile
End Sub
| |
| SteveC 2005-08-29, 6:55 pm |
| Thanks again Rick, It's just about what I need, I added an inputbox so
as to make individual adjustments,and added a cancel to the msgbox,and
I going to try to select text on the line with the F, to make it easier
to see.
SteveC
Rick Rothstein [MVP - Visual Basic] wrote:
> buttons
>
> I have no idea either.
>
>
>
> Good!
>
>
>
> Below is some modified code to show one approach to this. Note, however,
> that the actual changes are delayed until all "F" values have been
> queried. If you want an interactive question and text update upon
> approval, you will have to modify how the loop functions.
>
> Dim TotalFile As String
>
> ' Here I've used a CommandButton to kick off the
> ' routine, move the code to whatever event it needs
> ' to be executed from.
> Private Sub Command1_Click()
> Dim X As Long
> Dim Answer As Long
> Dim ActualValue As Double
> Dim FNumber() As String
> Dim FSplit() As String
> Dim Lines() As String
> TotalFile = RichTextBox1.Text
> Lines = Split(TotalFile, vbNewLine)
> For X = 0 To UBound(Lines)
> If InStr(Left$(Lines(X), InStr(Lines(X) & "(", "(")), "F") Then
> FSplit = Split(Lines(X), "F")
> FNumber = Split(FSplit(1))
> Answer = MsgBox("F-Value on Line #" & CStr(X + 1) & _
> " equals " & FNumber(0) & vbNewLine & vbNewLine & _
> "Do you want to change it?", vbYesNo)
> If Answer = vbYes Then
> ActualValue = CDbl(FNumber(0))
> '
> ' Perform your actual modifcation
> ' to the "F" value here. Below, I'm
> ' increasing it by 10%.
> '
> ActualValue = 1.1 * ActualValue
> FNumber(0) = CStr(ActualValue)
> FSplit(1) = Join(FNumber, " ")
> Lines(X) = Join(FSplit, "F")
> End If
> End If
> Next
> TotalFile = Join(Lines, vbNewLine)
> RichTextBox1.Text = TotalFile
> End Sub
>
>
> processor,part
> begining
>
> Are you saying that comments (text surrounded by brackets, what I call
> parentheses) can come first on a line and the values, including "F"
> values can follow it? If so, the code I posted won't process that type
> of line correctly.
>
>
> Rick
| |
| Rick Rothstein [MVP - Visual Basic] 2005-08-29, 6:55 pm |
| > Thanks again Rick, It's just about what I need, I added an inputbox so
> as to make individual adjustments,and added a cancel to the msgbox,and
> I going to try to select text on the line with the F, to make it
easier
> to see.
While you will still need to change the code to suit your requirements,
did you see my last posted code? It will allow the comment to appear
anywhere inside the line, even the beginning. Ultimately, that may be
more suitable for your needs.
Rick
|
|
|
|
|