def test_write_step_file(self):
        ''' Exports a colored box into a STEP file '''
        ### initialisation
        h_doc = Handle_TDocStd_Document()
        assert (h_doc.IsNull())
        # Create the application
        app = XCAFApp_Application.GetApplication().GetObject()
        app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc)
        # Get root assembly
        doc = h_doc.GetObject()
        h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
        l_Colors = XCAFDoc_DocumentTool_ColorTool(doc.Main())
        shape_tool = h_shape_tool.GetObject()
        colors = l_Colors.GetObject()
        ### create the shape to export
        test_shape = BRepPrimAPI_MakeBox(100., 100., 100.).Shape()

        ### add shape
        shp_label = shape_tool.AddShape(test_shape)
        ### set a color for this shape
        r = 1
        g = b = 0.5
        red_color = Quantity_Color(r, g, b, 0)
        colors.SetColor(shp_label, red_color, XCAFDoc_ColorGen)
        # write file
        WS = XSControl_WorkSession()
        writer = STEPCAFControl_Writer(WS.GetHandle(), False)
        writer.Transfer(h_doc, STEPControl_AsIs)
        status = writer.Write("./test_io/test_ocaf_generated.stp")
        assert status
        assert os.path.isfile("./test_io/test_ocaf_generated.stp")
Example #2
0
def export_STEPFile_single(shape, filename, tol=1.0E-6):
    """
    Exports a .stp file containing the input shapes

    Parameters
    ----------
    shape : TopoDS_Shape
    
    filename : string
        The output filename
    """

    step = STEPCAFControl_Writer()
    step.SetNameMode(True)
    step.SetPropsMode(True)
    h_doc = Handle_TDocStd_Document()
    x_app = XCAFApp_Application.GetApplication().GetObject()
    x_app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc)
    doc = h_doc.GetObject()
    h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
    shape_tool = h_shape_tool.GetObject()
    Interface_Static_SetCVal("write.step.schema", "AP214")

    # transfer shapes
    print(filename)
    shape_tool.AddShape(shape)
    step.Transfer(h_doc, STEPControl_AsIs)
    status = step.Write(filename)
    assert (status == IFSelect_RetDone)
Example #3
0
def read_step_file_shapes(filename):
    _shapes = []

    # create an handle to a document
    h_doc = Handle_TDocStd_Document()

    # Create the application
    app = XCAFApp_Application.GetApplication().GetObject()
    app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc)

    # Get root assembly
    doc = h_doc.GetObject()
    h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())

    step_reader = STEPCAFControl_Reader()
    step_reader.SetNameMode(True)

    status = step_reader.ReadFile(filename)
    if status == IFSelect_RetDone:
        step_reader.Transfer(doc.GetHandle())

    labels = TDF_LabelSequence()
    shape_tool = h_shape_tool.GetObject()
    h_shape_tool.GetObject().GetFreeShapes(labels)

    print("Number of shapes at root :%i" % labels.Length())
    for i in range(labels.Length()):
        label = labels.Value(i + 1)
        a_shape = h_shape_tool.GetObject().GetShape(label)
        _shapes.append(a_shape)
    return _shapes
Example #4
0
 def __init__(self, name="name", tol=1.0E-10):
     self.name = name
     self.step = STEPCAFControl_Writer()
     self.step.SetNameMode(True)
     self.h_doc = Handle_TDocStd_Document()
     self.x_app = XCAFApp_Application.GetApplication().GetObject()
     self.x_app.NewDocument(TCollection_ExtendedString("MDTV-CAF"),
                            self.h_doc)
     self.doc = self.h_doc.GetObject()
     self.h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(self.doc.Main())
     self.shape_tool = self.h_shape_tool.GetObject()
     Interface_Static_SetCVal("write.step.schema", "AP214")
Example #5
0
class ExportCAFMethod(object):
    def __init__(self, name="name", tol=1.0E-10):
        self.name = name
        self.step = STEPCAFControl_Writer()
        self.step.SetNameMode(True)
        self.h_doc = Handle_TDocStd_Document()
        self.x_app = XCAFApp_Application.GetApplication().GetObject()
        self.x_app.NewDocument(TCollection_ExtendedString("MDTV-CAF"),
                               self.h_doc)
        self.doc = self.h_doc.GetObject()
        self.h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(self.doc.Main())
        self.shape_tool = self.h_shape_tool.GetObject()
        Interface_Static_SetCVal("write.step.schema", "AP214")

    def Add(self, shape, name="name"):
        """
        STEPControl_AsIs                   translates an Open CASCADE shape to its highest possible STEP representation.
        STEPControl_ManifoldSolidBrep      translates an Open CASCADE shape to a STEP manifold_solid_brep or brep_with_voids entity.
        STEPControl_FacetedBrep            translates an Open CASCADE shape into a STEP faceted_brep entity.
        STEPControl_ShellBasedSurfaceModel translates an Open CASCADE shape into a STEP shell_based_surface_model entity.
        STEPControl_GeometricCurveSet      translates an Open CASCADE shape into a STEP geometric_curve_set entity.
        """
        label = self.shape_tool.AddShape(shape)
        self.step.Transfer(self.h_doc, STEPControl_AsIs)

    def Write(self, filename=None):
        if not filename:
            filename = self.name
        path, ext = os.path.splitext(filename)
        if not ext:
            ext = ".stp"
        status = self.step.Write(path + ext)
        assert (status == IFSelect_RetDone)
Example #6
0
def export_STEPFile_Airconics(AirconicsShapes, filename):
    """ Writes a Step file with names defined in the AirconicsShapes. This
    function is not fully tested and should not yet be used.

    Notes
    -----
    Work in progress
    """
    print("This function is a work in progress. For now, use export_STEPFile")
    # create an handle to a document
    h_doc = Handle_TDocStd_Document()

    # Create the application
    app = XCAFApp_Application.GetApplication().GetObject()
    app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc)

    # Get root assembly
    doc = h_doc.GetObject()
    shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main()).GetObject()
    #    l_colors = XCAFDoc_DocumentTool_ColorTool(doc.Main())
    #    l_layers = XCAFDoc_DocumentTool_LayerTool(doc.Main())
    #    l_materials = XCAFDoc_DocumentTool_MaterialTool(doc.Main())

    step_writer = STEPCAFControl_Writer()
    step_writer.SetColorMode(True)
    step_writer.SetLayerMode(True)
    step_writer.SetNameMode(True)
    #    step_writer.SetMaterialMode(True)

    for ACshape in AirconicsShapes:
        for comp in ACshape.Components:
            print("Writing {} to {}".format(comp, filename))
            lbl = shape_tool.AddShape(ACshape.Components[comp])
            name = TCollection_ExtendedString(comp)
            #            tdn = TDataStd_Name()
            #            tdn.Set(lbl, name)
            step_writer.Transfer(lbl, STEPControl_AsIs)

    status = step_writer.Write(filename)
    assert (status == IFSelect_RetDone)
    return status
    def test_read_step_file(self):
        ''' Reads the previous step file '''
        # create an handle to a document
        h_doc = Handle_TDocStd_Document()
        # Create the application
        app = XCAFApp_Application.GetApplication().GetObject()
        app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc)
        # Get root assembly
        doc = h_doc.GetObject()
        h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
        l_colors = XCAFDoc_DocumentTool_ColorTool(doc.Main())
        step_reader = STEPCAFControl_Reader()
        step_reader.SetColorMode(True)
        step_reader.SetLayerMode(True)
        step_reader.SetNameMode(True)
        step_reader.SetMatMode(True)
        status = step_reader.ReadFile("./test_io/test_ocaf.stp")
        if status == IFSelect_RetDone:
            step_reader.Transfer(doc.GetHandle())

        labels = TDF_LabelSequence()
        color_labels = TDF_LabelSequence()

        shape_tool = h_shape_tool.GetObject()
        h_shape_tool.GetObject().GetFreeShapes(labels)

        assert (labels.Length() == 1)
        sub_shapes_labels = TDF_LabelSequence()
        assert (not shape_tool.IsAssembly(labels.Value(1)))
        shape_tool.GetSubShapes(labels.Value(1), sub_shapes_labels)
        assert (sub_shapes_labels.Length() == 0)

        l_colors.GetObject().GetColors(color_labels)
        assert (color_labels.Length() == 1)

        label_shp = labels.Value(1)
        a_shape = h_shape_tool.GetObject().GetShape(label_shp)
        assert (not a_shape.IsNull())
from OCC.Display.SimpleGui import init_display

filename = './models/as1_pe_203.stp'
_shapes = []

# create an handle to a document
h_doc = Handle_TDocStd_Document()

# Create the application
app = XCAFApp_Application.GetApplication().GetObject()
app.NewDocument(TCollection_ExtendedString("MDTV-CAF"), h_doc)

# Get root assembly
doc = h_doc.GetObject()
h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
l_colors = XCAFDoc_DocumentTool_ColorTool(doc.Main())
l_layers = XCAFDoc_DocumentTool_LayerTool(doc.Main())
l_materials = XCAFDoc_DocumentTool_MaterialTool(doc.Main())

step_reader = STEPCAFControl_Reader()
step_reader.SetColorMode(True)
step_reader.SetLayerMode(True)
step_reader.SetNameMode(True)
step_reader.SetMatMode(True)

status = step_reader.ReadFile(filename)
if status == IFSelect_RetDone:
    step_reader.Transfer(doc.GetHandle())

labels = TDF_LabelSequence()
FR = 3 # radius
FD = 2 # depth
FS = 2 # fan's distance from radiator's bottom
FX = float(RL)/2+RX; FYleft = RY; FYright = RY+RW; FZ = RZ+FS+FR
p = gp_Ax2(gp_Pnt(FX,FYleft,FZ),-gp_DY())
leftFan = BRepPrimAPI_MakeCylinder(p,FR,FD).Shape()
p = gp_Ax2(gp_Pnt(FX,FYright,FZ),gp_DY())
rightFan = BRepPrimAPI_MakeCylinder(p,FR,FD).Shape()


# initialisation
h_doc = Handle_TDocStd_Document()
assert(h_doc.IsNull())
# create the application
app = XCAFApp_Application.GetApplication().GetObject()
app.NewDocument(TCollection_ExtendedString("MDTV-CAF"),h_doc)
# get root assembly
doc = h_doc.GetObject()
h_shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())
shape_tool = h_shape_tool.GetObject()
#add shape
shp_label = shape_tool.AddShape(tank)


WS = XSControl_WorkSession()
writer = STEPCAFControl_Writer(WS.GetHandle(),False)
writer.Transfer(h_doc,STEPControl_AsIs)
writer.SetNameMode(True)
writer.Write("CAFtestPyOCC.stp")