Thursday, January 11, 2007

Exactly the way you want it

There are times where Vr gives you all the tools you need to do something, but there may be just a little twist that still makes it worthwhile to write some quick code. In this case all that was necessary was to digitize some locations and have those written to a text file. There are several ways I can think of to do the exact same thing using tools that exist, but in this case there was no reason (or desire) to actually store points in the file, and there was a specific very simple output format desired (which of course could be easily modified for other purposes. This will also give us a reason to interact with a separate external file.

fprintf = lambda afile,fmt,*args: afile.write(fmt%args)

Gui=PyVrGui()

TextFileName='dig'
PointsFound=0
Stat=0

TextFile=open(TextFileName+'.txt','w')
while Stat==0:
..Stat,x,y,z=Gui.GetCoord('Dig_Txt')
..fprintf(TextFile,'%12.2lf%12.2lf%10.2lf\n',x,y,z)
..PointsFound=PointsFound+1
print '\n\a',PointsFound,' Points found and written to file ',TextFileName,'.txt\n'
TextFile.close()


Line by line.

fprintf = lambda afile,fmt,*args: afile.write(fmt%args)
This is mentioned elsewhere as one of my favorite tricks, it exactly reproduces the functionality of the C fprintf statement which formats text for output. Python has some very easy tricks for formatting, this just give me a familiar interface.

Gui=PyVrGui()
Always remember to create the Vr objects you are going to interact with, whether line, symbol, dtm, or in this case gui.

TextFileName='dig'
Points are going to be stored in a file, the name isn't important but let's put it in the block of code that is at the top and easy to find if someone needs to modify it.

PointsFound=0
Stat=0
These need to start at zero so we'll go ahead and initialize them as well.

TextFile=open(TextFileName+'.txt','w')
In this example we'll use the string addition method to add an extension which will end up creating a file named "dig.txt". This is just included to illustrate how if you have a base file name you can open several files with different extensions by just adding a different one. TextFile will become an instance of an open file object in "w" or write mode, there are several modes which mirror their c counterparts. Interacting with files is one of the more helpful actions to pick up. Here we will just be dumping formatted text to an ascii file, but there are times when it will be necessary to read and write from both binary and ascii files, all of which is controlled by the "mode".

while Stat==0:
GetCoord is going to return a status flag which is determined by if the user actually digitized a coordinate or hit End. This loop will just keep it running as long as they keep reading points, and to answer the most obvious question a status of 0 means yes a point was digitized; no I don't know why 0 is yes and 1 is no.

..Stat,x,y,z=Gui.GetCoord('Dig_Txt')
Digitize a point, as a side note the text string there is a title that is said to "show up in the VrOne message area". I have never actually seen it show up anywhere, but something is required to be there and I just enter relevant text in case it ever does.

..fprintf(TextFile,'%12.2lf%12.2lf%12.2lf\n',x,y,z)
The fprintf is discussed elsewhere but suffice it to say the text file we set up is going to get a piece of text formatted to 12 columns, 2 decimal places for each coordinate.

..PointsFound=PointsFound+1
print '\n\a',PointsFound,' Points found and written to file ',TextFileName,'.txt\n'
No real point, I just like to keep track of things like this and print the status when done.

TextFile.close()
Python is supposed to clean up after itself and close files when it exits, but as a rule it is a good habit of always doing the housekeeping explicitly.


Want to just see the least common denominator for a couple of the things mentioned.
To digitize a point, open pyedi and copy in
Gui=PyVrGui()
print Gui.GetCoord('')

Want to write something to a file, do the same (or open a python editor) with the following
file=open('/tmp/tmp.txt','w')#change the directory to something you have
file.write('a line of text')
file.close()

As an addendum, I want to plug a python forum that I mentioned earlier. I can even see it as a place that VrPython users could interact in a conversational way. I enjoy the forum so much that I tend to monitor it on a semi-regular basis and am likely to find a Vr related post fairly quick. Information is power, and sharing it has benefit for everyone. Check it out at The Scripts Developers Network or otherwise known as thescripts.com see you there!

No comments:

For anyone interested in trying VrPython for the first time or if you are early in the game, I suggest going to the earliest posts and working forward. I use VrPython every day for many wonderful things, needless to say it will change and could potentially damage a file. Any risk associated with using VrPython or any code or scripts mentioned here lies solely with the end user.

The "Personal VrPython page" in the link section will contain many code examples and an organized table of contents to this blog in a fairly un-attractive (for now) form.