noob question: loop not working - eof error when reading a txt file

noob question: loop not working - eof error when reading a txt file

Anonymous
Not applicable
865 Views
10 Replies
Message 1 of 11

noob question: loop not working - eof error when reading a txt file

Anonymous
Not applicable
I am trying to write a script that will read in two component codes from each line of a text file and edit the corresponding components.

The code works for the first line of the text file and edits the components correctly(whithout the loop). However when i tried to get it to loop through the rest of the lines in the file it fails and i get end of file errors.

I cant seem to find a loop that works properly
here is the code i am working on:


global fileUnderTest = "c:\\test1.txt"

--fn overlapHighlight dirString=
--(
max select all
$.xray = on
--fileUnderTest = dirString
fs = openFile fileUnderTest

seek fs #eof -- couldnt get a while loop to work so i tried this instead
maxlen=filepos fs
seek fs 0
count = 1

while count != maxlen do (
count = filepos fs

skipToString fs "Component "
obj1 = readChars fs 6
skipToString fs "Component "
obj2 = readChars fs 6
myObj1 = getNodeByName obj1
myObj1.xray = False
myObj2 = getNodeByName obj2
myObj2.xray = False
selectionSets = #(myObj1, myObj2)
)

close fs
--)





This is a sample from the text file i am trying to read


!! Component ab1001 OVERLAPS Component ab1002 !!
!! Component ab1003 OVERLAPS Component ab1004 !!
!! Component ab1001 OVERLAPS Component ab1001 !!
!! Component ab1001 OVERLAPS Component ab1004 !!
!! Component ab1001 OVERLAPS Component ab1002 !!
!! Component ab1003 OVERLAPS Component ab1004 !!
!! Component ab1001 OVERLAPS Component ab1001 !!
!! Component ab1007 OVERLAPS Component ab1007 !!
0 Likes
866 Views
10 Replies
Replies (10)
Message 2 of 11

Steve_Curley
Mentor
Mentor

(
fn overlapHighlight inFileName =
(
local theFile, myObjects = #()

geometry.xray = True
theFile = openFile infileName

while not eof theFile do
(
skipToString thefile "Component "
myObjects = getNodeByName (readDelimitedString thefile " ")
skipToString thefile "Component "
myObjects = getNodeByName (readDelimitedString thefile " ")

myObjects.xray = False
selectionSets.name + "--" + myObjects.name] = myObjects

skipToNextLine theFile
)

close theFile
)

overLapHighlight "c:\\test1.txt"
)


Looks better with proper indentation (forum problem...) - actual script is attached.

overlappingobjects.ms.zip


Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 3 of 11

Anonymous
Not applicable
Hi mate,
Thanks for that,
But for some reason the myObjects is not getting populated
it contains (undefined, undefined) which causes the .xray command to fail etc
any clue why?


i am using max 2009 attached is the text file and a test max file (this might help?)
0 Likes
Message 4 of 11

Steve_Curley
Mentor
Mentor
Attachments (other than images) must be zipped first.

I did, obviously, test it here before I posted it. If the myObjects are not being populated then either
a) there are no objects in the scene with the same names as are in the file, or
b) the format of the file is different to your example a few posts up and you didn't alter the code to suit, or
c) you did change the code to suit a different file format. The spaces after the "Components " are necessary, not accidental, for example.

Try attaching the scene and text file again - zip them both together - because it will be a lot easier to diagnose the problem in situ.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 5 of 11

Anonymous
Not applicable
Sorry about that,
Didin realise they had to be zipped up

It is strange, its the same problem i was having before, if i remark out the while loop the code works fine for the first line of the txt file, however with the loop it falls over and the array will not populate

your help is much appreciated!

desktop.zip

0 Likes
Message 6 of 11

Steve_Curley
Mentor
Mentor
The object names in the max scene have spaces in them - "tk 1001" - but in the file they do not - "tk1001". In this instance it will be easier to change the names in the scene rather than the file, because reading the split names will be more difficult. Or you could put underscores in both, but no spaces.

Also, the last line of the text file does not have a CRLF (0x0D 0x0A) - this will cause problems reading that last line.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 7 of 11

Anonymous
Not applicable
One of the objects in the txt file was not in the scene tk0007

however
It still appears to be a read past end of file error caused by the "skipToNextLine theFile" line
if you remove this you get the following

it populates the array - however for some reason the last two values in the array are undefined- this appears to be where it is now falling over


$Box:tk1001 @
$Box:tk1002 @
$Box:tk1003 @
$Box:tk1004 @
$Box:tk1001 @
$Box:tk1001 @
$Box:tk1001 @
$Box:tk1004 @
$Box:tk1001 @
$Box:tk1002 @
$Box:tk1003 @
$Box:tk1004 @
$Box:tk1001 @
$Box:tk1001 @
$Box:tk1007 @
$Box:tk1007 @
undefined
undefined

Arrrrg!
0 Likes
Message 8 of 11

Anonymous
Not applicable
Hi i just saw your reply

The Object names in the max file are identical to the ones in the txt file, there are no spaces??

they are tk1001 tk1002 etc

There should be a carrage return at the end of the file, sorry that was my error.
It doesnt seem to make any difference though... can you get it to work?
0 Likes
Message 9 of 11

Steve_Curley
Mentor
Mentor
Ok, forget the spaces - old eyes playing tricks on me :(

Right. Load the scene, change "tk1008" to "tk1007".
Open the text file with notepad, move the cursor to the end of the last line, press Enter and then save the file. Then it all works as expected.
The file error is being caused by the missing CRLF.

If you need to do this job repeatedly, then it would pay to put some error trapping in to handle the various possible errors.

The file wouldn't open
The object(s) don't exist in the scene
The end of file was reached unexpectedly
etc.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 10 of 11

Anonymous
Not applicable
yup,
Hats off to you,

I had a break came back to the pc and have been playing around with the different .txt encoding methods to try and get it to work

you were right It was the missing return at the end of the file. I was sure i had tried that before.

The annoying thing is that the file will be generated with a carrage return on it from a unix script.

Thanks for all your help man, this part was only supposed to take me 10 mins its part of a much bigger CAD project. LOL
0 Likes
Message 11 of 11

Steve_Curley
Mentor
Mentor
No problem - glad we got it sorted :)

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes