Example #1
0
def export_shapes2(shapeList, filename, colorList=[]):
    h_doc = TDocStd.Handle_TDocStd_Document()
    app = XCAFApp.XCAFApp_Application_GetApplication().GetObject()
    app.NewDocument(TCollection.TCollection_ExtendedString("MDTV-CAF"),h_doc)
    doc = h_doc.GetObject()
    h_shape_tool = XCAFDoc.XCAFDoc_DocumentTool().ShapeTool(doc.Main())
    h_color_tool = XCAFDoc.XCAFDoc_DocumentTool().ColorTool(doc.Main())
    shape_tool = h_shape_tool.GetObject()
    color_tool = h_color_tool.GetObject()
    top_label = shape_tool.NewShape()
    
    colorList.extend(["brown"]*(len(shapeList) - len(colorList)))
    cmap = get_color_map()
    
    tr = gp.gp_Trsf()
    loc = TopLoc.TopLoc_Location(tr)
    print(colorList)
    print(shapeList)
    colorMap = dict((c, Quantity.Quantity_Color(cmap.get(c, 133))) for c in colorList)
    
    for shape, color in zip(shapeList, colorList):
        if not shape:
            continue
        print("color:", color, "shape", shape)
        label = shape_tool.AddShape(shape, False, True)
        ref_label = shape_tool.AddComponent(top_label, label, loc)
        c = colorMap[color]
        color_tool.SetColor(ref_label, c, XCAFDoc.XCAFDoc_ColorGen)
        
    mode = STEPControl.STEPControl_AsIs
    writer = STEPCAFControl.STEPCAFControl_Writer()
    writer.Transfer(h_doc, mode)
    writer.Write(str(filename))
Example #2
0
    def write_file(self):
        r"""Write file"""
        work_session = XSControl.XSControl_WorkSession()
        writer = STEPCAFControl.STEPCAFControl_Writer(work_session.GetHandle(),
                                                      False)

        transfer_status = writer.Transfer(self.h_doc,
                                          STEPControl.STEPControl_AsIs)
        if transfer_status != IFSelect.IFSelect_RetDone:
            msg = "An error occurred while transferring a shape to the STEP writer"
            logger.error(msg)
            raise ValueError(msg)
        logger.info('Writing STEP file')

        write_status = writer.Write(self.filename)
        if write_status == IFSelect.IFSelect_RetDone:
            logger.info("STEP file write successful.")
        else:
            msg = "An error occurred while writing the STEP file"
            logger.error(msg)
            raise ValueError(msg)