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