Exemple #1
0
    def save_bodies(fn, bodies, binary=True):
        """
        Save Body instances.

        :param str fn: The filename.
        :param collections.Sequence(afem.oml.entities.Body) bodies: The Body
            instances
        :param bool binary: If *True*, the document will be saved in a binary
            format. If *False*, the document will be saved in an XML format.

        :return: *True* if saved, *False* otherwise.
        :rtype: bool

        .. note::

            This method only saves the name, shape, reference surface, and
            color of the Body. Other user-defined metadata is currently not
            saved.
        """
        from afem.exchange.xde import XdeDocument
        # Create document
        doc = XdeDocument(binary)

        # Add the bodies
        for body in bodies:
            label = doc.add_shape(body.shape, body.name, False)
            label.set_string('Body')
            label.set_color(body.color)

            # Sref
            if body.sref is not None:
                face = FaceBySurface(body.sref).face
                label = doc.add_shape(face, body.name, False)
                label.set_string('SREF')

        return doc.save_as(fn)
Exemple #2
0
    def save_model(cls, fn, binary=True):
        """
        Save the model.

        :param str fn: The filename.
        :param bool binary: If *True*, the document will be saved in a binary
            format. If *False*, the document will be saved in an XML format.

        :return: *True* if saved, *False* otherwise.
        :rtype: bool
        """
        group = cls.get_master()

        # Create document and application
        doc = XdeDocument(binary)

        # Store parts as top-level shapes
        # TODO Support group hierarchy
        parts = group.get_parts()
        for part in parts:
            name = doc.add_shape(part.shape, part.name, False)
            name.set_string(part.type)
            name.set_color(part.color)

            # Reference curve
            if part.has_cref:
                edge = EdgeByCurve(part.cref).edge
                name = doc.add_shape(edge, part.name, False)
                name.set_string('CREF')

            # Reference surface
            if part.has_sref:
                face = FaceBySurface(part.sref).face
                name = doc.add_shape(face, part.name, False)
                name.set_string('SREF')

        return doc.save_as(fn)
Exemple #3
0
    def export_step(self,
                    fn,
                    label_solids=True,
                    label_faces=False,
                    names=None):
        """
        Export the OpenVSP model as a STEP file using Extended Data Exchange.
        Each OpenVSP component will be a named product in the STEP file
        structure.

        :param str fn: The filename.
        :param bool label_solids: Option to label the solid bodies in the
            STEP entity. The name will be the same as the OpenVSP component.
        :param bool label_faces: Option to label the faces in each of the
            solids. Each face of the solid body will be labeled "Face 1",
            "Face 2", etc.
        :param names: List of Body names that will be included in export. If
            *None* then all are exported.
        :type names: collections.Sequence(str) or None

        :return: None.
        """
        # Initialize the document
        doc = XdeDocument()

        # Get bodies to include
        if names is None:
            bodies = self.all_bodies
        else:
            bodies = [self.get_body(name) for name in names]

        # Gather OpenVSP bodies and names and build single compound
        solids = []
        names = []
        for body in bodies:
            solids.append(body.shape)
            names.append(body.name)
        cmp = Compound.by_shapes(solids)

        # Add main shape and top-level assembly
        main = doc.add_shape(cmp, 'Vehicle')

        # Each body should be a product so names are transferred to other
        # CAD systems
        for name, solid in zip(names, solids):
            doc.add_subshape(main, solid, name)

        # Transfer the document and then modify the STEP item name directly
        # rather than using a label. This only applies when labeling
        # sub-shapes.
        doc.transfer_step()
        if label_solids or label_faces:
            for name, solid in zip(names, solids):
                if label_solids:
                    doc.set_shape_name(solid, name)
                if label_faces:
                    i = 1
                    for f in solid.faces:
                        name = ' '.join(['Face', str(i)])
                        doc.set_shape_name(f, name)
                        i += 1

        doc.write_step(fn)
Exemple #4
0
    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
Exemple #5
0
    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