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