Dear PythonOCC users,

The Viewer3d.ExportToImage method creates a BMP file of the displayed CAD 
image. It worked well while using WinXP 32bit. However, with Win7 64bit, the 
method produces a blank image. Can you please help to resolve the 
ExportToImage problem?

I am using Python 2.6 32bit and pythonOCC 0.5 All-In-One with Win7 OS 64bit 
with programs in the directory C:\Program Files (x86).

Thank you for your help!

Below is the CADViewer.py code modified to use the ExportToImage method:

import sys, os
import wx
import time

import OCC
from OCC.Display.wxDisplay import wxViewer3d

class AppFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "CAD Viewer - pythonOCC%s"%
OCC.VERSION,
                          style=wx.DEFAULT_FRAME_STYLE, pos=(1900,0),
                          size = (640,480))
        self.canva = wxViewer3d(self)
        # Creating Menu
        menuBar = wx.MenuBar()
        menu1 = wx.Menu()
        menu1.Append(101, "&Open", "Open a CAD file")
        menu1.Append(103, "&Save Image", "Save CAD image")
        menu1.Append(102, "&Exit", "Exit application")
        self.Bind(wx.EVT_MENU, self.OnOpen, id=101)
        self.Bind(wx.EVT_MENU, self.OnExit, id=102)
        self.Bind(wx.EVT_MENU, self.OnSaveImage, id=103)
        menuBar.Append(menu1, "&File")
        self.SetMenuBar(menuBar)
       
    def OnOpen(self,event):
        # Choose file dialog
        dlg = wx.FileDialog(
            self, message="Choose a STEP/IGES/STL/BRep file",
            defaultDir=os.getcwd(), 
            defaultFile="",
            wildcard="STEP file (*.stp)|*.stp|STL files (*.stl)|*.stl|IGES 
files (*.iges, *.igs)|*.iges|All files (*.*)|*.*|" ,
            style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR
            )
        if dlg.ShowModal() == wx.ID_OK:
            paths = dlg.GetPaths()
            self.LoadFile(paths[0])
            
    def LoadFile(self,filename):
        extension = os.path.basename(filename).split(".").pop().lower()
        start_time = time.time()
        if extension =="step" or extension == "stp":
            from OCC import STEPControl
            stepReader = STEPControl.STEPControl_Reader()
            stepReader.ReadFile(str(filename))
            numTranslated = stepReader.TransferRoots()
            shape = stepReader.OneShape()
        elif extension == "stl":
            from OCC import TopoDS, StlAPI
            shape = TopoDS.TopoDS_Shape()
            stl_reader = StlAPI.StlAPI_Reader()
            stl_reader.Read(shape,str(filename))
        elif extension =="iges" or extension =="igs":
            from OCC import IGESControl
            i  = IGESControl.IGESControl_Controller()
            i.Init()
            iges_reader = IGESControl.IGESControl_Reader()
            iges_reader.ReadFile(str(filename))
            iges_reader.TransferRoots()
            shape = iges_reader.OneShape()
        elif extension == "brep":
            from OCC import TopoDS, BRep, BRepTools
            shape = TopoDS.TopoDS_Shape()
            builder = BRep.BRep_Builder()
            BRepTools.BRepTools().Read(shape,str(filename),builder)
        else:
            return True
        self.canva._display.DisplayShape(shape)
        end_time = time.time()
        self.SetTitle("CAD Viewer - pythonOCC %s:%s"%(OCC.VERSION,filename))
        duration = end_time-start_time
        print "%s STEP file loaded and displayed in %f seconds."%
(filename,duration)

    def OnSaveImage(self,event):
        self.canva._display.Repaint()
        sp = wx.StandardPaths.Get()
        fn = os.path.join(sp.GetTempDir(), "CADImage.bmp")
        self.canva._display.ExportToImage(str(fn))
        print "saving image to file: %s"%str(fn)
        self.canva._display.Repaint()
        
    def OnExit(self,event):
        sys.exit(0)

if __name__=="__main__":
    app = wx.PySimpleApp()
    wx.InitAllImageHandlers()
    frame = AppFrame(None)
    frame.Show(True)
    wx.SafeYield() #under Linux, frame must be shown before Display3D is 
initialized
    frame.canva.InitDriver()
    app.SetTopWindow(frame)
    app.MainLoop()  



_______________________________________________
Pythonocc-users mailing list
Pythonocc-users@gna.org
https://mail.gna.org/listinfo/pythonocc-users

Reply via email to