2013-09-25

Create FME Parameter Dialog Box with Python Script

I posted a way to create a message dialog box in the Community site.
> Message or Alert Box
That was only to show a message string, I think it's a special usage of FME parameter dialog box. Here I put an example for general usage. FME GUI Directives and file operations are the points.
-----
import fmeobjects
import os, tempfile, winsound

# FME GUI directives
kGuiDirectives = """\
GUI TITLE Context Parameters
GUI GROUP 1%PARAM1%PARAM2%PARAM3 Context Parameters
GUI TEXT PARAM1 Text Parameter:
GUI INTEGER PARAM2 Integer Parameter:
GUI FLOAT PARAM3 Float Parameter:
"""

class FeatureProcessor(object):
    def __init__(self):
        # Dictionary to save parameter values.
        self.params = {}
     
        # Flag indicates whether parameters are entered or not.
        self.complete = False
     
        # If it's not necessary to enter parameters in a certain condition,
        # test the condition and finish this function when it's true.
        # if <condition>:
        #     self.complete = True
        #     return
     
        filepath = None
        try:
            # Create a temporary file defining FME GUI directives.
            fd, filepath = tempfile.mkstemp(dir = '.')
            os.write(fd, kGuiDirectives)
            os.close(fd)
         
            # *** Windows only ***
            # Play a Windows sound before showing the dialog box.
            winsound.PlaySound('SystemAsterisk', winsound.SND_ASYNC)
         
            # Create and show a parameter settings dialog box.
            # If [OK] button is clicked to close the dialog box,
            # save entered parameter values.
            # FME Objects Python API description:
            # "If the user exits the form with OK, then the file will be overwritten
            # with a new file that alternates MACRO NAME and VALUE, line by line."
            dlg = fmeobjects.FMEDialog()
            if dlg.parameterPrompt(filepath):
                f = open(filepath)
                rows = [r.strip() for r in f.readlines()]
                self.params = dict(zip(rows[0::2], rows[1::2]))
                f.close()
                self.complete = True
        except:
            logger = fmeobjects.FMELogFile()
            logger.logMessageString('Parameters getting failure.',
                fmeobjects.FME_ERROR)
        finally:
            # Remove the temporary file.
            if filepath and os.path.exists(filepath):
                os.remove(filepath)
         
    def input(self, feature):
        # If parameters have not been entered, abort the translation.
        if not self.complete:
            raise fmeobjects.FMEException('Parameters have not been entered.')
         
        # Use the parameter values.
        # Set them to each input feature, for example.
        for name in self.params.keys():
            feature.setAttribute(name, self.params[name])
        self.pyoutput(feature)
     
    def close(self):
        pass
-----
=====
2014-11-29: An application.
Community Answers > select different feature type each time workspace runs
=====

Related links:
> FME GUI Directives * currently disabled. 2013-10-28
> FME GUI DIRECTIVES
> 35.4. winsound — Sound-playing interface for Windows

No comments:

Post a Comment