The benefit of using a scripting language like Python is that it encapsulates huge amounts of work into convenient Modules, Classes, and Functions. This is the case with Python's Tcl/Tk/Tkinter modules.

Quickly, as an example, these modules can be used to present your user with a familiar Window's dialog that enables them to browse and select a folder, file, or a list of files with just a few lines of code. Anyone that has programmed a full blown Window's application can appreciate this...even if you haven't, trust me...asking a user for a file name should not be rocket science. If you can spare a few bucks...please thank the team at the Python Software Foundation for their years of effort...for US Tax payers and some other countries it is tax deductible...check the site if you are unsure.

This post has two purposes. One, through the KB Article, mention the Tcl/Tk modules, the bugs, and what it takes to get them to work from within our applications, and two, announce the new Folder and File Selection Dialog we are adding to our Geomagic Python Scripting Environment (GPSE - pronounced"gypsy") in the upcoming Service Relase. This enhancement eliminates the known bugs and requires nothing outside of a standard Geomagic Qualify, Studio, Wrap, or Qualify Probe 2012.1 installation.

Stay tuned for the Service Release!

Here's the nugget...

Q: How do I use Tkinter while running a script inside the application?

A: When using Tcl/Tk (Tkinter) within our scripting environment, Python 2.7 (with Tcl/Tk modules) must be installed for Tcl/Tk to work within Geomagic Qualify, Qualify Probe, Studio, and Wrap 2012.

What is Tkinter and how do I use it?

Note: Python 2.7 and the installed Geomagic application must be the same bit-flavor. Geomagic Qualify/Studio/Wrap 2012 and Python must either both be 32 or 64 bit. They cannot be mixed.The only issue we found getting Tkinter up and running was the need to provide the sys.argv as seen in the example below.

Note: This example is in HTML format and will have issues if Copied and Pasted directly into your source script. We suggest to pass it through Notepad or the like prior to pasting it into your Scripting Tab editor window.

import sys, Tkinter, tkFileDialog
import geoappall
for m in geoappall.execStrings: exec m in locals(), globals()

# You need this for Tkinter to work within the app
# Outside our app Windows provides the argv
# This is why we must also import sys

sys.argv = ['']

## create a root Tk widget (so we can destroy it later)
root = Tkinter.Tk()

## get the filename with askopenfilename and parent it to root
getFilename = tkFileDialog.askopenfilename(parent=root,title='Choose a file')

## open the returned filename
if getFilename != None:
print"File:", getFilename
geoapp.openFile(unicode(getFilename))
activeModel = geoapp.getActiveModel()

## kill the root widget
root.destroy()

Note: Also, there is one"bug" within the Tcl/Tk library that we know of...it involves using the askopenfilename dialog function when returning multiple names. This function supports multiple file picking when the command parameter 'multiple' is set to True.

This example below provides a work around for this Tcl bug (the highlighted script shows the function call and fix).

Note: This example is in HTML format and will have issues if Copied and Pasted directly into your source script. We suggest to pass it through Notepad or the like prior to pasting it into your Scripting Tab editor window.

'''
Geomagic Example Script - MiniBatch for Point File(s)

MiniBatch-Points_Files__User_Selects_Filenames__Hardcoded_RecordedMacro__SaveWrp.py

Instructions:

Record or author the tasks and insert below at the comment:
"insert your point macros here"

Run this script

The operator is prompted to select the Point filename(s)

This script will process the files and save them as .wrp files in the same
folder with"_processed" appended to the file name.

'''
import os, sys, string, Tkinter, tkFileDialog
import geoappall
for m in geoappall.execStrings: exec m in locals(), globals()

## you need this for Tkinter to work within the app
sys.argv = ['']

## create a root Tk widget (so we can destroy it later)
root = Tkinter.Tk()
root.withdraw()

# get the filenames with tkFileDialog.askopenfilename
getFiles = tkFileDialog.askopenfilename( parent = None, title = 'Choose a Point, Ordered Points, or Polygon files...', multiple = True )

# known python 2.x issue: Tcl list returned from tkFileDialog
# using splitlist to correct (i.e.,create a list)
fileList = root.tk.splitlist( getFiles )


# destroy the root widget
root.destroy()

# process the returned filenames
for f in fileList:

# break up the filename, extension, and path into usable chunks

fClean = os.path.normpath( f )
fPathSplit = os.path.split( fClean )
fPath = fPathSplit[0]
fFile = fPathSplit[1]
fFileSplit = os.path.splitext( fFile )
fName = fFileSplit[0]
fExt = fFileSplit[1]

# open the file if it exists
if os.path.exists( fClean ) :

# do not 'FileRead' .wrp files
if string.upper( fExt ) !=".WRP":

print"Opening:", fClean

# create FileRead object
filereader = FileRead()

# set path
filereader.filename = unicode( fClean )

# read the file
filereader.run()

# get point object
pts = filereader.points

# get grid object
grid = filereader.gridModel

# get mesh object
mesh = filereader.mesh


if pts != None:

print ("Adding points to the model manager" )
geoapp.addModel( pts, unicode( fName +"- Points" ) )

print ("Processing points in" + str( fName ) )

##
## insert your points macros here
##

else:

print ("No points found in" + str( fClean ) +" !!!" )

#
# Save the results in WRP format
#

# insert the processed suffix in the new WRP file name
fSave = fPath + fName + '_processed.wrp'
print"saving file:", fSave

if ( not geoapp.saveAs( unicode( fSave ) ) ):
print"Could not save:", fSave

else:
print ("Use MiniBatch-WrpFilesOnly for:" + str( fClean ) +" !!!" )

else:
print ( unicode( fClean ) +" does not exist!" )

What is Tkinter and how do I use it?