def get_next_unused_node_identifier(nodeset: Nodeset, start_identifier=1) -> int: """ :return: Unused node identifier >= start_identifier. """ identifier = start_identifier node = nodeset.findNodeByIdentifier(identifier) while node.isValid(): identifier += 1 node = nodeset.findNodeByIdentifier(identifier) return identifier
def get_node_name_centres(nodeset: Nodeset, coordinates_field: Field, name_field: Field): """ Find mean locations of node coordinate with the same names. :param nodeset: Zinc Nodeset or NodesetGroup to search. :param coordinates_field: The coordinate field to evaluate. :param name_field: The name field to match. :return: Dict of names -> coordinates. """ components_count = coordinates_field.getNumberOfComponents() fieldmodule = nodeset.getFieldmodule() fieldcache = fieldmodule.createFieldcache() name_records = {} # name -> (coordinates, count) nodeiter = nodeset.createNodeiterator() node = nodeiter.next() while node.isValid(): fieldcache.setNode(node) name = name_field.evaluateString(fieldcache) coordinates_result, coordinates = coordinates_field.evaluateReal( fieldcache, components_count) if name and (coordinates_result == RESULT_OK): name_record = name_records.get(name) if name_record: name_centre = name_record[0] for c in range(components_count): name_centre[c] += coordinates[c] name_record[1] += 1 else: name_records[name] = [coordinates, 1] node = nodeiter.next() # divide centre coordinates by count name_centres = {} for name in name_records: name_record = name_records[name] name_count = name_record[1] name_centre = name_record[0] if name_count > 1: scale = 1.0 / name_count for c in range(components_count): name_centre[c] *= scale name_centres[name] = name_centre return name_centres
def get_maximum_node_identifier(nodeset: Nodeset) -> int: """ :return: Maximum node identifier in nodeset or -1 if none. """ maximum_node_id = -1 node_iterator = nodeset.createNodeiterator() node = node_iterator.next() while node.isValid(): node_id = node.getIdentifier() if node_id > maximum_node_id: maximum_node_id = node_id node = node_iterator.next() return maximum_node_id
def find_node_with_name(nodeset: Nodeset, name_field: Field, name): """ Get single node in nodeset with supplied name. :param nodeset: Zinc Nodeset or NodesetGroup to search. :param name_field: The name field to match. :param name: The name to match in nameField. :return: Node with name, or None if 0 or multiple nodes with name. """ fieldmodule = nodeset.getFieldmodule() fieldcache = fieldmodule.createFieldcache() nodeiter = nodeset.createNodeiterator() node_with_name = None node = nodeiter.next() while node.isValid(): fieldcache.setNode(node) temp_name = name_field.evaluateString(fieldcache) if temp_name == name: if node_with_name: return None node_with_name = node node = nodeiter.next() return node_with_name
def find_node_with_name(nodeset: Nodeset, name_field: Field, name: str, ignore_case=False, strip_whitespace=False): """ Get single node in nodeset with supplied name. :param nodeset: Zinc Nodeset or NodesetGroup to search. :param name_field: The name field to match. :param name: The name to match in nameField. :param ignore_case: Set to True to ignore case differences. :param strip_whitespace: Set to True to ignore leading and trailing whitespace differences. :return: Zinc Node with name, or None if 0 or multiple nodes with name. """ match_name = name if strip_whitespace: match_name = match_name.strip() if ignore_case: match_name = match_name.casefold() fieldmodule = nodeset.getFieldmodule() fieldcache = fieldmodule.createFieldcache() nodeiter = nodeset.createNodeiterator() node_with_name = None node = nodeiter.next() while node.isValid(): fieldcache.setNode(node) temp_name = name_field.evaluateString(fieldcache) if strip_whitespace: temp_name = temp_name.strip() if ignore_case: temp_name = temp_name.casefold() if temp_name == match_name: if node_with_name: return None node_with_name = node node = nodeiter.next() return node_with_name
def evaluate_field_nodeset_mean(field: Field, nodeset: Nodeset): """ :return: Mean of field over nodeset. """ fieldmodule = nodeset.getFieldmodule() components_count = field.getNumberOfComponents() with ChangeManager(fieldmodule): mean_field = fieldmodule.createFieldNodesetMean(field, nodeset) fieldcache = fieldmodule.createFieldcache() result, mean_values = mean_field.evaluateReal(fieldcache, components_count) assert result == RESULT_OK del mean_field del fieldcache return mean_values
def getNodesetConditionalSize(nodeset: Nodeset, conditionalField: Field): """ :return: Number of objects in nodeset for which conditionalField is True. """ assert conditionalField.getNumberOfComponents() == 1 fieldmodule = conditionalField.getFieldmodule() fieldcache = fieldmodule.createFieldcache() nodeiterator = nodeset.createNodeiterator() size = 0 node = nodeiterator.next() while node.isValid(): fieldcache.setNode(node) result, value = conditionalField.evaluateReal(fieldcache, 1) if value != 0.0: size += 1 node = nodeiterator.next() return size
def evaluate_field_nodeset_range(field: Field, nodeset: Nodeset): """ :return: min, max range of field over nodes. """ fieldmodule = nodeset.getFieldmodule() components_count = field.getNumberOfComponents() with ChangeManager(fieldmodule): min_field = fieldmodule.createFieldNodesetMinimum(field, nodeset) max_field = fieldmodule.createFieldNodesetMaximum(field, nodeset) fieldcache = fieldmodule.createFieldcache() result, min_values = min_field.evaluateReal(fieldcache, components_count) assert result == RESULT_OK result, max_values = max_field.evaluateReal(fieldcache, components_count) assert result == RESULT_OK del min_field del max_field del fieldcache return min_values, max_values