d5ebad740e780c2f9837145dbe4fb6b483f83215
[occt.git] / samples / mfc / standard / 08_HLR / src / HLRDoc.cpp
1 // HLRDoc.cpp : implementation of the CHLRDoc class
2 //
3
4
5 #include "stdafx.h"
6
7 #include "HLRDoc.h"
8 #include "HLRApp.h"
9 #include <OCC_2dView.h>
10 #include <OCC_3dView.h>
11
12
13 #include <ImportExport/ImportExport.h>
14 #include "AISDialogs.h"
15 #include <AIS_ListOfInteractive.hxx>
16
17 #ifdef _DEBUG
18 //#define new DEBUG_NEW  // by cascade
19 #undef THIS_FILE
20 static char THIS_FILE[] = __FILE__;
21 #endif
22
23 /////////////////////////////////////////////////////////////////////////////
24 // CHLRDoc
25
26 IMPLEMENT_DYNCREATE(CHLRDoc, CDocument)
27
28
29 BEGIN_MESSAGE_MAP(CHLRDoc, OCC_3dBaseDoc)
30   //{{AFX_MSG_MAP(CHLRDoc)
31   ON_COMMAND(ID_WINDOW_NEW3D, OnWindowNew3d)
32   ON_COMMAND(ID_WINDOW_NEW2D, OnWindowNew2d)
33   ON_COMMAND(ID_FILE_HLR, OnBUTTONHLRDialog)
34   ON_COMMAND(ID_FILE_IMPORT_BREP, OnFileImportBrep)
35   ON_COMMAND(ID_BUTTON_HLRDialog, OnBUTTONHLRDialog)
36   ON_COMMAND(ID_OBJECT_ERASE, OnObjectErase)
37   //}}AFX_MSG_MAP
38
39
40 END_MESSAGE_MAP()
41
42 /////////////////////////////////////////////////////////////////////////////
43 // CHLRDoc construction/destruction
44
45 CHLRDoc::CHLRDoc()
46 {
47   // TODO: add one-time construction code here
48   Handle(Graphic3d_GraphicDriver) theGraphicDriver = 
49     ((CHLRApp*)AfxGetApp())->GetGraphicDriver();
50
51   // VIEWER 3D
52   myViewer = new V3d_Viewer (theGraphicDriver);
53   myViewer->SetDefaultLights();
54   myViewer->SetLightOn();
55
56   myAISContext =new AIS_InteractiveContext (myViewer);
57
58   // 2D VIEWER: exploit V3d viewer for 2D visualization
59   my2DViewer = new V3d_Viewer (theGraphicDriver);
60   my2DViewer->SetCircularGridValues (0, 0, 10, 8, 0);
61   my2DViewer->SetRectangularGridValues (0, 0, 10, 10, 0);
62
63   //Set projection mode for 2D visualization
64   my2DViewer->SetDefaultViewProj (V3d_Zpos);
65
66   myInteractiveContext2D = new AIS_InteractiveContext (my2DViewer);
67
68   CFrameWnd* pFrame2d = ((CHLRApp*)AfxGetApp())->CreateView2D (this);
69   pFrame2d->ShowWindow (SW_SHOWNORMAL);
70   myCSelectionDialogIsCreated = false;
71 }
72
73 CHLRDoc::~CHLRDoc()
74 {
75   if (myCSelectionDialogIsCreated)
76   {
77     myCSelectionDialog->ShowWindow(SW_ERASE);
78     delete myCSelectionDialog;
79   }
80 }
81
82 void CHLRDoc::OnWindowNew2d()
83 {
84   ((CHLRApp*)AfxGetApp())->CreateView2D(this);
85 }
86
87 void CHLRDoc::OnWindowNew3d()
88 {
89   ((CHLRApp*)AfxGetApp())->CreateView3D(this);
90 }
91
92 //  nCmdShow could be :    ( default is SW_RESTORE )
93 // SW_HIDE   SW_SHOWNORMAL   SW_NORMAL   
94 // SW_SHOWMINIMIZED     SW_SHOWMAXIMIZED    
95 // SW_MAXIMIZE          SW_SHOWNOACTIVATE   
96 // SW_SHOW              SW_MINIMIZE         
97 // SW_SHOWMINNOACTIVE   SW_SHOWNA           
98 // SW_RESTORE           SW_SHOWDEFAULT      
99 // SW_MAX    
100
101 // use pViewClass = RUNTIME_CLASS( CHLRView3D ) for 3D Views
102 // use pViewClass = RUNTIME_CLASS( CHLRView2D ) for 2D Views
103
104 void CHLRDoc::ActivateFrame(CRuntimeClass* pViewClass,int nCmdShow)
105 {
106   POSITION position = GetFirstViewPosition();
107   while (position != (POSITION)NULL)
108   {
109     CView* pCurrentView = (CView*)GetNextView(position);
110     if(pCurrentView->IsKindOf(pViewClass) )
111     {
112       ASSERT_VALID(pCurrentView);
113       CFrameWnd* pParentFrm = pCurrentView->GetParentFrame();
114       ASSERT(pParentFrm != (CFrameWnd *)NULL);
115       // simply make the frame window visible
116       pParentFrm->ActivateFrame(nCmdShow);
117     }
118   }
119 }
120
121 void CHLRDoc::FitAll2DViews(Standard_Boolean UpdateViewer)
122 {
123   if (UpdateViewer)   my2DViewer->Update();
124   POSITION position = GetFirstViewPosition();
125   while (position != (POSITION)NULL)
126   {
127     CView* pCurrentView = (CView*)GetNextView(position);
128     if(pCurrentView->IsKindOf(RUNTIME_CLASS(OCC_2dView)) )
129     {
130       ASSERT_VALID(pCurrentView);
131       ((OCC_2dView*)pCurrentView)->GetV2dView()->FitAll();
132     }
133   }
134 }
135
136 /////////////////////////////////////////////////////////////////////////////
137 // CHLRDoc diagnostics
138
139 #ifdef _DEBUG
140 void CHLRDoc::AssertValid() const
141 {
142   CDocument::AssertValid();
143 }
144
145 void CHLRDoc::Dump(CDumpContext& dc) const
146 {
147   CDocument::Dump(dc);
148 }
149 #endif //_DEBUG
150
151 /////////////////////////////////////////////////////////////////////////////
152 // CHLRDoc commands
153 void CHLRDoc::OnBUTTONHLRDialog()
154 {
155   if (!myCSelectionDialogIsCreated)
156   {
157     myCSelectionDialog = new CSelectionDialog(this,AfxGetMainWnd());
158     myCSelectionDialog->Create(CSelectionDialog::IDD, AfxGetMainWnd());
159     myCSelectionDialogIsCreated = true;
160   }
161   myCSelectionDialog->ShowWindow(SW_RESTORE);
162   myCSelectionDialog->UpdateWindow();
163 }
164
165 void CHLRDoc::OnFileImportBrep() 
166 {   CImportExport::ReadBREP(myAISContext);
167         Fit();
168 }
169 void CHLRDoc::Fit()
170 {
171   POSITION position = GetFirstViewPosition();
172   while (position != (POSITION)NULL)
173   {
174     CView* pCurrentView = (CView*)GetNextView(position);
175     if(pCurrentView->IsKindOf(RUNTIME_CLASS(OCC_3dView)) )
176     {
177       ((OCC_3dView *) pCurrentView)->FitAll();
178     }
179   }
180 }
181
182 void CHLRDoc::OnObjectErase()
183 {
184   Standard_Boolean toUpdateDisplayable = Standard_False;
185   myAISContext->InitSelected();
186   while (myAISContext->MoreSelected())
187   {
188     if (myAISContext->SelectedInteractive()->Type() == AIS_KOI_Shape && myCSelectionDialogIsCreated)
189     {
190       myCSelectionDialog->DiplayableShape()->Remove (Handle(AIS_Shape)::DownCast (myAISContext->SelectedInteractive())->Shape());
191       toUpdateDisplayable = Standard_True;
192     }
193
194     myAISContext->Erase (myAISContext->SelectedInteractive(), Standard_True);
195     myAISContext->InitSelected();
196   }
197
198   myAISContext->ClearSelected();
199
200   if (toUpdateDisplayable)
201   {
202     // Update view in the HLR dialog if list of displayable shapes has been changed.
203     myCSelectionDialog->UpdateViews();
204   }
205 }