Hi all,

I understood the origin of the issue with my previous 3D sample: 
wxPython window must be shown before the V3d_Viewer is inited. I 
attached the script wxDisplay.py that is able from which I produced the 
screenshots below. I also changed my sphere to a torus: it's difficult 
to see a sphere rotate around his center unless your eyes are 5cm far 
from the screen!

Once launched ("python wxDisplay.py"), you can:
- hit "W" key to set display mode to wireframe,
- hit "S" to switch to shaded mode,
- left click and move to rotate,
- middle click and move to pan,
- right click and move to zoom in/out.

The script is quite small: only 150 lines of code, easy to understand 
and to extend. Enjoy this sample with your own shapes!

Thomas



<<attachment: ATT2623133.jpg>>

<<attachment: ATT2623134.jpg>>

from OCC import *

import wx
import wx.glcanvas

class GraphicsCanva3D(wx.glcanvas.GLCanvas):
    def __init__(self, parent):
        wx.glcanvas.GLCanvas.__init__(self, parent)
 
        wx.EVT_SIZE(self, self.OnSize)
        wx.EVT_IDLE(self, self.OnIdle)
        wx.EVT_MOVE(self, self.OnMove)
        wx.EVT_SET_FOCUS(self, self.OnFocus)
        wx.EVT_KILL_FOCUS(self, self.OnLostFocus)
        wx.EVT_MAXIMIZE(self, self.OnMaximize)

        wx.EVT_LEFT_DOWN(self, self.OnLeftDown)
        wx.EVT_RIGHT_DOWN(self, self.OnRightDown)
        wx.EVT_MIDDLE_DOWN(self, self.OnMiddleDown)
        wx.EVT_LEFT_UP(self, self.OnLeftUp)
        wx.EVT_RIGHT_UP(self, self.OnRightUp)
        wx.EVT_MIDDLE_UP(self, self.OnMiddleUp)
        wx.EVT_MOTION(self, self.OnMotion)
        wx.EVT_KEY_DOWN(self,self.OnKeyDown)

        self._3dDisplay = None
        self._inited = False
        self._leftisdown = False
        self._middleisdown = False
        self._rightisdown = False

    def Init3dViewer(self):
        self._3dDisplay = Display3d()
        self._3dDisplay.Init(self.GetHandle())
        self._inited = True

    def OnKeyDown(self,evt):
        key_code = evt.GetKeyCode()
        if key_code==87:#"W"
            self._3dDisplay.SetDisplayModeWireFrame()
        elif key_code==83:#"S"
            self._3dDisplay.SetDisplayModeShaded()
        
    def OnSize(self, event):
        if self._inited:
            self._3dDisplay.Repaint()

    def OnMaximize(self, event):
        if self._inited:
            self._3dDisplay.Repaint()
        
    def OnMove(self, event):
        if self._inited:
            self._3dDisplay.Repaint()

    def OnIdle(self, event):
        if self._inited:
            self._3dDisplay.Repaint()

    def Test(self):
        if self._inited:
            self._3dDisplay.Test()
        
    def OnFocus(self, event):
        if self._inited:
            self._3dDisplay.Repaint()
        
    def OnLostFocus(self, event):
        if self._inited:
            self._3dDisplay.Repaint()

    def OnPaint(self, event):
        if self._inited:
            self._3dDisplay.Repaint()
            
    def ZoomAll(self, evt):
        self._3dDisplay.Zoom_FitAll()

    def Repaint(self, evt):
       if self._inited:
            self._3dDisplay.Repaint()
            
    def OnLeftDown(self, evt):
        self._leftisdown = True
        self.dragStartPos = evt.GetPosition()
        self._3dDisplay.StartRotation(self.dragStartPos.x,self.dragStartPos.y) 

    def OnLeftUp(self,evt):
        self._leftisdown = False

    def OnRightUp(self,evt):
        self._rightisdown = False

    def OnMiddleUp(self,evt):
        self._middleisdown = False
        
    def OnRightDown(self, evt):
        self._rightisdown = True
        self.dragStartPos = evt.GetPosition()
        self._3dDisplay.StartRotation(self.dragStartPos.x,self.dragStartPos.y)  
      
            
    def OnMiddleDown(self, evt):
        self._middleisdown = True
        self.dragStartPos = evt.GetPosition()
        self._3dDisplay.StartRotation(self.dragStartPos.x,self.dragStartPos.y) 
                   
    def OnMotion(self, evt):
        pt = evt.GetPosition()
        # ROTATE
        if self._leftisdown:
            dx = pt.x - self.dragStartPos.x
            dy = pt.y - self.dragStartPos.y
            self._3dDisplay.Rotation(pt.x,pt.y)
        # DYNAMIC ZOOM
        if self._rightisdown:
            self._3dDisplay.Repaint()
            self._3dDisplay.DynamicZoom(abs(self.dragStartPos.x), 
abs(self.dragStartPos.y), abs(pt.x), abs(pt.y))
            self.dragStartPos.x = pt.x 
            self.dragStartPos.y = pt.y
        # PAN
        elif self._middleisdown:
            dx = pt.x - self.dragStartPos.x
            dy = pt.y - self.dragStartPos.y
            self.dragStartPos.x = pt.x 
            self.dragStartPos.y = pt.y
            self._3dDisplay.Pan(dx,-dy)

if __name__=="__main__":
    class AppFrame(wx.Frame):
        def __init__(self, parent):
            wx.Frame.__init__(self, parent, -1, "pythonOCC 0.98 test - Thomas 
Paviot", style=wx.DEFAULT_FRAME_STYLE,size = (640,480))
            self.canva = GraphicsCanva3D(self)
                  
    app = wx.PySimpleApp()
    wx.InitAllImageHandlers()
    frame = AppFrame(None)
    frame.Show()
    frame.canva.Init3dViewer()
    frame.canva._3dDisplay.SetDisplayModeShaded()
    # Draw and display a torus
    S = BRepPrimAPI_MakeTorus(400,100)
    shape = S.Shape()
    frame.canva._3dDisplay.DisplayShape(S.Shape())
    app.SetTopWindow(frame)
    app.MainLoop()            

_______________________________________________
Minerva-pythonocc mailing list
Minerva-pythonocc@gna.org
https://mail.gna.org/listinfo/minerva-pythonocc

Reply via email to