Examples#

Example 01 - “Pre Process SETGET” process#

XML file defining the process “Pre Process SETGET” and its individual tasks#
  1<root>
  2    <process label="Pre Process SETGET"
  3         tag="preprocess"
  4         title="TM Example 1"
  5         saveonclose="False"
  6         helpdocument="tmexample1HELP.pdf"
  7         eeposition="undocked"
  8         processloadprecommand="task_tmexample1_setget.preload(%S, %P)"
  9         processloadpostcommand="task_tmexample1_setget.postload(%S, %P)"
 10         sessionsavepostcommand="task_tmexample1_setget.postsave(%S, %P)"
 11         sessionsaveprecommand="task_tmexample1_setget.presave(%S, %P)">
 12        <task label="Config"
 13            tag="config"
 14            command="task_tmexample1_setget.applyConfig()"
 15            applybuttonstate="Enabled">
 16            <category tag="cat_process" label="Process">
 17                <property label="Working directory"
 18                            tag="workingdir"
 19                            displaytype="choosedir"
 20                            getcommand="task_tmexample1_setget.getWorkDirectory()"
 21                            setcommand="task_tmexample1_setget.setWorkDirectory(%V)" />
 22                <property label="Description"
 23                            tag="description"
 24                            displaytype="string"
 25                            help="Max number of characters: 25"
 26                            validatecommand="task_tmexample1_setget.validateDescription(%V)"
 27                            setcommand="task_tmexample1_setget.setValue(description, %V)"
 28                            getcommand="task_tmexample1_setget.getValue(description)" />
 29            </category>
 30            <category tag="config_hm" label="HM file">
 31                <property label="Select file"
 32                        tag="btn_selectfiles"
 33                        help="custom image"
 34                        displaytype="actionbutton"
 35                        image="toolbarFileOpenStrip-16.png"
 36                        setcommand="task_tmexample1_setget.selectHMFiles()"/>
 37                <property label="HM file"
 38                        tag="config_hmfile"
 39                        enabled="True"
 40                        displaytype="fileopen"
 41                        getvaluelistcommand="task_tmexample1_setget.getHMFileTypes()"
 42                        setcommand="task_tmexample1_setget.setValue(hmfile, %V)"
 43                        getcommand="task_tmexample1_setget.getValue(hmfile)"
 44                        help="Select an hm file"/>
 45            </category>
 46            <category tag="cat_config_trans" label="Translate">
 47                <property label="Direction"
 48                    tag="config_transdir"
 49                    displaytype="combobox"
 50                    value="x"
 51                    getcommand="task_tmexample1_setget.getValue(transdir)"
 52                    getvaluelistcommand="task_tmexample1_setget.getValueList(transdir)"
 53                    setcommand="task_tmexample1_setget.setValue(transdir, %V)"/>
 54                <property label="Distance (integer)"
 55                    tag="config_transdist"
 56                    displaytype="integer"
 57                    getcommand="task_tmexample1_setget.getValue(transdist)"
 58                    setcommand="task_tmexample1_setget.setValue(transdist, %V)"/>
 59            </category>
 60            <category tag="config_cat_mesh" label="Mesh">
 61                <property label="Mesh size"
 62                        tag="config_meshsize"
 63                        displaytype="real"
 64                        value="0.6"
 65                        validatecommand="task_tmexample1_setget.validateMeshSize(%V)"
 66                        setcommand="task_tmexample1_setget.setValue(meshsize, %V)"
 67                        getcommand="task_tmexample1_setget.getValue(meshsize)" />
 68            </category>
 69            <category tag="config_export" label="Export">
 70                <property label="File path"
 71                        tag="config_optistructfile"
 72                        displaytype="filesave"
 73                        getvaluelistcommand="task_tmexample1_setget.getOptistructFileTypes()"
 74                        setcommand="task_tmexample1_setget.setValue(optistructfile, %V)"
 75                        getcommand="task_tmexample1_setget.getValue(optistructfile)" />
 76            </category>
 77        </task>
 78        <category tag="cat_tasks" label="Task" expand="True">
 79            <task label="Open HM file"
 80                tag="import_hm"
 81                command="task_tmexample1_setget.importHM()"
 82                help="select hm file and open"
 83                precommand="task_tmexample1_setget.preEnter()"
 84                postcommand="task_tmexample1_setget.postRunTask(%T)"
 85                >
 86                <category tag="task_cat_hm" label="HM file">
 87                    <property label="Select file"
 88                            tag="btn_hm_selectfiles"
 89                            displaytype="actionbutton"
 90                            setcommand="task_tmexample1_setget.selectHMFiles()"/>
 91                    <property label="HM file"
 92                            tag="task_hmfile"
 93                            enabled="True"
 94                            displaytype="fileopen"
 95                            getvaluelistcommand="task_tmexample1_setget.getHMFileTypes()"
 96                            setcommand="task_tmexample1_setget.setValue(hmfile_1, %V)"
 97                            getcommand="task_tmexample1_setget.getValue(hmfile_1)"
 98                            help="Select an HM file"/>
 99                </category>
100            </task>
101            <task label="Renumber Components"
102                tag="renumber"
103                help="Renumber all component from 1"
104                enabled="True"
105                command="task_tmexample1_setget.renumberComps()"
106                postcommand="task_tmexample1_setget.postRunTask(%T)"/>
107            <task label="Translate"
108                tag="translate"
109                enabled="True"
110                command="task_tmexample1_setget.translate()"
111                postcommand="task_tmexample1_setget.postRunTask(%T)">
112                <property label="Direction"
113                        tag="transdir"
114                        displaytype="combobox"
115                        value="x"
116                        getcommand="task_tmexample1_setget.getValue(transdir)"
117                        getvaluelistcommand="task_tmexample1_setget.getValueList(transdir)"
118                        setcommand="task_tmexample1_setget.setValue(transdir, %V)"/>
119                <property label="Distance (integer)"
120                        tag="transdist"
121                        displaytype="integer"
122                        getcommand="task_tmexample1_setget.getValue(transdist)"
123                        setcommand="task_tmexample1_setget.setValue(transdist, %V)"/>
124            </task>
125            <task label="Create mesh"
126                tag="mesh"
127                command="task_tmexample1_setget.mesh()"
128                help="Mesh selected surface or component"
129                enabled="True"
130                postcommand="task_tmexample1_setget.postRunTask(%T)">
131                <category tag="cat_mesh" label="Mesh">
132                    <property label="NumSelectedSurfs"
133                            tag="numselcompmesh"
134                            displaytype="info"
135                            getcommand="task_tmexample1_setget.getValue(numselcompmesh)" />
136                    <property label="Select surfaces"
137                            tag="selcompmesh"
138                            displaytype="hm_multiselector"
139                            getcommand="task_tmexample1_setget.getValue(selcompmesh)"
140                            getvaluelistcommand="task_tmexample1_setget.getValueList(selcompmesh)"
141                            setcommand="task_tmexample1_setget.setSurfsOrCompsToMesh(%V)"/>
142                    <property label="Mesh size"
143                            tag="task_meshsize"
144                            displaytype="real"
145                            getcommand="task_tmexample1_setget.getValue(meshsize)"
146                            setcommand="task_tmexample1_setget.setValue(meshsize, %V)"/>
147                </category>
148            </task>
149            <task label="Export"
150                tag="export"
151                command="task_tmexample1_setget.export()"
152                enabled="True"
153                postcommand="task_tmexample1_setget.postRunTask(%T)"
154                >
155                <property label="File path"
156                        tag="task_optistruct"
157                        displaytype="filesave"
158                        getvaluelistcommand="task_tmexample1_setget.getOptistructFileTypes()"
159                        setcommand="task_tmexample1_setget.setValue(optistructfile, %V)"
160                        getcommand="task_tmexample1_setget.getValue(optistructfile)"/>
161            </task>
162        </category>
163    </process>
164</root>
Python code saved as task_tmexample1_setget.py providing the business logic for the “Pre Process SETGET” process#
  1# task_tmexample1
  2"""
  3This file is imported when loading the process.
  4It loads all the functions used by the task manager process in the xml file.
  5"""
  6
  7import os
  8import sys
  9import hwinterp
 10from hwx import gui
 11import hm
 12import hm.entities as ent
 13import hw.taskmanager as task_manager
 14
 15# Import the rest of the modules for this test case
 16sourcepath = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", ".."))
 17if sourcepath not in sys.path:
 18    sys.path.append(sourcepath)
 19import Task_Manager_Example1.tools.actions as actions
 20
 21# Since I donw't know all I need in python yet some commands might run in tcl
 22tcl = hwinterp.hwInterp_Get("tcl")
 23
 24tm = task_manager.getTaskManager()
 25# PROJECT PROPERTIES
 26# These may be set to another number when loading the process, if the process xml file
 27# both has a setcommand and a value
 28workingDir = os.getcwd()
 29propertyValues = {
 30    # meshsize should be set to 0.6 on load xml file from xml file
 31    "meshsize": 0.9,
 32    "hmfile": "",
 33    "optistructfile": "",
 34    "selcompmesh": "Surface",
 35    "transdist": 50,
 36    "transdir": "y",
 37    "description": "Project description",
 38    "numselcompmesh": 0,
 39}
 40propertyValueLists = {"selcompmesh": "Surface, Component", "transdir": "x, y, z"}
 41
 42
 43def printcustom(*args):
 44    """
 45    Prints arguments in Python Interface (e.g. different tasks or task property values)
 46    """
 47    print("From tmexample1_setget:", *args)
 48
 49
 50def preload(s, p):
 51    """
 52    Print banner (xml path) in Preload phase of process item (see processloadprecommand
 53    attribute in pre_process_setget.xml)
 54    """
 55    printcustom("RUN preload ***")
 56    printcustom(f"{s=}")
 57    printcustom(f"{p=}")
 58    return 1
 59
 60
 61def postload(s, p):
 62    """
 63    Print banner (xml path) in Postload phase of process item (see processloadpostcommand
 64    attribute in pre_process_setget.xml)
 65    """
 66    printcustom("RUN postload ***")
 67    printcustom(f"{s=}")
 68    printcustom(f"{p=}")
 69    if not os.path.isfile(s):
 70        workdir = os.getcwd()
 71    else:
 72        workdir = os.path.dirname(s)
 73    setWorkDirectory(workdir)
 74    return 1
 75
 76
 77def presave(s, p):
 78    """
 79    Print banner (xml path) in Presave phase of process item where the Process is saved in
 80    an xml file (see sessionsaveprecommand attribute in pre_process_setget.xml)
 81    """
 82    printcustom("RUN PRESAVE")
 83    printcustom(f"{s=}")
 84    printcustom(f"{s} is file:", os.path.isfile(s))
 85    printcustom(f"{p=}")
 86    printcustom("***")
 87
 88
 89def postsave(s, p):
 90    """
 91    Print banner (xml path) in Presave phase of process item where the Process is saved in
 92    an xml file (see sessionsavepostcommand attribute in pre_process_setget.xml)
 93    """
 94    printcustom("RUN POSTSAVE")
 95    printcustom(f"{s=}")
 96    printcustom(f"{s} is file:", os.path.isfile(s))
 97    printcustom(f"{p=}")
 98    printcustom("***")
 99
100
101def validateDescription(value):
102    """
103    Function that is activated via validatecommand and checks the length of string in
104    Description property of property with tag = "description"
105    """
106    if len(value) > 25:
107        return False
108    return True
109
110
111def validateMeshSize(value):
112    """
113    Function that is activated via validatecommand and checks the sign of mesh size
114    in Mesh size property of property with tag = "config_mesh"
115    """
116    if float(value) < 0:
117        return False
118    return True
119
120
121def getValue(tag):
122    """
123    Function that prints the value that has passed from the task manager GUI in code
124    using the getcommand attribute
125    """
126    printcustom("getValue", tag, propertyValues.get(tag, ""))
127    return propertyValues.get(tag, "")
128
129
130def getValueList(tag):
131
132    return propertyValueLists.get(tag, "")
133
134
135def setValue(tag, value):
136    """
137    Function that prints the value that we want to pass from the task manager GUI
138    in code using the setcommand attribute
139    """
140    printcustom("setValue", tag, value)
141    propertyValues[tag] = value
142
143
144def getWorkDirectory():
145    return workingDir
146
147
148def setWorkDirectory(workdir):
149    global workingDir
150    workingDir = workdir
151    return 1
152
153
154def preEnter():
155    """
156    Function that is activated via precommand attribute in some task items
157    """
158    printcustom("RUNNING PRECOMMAND")
159
160
161def postRunTask(tag):
162    """
163    Function that is activated via postcommand attribute in some task items.
164    It prints the tag for the current and next task after task's command is executed
165    """
166    printcustom("RUNNING POSTCOMMAND")
167    printcustom(tag)
168    tagList = ("import_hm", "renumber", "translate", "mesh", "export")
169    index = tagList.index(tag) + 1
170    if index >= len(tagList):
171        return
172    nextTag = tagList[index]
173    printcustom(nextTag)
174    task = tm.getTask(nextTag)
175    task.enabled = True
176    return 1
177
178
179def getOptistructFileTypes():
180    return "OptiStruct (*.fem *.param);;All Files (*)"
181
182
183def getHMFileTypes():
184    return "HyperMesh files (*.hm);;All Files (*)"
185
186
187def getXMLFileTypes():
188    return "xml files (*.xml);;All Files (*)"
189
190
191def applyConfig():
192    # don't do anything
193    return 1
194
195
196def setSurfsOrCompsToMesh(surforcompCol):
197    """
198    Command that is executed via setcommand attribute. We set in hm_multiselctor
199    whether we want to select Surfaces or Components.
200    """
201    surfCol = surforcompCol
202    if surforcompCol.eclass is ent.Component:
203        s = hm.Session()
204        m = hm.Model(s.get_all_models()[0])
205        rule = hm.FilterByCollection(ent.Surface, ent.Component, [""])
206        surfCol = hm.Collection(m, rule, surforcompCol)
207    setValue("numselcompmesh", len(surforcompCol))
208    # updating business logic
209    tm.getTask().setPropertyValue("numselcompmesh", len(surfCol))
210    # set value to the GUI
211    actions.setSurfColToMesh(surfCol)
212    return 1
213
214
215def selectHMFiles():
216    """
217    Command that is executed via setcommand attribute. It is been executed in
218    two tasks and with this we select the path of hm file.
219    """
220
221    hmFile = gui.getOpenFileName(caption="Select hmetry files", filter=getHMFileTypes())
222    if not hmFile:
223        return 0
224    configTask = tm.getTask("config")
225    importTask = tm.getTask("import_hm")
226    configTask.setPropertyValue("config_hmfile", hmFile)
227    configTask.setPropertyEnabled("config_hmfile", True)
228    importTask.setPropertyValue("task_hmfile", hmFile)
229    importTask.setPropertyEnabled("task_hmfile", True)
230    setValue("hmfile_1", hmFile)
231
232    return 1
233
234
235def importHM():
236    """
237    Command that is executed via command attribute. The command is used in task
238    with tag="import_hm". This command imports an hm file by selecting its path
239    interactively via a GUI tool.
240    """
241    task = tm.getTask("import_hm")
242    hmFile = task.getPropertyValue("task_hmfile")
243    printcustom(hmFile)
244    ret = actions.importOneHmFile(hmFile)
245
246    printcustom("Import", f"{ret=}")
247    return ret
248
249
250def translate():
251    """
252    Command that is executed via command attribute. The command is used in task
253    with tag="translate". In this command we set in the GUI the values for direction
254    and distance that will be taken by action and will be updated in task manager
255    GUI via setcommand and getcommand attributes of task's properties.
256    """
257    direction = tm.getTask().getPropertyValue("transdir")
258    dist = tm.getTask().getPropertyValue("transdist")
259    actions.translateAll(direction, float(dist))
260    return 1
261
262
263def renumberComps():
264    """
265    Command that is executed via command attribute. The command is used in task
266    with tag="renumber". This command renumbers the ID of component UP to 77
267    and the ID of component DOWN to 88.
268    """
269    actions.renumberComps()
270    return 1
271
272
273def mesh():
274    """
275    Command that is executed via command attribute. The command is used in task
276    with tag="mesh". It creates the mesh in the surfaces by reading first in the
277    GUI the value of property with tag="task_meshsize".
278    """
279    task = tm.getTask()
280    meshSize = task.getPropertyValue("task_meshsize")
281    retval = actions.mesh(meshSize)
282    printcustom("mesh retval", retval)
283    return retval
284
285
286def export():
287    """
288    Command that is executed via command attribute. The command is used in task
289    with tag="export". It exports the mesh properties from previous tasks
290    (element and node coordinates) in a OptiStruct format. The file path is set
291    interactively in task property with tag="task_optistruct" and proper
292    setcommand, getcommand attributes.
293    """
294    task = tm.getTask()
295    filepath = task.getPropertyValue("task_optistruct")
296    if not os.path.isdir(os.path.dirname(filepath)):
297        gui.warnUser("Please select valid file")
298        return 0
299
300    return actions.exportfile(filepath)

../../_images/example011.png

Figure 1. “Pre Process SETGET” process loaded in Task Manager