0023654: Problem with displaying vertices in OCC view after closing all OCC views...
[occt.git] / samples / mfc / standard / Common / OCC_3dBaseDoc.cpp
1 // OCC_3dBaseDoc.cpp: implementation of the OCC_3dBaseDoc class.
2 //
3 //////////////////////////////////////////////////////////////////////
4
5 #include <stdafx.h>
6
7 #include "OCC_3dBaseDoc.h"
8
9 #include "OCC_3dView.h"
10 #include "OCC_App.h"
11 #include <res\OCC_Resource.h>
12 #include "ImportExport/ImportExport.h"
13 #include "AISDialogs.h"
14 #include <AIS_ListOfInteractive.hxx>
15 #include <AIS_ListIteratorOfListOfInteractive.hxx>
16
17 BEGIN_MESSAGE_MAP(OCC_3dBaseDoc, OCC_BaseDoc)
18   //{{AFX_MSG_MAP(OCC_3dBaseDoc)
19   ON_COMMAND(ID_FILE_IMPORT_BREP, OnFileImportBrep)
20   ON_COMMAND(ID_FILE_EXPORT_BREP, OnFileExportBrep)
21   ON_COMMAND(ID_OBJECT_ERASE, OnObjectErase)
22   ON_UPDATE_COMMAND_UI(ID_OBJECT_ERASE, OnUpdateObjectErase)
23   ON_COMMAND(ID_OBJECT_COLOR, OnObjectColor)
24   ON_UPDATE_COMMAND_UI(ID_OBJECT_COLOR, OnUpdateObjectColor)
25   ON_COMMAND(ID_OBJECT_SHADING, OnObjectShading)
26   ON_UPDATE_COMMAND_UI(ID_OBJECT_SHADING, OnUpdateObjectShading)
27   ON_COMMAND(ID_OBJECT_WIREFRAME, OnObjectWireframe)
28   ON_UPDATE_COMMAND_UI(ID_OBJECT_WIREFRAME, OnUpdateObjectWireframe)
29   ON_COMMAND(ID_OBJECT_TRANSPARENCY, OnObjectTransparency)
30   ON_UPDATE_COMMAND_UI(ID_OBJECT_TRANSPARENCY, OnUpdateObjectTransparency)
31   ON_COMMAND(ID_OBJECT_MATERIAL, OnObjectMaterial)
32   ON_UPDATE_COMMAND_UI(ID_OBJECT_MATERIAL, OnUpdateObjectMaterial)
33   ON_COMMAND(ID_OBJECT_DISPLAYALL, OnObjectDisplayall)
34   ON_UPDATE_COMMAND_UI(ID_OBJECT_DISPLAYALL, OnUpdateObjectDisplayall)
35   ON_COMMAND(ID_OBJECT_REMOVE, OnObjectRemove)
36   ON_UPDATE_COMMAND_UI(ID_OBJECT_REMOVE, OnUpdateObjectRemove)
37   //}}AFX_MSG_MAP
38   ON_COMMAND_EX_RANGE(ID_OBJECT_MATERIAL_BRASS,ID_OBJECT_MATERIAL_DEFAULT, OnObjectMaterialRange)
39   ON_UPDATE_COMMAND_UI_RANGE(ID_OBJECT_MATERIAL_BRASS,ID_OBJECT_MATERIAL_DEFAULT, OnUpdateObjectMaterialRange)
40 END_MESSAGE_MAP()
41
42
43 //////////////////////////////////////////////////////////////////////
44 // Construction/Destruction
45 //////////////////////////////////////////////////////////////////////
46
47 OCC_3dBaseDoc::OCC_3dBaseDoc()
48 {
49   AfxInitRichEdit();
50
51   Handle(Graphic3d_GraphicDriver) aGraphicDriver = 
52     ((OCC_App*)AfxGetApp())->GetGraphicDriver();
53
54   myViewer = new V3d_Viewer (aGraphicDriver, Standard_ExtString("Visu3D") );
55   myViewer->SetDefaultLights();
56   myViewer->SetLightOn();
57   myAISContext = new AIS_InteractiveContext (myViewer);
58 }
59
60 //-----------------------------------------------------------------------------------------
61 //
62 //-----------------------------------------------------------------------------------------
63 void OCC_3dBaseDoc::DragEvent (const Standard_Integer theMouseX,
64                                const Standard_Integer theMouseY,
65                                const Standard_Integer theState,
66                                const Handle(V3d_View)& theView)
67 {
68   // TheState == -1  button down
69   // TheState ==  0  move
70   // TheState ==  1  button up
71
72   static Standard_Integer aStartDragX = 0;
73   static Standard_Integer aStartDragY = 0;
74
75   if (theState == -1)
76   {
77     // button down
78     aStartDragX = theMouseX;
79     aStartDragY = theMouseY;
80   }
81
82   if (theState == 1)
83   {
84     // button up
85     myAISContext->Select (aStartDragX, aStartDragY,
86                           theMouseX, theMouseY,
87                           theView);
88   }
89 }
90
91 //-----------------------------------------------------------------------------------------
92 //
93 //-----------------------------------------------------------------------------------------
94 void OCC_3dBaseDoc::InputEvent (const Standard_Integer theMouseX,
95                                 const Standard_Integer theMouseY,
96                                 const Handle(V3d_View)& theView)
97 {
98   myAISContext->MoveTo (theMouseX, theMouseY, theView);
99   myAISContext->Select();
100 }
101
102 //-----------------------------------------------------------------------------------------
103 //
104 //-----------------------------------------------------------------------------------------
105 void OCC_3dBaseDoc::MoveEvent (const Standard_Integer theMouseX,
106                                const Standard_Integer theMouseY,
107                                const Handle(V3d_View)& theView)
108 {
109   myAISContext->MoveTo (theMouseX, theMouseY, theView);
110 }
111
112 //-----------------------------------------------------------------------------------------
113 //
114 //-----------------------------------------------------------------------------------------
115 void OCC_3dBaseDoc::ShiftMoveEvent (const Standard_Integer theMouseX,
116                                     const Standard_Integer theMouseY,
117                                     const Handle(V3d_View)& theView)
118 {
119   myAISContext->MoveTo (theMouseX, theMouseY, theView);
120 }
121
122 //-----------------------------------------------------------------------------------------
123 //
124 //-----------------------------------------------------------------------------------------
125 void OCC_3dBaseDoc::ShiftDragEvent (const Standard_Integer theMouseX,
126                                     const Standard_Integer theMouseY,
127                                     const Standard_Integer theState,
128                                     const Handle(V3d_View)& theView)
129 {
130   // TheState == -1  button down
131   // TheState ==  0  move
132   // TheState ==  1  button up
133
134   static Standard_Integer aStartDragX = 0;
135   static Standard_Integer aStartDragY = 0;
136
137   if (theState == -1)
138   {
139     // button down
140     aStartDragX = theMouseX;
141     aStartDragY = theMouseY;
142   }
143
144   if (theState == 0)
145   {
146     // button up
147     myAISContext->ShiftSelect (aStartDragX, aStartDragY,
148                                theMouseX, theMouseY,
149                                theView);
150   }
151 }
152
153 //-----------------------------------------------------------------------------------------
154 //
155 //-----------------------------------------------------------------------------------------
156 void OCC_3dBaseDoc::ShiftInputEvent (const Standard_Integer theMouseX,
157                                      const Standard_Integer theMouseY,
158                                      const Handle(V3d_View)& theView)
159 {
160   myAISContext->ShiftSelect();
161 }
162
163 //-----------------------------------------------------------------------------------------
164 //
165 //-----------------------------------------------------------------------------------------
166 void  OCC_3dBaseDoc::Popup (const Standard_Integer theMouseX,
167                             const Standard_Integer theMouseY,
168                             const Handle(V3d_View)& theView)
169 {
170   Standard_Integer PopupMenuNumber=0;
171   myAISContext->InitCurrent();
172   if (myAISContext->MoreCurrent())
173     PopupMenuNumber=1;
174
175   CMenu menu;
176   VERIFY(menu.LoadMenu(IDR_Popup3D));
177   CMenu* pPopup = menu.GetSubMenu(PopupMenuNumber);
178
179   ASSERT(pPopup != NULL);
180    if (PopupMenuNumber == 1) // more than 1 object.
181   {
182     bool OneOrMoreInShading = false;
183         for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
184     if (myAISContext->IsDisplayed(myAISContext->Current(),1)) OneOrMoreInShading=true;
185         if(!OneOrMoreInShading)
186         pPopup->EnableMenuItem(5, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);
187    }
188
189   POINT winCoord = { theMouseX , theMouseY };
190   Handle(WNT_Window) aWNTWindow=
191   Handle(WNT_Window)::DownCast(theView->Window());
192   ClientToScreen ( (HWND)(aWNTWindow->HWindow()),&winCoord);
193   pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON , winCoord.x, winCoord.y , 
194                          AfxGetMainWnd());
195 }
196
197 void OCC_3dBaseDoc::Fit()
198 {
199         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
200         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
201         OCC_3dView *pView = (OCC_3dView *) pChild->GetActiveView();
202         pView->FitAll();
203 }
204
205 int OCC_3dBaseDoc::OnFileImportBrep_WithInitDir(LPCTSTR InitialDir) 
206 {   
207         if(CImportExport::ReadBREP(myAISContext, InitialDir) == 1)
208                 return 1;
209         Fit();
210         return 0;
211 }
212
213 void OCC_3dBaseDoc::OnFileImportBrep() 
214 {   
215         if(CImportExport::ReadBREP(myAISContext) == 1)
216                 return;
217         Fit();
218         
219 }
220
221 void OCC_3dBaseDoc::OnFileExportBrep() 
222 {   CImportExport::SaveBREP(myAISContext);}
223
224 void OCC_3dBaseDoc::OnObjectColor() 
225 {
226         Handle_AIS_InteractiveObject Current ;
227         COLORREF MSColor ;
228         Quantity_Color CSFColor ;
229
230         myAISContext->InitCurrent();
231     Current = myAISContext->Current();
232         if ( Current->HasColor () ) {
233       CSFColor = myAISContext->Color(myAISContext->Current());
234       MSColor = RGB (CSFColor.Red()*255.,CSFColor.Green()*255.,CSFColor.Blue()*255.);
235         }
236         else {
237           MSColor = RGB (255,255,255) ;
238         }
239         
240         CColorDialog dlgColor(MSColor);
241         if (dlgColor.DoModal() == IDOK)
242         {
243           MSColor = dlgColor.GetColor();
244           CSFColor = Quantity_Color (GetRValue(MSColor)/255.,GetGValue(MSColor)/255.,
245                                                          GetBValue(MSColor)/255.,Quantity_TOC_RGB); 
246           for (;myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
247           myAISContext->SetColor (myAISContext->Current(),CSFColor.Name());
248         }
249 }
250 void OCC_3dBaseDoc::OnUpdateObjectColor(CCmdUI* pCmdUI) 
251 {
252   bool OneOrMoreIsShadingOrWireframe = false;
253   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
254     if (myAISContext->IsDisplayed(myAISContext->Current(),0)
255                 ||myAISContext->IsDisplayed(myAISContext->Current(),1)) 
256                 OneOrMoreIsShadingOrWireframe=true;
257   pCmdUI->Enable (OneOrMoreIsShadingOrWireframe);       
258 }
259
260 void OCC_3dBaseDoc::OnObjectErase() 
261 {
262   for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->NextCurrent())
263         myAISContext->Erase(myAISContext->Current(),Standard_True);
264         myAISContext->ClearCurrents();
265 }
266 void OCC_3dBaseDoc::OnUpdateObjectErase(CCmdUI* pCmdUI) 
267 {
268   bool OneOrMoreIsDisplayed = false;
269   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
270     if (myAISContext->IsDisplayed(myAISContext->Current())) OneOrMoreIsDisplayed=true;
271   pCmdUI->Enable (OneOrMoreIsDisplayed);        
272 }
273
274 void OCC_3dBaseDoc::OnObjectWireframe() 
275 {
276   for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->NextCurrent())
277         myAISContext->SetDisplayMode(myAISContext->Current(),0);
278 }
279 void OCC_3dBaseDoc::OnUpdateObjectWireframe(CCmdUI* pCmdUI) 
280 {
281   bool OneOrMoreInShading = false;
282   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
283     if (myAISContext->IsDisplayed(myAISContext->Current(),1)) OneOrMoreInShading=true;
284         pCmdUI->Enable (OneOrMoreInShading);    
285 }
286
287 void OCC_3dBaseDoc::OnObjectShading() 
288 {
289   for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->NextCurrent())
290       myAISContext->SetDisplayMode(myAISContext->Current(),1);
291 }
292
293 void OCC_3dBaseDoc::OnUpdateObjectShading(CCmdUI* pCmdUI) 
294 {
295   bool OneOrMoreInWireframe = false;
296   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
297     if (myAISContext->IsDisplayed(myAISContext->Current(),0)) OneOrMoreInWireframe=true;
298         pCmdUI->Enable (OneOrMoreInWireframe);  
299 }
300
301 void OCC_3dBaseDoc::OnObjectMaterial() 
302 {
303   CDialogMaterial DialBox(myAISContext);
304   DialBox.DoModal();
305   CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
306   CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
307   OCC_3dView *pView = (OCC_3dView *) pChild->GetActiveView();
308 }
309
310 void OCC_3dBaseDoc::OnUpdateObjectMaterial(CCmdUI* pCmdUI) 
311 {
312   bool OneOrMoreInShading = false;
313   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
314     if (myAISContext->IsDisplayed(myAISContext->Current(),1)) OneOrMoreInShading=true;
315         pCmdUI->Enable (OneOrMoreInShading);    
316 }
317
318 BOOL OCC_3dBaseDoc::OnObjectMaterialRange(UINT nID) 
319 {
320   // the range ID_OBJECT_MATERIAL_BRASS to ID_OBJECT_MATERIAL_SILVER is
321   // continue with the same values as enumeration Type Of Material
322   Standard_Real aTransparency;
323
324   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ()){
325         aTransparency = myAISContext->Current()->Transparency();
326         myAISContext->SetMaterial (myAISContext->Current(),(Graphic3d_NameOfMaterial)(nID-ID_OBJECT_MATERIAL_BRASS));
327         myAISContext->SetTransparency (myAISContext->Current(),aTransparency);
328   }
329   return true;
330
331 }
332
333 void OCC_3dBaseDoc::OnUpdateObjectMaterialRange(CCmdUI* pCmdUI) 
334 {
335   bool OneOrMoreInShading = false;
336   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
337     if (myAISContext->IsDisplayed(myAISContext->Current(),1)) OneOrMoreInShading=true;
338         pCmdUI->Enable (OneOrMoreInShading);
339   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
340     if (myAISContext->Current()->Material() - (pCmdUI->m_nID - ID_OBJECT_MATERIAL_BRASS) == 0) 
341                 pCmdUI->SetCheck(1);    
342 }
343
344
345 void OCC_3dBaseDoc::OnObjectTransparency()
346 {
347         CDialogTransparency DialBox(myAISContext);
348         DialBox.DoModal();
349         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
350         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
351         OCC_3dView *pView = (OCC_3dView *) pChild->GetActiveView();
352 //      pView->Redraw();
353 }
354
355 void OCC_3dBaseDoc::OnUpdateObjectTransparency(CCmdUI* pCmdUI) 
356 {
357   bool OneOrMoreInShading = false;
358   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
359     if (myAISContext->IsDisplayed(myAISContext->Current(),1)) OneOrMoreInShading=true;
360         pCmdUI->Enable (OneOrMoreInShading);    
361 }
362
363
364 void OCC_3dBaseDoc::OnObjectDisplayall() 
365 {
366         myAISContext->DisplayAll();
367 }
368
369 void OCC_3dBaseDoc::OnUpdateObjectDisplayall(CCmdUI* pCmdUI) 
370 {
371         
372         AIS_ListOfInteractive aList;
373         myAISContext->ObjectsInside(aList,AIS_KOI_Shape);
374         AIS_ListIteratorOfListOfInteractive aLI;
375         Standard_Boolean IS_ANY_OBJECT_ERASED=FALSE;
376         for (aLI.Initialize(aList);aLI.More();aLI.Next()){
377                 if(!myAISContext->IsDisplayed(aLI.Value()))
378                 IS_ANY_OBJECT_ERASED=TRUE;
379         }
380         pCmdUI->Enable (IS_ANY_OBJECT_ERASED);
381
382 }
383
384 void OCC_3dBaseDoc::OnObjectRemove() 
385 {
386         for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->InitCurrent())
387         myAISContext->Remove(myAISContext->Current(),Standard_True);
388 }
389
390 void OCC_3dBaseDoc::OnUpdateObjectRemove(CCmdUI* pCmdUI) 
391 {
392   bool OneOrMoreIsDisplayed = false;
393   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
394     if (myAISContext->IsDisplayed(myAISContext->Current())) OneOrMoreIsDisplayed=true;
395   pCmdUI->Enable (OneOrMoreIsDisplayed);        
396 }
397
398 void OCC_3dBaseDoc::SetMaterial(Graphic3d_NameOfMaterial Material) 
399 {
400   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
401     myAISContext->SetMaterial (myAISContext->Current(),
402     (Graphic3d_NameOfMaterial)(Material));
403 }