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 = {
 41    'selcompmesh': 'Surface, Component',
 42    'transdir': 'x, y, z'
 43    }
 44
 45def printcustom(*args):
 46    '''
 47    Prints arguments in Python Interface (e.g. different tasks or task property values)
 48    '''
 49    print('From tmexample1_setget:', *args)
 50
 51
 52def preload(s, p):
 53    '''
 54    Print banner (xml path) in Preload phase of process item (see processloadprecommand attribute in pre_process_setget.xml)
 55    '''
 56    printcustom("RUN preload ***")
 57    printcustom(f'{s=}')
 58    printcustom(f'{p=}')
 59    return 1
 60def postload(s, p):
 61    '''
 62    Print banner (xml path) in Postload phase of process item (see processloadpostcommand attribute in pre_process_setget.xml)
 63    '''
 64    printcustom("RUN postload ***")
 65    printcustom(f'{s=}')
 66    printcustom(f'{p=}')
 67    if not os.path.isfile(s):
 68        workdir = os.getcwd()
 69    else:
 70        workdir = os.path.dirname(s)
 71    setWorkDirectory(workdir)
 72    return 1
 73
 74def presave(s, p):
 75    '''
 76    Print banner (xml path) in Presave phase of process item where the Process is saved in a xml file (see sessionsaveprecommand attribute in pre_process_setget.xml)
 77    '''
 78    printcustom('RUN PRESAVE')
 79    printcustom(f'{s=}')
 80    printcustom(f'{s} is file:', os.path.isfile(s))
 81    printcustom(f'{p=}')
 82    printcustom('***')
 83
 84def postsave(s, p):
 85    '''
 86    Print banner (xml path) in Presave phase of process item where the Process is saved in a xml file (see sessionsavepostcommand attribute in pre_process_setget.xml)
 87    '''
 88    printcustom('RUN POSTSAVE')
 89    printcustom(f'{s=}')
 90    printcustom(f'{s} is file:', os.path.isfile(s))
 91    printcustom(f'{p=}')
 92    printcustom('***')
 93
 94def validateDescription(value):
 95    '''
 96    Function that is activated via validatecommand and checks the length of string in Description property of property with tag = "description"
 97    '''
 98    if len(value)>25:
 99        return False
100    return True
101
102def validateMeshSize(value):
103    '''
104    Function that is activated via validatecommand and checks the sign of mesh size in Mesh size property of property with tag = "config_mesh"
105    '''
106    if float(value)<0:
107        return False
108    return True
109
110def getValue(tag):
111    '''
112    Function that prints the value that has passed from the task manager GUI in code using the getcommand attribute
113    '''
114    printcustom('getValue', tag, propertyValues.get(tag,'') )
115    return propertyValues.get(tag,'')
116
117def getValueList(tag):
118
119    return propertyValueLists.get(tag,'')
120
121def setValue(tag, value):
122    '''
123    Function that prints the value that we want to pass from the task manager GUI in code using the setcommand attribute
124    '''
125    printcustom('setValue', tag, value)
126    propertyValues[tag] = value
127
128def getWorkDirectory():
129    return workingDir
130def setWorkDirectory(workdir):
131    global workingDir
132    workingDir = workdir
133    return 1
134
135def preEnter():
136    '''
137    Function that is activated via precommand attribute in some task items
138    '''
139    printcustom('RUNNING PRECOMMAND')
140
141def postRunTask(tag):
142    '''
143    Function that is activated via postcommand attribute in some task items. It prints the tag for the current and next task after task's command is executed
144    '''
145    printcustom('RUNNING POSTCOMMAND')
146    printcustom(tag)
147    tagList = ('import_hm','renumber', 'translate', 'mesh', 'export')
148    index = tagList.index(tag) + 1
149    if index >= len(tagList):return
150    nextTag = tagList[index]
151    printcustom(nextTag)
152    task = tm.getTask(nextTag)
153    task.enabled = True
154    return 1
155
156def getOptistructFileTypes():
157    return "OptiStruct (*.fem *.param);;All Files (*)"
158def getHMFileTypes():
159    return "HyperMesh files (*.hm);;All Files (*)"
160def getXMLFileTypes():
161    return "xml files (*.xml);;All Files (*)"
162
163def applyConfig():
164    #don't do anything
165    return 1
166
167def setSurfsOrCompsToMesh(surforcompCol):
168    '''
169    Command that is executed via setcommand attribute. We set in hm_multiselctor whether we want to select Surfaces or Components.
170    '''
171    surfCol = surforcompCol
172    if surforcompCol.eclass is ent.Component:
173        s = hm.Session()
174        m = hm.Model(s.get_all_models()[0])
175        rule = hm.FilterByCollection(ent.Surface,ent.Component,[''])
176        surfCol = hm.Collection(m,rule,surforcompCol)
177    setValue('numselcompmesh', len(surforcompCol));#updating business logic
178    tm.getTask().setPropertyValue('numselcompmesh', len(surfCol));# set value to the GUI
179    actions.setSurfColToMesh(surfCol)
180    return 1
181
182def selectHMFiles():
183    '''
184    Command that is executed via setcommand attribute. It is been executed in two tasks and with this we select the path of hm file.
185    '''
186
187    hmFile = gui.getOpenFileName(caption="Select hmetry files", filter=getHMFileTypes())
188    if not hmFile:return 0
189    configTask = tm.getTask('config')
190    importTask = tm.getTask('import_hm')
191    configTask.setPropertyValue('config_hmfile', hmFile)
192    configTask.setPropertyEnabled('config_hmfile', True)
193    importTask.setPropertyValue('task_hmfile', hmFile)
194    importTask.setPropertyEnabled('task_hmfile', True)
195    setValue('hmfile_1', hmFile)
196
197    return 1
198
199
200def importHM():
201    '''
202    Command that is executed via command attribute. The command is used in task with tag="import_hm".
203    This command imports an hm file by selecting its path interactively via a GUI tool.
204    '''
205    task = tm.getTask('import_hm')
206    hmFile = task.getPropertyValue('task_hmfile')
207    printcustom(hmFile)
208    ret = actions.importOneHmFile(hmFile)
209
210    printcustom('Import', f'{ret=}')
211    return ret
212
213
214def translate():
215    '''
216    Command that is executed via command attribute. The command is used in task with tag="translate"
217    In this command we set in the GUI the values for direction and distance that will be taken by action and will be updated in task manager GUI via setcommand and getcommand attributes of task's properties.
218    '''
219    direction = tm.getTask().getPropertyValue('transdir')
220    dist = tm.getTask().getPropertyValue('transdist')
221    actions.translateAll(direction, float(dist))
222    return 1
223
224
225def renumberComps():
226    '''
227    Command that is executed via command attribute. The command is used in task with tag="renumber"
228    This command renumbers the ID of component UP to 77 and the ID of component DOWN to 88.
229    '''
230    actions.renumberComps()
231    return 1
232
233
234def mesh():
235    '''
236    Command that is executed via command attribute. The command is used in task with tag="mesh"
237    It creates the mesh in the surfaces by reading first in the GUI the value of property with tag="task_meshsize".
238    '''
239    task = tm.getTask()
240    meshSize = task.getPropertyValue('task_meshsize')
241    retval= actions.mesh(meshSize)
242    printcustom('mesh retval', retval)
243    return retval
244
245def export():
246    '''
247    Command that is executed via command attribute. The command is used in task with tag="export"
248    It exports the mesh properties from previous tasks (element and node coordinates) in a OptiStruct format. The file path is set interactively in task property with tag="task_optistruct" and proper setcommand, getcommand attributes.
249    '''
250    task = tm.getTask();
251    filepath = task.getPropertyValue('task_optistruct')
252    if not os.path.isdir(os.path.dirname(filepath)):
253        gui.warnUser('Please select valid file')
254        return 0
255
256    return actions.exportfile(filepath)

../../_images/example011.png

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