def load_model(cls, fn, group=None): """ Load a model. :param str fn: The filename. The extension should be either ".xbf" for a binary file or ".xml" for an XML file. :param afem.structure.group.Group group: The group to load the parts into. If *None* then the active group is used. :return: *True* if loaded, *False* otherwise. :rtype: bool :raise TypeError: If the file extension type is not supported. """ from afem.structure.create import CreatePartByName if fn.endswith('.xbf'): binary = True elif fn.endswith('.xml'): binary = False else: raise TypeError('Document type not supported.') group = cls.get_group(group) # Open document doc = XdeDocument(binary) doc.open(fn) # Get the main name and iterate on top-level children which # should be parts name = doc.shapes_label part_data = [] cref_to_part = {} sref_to_part = {} for current in name.children_iter: name = current.name shape = current.shape type_ = current.string color = current.color if None in [name, shape, type_]: continue # Check for reference geometry if type_ == 'CREF': cref_to_part[name] = shape.curve continue if type_ == 'SREF': sref_to_part[name] = shape.surface continue # Add part data part_data.append((type_, name, shape, color)) # Create parts # TODO Support group hierarchy for type_, name, shape, color in part_data: cref, sref = None, None if name in cref_to_part: cref = cref_to_part[name] if name in sref_to_part: sref = sref_to_part[name] part = CreatePartByName(type_, name=name, shape=shape, group=group, cref=cref, sref=sref).part if color is not None: r, g, b = color.Red(), color.Green(), color.Blue() part.set_color(r, g, b) return True
def load_bodies(fn): """ Load saved Body instances. :param str fn: The filename. The extension should be either ".xbf" for a binary file or ".xml" for an XML file. :return: A dictionary where the key is the name and the value is the Body instance. :rtype: dict(str, afem.oml.entities.Body) :raise TypeError: If the file extension type is not supported. """ from afem.exchange.xde import XdeDocument if fn.endswith('.xbf'): binary = True elif fn.endswith('.xml'): binary = False else: raise TypeError('Document type not supported.') # Open document doc = XdeDocument(binary) doc.open(fn) # Get the main label and iterate on top-level children which # should be parts label = doc.shapes_label body_data = [] sref_to_body = {} for current in label.children_iter: name = current.name shape = current.shape type_ = current.string color = current.color if None in [name, shape, type_]: continue # Check for reference geometry if type_ == 'SREF' and shape.is_face: sref_to_body[name] = shape.surface continue # Add part data body_data.append((name, shape, color)) # Create bodies label_to_bodies = {} for label, shape, color in body_data: sref = None if label in sref_to_body: sref = sref_to_body[label] body = Body(shape, label) if sref is not None: body.set_sref(sref) if color is not None: r, g, b = color.Red(), color.Green(), color.Blue() body.set_color(r, g, b) label_to_bodies[label] = body return label_to_bodies