Implement the 3D view window in a Dialog with OpenCascade

类别:VC语言 点击:0 评论:0 推荐:

Implement the 3D view window in a Dialog with OpenCascade


【Preview】 



【Motivation】

       In my application about simulation on the Real Structure Elements in a construction, the calculation of some parameters is the mainly task which will be solved by the 3D graphic boolean method with Opencascade SDK. However, the white-box test unit should be established before the main module.

First, I think that the visual tools like the a dialog or a window will be helpful to check the boolean arithmetic in my application. Second, a window OCX or light-weight visual tool is mascara for the future.

【Design Item】

A window class “C3DWnd” is designed for disaplaying the Simulated Elements using in the main module. And a instance of the C3DWnd is aggregated in a Dialog class. Based the Idea of the Component, C3DWnd is nested in a DLL module which can be encapsulated as an OCX. On the other hand the “Dialog class” is nested in another module as an “exe file”.

【Code Background】

System: Window XP,sp1;

Compiler: Visual C++ 6.0,sp6;

SDK: Opencascade 5.1;

【Detail】

1、C3DWnd class

// 3DWnd.h : header file

//

#include <V3d_View.hxx>

#include <TPrsStd_AISViewer.hxx>

#include <Graphic3d_WNTGraphicDevice.hxx>

#include <AIS_InteractiveContext.hxx>

#include <WNT_Window.hxx>

#include <Handle_V3d_PositionalLight.hxx>

 

#ifndef __AFXWIN_H__

#error include 'stdafx.h' before including this file for PCH

#endif

 

#undef DLLIMPEXP

#ifdef _3DViewDLL_ 

#define DLLIMPEXP __declspec(dllexport)

#else

#define DLLIMPEXP

#endif

 

/////////////////////////////////////////////////////////////////////////////

// C3DWnd window

enum View3D_CurrentAction {

       CurAction3d_Nothing,

              CurAction3d_DynamicZooming,

              CurAction3d_WindowZooming,

              CurAction3d_DynamicPanning,

              CurAction3d_GlobalPanning,

              CurAction3d_DynamicRotation,

              CurAction3d_BeginSpotLight,

              CurAction3d_TargetSpotLight,

              CurAction3d_EndSpotLight,

              CurAction3d_BeginPositionalLight,

              CurAction3d_BeginDirectionalLight,

              CurAction3d_EndDirectionalLight

};

 

class __declspec(dllexport) C3DWnd : public CWnd

{

// Construction

public:

       C3DWnd();

 

// Attributes

public:

       enum LineStyle { Solid, Dot, ShortDash, LongDash, Default };

// Operations

public:

 

    virtual void DrawRectangle (const Standard_Integer  MinX  ,

                                                        const Standard_Integer  MinY  ,

                                                        const Standard_Integer  MaxX  ,

                                                        const Standard_Integer  MaxY  ,

                                                        const Standard_Boolean  Draw  ,

                                                        const LineStyle aLineStyle = Default  );

 

      

// Overrides

       // ClassWizard generated virtual function overrides

       //{{AFX_VIRTUAL(C3DWnd)

       public:

       //}}AFX_VIRTUAL

 

// Implementation

public:

       void ShowFace(unsigned long nFaceID);

       void ShowSolid(const int nID);

       void DispplayText();

       void DisplayAxis();

       void Refresh();

       void Clear();

       void Right();

       void Top();

       void Left();

       void Front();

       void Bottom();

       void Back();

       void Axo();

       void HideOff();

       void HideOn();

       void Rotate();

       void PanGlobal();

       void Pan();

       void ZoomProg();

       void ZoomWin();

       void ZoolAll();

       void ShowVolume();

       virtual ~C3DWnd();

 

       // Generated message map functions

protected:

       //{{AFX_MSG(C3DWnd)

       afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

       afx_msg void OnSize(UINT nType, int cx, int cy);

       afx_msg void OnPaint();

       afx_msg void OnMouseMove(UINT nFlags, CPoint point);

       afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);

       afx_msg void OnLButtonUp(UINT nFlags, CPoint point);

       afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

       //}}AFX_MSG

       DECLARE_MESSAGE_MAP()

private:

       //画鼠标拉框用的笔:

       CPen* m_pPen;                  

       //3D视图指针:

       Handle_V3d_View   m_pView;

       //视图容器类:

       Handle_V3d_Viewer m_pViewer;

       //window窗口框架:

       Handle_WNT_Window m_pWNTWindow;

       //选择图形和管理图形行为:

    Handle_AIS_InteractiveContext m_pAISContext;

       //初始化Windows NT Graphic Device:

       Handle_Graphic3d_WNTGraphicDevice m_pGraphicDevice;

       //位置灯光设置

       Handle_V3d_PositionalLight m_pCurrent_PositionalLight;

       //隐藏的标志:

       Standard_Boolean     m_DegenerateModeIsOn;

       //当前的命令状态:

       View3D_CurrentAction m_CurrentMode;

       //当前缩放比例:

    Quantity_Factor      m_CurZoom;

       //图像视图的宽度:

       Standard_Real         m_rWidth;

       //图像视图的高度:

       Standard_Real         m_rHeight;

      

    Standard_Integer     m_Atx  , m_Aty  , m_Atz  ;//

       Standard_Integer     m_Eyex , m_Eyey , m_Eyez ;//

};

/////////////////////////////////////////////////////////////////////////////

 

// 3DWnd.cpp : implementation file

//

 

#include "stdafx.h"

#include "3DWnd.h"

#include <AIS_ListOfInteractive.hxx>

#include <AIS_ListIteratorOfListOfInteractive.hxx>

#include <BRepPrimAPI_MakeSphere.hxx>

#include <AIS_Trihedron.hxx>

#include <Geom_Axis2Placement.hxx>

#include "..\..\..\..\GCL7\Source\GeoCalc\ExportFunc.h"

 

#ifdef _DEBUG

//#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

 

gp_Pnt p1,p2,p3;

/////////////////////////////////////////////////////////////////////////////

// C3DWnd

Standard_Integer cXmin = 0;

Standard_Integer cYmin = 0; 

Standard_Integer cXmax = 600;

Standard_Integer cYmax = 600;

Standard_Integer cZmin = 0;

Standard_Integer cZmax = 200;

Standard_Integer cZoomStep = 20;

 

#define ValZWMin 1

 

gp_Pnt ConvertClickToPoint(Standard_Real x, Standard_Real y, Handle(V3d_View) aView)

{

       V3d_Coordinate XEye,YEye,ZEye,XAt,YAt,ZAt;

       aView->Eye(XEye,YEye,ZEye);

       aView->At(XAt,YAt,ZAt);

       gp_Pnt EyePoint(XEye,YEye,ZEye);

       gp_Pnt AtPoint(XAt,YAt,ZAt);

      

       gp_Vec EyeVector(EyePoint,AtPoint);

       gp_Dir EyeDir(EyeVector);

      

       gp_Pln PlaneOfTheView = gp_Pln(AtPoint,EyeDir);

       Standard_Real X,Y,Z;

       aView->Convert(int(x),int(y),X,Y,Z);

       gp_Pnt ConvertedPoint(X,Y,Z);

       gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView,ConvertedPoint);

      

       gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(),

              ConvertedPointOnPlane.Y(),

              PlaneOfTheView);

       return ResultPoint;

}

 

C3DWnd::C3DWnd()

{

       m_pView = NULL;

       m_pViewer = NULL;

       m_pAISContext = NULL;

       m_pGraphicDevice = NULL;

       m_pWNTWindow = NULL;

    m_CurrentMode = CurAction3d_Nothing;

    m_CurZoom = 0;

       m_pPen = NULL;

    m_DegenerateModeIsOn=Standard_True;

}

 

C3DWnd::~C3DWnd()

{

       m_pView->Remove();

       if (m_pPen != NULL)

              delete m_pPen;

}

 

 

BEGIN_MESSAGE_MAP(C3DWnd, CWnd)

       //{{AFX_MSG_MAP(C3DWnd)

       ON_WM_CREATE()

       ON_WM_SIZE()

       ON_WM_PAINT()

       ON_WM_MOUSEMOVE()

       ON_WM_MOUSEWHEEL()

       ON_WM_LBUTTONUP()

       ON_WM_LBUTTONDOWN()

       //}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

 

/////////////////////////////////////////////////////////////////////////////

// C3DWnd message handlers

 

int C3DWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

       if (CWnd::OnCreate(lpCreateStruct) == -1)

              return -1;

      

       // TODO: Add your specialized creation code here

       // Creating View

       try

       {

              m_pGraphicDevice = new Graphic3d_WNTGraphicDevice();

       }

       catch(Standard_Failure)

       {

              AfxMessageBox("Fatal Error During Graphic Initialisation");

       };

 

       m_pViewer = new V3d_Viewer(m_pGraphicDevice, (short *) (""));

       m_pViewer->SetDefaultLights();

       m_pViewer->SetLightOn();

      

       // Create an interactive context based on the m_pViewer

       m_pAISContext = new AIS_InteractiveContext(m_pViewer);

       m_pView = m_pViewer->CreateView();    

       m_pView->SetDegenerateModeOn();

    // store for restore state after rotation (witch is in Degenerated mode)

    m_DegenerateModeIsOn = Standard_True;

 

       // Creating new Window and attaching it to View

       m_pWNTWindow = new WNT_Window(m_pGraphicDevice,GetSafeHwnd());

    m_pView->SetWindow(m_pWNTWindow );

    if (!m_pWNTWindow->IsMapped()) m_pWNTWindow ->Map();

 

       DisplayAxis();

 

       return 0;

}

 

void C3DWnd::OnSize(UINT nType, int cx, int cy)

{

       CWnd::OnSize(nType, cx, cy);

      

       // TODO: Add your message handler code here

       if (m_pView != NULL){

              m_pView->MustBeResized();

              m_pView->Size(m_rWidth, m_rHeight);

       }

}

 

void C3DWnd::OnPaint()

{

       CPaintDC dc(this); // device context for painting

       CRect aRect;

       GetWindowRect(aRect);

       if(m_rWidth != aRect.Width() || m_rHeight != aRect.Height()) {

              m_rWidth = aRect.Width();

              m_rHeight = aRect.Height();

              ::PostMessage ( GetSafeHwnd () , WM_SIZE , SW_SHOW , m_rWidth + m_rHeight*65536 );

       }

      

       // TODO: Add your message handler code here

       m_pView->Redraw();

       // Do not call CWnd::OnPaint() for painting messages

}

 

void C3DWnd::ShowVolume()

{

       Clear();

       CArrLong arrID;

       GetSolidsID(arrID);

       for (int i = 0; i < arrID.GetSize(); i++){

              TopoDS_Solid solid = GetSolidByID(arrID.GetAt(i));

              Handle(AIS_Shape) ais = new AIS_Shape(solid);

              m_pAISContext->SetColor(ais,Quantity_NOC_GREEN,Standard_False);

              m_pAISContext->SetMaterial(ais,Graphic3d_NOM_GOLD,Standard_False);  

              m_pAISContext->SetDisplayMode(ais,1);

              m_pAISContext->Display(ais,Standard_False);

       }

       GetFacesID(arrID);

       for ( i = 0; i < arrID.GetSize(); i++){

              TopoDS_Face Face ;

              if (!GetFaceByID(arrID.GetAt(i), Face)) break;

              Handle(AIS_Shape) ais = new AIS_Shape(Face);

              m_pAISContext->SetColor(ais,Quantity_NOC_GRAY99,Standard_False);

              m_pAISContext->SetMaterial(ais,Graphic3d_NOM_STONE,Standard_False);  

              m_pAISContext->SetDisplayMode(ais,1);

              m_pAISContext->Display(ais,Standard_False);

       }    

       m_pView->FitAll();

       m_pView->ZFitAll();      

/*    TopoDS_Shape S = BRepPrimAPI_MakeWedge(600.,100.,80.,20.);

       Handle(AIS_Shape) ais1 = new AIS_Shape(S);

       m_pAISContext->SetColor(ais1,Quantity_NOC_GREEN,Standard_False);

       m_pAISContext->SetMaterial(ais1,Graphic3d_NOM_PLASTIC,Standard_False);  

       m_pAISContext->Display(ais1,Standard_False);

 

       BRepPrimAPI_MakeSphere S2(gp_Pnt(0,300,0), 100.);

       Handle(AIS_Shape) aSphere = new AIS_Shape(S2.Shape());

       m_pAISContext->SetMaterial(aSphere,Graphic3d_NOM_SILVER);   

       m_pAISContext->SetDisplayMode(aSphere,1);

       m_pAISContext->Display(aSphere);

       m_pView->FitAll();

       m_pView->ZFitAll();      

       AIS_ListOfInteractive aList;

       m_pAISContext->DisplayedObjects(aList);

       AIS_ListIteratorOfListOfInteractive aListIterator;

       for(aListIterator.Initialize(aList);aListIterator.More();aListIterator.Next()){

              m_pAISContext->Remove(aListIterator.Value());

       }

      

      

       TColgp_Array1OfPnt Points1(1,4);

       Points1.SetValue(1,gp_Pnt(0,0,0));

       Points1.SetValue(2,gp_Pnt(2,1,0));

       Points1.SetValue(3,gp_Pnt(4,0,0));

       Points1.SetValue(4,gp_Pnt(6,2,0));

       GeomAPI_PointsToBSpline PTBS1(Points1);

       Handle(Geom_BSplineCurve) BSC1 = PTBS1.Curve();

       TopoDS_Edge S = BRepBuilderAPI_MakeEdge(BSC1).Edge();

      

       Handle(AIS_Shape) ais1 = new AIS_Shape(S);

       m_pAISContext->SetColor(ais1,Quantity_NOC_GREEN,Standard_False);

       m_pAISContext->SetMaterial(ais1,Graphic3d_NOM_PLASTIC,Standard_False);  

       m_pAISContext->Display(ais1,Standard_False);

       m_pView->FitAll();

       m_pView->ZFitAll();*/

}

 

 

void C3DWnd::OnMouseMove(UINT nFlags, CPoint point)

{

       // TODO: Add your message handler code here and/or call default

       if ( nFlags & MK_LBUTTON)

    {

              if ( nFlags & MK_CONTROL )

              {

                     // move with MB1 and Control : on the dynamic zooming 

                     // Do the zoom in function of mouse's coordinates 

                     m_pView->Zoom(cXmax,cYmax,point.x,point.y);

                     // save the current mouse coordinate in min

                     cXmax = point.x;

                     cYmax = point.y;      

              }

              else // if ( Ctrl )

              {

                     switch (m_CurrentMode)

                     {

                     case CurAction3d_Nothing :

                            cXmax = point.x;  cYmax = point.y;

                            DrawRectangle(cXmin,cYmin,cXmax,cYmax,Standard_False);

                            DrawRectangle(cXmin,cYmin,cXmax,cYmax,Standard_True);

                           

                            break;

                     case CurAction3d_DynamicZooming :

                            m_pView->Zoom(cXmax,cYmax,point.x,point.y);

                            // save the current mouse coordinate in min \n";

                            cXmax=point.x;  cYmax=point.y;

                            break;

                     case CurAction3d_WindowZooming :

                            cXmax = point.x; cYmax = point.y;     

                            DrawRectangle(cXmin,cYmin,cXmax,cYmax,Standard_False,LongDash);

                            DrawRectangle(cXmin,cYmin,cXmax,cYmax,Standard_True,LongDash);

                            break;

                     case CurAction3d_DynamicPanning :

                            m_pView->Pan(point.x-cXmax,cYmax-point.y); // Realize the panning

                            cXmax = point.x; cYmax = point.y;     

                            break;

                     case CurAction3d_GlobalPanning : // nothing          

                            break;

                     case  CurAction3d_DynamicRotation :

                            m_pView->Rotation(point.x,point.y);

                            m_pView->Redraw();

                            break;

                     default :

                            Standard_Failure::Raise(" incompatible Current Mode ");

                            break;

                     }

              }

    }

       CWnd::OnMouseMove(nFlags, point);

}

 

void C3DWnd::DrawRectangle(const Standard_Integer  MinX ,

                       const Standard_Integer  MinY ,

                       const Standard_Integer  MaxX ,

                       const Standard_Integer  MaxY ,

                       const Standard_Boolean  Draw ,

                       const LineStyle aLineStyle)

{

    static int m_DrawMode;

    if  (!m_pPen && aLineStyle ==Solid )

       {m_pPen = new CPen(PS_SOLID, 1, RGB(0,0,0)); m_DrawMode = R2_MERGEPENNOT;}

    else if (!m_pPen && aLineStyle ==Dot )

       {m_pPen = new CPen(PS_DOT, 1, RGB(0,0,0));   m_DrawMode = R2_XORPEN;}

    else if (!m_pPen && aLineStyle == ShortDash)

       {m_pPen = new CPen(PS_DASH, 1, RGB(255,0,0));     m_DrawMode = R2_XORPEN;}

    else if (!m_pPen && aLineStyle == LongDash)

       {m_pPen = new CPen(PS_DASH, 1, RGB(0,0,0));  m_DrawMode = R2_NOTXORPEN;}

    else if (aLineStyle == Default)

       { m_pPen = NULL;   m_DrawMode = R2_MERGEPENNOT;}

      

    CPen* aOldPen;

    CClientDC clientDC(this);

    if (m_pPen) aOldPen = clientDC.SelectObject(m_pPen);

    clientDC.SetROP2(m_DrawMode);

      

    static            Standard_Integer StoredMinX, StoredMaxX, StoredMinY, StoredMaxY;

    static            Standard_Boolean m_IsVisible;

      

    if ( m_IsVisible && !Draw) // move or up  : erase at the old position

    {

              clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY);

              clientDC.LineTo(StoredMaxX,StoredMaxY);

              clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);

              m_IsVisible = false;

    }

      

    StoredMinX = min ( MinX, MaxX );

    StoredMinY = min ( MinY, MaxY );

    StoredMaxX = max ( MinX, MaxX );

    StoredMaxY = max ( MinY, MaxY);

      

    if (Draw) // move : draw

    {

              clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY);

              clientDC.LineTo(StoredMaxX,StoredMaxY);

              clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);

              m_IsVisible = true;

       }

      

    if (m_pPen) clientDC.SelectObject(aOldPen);

}

 

BOOL C3DWnd::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)

{

       // move with MB1 and Control : on the dynamic zooming 

       // Do the zoom in function of mouse's coordinates 

       m_CurZoom = m_pView->Scale();

       cXmax = pt.x + zDelta/cZoomStep;

       cYmax = pt.y + zDelta/cZoomStep;

       if (cYmax < 0)    cYmax = 0;

       if (cXmax < 0)    cXmax = 0;

       m_pView->Zoom(cXmax,cYmax,pt.x,pt.y);

 

       return TRUE;

//     return CWnd::OnMouseWheel(nFlags, zDelta, pt);

}

 

void C3DWnd::OnLButtonUp(UINT nFlags, CPoint point)

{

       if ( nFlags & MK_CONTROL )

       {

        return;

       }

       else // if ( Ctrl )

       {

        switch (m_CurrentMode)

        {

              case CurAction3d_Nothing :

                     if (point.x == cXmin && point.y == cYmin)

                     { // no offset between down and up --> selectEvent

                            cXmax=point.x; 

                            cYmax=point.y;

                     } else

                     {

                            DrawRectangle(cXmin,cYmin,cXmax,cYmax,Standard_False);

                            cXmax=point.x; 

                            cYmax=point.y;

                     }

                     break;

              case CurAction3d_DynamicZooming :

                     m_CurrentMode = CurAction3d_Nothing;

                     break;

              case CurAction3d_WindowZooming :

                     DrawRectangle(cXmin,cYmin,cXmax,cYmax,Standard_False);

                     cXmax=point.x; 

                     cYmax=point.y;

                     if ((abs(cXmin-cXmax)>ValZWMin) || (abs(cYmin-cYmax)>ValZWMin))

                     {

                            // Do the zoom window between Pmin and Pmax

                            m_pView->WindowFitAll(cXmin,cYmin,cXmax,cYmax); 

                     } 

                     m_CurrentMode = CurAction3d_Nothing;

                     break;

              case CurAction3d_DynamicPanning :

                     m_CurrentMode = CurAction3d_Nothing;

                     break;

              case CurAction3d_GlobalPanning :

                     m_pView->Place(point.x,point.y,m_CurZoom);

                     m_CurrentMode = CurAction3d_Nothing;

                     break;

        case  CurAction3d_DynamicRotation :

                     m_CurrentMode = CurAction3d_Nothing;

                     break;

        }

    }

}

 

void C3DWnd::OnLButtonDown(UINT nFlags, CPoint point)

{

  //  save the current mouse coordinate in min

  cXmin=point.x;  cYmin=point.y;

  cXmax=point.x;  cYmax=point.y;

 

  if ( nFlags & MK_CONTROL )

         {

           // Button MB1 down Control :start zomming

        // SetCursor(AfxGetApp()->LoadStandardCursor());

         }

       else // if ( Ctrl )

         {

        switch (m_CurrentMode)

        {

         case CurAction3d_Nothing : // start a drag

         break;

         case CurAction3d_DynamicZooming :

         break;

         case CurAction3d_WindowZooming :

         break;

         case CurAction3d_DynamicPanning :

         break;

         case CurAction3d_GlobalPanning :

         break;

         case CurAction3d_DynamicRotation :

                     if (!m_DegenerateModeIsOn)

                       m_pView->SetDegenerateModeOn();

                     m_pView->StartRotation(point.x,point.y); 

        break;

        case  CurAction3d_BeginPositionalLight :

 

        break;

        case  CurAction3d_BeginSpotLight :

 

        break;

        case  CurAction3d_TargetSpotLight :

 

              break;

        case  CurAction3d_EndSpotLight :

 

        break;

              case CurAction3d_BeginDirectionalLight:

 

              break;

              case CurAction3d_EndDirectionalLight:

                    

              break;

        }

    }

       CWnd::OnLButtonDown(nFlags, point);

}

 

void C3DWnd::ZoolAll()

{

       m_pView->FitAll();

       m_pView->ZFitAll();

}

 

void C3DWnd::ZoomWin()

{

       m_CurrentMode = CurAction3d_WindowZooming;

}

 

void C3DWnd::ZoomProg()

{

       m_CurrentMode = CurAction3d_DynamicZooming;

}

 

void C3DWnd::Pan()

{

       m_CurrentMode = CurAction3d_DynamicPanning;

}

 

void C3DWnd::PanGlobal()

{

       m_CurZoom = m_pView->Scale();

       // Do a Global Zoom

       //myView->FitAll();

       m_CurrentMode = CurAction3d_GlobalPanning;

}

 

void C3DWnd::Rotate()

{

       m_CurrentMode = CurAction3d_DynamicRotation;

}

 

void C3DWnd::HideOn()

{

       m_pView->SetDegenerateModeOff();

       m_DegenerateModeIsOn = Standard_False;

}

 

void C3DWnd::HideOff()

{

       m_pView->SetDegenerateModeOn();

       m_DegenerateModeIsOn = Standard_True;

}

 

void C3DWnd::Axo()

{

       m_pView->SetProj(V3d_XposYnegZpos);

}

 

void C3DWnd::Back()

{

       m_pView->SetProj(V3d_Xneg);

}

 

void C3DWnd::Bottom()

{

       m_pView->SetProj(V3d_Zneg);

}

 

void C3DWnd::Front()

{

       m_pView->SetProj(V3d_Xpos);

}

 

void C3DWnd::Left()

{

       m_pView->SetProj(V3d_Ypos);

}

 

void C3DWnd::Top()

{

       m_pView->SetProj(V3d_Zpos);

}

 

void C3DWnd::Right()

{

       m_pView->SetProj(V3d_Yneg);

}

 

void C3DWnd::Clear()

{

       AIS_ListOfInteractive aList;

       m_pAISContext->DisplayedObjects(aList);

       AIS_ListIteratorOfListOfInteractive aListIterator;

       for(aListIterator.Initialize(aList);aListIterator.More();aListIterator.Next()){

              m_pAISContext->Remove(aListIterator.Value());

       }

}

 

void C3DWnd::Refresh()

{

       Clear();

       DisplayAxis();

}

 

void C3DWnd::DisplayAxis()

{

       Handle(AIS_Trihedron) aTrihedron;

       Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());

       aTrihedron = new AIS_Trihedron(anAxis);

       m_pAISContext->Display(aTrihedron);

}

 

void C3DWnd::DispplayText()

{

 /*

       TCollection_ExtendedString Text("Test");

        Handle (Sample2D_Text) aText  =

            new Sample2D_Text(Text,

                 140,15*(j-5),  //  anX  , anY

                 Aspect_TOT_SOLID,//  aType 

                 0.0 ,            //  anAngle 

                 0.0 ,            //  aSlant 

                 color++,         //  aColorIndex 

                 2,               //  aFontIndex 

                 j/10,               //  aScale

                 Standard_False,  // aIsUnderline

                 Standard_True,   // aIsZoomable

                 Graphic2d_TOA_TOPLEFT); // aTypeOfAlignment

        myAISInteractiveContext2D->Display(aText,            // object

                 Standard_False);  // Redraw

        if (color == my2DViewer->ColorMap()->Size()) color =1;*/

   

}

 

void C3DWnd::ShowSolid(const int nID)

{

       Clear();

       TopoDS_Solid solid = GetSolidByID(nID);

       Handle(AIS_Shape) ais = new AIS_Shape(solid);

       m_pAISContext->SetColor(ais,Quantity_NOC_GREEN,Standard_False);

       m_pAISContext->SetMaterial(ais,Graphic3d_NOM_GOLD,Standard_False);  

       m_pAISContext->SetDisplayMode(ais,1);

       m_pAISContext->Display(ais,Standard_False);

 

       m_pView->FitAll();

       m_pView->ZFitAll();      

}

 

void C3DWnd::ShowFace(unsigned long nFaceID)

{

       Clear();

       TopoDS_Face Face ;

       GetFaceByID(nFaceID, Face);

       Handle(AIS_Shape) ais = new AIS_Shape(Face);

       m_pAISContext->SetColor(ais,Quantity_NOC_GRAY99,Standard_False);

       m_pAISContext->SetMaterial(ais,Graphic3d_NOM_STONE,Standard_False);  

       m_pAISContext->SetDisplayMode(ais,1);

       m_pAISContext->Display(ais,Standard_False);

      

       m_pView->FitAll();

       m_pView->ZFitAll();      

}

2、CTest3DViewDlg class

/////////////////////////////////////////////////////////////////////////////

// // Test3DViewDlg.h : header file

class C3DWnd;

 

class CTest3DViewDlg : public CDialog

{

// Construction

public:

       CTest3DViewDlg(CWnd* pParent = NULL);   // standard constructor

       ~CTest3DViewDlg();

// Dialog Data

       //{{AFX_DATA(CTest3DViewDlg)

       enum { IDD = IDD_TEST3DVIEW_DIALOG };

              // NOTE: the ClassWizard will add data members here

       //}}AFX_DATA

 

       // ClassWizard generated virtual function overrides

       //{{AFX_VIRTUAL(CTest3DViewDlg)

       protected:

       virtual void DoDataExchange(CDataExchange* pDX);  // DDX/DDV support

       //}}AFX_VIRTUAL

 

// Implementation

protected:

       HICON m_hIcon;

 

       // Generated message map functions

       //{{AFX_MSG(CTest3DViewDlg)

       virtual BOOL OnInitDialog();

       afx_msg void OnSysCommand(UINT nID, LPARAM lParam);

       afx_msg void OnPaint();

       afx_msg HCURSOR OnQueryDragIcon();

       virtual void OnOK();

       afx_msg void OnBtnShowvolume();

       afx_msg void OnBtnZoomall();

       afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);

       afx_msg void OnBtnZoomwin();

       afx_msg void OnBtnZoomprog();

       afx_msg void OnBtnPan();

       afx_msg void OnBtnPanGlobal();

       afx_msg void OnBtnRotate();

       afx_msg void OnBtnHideOn();

       afx_msg void OnBtnHideOff();

       afx_msg void OnBtnFront();

       afx_msg void OnBtnTop();

       afx_msg void OnBtnLeft();

       afx_msg void OnBtnRight();

       afx_msg void OnBtnBack();

       afx_msg void OnBtnBottom();

       afx_msg void OnBtnAxo();

       afx_msg void OnBtnLoadData();

       afx_msg void OnBtnBoolean();

       afx_msg void OnBtnDisplay();

       afx_msg void OnBtnBoolean2();

       //}}AFX_MSG

       DECLARE_MESSAGE_MAP()

private:

       C3DWnd *m_p3dWnd;

 

};

 

// Test3DViewDlg.cpp : implementation file

//

 

#include "stdafx.h"

#include "Test3DView.h"

#include "Test3DViewDlg.h"

#include "..\3DViewDLL\3dWnd.h"

#include "LoadDataDlg.h"

#include "BoolDlg.h"

#include "DisplayDlg.h"

#include "BoolFaceCutDlg.h"

 

 

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

 

/////////////////////////////////////////////////////////////////////////////

// CAboutDlg dialog used for App About

 

class CAboutDlg : public CDialog

{

public:

       CAboutDlg();

 

// Dialog Data

       //{{AFX_DATA(CAboutDlg)

       enum { IDD = IDD_ABOUTBOX };

       //}}AFX_DATA

 

       // ClassWizard generated virtual function overrides

       //{{AFX_VIRTUAL(CAboutDlg)

       protected:

       virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

       //}}AFX_VIRTUAL

 

// Implementation

protected:

       //{{AFX_MSG(CAboutDlg)

       //}}AFX_MSG

       DECLARE_MESSAGE_MAP()

};

 

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)

{

       //{{AFX_DATA_INIT(CAboutDlg)

       //}}AFX_DATA_INIT

}

 

void CAboutDlg::DoDataExchange(CDataExchange* pDX)

{

       CDialog::DoDataExchange(pDX);

       //{{AFX_DATA_MAP(CAboutDlg)

       //}}AFX_DATA_MAP

}

 

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)

       //{{AFX_MSG_MAP(CAboutDlg)

              // No message handlers

       //}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

/////////////////////////////////////////////////////////////////////////////

// CTest3DViewDlg dialog

 

CTest3DViewDlg::CTest3DViewDlg(CWnd* pParent /*=NULL*/)

       : CDialog(CTest3DViewDlg::IDD, pParent)

{

       //{{AFX_DATA_INIT(CTest3DViewDlg)

              // NOTE: the ClassWizard will add member initialization here

       //}}AFX_DATA_INIT

       // Note that LoadIcon does not require a subsequent DestroyIcon in Win32

       m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

       m_p3dWnd = NULL;

}

 

CTest3DViewDlg::~CTest3DViewDlg()

{

       if (m_p3dWnd != NULL)

              delete m_p3dWnd;

}

 

void CTest3DViewDlg::DoDataExchange(CDataExchange* pDX)

{

       CDialog::DoDataExchange(pDX);

       //{{AFX_DATA_MAP(CTest3DViewDlg)

              // NOTE: the ClassWizard will add DDX and DDV calls here

       //}}AFX_DATA_MAP

}

 

BEGIN_MESSAGE_MAP(CTest3DViewDlg, CDialog)

       //{{AFX_MSG_MAP(CTest3DViewDlg)

       ON_WM_SYSCOMMAND()

       ON_WM_PAINT()

       ON_WM_QUERYDRAGICON()

       ON_BN_CLICKED(IDC_BTN_SHOWVOLUME, OnBtnShowvolume)

       ON_BN_CLICKED(IDC_BTN_ZOOMALL, OnBtnZoomall)

       ON_WM_MOUSEWHEEL()

       ON_BN_CLICKED(IDC_BTN_ZOOMWIN, OnBtnZoomwin)

       ON_BN_CLICKED(IDC_BTN_ZOOMPROG, OnBtnZoomprog)

       ON_BN_CLICKED(IDC_BTN_PAN, OnBtnPan)

       ON_BN_CLICKED(IDC_BTN_PANGLOBAL, OnBtnPanGlobal)

       ON_BN_CLICKED(IDC_BTN_ROTATE, OnBtnRotate)

       ON_BN_CLICKED(IDC_BTN_HIDEON, OnBtnHideOn)

       ON_BN_CLICKED(IDC_BTN_HIDEOFF, OnBtnHideOff)

       ON_BN_CLICKED(IDC_BTN_FRONT, OnBtnFront)

       ON_BN_CLICKED(IDC_BTN_TOP, OnBtnTop)

       ON_BN_CLICKED(IDC_BTN_LEFT, OnBtnLeft)

       ON_BN_CLICKED(IDC_BTN_RIGHT, OnBtnRight)

       ON_BN_CLICKED(IDC_BTN_BACK, OnBtnBack)

       ON_BN_CLICKED(IDC_BTN_BOTTOM, OnBtnBottom)

       ON_BN_CLICKED(IDC_BTN_AXO, OnBtnAxo)

       ON_BN_CLICKED(IDC_BTN_LOADDATA, OnBtnLoadData)

       ON_BN_CLICKED(IDC_BTN_BOOLEAN, OnBtnBoolean)

       ON_BN_CLICKED(IDC_BTN_DISPLAY, OnBtnDisplay)

       ON_BN_CLICKED(IDC_BTN_BOOLEAN2, OnBtnBoolean2)

       //}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

/////////////////////////////////////////////////////////////////////////////

// CTest3DViewDlg message handlers

 

BOOL CTest3DViewDlg::OnInitDialog()

{

       CDialog::OnInitDialog();

 

       // Add "About..." menu item to system menu.

 

       // IDM_ABOUTBOX must be in the system command range.

       ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);

       ASSERT(IDM_ABOUTBOX < 0xF000);

 

       CMenu* pSysMenu = GetSystemMenu(FALSE);

       if (pSysMenu != NULL)

       {

              CString strAboutMenu;

              strAboutMenu.LoadString(IDS_ABOUTBOX);

              if (!strAboutMenu.IsEmpty())

              {

                     pSysMenu->AppendMenu(MF_SEPARATOR);

                     pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);

              }

       }

 

       // Set the icon for this dialog.  The framework does this automatically

       //  when the application's main window is not a dialog

       SetIcon(m_hIcon, TRUE);                    // Set big icon

       SetIcon(m_hIcon, FALSE);           // Set small icon

      

       // TODO: Add extra initialization here

       m_p3dWnd =  new C3DWnd;

 

       CRect rect;

/*

              CWnd *pWnd = NULL;

              pWnd = GetDlgItem(IDC_3DWND);

              ASSERT(pWnd);

              pWnd->GetClientRect(rect);*/

      

       m_p3dWnd->Create(NULL, "Test3D", WS_CHILD|WS_VISIBLE, rect, this, ID_3DVIEWER);

 

      

 

       return TRUE;  // return TRUE  unless you set the focus to a control

}

 

void CTest3DViewDlg::OnSysCommand(UINT nID, LPARAM lParam)

{

       if ((nID & 0xFFF0) == IDM_ABOUTBOX)

       {

              CAboutDlg dlgAbout;

              dlgAbout.DoModal();

       }

       else

       {

              CDialog::OnSysCommand(nID, lParam);

       }

}

 

// If you add a minimize button to your dialog, you will need the code below

//  to draw the icon.  For MFC applications using the document/view model,

//  this is automatically done for you by the framework.

 

void CTest3DViewDlg::OnPaint()

{

       if (IsIconic())

       {

              CPaintDC dc(this); // device context for painting

 

              SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

 

              // Center icon in client rectangle

              int cxIcon = GetSystemMetrics(SM_CXICON);

              int cyIcon = GetSystemMetrics(SM_CYICON);

              CRect rect;

              GetClientRect(&rect);

              int x = (rect.Width() - cxIcon + 1) / 2;

              int y = (rect.Height() - cyIcon + 1) / 2;

 

              // Draw the icon

              dc.DrawIcon(x, y, m_hIcon);

       }

       else

       {

              CDialog::OnPaint();

       }

 

       CRect rect;

       CWnd *pWnd = NULL;

       pWnd = GetDlgItem(IDC_3DWND);

       ASSERT(pWnd);

       pWnd->GetClientRect(rect);

       if (m_p3dWnd != NULL)

              m_p3dWnd->MoveWindow(rect);

}

 

// The system calls this to obtain the cursor to display while the user drags

//  the minimized window.

HCURSOR CTest3DViewDlg::OnQueryDragIcon()

{

       return (HCURSOR) m_hIcon;

}

 

void CTest3DViewDlg::OnOK()

{

       // TODO: Add extra validation here

      

       CDialog::OnOK();

}

 

void CTest3DViewDlg::OnBtnShowvolume()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->ShowVolume();

}

 

void CTest3DViewDlg::OnBtnZoomall()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->ZoolAll();

}

 

BOOL CTest3DViewDlg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)

{

       // TODO: Add your message handler code here and/or call default

       WPARAM wParam;

    LPARAM lParam;

    wParam = MAKEWPARAM(nFlags,zDelta); // We can put whatever we

    lParam = MAKELPARAM(pt.x, pt.y); // want in wParam & lParam

       if (m_p3dWnd != NULL)

              return m_p3dWnd->SendMessage(WM_MOUSEWHEEL, wParam, lParam);

 

       return CDialog::OnMouseWheel(nFlags, zDelta, pt);

}

 

void CTest3DViewDlg::OnBtnZoomwin()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->ZoomWin();      

}

 

void CTest3DViewDlg::OnBtnZoomprog()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->ZoomProg();     

}

 

void CTest3DViewDlg::OnBtnPan()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->Pan();         

}

 

void CTest3DViewDlg::OnBtnPanGlobal()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->PanGlobal();     

}

 

void CTest3DViewDlg::OnBtnRotate()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->Rotate();           

}

 

void CTest3DViewDlg::OnBtnHideOn()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->HideOn();         

}

 

void CTest3DViewDlg::OnBtnHideOff()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->HideOff();        

}

 

void CTest3DViewDlg::OnBtnFront()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->Front();      

}

 

void CTest3DViewDlg::OnBtnTop()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->Top();  

}

 

void CTest3DViewDlg::OnBtnLeft()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->Left(); 

}

 

void CTest3DViewDlg::OnBtnRight()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->Right();      

}

 

void CTest3DViewDlg::OnBtnBack()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->Back();

}

 

void CTest3DViewDlg::OnBtnBottom()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->Bottom();   

}

 

void CTest3DViewDlg::OnBtnAxo()

{

       // TODO: Add your control notification handler code here

       ASSERT(m_p3dWnd);

       m_p3dWnd->Axo();        

}

 

void CTest3DViewDlg::OnBtnLoadData()

{

       // TODO: Add your control notification handler code here

       CLoadDataDlg dlg(this);

       dlg.DoModal();

}

 

void CTest3DViewDlg::OnBtnBoolean()

{

       // TODO: Add your control notification handler code here

       CBoolDlg dlg;

       dlg.DoModal();

 

}

 

void CTest3DViewDlg::OnBtnDisplay()

{

       // TODO: Add your control notification handler code here

       CDisplayDlg dlg;

       dlg.DoModal();

       ASSERT(m_p3dWnd);

       if (dlg.m_nID < 0 ) return;

       if (dlg.m_bFace){

              m_p3dWnd->ShowFace(dlg.m_nID);

       }else{

              m_p3dWnd->ShowSolid(dlg.m_nID);

       }

      

}

 

void CTest3DViewDlg::OnBtnBoolean2()

{

       // TODO: Add your control notification handler code here

       CBoolFaceCutDlg dlg;

       dlg.DoModal();

}

 
If you want the Sourece Codes, write down your email as below.

本文地址:http://com.8s8s.com/it/it294.htm