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 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
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