Parts DataGrid

Description

This demonstrates the DataGrid UI component where all of the parts in the current model are displayed with their node and element counts.

Python Script Files

  • parts_data_grid_ui.py
  • parts_data_grid.py

Significant Script Features

parts_data_grid_ui.py:

# Define Layout and Components
def getCustomToolPropertyContent():

   # Create a Grid
   my_Grid = WPFControls.Grid()

   # Add 1 Row and One Column
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())
   my_Grid.ColumnDefinitions.Add(WPFControls.ColumnDefinition())
  
   # Create a DataGrid
   global myDataGrid
   myDataGrid = WPFControls.DataGrid()      
   
   # Add the DataGrid to the Grid 
   my_Grid.Children.Add(myDataGrid)

   # Return the Grid   
   return my_Grid

#This function receives data from the Script API function and updates the Tool UI 
@apex_sdk.errorhandler
def UpdateDataGridView():

   # Retrieve grid results
   grid = apex_sdk.getScriptFunctionReturnData()

   # Create parts table
   data_table = WPFData.DataTable("Part Contents")
   data_table.Columns.Add("Part")
   data_table.Columns.Add("Nodes")
   data_table.Columns.Add("Elements")
   
   # Add data to the table for each part
   parts = grid['parts']
   for part in parts:      
      data_table.Rows.Add(part['part_name'], part['nodes_count'], part['elements_count'])
   
   # Set the IetmsSource for the data grid to the DataTable 
   myDataGrid.ItemsSource = data_table.DefaultView 
   
   # Position the DataGrid in the first row, column of the Grid
   WPFControls.Grid.SetRow(myDataGrid, 0)
   WPFControls.Grid.SetColumn(myDataGrid, 0)

parts_data_grid.py:

def prepare_data_grid(grid={}):

    import apex

    # Disable showing output dialog at the end
    apex.disableShowOutput()

    # Display nodes and elements counts
    model = apex.currentModel()

    parts = [] 
    grid['parts'] = parts
    for part in model.parts:   
        partNode = {}
        parts.append(partNode)    
        partNode['part_name'] = part.name
        partNode['nodes_count'] = getTotalNodesCountFromPart(part)
        partNode['elements_count'] = getTotalElementsCountFromPart(part)
    
    return grid

def getTotalNodesCountFromPart(part):
    nodes = 0
    for entity in part.surfaces:
        nodes += entity.nodes.len()
    return nodes

def getTotalElementsCountFromPart(part):
    elements = 0
    for entity in part.surfaces:
        elements += entity.elements.len()
    return elements