0028105: HLR rendering crash in MFC sample
[occt.git] / samples / mfc / standard / 04_Viewer3d / src / Viewer3dDoc.cpp
CommitLineData
7fd59977 1// Viewer3dDoc.cpp : implementation of the CViewer3dDoc class
2//
3
4#include "stdafx.h"
5#include "Viewer3dApp.h"
6#include "BoxRadius.h"
7#include "DlgIsos.h"
8
9#include "Viewer3dDoc.h"
10#include "Viewer3dView.h"
11#include "OffsetDlg.h"
12#include "ResultDialog.h"
13#include "User_Cylinder.hxx"
14
15#include "AIS_Trihedron.hxx"
16#include "Geom_Axis2Placement.hxx"
17
18/////////////////////////////////////////////////////////////////////////////
19// CViewer3dDoc
20
21IMPLEMENT_DYNCREATE(CViewer3dDoc, CDocument)
22
47162471 23BEGIN_MESSAGE_MAP(CViewer3dDoc, OCC_3dDoc)
24 //{{AFX_MSG_MAP(CViewer3dDoc)
25 ON_COMMAND(ID_BOX, OnBox)
26 ON_COMMAND(ID_Cylinder, OnCylinder)
27 ON_COMMAND(ID_SPHERE, OnSphere)
28 ON_COMMAND(ID_ERASEALL, OnRemoveAll)
29 ON_COMMAND(ID_NBISOS, OnNbisos)
30 ON_COMMAND(ID_FACES, OnFaces)
31 ON_COMMAND(ID_EDGES, OnEdges)
32 ON_COMMAND(ID_VERTICES, OnVertices)
33 ON_COMMAND(ID_NEUTRAL, OnNeutral)
34 ON_COMMAND(ID_USERCYLINDER_CHANGEFACECOLOR, OnUsercylinderChangefacecolor)
35 ON_COMMAND(ID_FILLET3D, OnFillet3d)
36 ON_COMMAND(ID_CIRCLE, OnCircle)
37 ON_COMMAND(ID_LINE, OnLine)
38 ON_COMMAND(ID_OVERLAPPED_BOX, OnOverlappedBox)
39 ON_COMMAND(ID_OVERLAPPED_CYLINDER, OnOverlappedCylinder)
40 ON_COMMAND(ID_OVERLAPPED_SPHERE, OnOverlappedSphere)
41 ON_COMMAND(ID_POLYGON_OFFSETS, OnPolygonOffsets)
42 ON_UPDATE_COMMAND_UI(ID_POLYGON_OFFSETS, OnUpdatePolygonOffsets)
43 ON_UPDATE_COMMAND_UI(ID_Cylinder, OnUpdateCylinder)
44 ON_UPDATE_COMMAND_UI(ID_SPHERE, OnUpdateSphere)
45 ON_UPDATE_COMMAND_UI(ID_BOX, OnUpdateBox)
46 ON_UPDATE_COMMAND_UI(ID_OVERLAPPED_CYLINDER, OnUpdateOverlappedCylinder)
47 ON_UPDATE_COMMAND_UI(ID_OVERLAPPED_SPHERE, OnUpdateOverlappedSphere)
48 ON_UPDATE_COMMAND_UI(ID_OVERLAPPED_BOX, OnUpdateOverlappedBox)
49 ON_COMMAND(ID_OBJECT_REMOVE, OnObjectRemove)
50 ON_COMMAND(ID_OBJECT_ERASE, OnObjectErase)
51 ON_COMMAND(ID_OBJECT_DISPLAYALL, OnObjectDisplayall)
52 ON_COMMAND(ID_OBJECT_COLORED_MESH, OnObjectColoredMesh)
53 ON_UPDATE_COMMAND_UI(ID_OBJECT_COLORED_MESH, OnUpdateObjectColoredMesh)
54 ON_UPDATE_COMMAND_UI(ID_OBJECT_SHADING, OnUpdateObjectShading)
55 ON_UPDATE_COMMAND_UI(ID_OBJECT_WIREFRAME, OnUpdateObjectWireframe)
56 ON_COMMAND(ID_OPTIONS_TRIHEDRON_DYNAMIC_TRIHEDRON, OnOptionsTrihedronDynamicTrihedron)
57 ON_UPDATE_COMMAND_UI(ID_OPTIONS_TRIHEDRON_DYNAMIC_TRIHEDRON, OnUpdateOptionsTrihedronDynamicTrihedron)
58 ON_UPDATE_COMMAND_UI(ID_OPTIONS_TRIHEDRON_STATIC_TRIHEDRON, OnUpdateOptionsTrihedronStaticTrihedron)
59 ON_COMMAND(ID_OBJECT_MATERIAL, OnObjectMaterial)
60 ON_COMMAND(ID_TEXTURE_ON, OnTextureOn)
61 ON_COMMAND(ID_BUTTONNext, OnBUTTONNext)
62 ON_COMMAND(ID_BUTTONStart, OnBUTTONStart)
63 ON_COMMAND(ID_BUTTONRepeat, OnBUTTONRepeat)
64 ON_COMMAND(ID_BUTTONPrev, OnBUTTONPrev)
65 ON_COMMAND(ID_BUTTONEnd, OnBUTTONEnd)
66 ON_UPDATE_COMMAND_UI(ID_BUTTONNext, OnUpdateBUTTONNext)
67 ON_UPDATE_COMMAND_UI(ID_BUTTONPrev, OnUpdateBUTTONPrev)
68 ON_UPDATE_COMMAND_UI(ID_BUTTONStart, OnUpdateBUTTONStart)
69 ON_UPDATE_COMMAND_UI(ID_BUTTONRepeat, OnUpdateBUTTONRepeat)
70 ON_UPDATE_COMMAND_UI(ID_BUTTONEnd, OnUpdateBUTTONEnd)
71 ON_COMMAND(ID_FILE_NEW, OnFileNew)
72 ON_COMMAND(ID_DUMP_VIEW, OnDumpView)
73 //}}AFX_MSG_MAP
7fd59977 74END_MESSAGE_MAP()
75
76/////////////////////////////////////////////////////////////////////////////
77// CViewer3dDoc construction/destruction
78
79CViewer3dDoc::CViewer3dDoc()
a6eb515f 80:OCC_3dDoc()
7fd59977 81{
47162471 82 myCylinder.Nullify();
83 mySphere.Nullify();
84 myBox.Nullify();
85 myOverlappedCylinder.Nullify();
86 myOverlappedSphere.Nullify();
87 myOverlappedBox.Nullify();
88 myOffsetDlg = NULL;
89 myStaticTrihedronAxisIsDisplayed = FALSE;
90 myState = -1;
7fd59977 91
47162471 92 isTextureSampleStarted = FALSE;
7fd59977 93
47162471 94 myPresentation = OCCDemo_Presentation::Current;
95 myPresentation->SetDocument(this);
7fd59977 96
97
47162471 98 myAISContext->DefaultDrawer()->ShadingAspect()->SetColor(Quantity_NOC_CHARTREUSE1);
99 myAISContext->DefaultDrawer()->ShadingAspect()->SetMaterial(Graphic3d_NOM_SILVER);
7fd59977 100
47162471 101 strcpy_s(myDataDir, "Data");
102 strcpy_s(myLastPath, ".");
7fd59977 103
47162471 104 /*
105 Handle(AIS_Trihedron) myTrihedron;
106 Handle(Geom_Axis2Placement) myTrihedronAxis=new Geom_Axis2Placement(gp::XOY());
107 myTrihedron=new AIS_Trihedron(myTrihedronAxis);
108 myAISContext->Display(myTrihedron);
109 */
7fd59977 110}
111
112CViewer3dDoc::~CViewer3dDoc()
113{
114}
115
116/////////////////////////////////////////////////////////////////////////////
117// CViewer3dDoc diagnostics
118
119#ifdef _DEBUG
120void CViewer3dDoc::AssertValid() const
121{
47162471 122 CDocument::AssertValid();
7fd59977 123}
124
125void CViewer3dDoc::Dump(CDumpContext& dc) const
126{
47162471 127 CDocument::Dump(dc);
7fd59977 128}
129#endif //_DEBUG
130
131/////////////////////////////////////////////////////////////////////////////
132// CViewer3dDoc commands
133
576f8b11 134void CViewer3dDoc::UpdateResultMessageDlg (CString theTitle, const TCollection_AsciiString& theMessage)
7fd59977 135{
47162471 136 CString aText (theMessage.ToCString());
137 myCResultDialog.SetText (aText);
138 myCResultDialog.SetTitle(theTitle);
576f8b11 139}
7fd59977 140
576f8b11 141void CViewer3dDoc::UpdateResultMessageDlg(CString theTitle, CString theMessage)
142{
143 myCResultDialog.SetText (theMessage);
144 myCResultDialog.SetTitle(theTitle);
7fd59977 145}
146
47162471 147void CViewer3dDoc::OnBox()
7fd59977 148{
47162471 149 if(myBox.IsNull())
150 {
151 BRepPrimAPI_MakeBox B(gp_Pnt(0,-400,-100), 200.,150.,100.);
7fd59977 152
153
47162471 154 myBox = new AIS_Shape(B.Shape());
7fd59977 155
47162471 156 myAISContext->SetMaterial(myBox,Graphic3d_NOM_PEWTER);
157 myAISContext->SetDisplayMode(myBox,1);
7fd59977 158
47162471 159 myAISContext->Display(myBox);
160 TCollection_AsciiString Message("\
7fd59977 161BRepPrimAPI_MakeBox Box1(gp_Pnt(0,-400,-100), 200.,150.,100.);\n\
47162471 162 ");
7fd59977 163
47162471 164 UpdateResultMessageDlg("Create Box",Message);
165 }
7fd59977 166}
167
47162471 168void CViewer3dDoc::OnCylinder()
7fd59977 169{
47162471 170 if(myCylinder.IsNull())
171 {
172 gp_Ax2 CylAx2(gp_Pnt(0,0,-100), gp_Dir(gp_Vec(gp_Pnt(0,0,-100),gp_Pnt(0,0,100))));
173 myCylinder = new User_Cylinder(CylAx2, 80.,200.);
7fd59977 174
47162471 175 myAISContext->SetDisplayMode(myCylinder,1);
7fd59977 176
47162471 177 myAISContext->Display(myCylinder);
7fd59977 178
47162471 179 TCollection_AsciiString Message("\
7fd59977 180gp_Ax2 CylAx2(gp_Pnt(0,0,-100), gp_Dir(gp_Vec(gp_Pnt(0,0,-100),gp_Pnt(0,0,100))));\n\
181C = new User_Cylinder(CylAx2, 80.,200.);;\n\
47162471 182 ");
7fd59977 183
47162471 184 UpdateResultMessageDlg("Create Cylinder",Message);
185 }
7fd59977 186}
187
47162471 188void CViewer3dDoc::OnSphere()
7fd59977 189{
47162471 190 if(mySphere.IsNull())
191 {
192 BRepPrimAPI_MakeSphere S(gp_Pnt(0,300,0), 100.);
7fd59977 193
47162471 194 mySphere = new AIS_Shape(S.Shape());
7fd59977 195
47162471 196 myAISContext->SetMaterial(mySphere,Graphic3d_NOM_BRONZE);
197 myAISContext->SetDisplayMode(mySphere,1);
198
199 myAISContext->Display(mySphere);
200 TCollection_AsciiString Message("\
7fd59977 201BRepPrimAPI_MakeSphere S(gp_Pnt(0,300,0), 100.);\n\
47162471 202 ");
203 UpdateResultMessageDlg("Create Sphere",Message);
204 }
7fd59977 205}
206
47162471 207void CViewer3dDoc::OnRemoveAll()
7fd59977 208
209{
47162471 210 AIS_ListOfInteractive aListOfObjects;
211 myAISContext->ObjectsInside(aListOfObjects,AIS_KOI_Shape);
7fd59977 212
47162471 213 AIS_ListIteratorOfListOfInteractive aListIterator;
214 for(aListIterator.Initialize(aListOfObjects);aListIterator.More();aListIterator.Next()){
215 myAISContext->Remove(aListIterator.Value());
216 }
7fd59977 217
47162471 218 myAISContext->Remove(myCylinder);
7fd59977 219
220
47162471 221 myCylinder.Nullify();
222 mySphere.Nullify();
223 myBox.Nullify();
224 myOverlappedCylinder.Nullify();
225 myOverlappedSphere.Nullify();
226 myOverlappedBox.Nullify();
227 if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
228 myOffsetDlg->UpdateValues();
7fd59977 229}
230
231void CViewer3dDoc::OnOverlappedBox()
232{
47162471 233 OnBox();
234 if(myOverlappedBox.IsNull()){
235 BRepPrimAPI_MakeBox B(gp_Pnt(0,-400,-100), 200.,150.,100.);
7fd59977 236
47162471 237 BRepBuilderAPI_NurbsConvert aNurbsConvert(B.Shape());
238 TopoDS_Shape aBoxShape2 = aNurbsConvert.Shape();
7fd59977 239
240
47162471 241 myOverlappedBox = new AIS_Shape(aBoxShape2);
7fd59977 242
47162471 243 myAISContext->SetMaterial(myOverlappedBox,Graphic3d_NOM_GOLD);
244 myAISContext->SetDisplayMode(myOverlappedBox,1);
7fd59977 245
47162471 246 myAISContext->Display(myOverlappedBox);
247 TCollection_AsciiString Message("\
7fd59977 248BRepPrimAPI_MakeBox Box1(gp_Pnt(0,-400,-100), 200.,150.,100.);\n\
249\n\
250BRepPrimAPI_MakeBox Box2(gp_Pnt(0,-400,-100), 200.,150.,100.);\n\
251BRepBuilderAPI_NurbsConvert aNurbsConvert(Box2.Shape());\n\
47162471 252 ");
253 UpdateResultMessageDlg("Create overlapped boxes",Message);
254 if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
255 myOffsetDlg->UpdateValues();
256 }
7fd59977 257}
258
47162471 259void CViewer3dDoc::OnOverlappedCylinder()
7fd59977 260{
47162471 261 OnCylinder();
262
263 if(myOverlappedCylinder.IsNull()){
264 gp_Ax2 CylAx2(gp_Pnt(0,0,-100), gp_Dir(gp_Vec(gp_Pnt(0,0,-100),gp_Pnt(0,0,100))));
265 BRepPrimAPI_MakeCylinder C(CylAx2, 80.,200.);
7fd59977 266
47162471 267 BRepBuilderAPI_NurbsConvert aNurbsConvert(C.Shape());
268 TopoDS_Shape aCylShape2 = aNurbsConvert.Shape();
7fd59977 269
47162471 270 myOverlappedCylinder = new AIS_Shape(aCylShape2);
7fd59977 271
7fd59977 272
47162471 273 myAISContext->SetMaterial(myOverlappedCylinder,Graphic3d_NOM_GOLD);
274 myAISContext->SetDisplayMode(myOverlappedCylinder,1);
7fd59977 275
47162471 276 myAISContext->Display(myOverlappedCylinder);
7fd59977 277
47162471 278 TCollection_AsciiString Message("\
7fd59977 279gp_Ax2 CylAx2(gp_Pnt(0,0,-100), gp_Dir(gp_Vec(gp_Pnt(0,0,-100),gp_Pnt(0,0,100))));\n\
280Cylinder1 = new User_Cylinder(CylAx2, 80.,200.);\n\
281\n\
282BRepPrimAPI_MakeCylinder Cylinder2(CylAx2, 80.,200.);\n\
283BRepBuilderAPI_NurbsConvert aNurbsConvert(Cylinder2.Shape());\n\
47162471 284 ");
285 UpdateResultMessageDlg("Create overlapped cylinders",Message);
286 if (myOffsetDlg && myOffsetDlg->IsWindowVisible())
287 {
288 myOffsetDlg->UpdateValues();
289 }
290 }
7fd59977 291}
292
47162471 293void CViewer3dDoc::OnOverlappedSphere()
7fd59977 294{
47162471 295 OnSphere();
296 if(myOverlappedSphere.IsNull()){
297 BRepPrimAPI_MakeSphere S(gp_Pnt(0,300,0), 100.);
7fd59977 298
47162471 299 BRepBuilderAPI_NurbsConvert aNurbsConvert(S.Shape());
300 TopoDS_Shape aSphereShape2 = aNurbsConvert.Shape();
7fd59977 301
47162471 302 myOverlappedSphere = new AIS_Shape(aSphereShape2);
7fd59977 303
47162471 304 myAISContext->SetMaterial(myOverlappedSphere,Graphic3d_NOM_GOLD);
7fd59977 305
47162471 306 myAISContext->SetDisplayMode(myOverlappedSphere,1);
7fd59977 307
47162471 308 myAISContext->Display(myOverlappedSphere);
7fd59977 309
47162471 310 TCollection_AsciiString Message("\
7fd59977 311BRepPrimAPI_MakeSphere Sphere1(gp_Pnt(0,300,0), 100.);\n\
312\n\
313BRepPrimAPI_MakeSphere Sphere2(gp_Pnt(0,300,0), 100.);\n\
314BRepBuilderAPI_NurbsConvert aNurbsConvert(Sphere2.Shape());\n\
47162471 315 ");
316 UpdateResultMessageDlg("Create overlapped spheres",Message);
317 if (myOffsetDlg && myOffsetDlg->IsWindowVisible())
318 {
319 myOffsetDlg->UpdateValues();
320 }
321 }
7fd59977 322}
323
47162471 324void CViewer3dDoc::OnPolygonOffsets()
7fd59977 325{
47162471 326 myOffsetDlg = new COffsetDlg(this);
327 myOffsetDlg->Create(COffsetDlg::IDD,NULL);
328 myAISContext->UpdateCurrentViewer();
7fd59977 329}
330
331void CViewer3dDoc::OnUpdatePolygonOffsets(CCmdUI* pCmdUI)
332{
47162471 333 Standard_Integer aOffsetMode;
334 Standard_ShortReal aFactor;
335 Standard_ShortReal aCylUnits = 0;
336 Standard_ShortReal aSphereUnits = 0;
337 Standard_ShortReal aBoxUnits = 0;
7fd59977 338
47162471 339 BOOL IsOverlappedCylinderDisplayed = myAISContext->IsDisplayed(myOverlappedCylinder);
340 BOOL IsOverlappedSphereDisplayed = myAISContext->IsDisplayed(myOverlappedSphere);
341 BOOL IsOverlappedBoxDisplayed = myAISContext->IsDisplayed(myOverlappedBox);
342 BOOL IsAnyOverlappedObjectDisplayed =
343 IsOverlappedCylinderDisplayed || IsOverlappedSphereDisplayed || IsOverlappedBoxDisplayed;
7fd59977 344
47162471 345 if(!myOverlappedCylinder.IsNull() && IsOverlappedCylinderDisplayed){
346 myOverlappedCylinder->PolygonOffsets(aOffsetMode,aFactor,aCylUnits);
347 }
7fd59977 348
47162471 349 if(!myOverlappedSphere.IsNull() && IsOverlappedSphereDisplayed){
350 myOverlappedSphere->PolygonOffsets(aOffsetMode,aFactor,aSphereUnits);
351 }
352
353 if(!myOverlappedBox.IsNull() && IsOverlappedBoxDisplayed){
354 myOverlappedBox->PolygonOffsets(aOffsetMode,aFactor,aBoxUnits);
355 }
356
357 if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
358 pCmdUI->SetCheck(TRUE);
359 else
360 pCmdUI->SetCheck(FALSE);
361 if (IsAnyOverlappedObjectDisplayed)
362 if(myOffsetDlg)
363 pCmdUI->Enable(!myOffsetDlg->IsWindowVisible());
364 else
365 pCmdUI->Enable(TRUE);
366 else
367 pCmdUI->Enable(FALSE);
7fd59977 368}
369
47162471 370void CViewer3dDoc::OnUpdateCylinder(CCmdUI* pCmdUI)
7fd59977 371{
47162471 372 pCmdUI->Enable(myCylinder.IsNull());
7fd59977 373}
374
47162471 375void CViewer3dDoc::OnUpdateSphere(CCmdUI* pCmdUI)
7fd59977 376{
47162471 377 pCmdUI->Enable(mySphere.IsNull());
7fd59977 378}
379
47162471 380void CViewer3dDoc::OnUpdateBox(CCmdUI* pCmdUI)
7fd59977 381{
47162471 382 pCmdUI->Enable(myBox.IsNull());
7fd59977 383}
384
47162471 385void CViewer3dDoc::OnUpdateOverlappedCylinder(CCmdUI* pCmdUI)
7fd59977 386{
47162471 387 pCmdUI->Enable (myOverlappedCylinder.IsNull()
388 || myCylinder.IsNull());
389
7fd59977 390}
391
47162471 392void CViewer3dDoc::OnUpdateOverlappedSphere(CCmdUI* pCmdUI)
7fd59977 393{
47162471 394 pCmdUI->Enable (myOverlappedSphere.IsNull()
395 || mySphere.IsNull());
7fd59977 396}
397
47162471 398void CViewer3dDoc::OnUpdateOverlappedBox(CCmdUI* pCmdUI)
7fd59977 399{
47162471 400 pCmdUI->Enable (myOverlappedBox.IsNull()
401 || myBox.IsNull());
7fd59977 402}
403
47162471 404void CViewer3dDoc::OnObjectRemove()
7fd59977 405{
47162471 406 if(myAISContext->IsCurrent(myBox))
407 myBox.Nullify();
7fd59977 408
47162471 409 if(myAISContext->IsCurrent(myCylinder))
410 myCylinder.Nullify();
7fd59977 411
47162471 412 if(myAISContext->IsCurrent(mySphere))
413 mySphere.Nullify();
7fd59977 414
47162471 415 if(myAISContext->IsCurrent(myOverlappedBox))
416 myOverlappedBox.Nullify();
7fd59977 417
47162471 418 if(myAISContext->IsCurrent(myOverlappedCylinder))
419 myOverlappedCylinder.Nullify();
7fd59977 420
47162471 421 if(myAISContext->IsCurrent(myOverlappedSphere))
422 myOverlappedSphere.Nullify();
7fd59977 423
7fd59977 424
47162471 425 for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->InitCurrent())
426 myAISContext->Remove(myAISContext->Current(),Standard_True);
7fd59977 427
47162471 428 if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
429 myOffsetDlg->UpdateValues();
7fd59977 430}
431
47162471 432void CViewer3dDoc::OnObjectErase()
7fd59977 433
434{
47162471 435 OCC_3dBaseDoc::OnObjectErase();
436 if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
437 myOffsetDlg->UpdateValues();
7fd59977 438}
439
47162471 440void CViewer3dDoc::OnObjectDisplayall()
7fd59977 441
442{
47162471 443 OCC_3dBaseDoc::OnObjectDisplayall();
444 if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
445 myOffsetDlg->UpdateValues();
7fd59977 446}
447
92efcf78 448Handle(User_Cylinder) CViewer3dDoc::GetCylinder()
7fd59977 449{
47162471 450 return myCylinder;
7fd59977 451}
452
453
92efcf78 454Handle(AIS_Shape) CViewer3dDoc::GetSphere()
7fd59977 455{
47162471 456 return mySphere;
7fd59977 457}
458
92efcf78 459Handle(AIS_Shape) CViewer3dDoc::GetBox()
7fd59977 460{
47162471 461 return myBox;
7fd59977 462}
463
92efcf78 464Handle(AIS_Shape) CViewer3dDoc::GetOverlappedCylinder()
7fd59977 465{
47162471 466 return myOverlappedCylinder;
7fd59977 467}
468
469
92efcf78 470Handle(AIS_Shape) CViewer3dDoc::GetOverlappedSphere()
7fd59977 471{
47162471 472 return myOverlappedSphere;
7fd59977 473}
474
92efcf78 475Handle(AIS_Shape) CViewer3dDoc::GetOverlappedBox()
7fd59977 476{
47162471 477 return myOverlappedBox;
7fd59977 478}
479
47162471 480void CViewer3dDoc::DragEvent (const Standard_Integer theX,
481 const Standard_Integer theY,
482 const Standard_Integer theState,
483 const Handle(V3d_View)& theView)
7fd59977 484{
47162471 485 OCC_3dBaseDoc::DragEvent (theX, theY, theState, theView);
486 if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
487 myOffsetDlg->UpdateValues();
7fd59977 488}
489
490//-----------------------------------------------------------------------------------------
491//
492//-----------------------------------------------------------------------------------------
5c573e69 493void CViewer3dDoc::InputEvent(const Standard_Integer /*x*/,
494 const Standard_Integer /*y*/,
495 const Handle(V3d_View)& /*aView*/ )
7fd59977 496{
5c1f974e 497 if (myOffsetDlg && myOffsetDlg->IsWindowVisible())
498 myOffsetDlg->UpdateValues();
7fd59977 499
5c1f974e 500 Quantity_Color CSFColor;
501 COLORREF MSColor;
502 myAISContext->Select();
7fd59977 503
5c1f974e 504 // Change the color of a selected face in a user cylinder
505 if (myState == FACE_COLOR)
506 {
507 myAISContext->InitSelected();
508 if (myAISContext->MoreSelected())
509 {
92efcf78 510 Handle(AIS_InteractiveObject) Current = myAISContext->SelectedInteractive();
5c1f974e 511 if (Current->HasColor())
512 {
513 CSFColor = myAISContext->Color (Current);
514 MSColor = RGB (CSFColor.Red()*255.0, CSFColor.Green()*255.0, CSFColor.Blue()*255.0);
515 }
516 else
517 {
518 MSColor = RGB (255, 255, 255);
519 }
520
521 CColorDialog dlgColor(MSColor);
522 if (dlgColor.DoModal() == IDOK)
523 {
524 MSColor = dlgColor.GetColor();
525 CSFColor = Quantity_Color (GetRValue (MSColor)/255.0,
526 GetGValue (MSColor)/255.0,
527 GetBValue (MSColor)/255.0,
528 Quantity_TOC_RGB);
529
4208cf18 530 Handle(AIS_InteractiveObject) aSelectedObject = myAISContext->SelectedInteractive();
531 Handle(User_Cylinder)::DownCast (aSelectedObject)->SetColor (CSFColor.Name());
532
533 myAISContext->Redisplay (aSelectedObject);
5c1f974e 534 myState = -1;
535 myAISContext->CloseLocalContext();
536 }
537 }
538
5c573e69 539 myCResultDialog.SetTitle("Change face color");
540 myCResultDialog.SetText(" TopoDS_Shape S = myAISContext->SelectedShape(); \n"
5c1f974e 541 " \n"
542 " Handle(Geom_Surface) Surface = BRep_Tool::Surface(TopoDS::Face(S));"
543 " if (Surface->IsKind(STANDARD_TYPE(Geom_Plane))) \n"
544 " Handle(User_Cylinder)::DownCast(myAISContext->Current())->SetPlanarFaceColor(CSFColor.Name()); \n"
545 " else \n"
546 " Handle(User_Cylinder)::DownCast(myAISContext->Current())->SetCylindricalFaceColor(CSFColor.Name()); \n"
547 " \n"
548 " myAISContext->Redisplay(myAISContext->Current()); \n"
549 " \n"
550 " myAISContext->CloseLocalContext(); \n"
551 " \n"
552 " \n"
553 " NOTE: a User_Cylinder is an object defined by the user. \n"
554 " The User_Cylinder class inherit from the AIS_InteractiveObject \n"
555 " Cascade class, it's use is the same as an AIS_InteractiveObject. \n"
556 " Methods SetPlanarFaceColor and SetCylindricalFaceColor are also \n"
557 " defined in the User_Cylinder class. \n"
558 " \n");
576f8b11 559 SetTitle (L"Change face color");
5c1f974e 560 }
561}
7fd59977 562
563//-----------------------------------------------------------------------------------------
564//
565//-----------------------------------------------------------------------------------------
47162471 566void CViewer3dDoc::ShiftDragEvent (const Standard_Integer theX,
567 const Standard_Integer theY,
568 const Standard_Integer theState,
569 const Handle(V3d_View)& theView)
7fd59977 570{
47162471 571 OCC_3dBaseDoc::ShiftDragEvent(theX, theY, theState, theView);
5c1f974e 572 if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
573 myOffsetDlg->UpdateValues();
7fd59977 574}
575
576
577//-----------------------------------------------------------------------------------------
578//
579//-----------------------------------------------------------------------------------------
47162471 580void CViewer3dDoc::ShiftInputEvent (const Standard_Integer theX,
581 const Standard_Integer theY,
582 const Handle(V3d_View)& theView)
7fd59977 583{
47162471 584 OCC_3dBaseDoc::ShiftInputEvent (theX, theY, theView);
5c1f974e 585 if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
586 myOffsetDlg->UpdateValues();
7fd59977 587}
588
47162471 589void CViewer3dDoc::OnObjectColoredMesh()
7fd59977 590{
591 for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->NextCurrent())
5c1f974e 592 if (myAISContext->Current()->IsKind(STANDARD_TYPE(User_Cylinder)))
593 {
594 myAISContext->ClearPrs(myAISContext->Current(),6,Standard_False);
595 myAISContext->RecomputePrsOnly(myAISContext->Current(),Standard_False);
596 myAISContext->SetDisplayMode(myAISContext->Current(),6);
597 }
7fd59977 598}
599
47162471 600void CViewer3dDoc::OnUpdateObjectColoredMesh(CCmdUI* pCmdUI)
7fd59977 601{
602 bool CylinderIsCurrentAndDisplayed = false;
603 for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
47162471 604 if(myAISContext->Current()->IsKind(STANDARD_TYPE(User_Cylinder)))
605 CylinderIsCurrentAndDisplayed=true;
606 pCmdUI->Enable (CylinderIsCurrentAndDisplayed);
7fd59977 607}
608
47162471 609void CViewer3dDoc::OnUpdateObjectWireframe(CCmdUI* pCmdUI)
7fd59977 610{
611 bool OneOrMoreInShadingOrColoredMesh = false;
612 for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
613 if (myAISContext->IsDisplayed(myAISContext->Current(),1) || myAISContext->IsDisplayed(myAISContext->Current(),6))
47162471 614 OneOrMoreInShadingOrColoredMesh=true;
615 pCmdUI->Enable (OneOrMoreInShadingOrColoredMesh);
7fd59977 616}
617
618
47162471 619void CViewer3dDoc::OnUpdateObjectShading(CCmdUI* pCmdUI)
7fd59977 620{
621 bool OneOrMoreInWireframeOrColoredMesh = false;
622 for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
623 if (myAISContext->IsDisplayed(myAISContext->Current(),0) || myAISContext->IsDisplayed(myAISContext->Current(),6))
47162471 624 OneOrMoreInWireframeOrColoredMesh=true;
625 pCmdUI->Enable (OneOrMoreInWireframeOrColoredMesh);
7fd59977 626}
627
47162471 628void CViewer3dDoc::OnOptionsTrihedronDynamicTrihedron()
7fd59977 629{
47162471 630 if (myAISContext -> IsDisplayed(myTrihedron))
631 {
632 myAISContext->Remove(myTrihedron);
633 }
634 else
635 {
636 Handle(Geom_Axis2Placement) myTrihedronAxis=new Geom_Axis2Placement(gp::XOY());
637 myTrihedron=new AIS_Trihedron(myTrihedronAxis);
638 myAISContext->SetTrihedronSize(200, Standard_True);
639 myAISContext->Display(myTrihedron);
640 }
7fd59977 641}
642
47162471 643void CViewer3dDoc::OnUpdateOptionsTrihedronDynamicTrihedron(CCmdUI* pCmdUI)
7fd59977 644{
47162471 645 if (myAISContext->IsDisplayed(myTrihedron))
646 pCmdUI->SetCheck(1);
647 else
648 pCmdUI->SetCheck(0);
7fd59977 649}
650
651void CViewer3dDoc::SetMyStaticTrihedronAxisIsDisplayed(BOOL IsDisplayed)
652{
47162471 653 myStaticTrihedronAxisIsDisplayed = IsDisplayed;
7fd59977 654}
655
656void CViewer3dDoc::OnUpdateOptionsTrihedronStaticTrihedron(CCmdUI* pCmdUI)
657{
47162471 658 if (myStaticTrihedronAxisIsDisplayed)
659 pCmdUI->SetCheck(1);
660 else
661 pCmdUI->SetCheck(0);
662
7fd59977 663}
664
a6eb515f 665void CViewer3dDoc::Popup (const Standard_Integer x,
666 const Standard_Integer y ,
667 const Handle(V3d_View)& aView)
7fd59977 668{
a6eb515f 669 myPopupMenuNumber=0;
670 // Specified check for context menu number to call
671 myAISContext->InitCurrent();
672 if (myAISContext->MoreCurrent())
7fd59977 673 {
a6eb515f 674 if (myAISContext->Current()->IsKind(STANDARD_TYPE(User_Cylinder)))
675 {
676 myPopupMenuNumber = 2;
677 //return;
678 }
679 }
680 OCC_3dBaseDoc::Popup(x,y, aView);
7fd59977 681}
5c1f974e 682
7fd59977 683//Set faces selection mode
684void CViewer3dDoc::OnFaces()
685{
5c1f974e 686 myAISContext->CloseAllContexts();
687 myAISContext->OpenLocalContext();
688 myAISContext->ActivateStandardMode (TopAbs_FACE);
7fd59977 689
5c573e69 690 myCResultDialog.SetTitle("Standard mode: TopAbs_FACE");
691 myCResultDialog.SetText(" myAISContext->OpenLocalContext(); \n"
5c1f974e 692 " \n"
693 " myAISContext->ActivateStandardMode(TopAbs_FACE); \n"
694 " \n");
576f8b11 695 SetTitle (L"Standard mode: TopAbs_FACE");
7fd59977 696}
697
698//Set edges selection mode
699void CViewer3dDoc::OnEdges()
700{
5c1f974e 701 myAISContext->CloseAllContexts();
702 myAISContext->OpenLocalContext();
703 myAISContext->ActivateStandardMode(TopAbs_EDGE);
7fd59977 704
5c573e69 705 myCResultDialog.SetTitle("Standard mode: TopAbs_EDGE");
706 myCResultDialog.SetText(" myAISContext->OpenLocalContext(); \n"
5c1f974e 707 " \n"
708 " myAISContext->ActivateStandardMode(TopAbs_EDGE); \n"
709 " \n");
576f8b11 710 SetTitle (L"Standard mode: TopAbs_EDGE");
7fd59977 711}
712
713// Set vertices selection mode
714void CViewer3dDoc::OnVertices()
715{
5c1f974e 716 myAISContext->CloseAllContexts();
717 myAISContext->OpenLocalContext();
718 myAISContext->ActivateStandardMode (TopAbs_VERTEX);
7fd59977 719
5c573e69 720 myCResultDialog.SetTitle("Standard mode: TopAbs_VERTEX");
721 myCResultDialog.SetText(" myAISContext->OpenLocalContext(); \n"
5c1f974e 722 " \n"
723 " myAISContext->ActivateStandardMode(TopAbs_VERTEX); \n"
724 " \n");
576f8b11 725 SetTitle (L"Standard mode: TopAbs_VERTEX");
7fd59977 726}
727
728//Neutral selection mode
729void CViewer3dDoc::OnNeutral()
730{
5c1f974e 731 myAISContext->CloseAllContexts();
7fd59977 732
5c573e69 733 myCResultDialog.SetTitle("Standard mode: Neutral");
734 myCResultDialog.SetText(" myAISContext->CloseAllContexts(); \n"
5c1f974e 735 " \n");
576f8b11 736 SetTitle (L"Standard mode: Neutral");
7fd59977 737}
738
739// Change the color of faces on a user cylinder
740void CViewer3dDoc::OnUsercylinderChangefacecolor()
741{
5c1f974e 742 myAISContext->OpenLocalContext();
743 myAISContext->Activate(myAISContext->Current(),4);
744 myState = FACE_COLOR;
745 // see the following of treatment in inputevent
7fd59977 746}
747
748// Make 3d fillets on solids
749// It is necessary to activate the edges selection mode and select edges on an object
750// before running this function
751void CViewer3dDoc::OnFillet3d()
752{
5c1f974e 753 if (!myAISContext->HasOpenedContext())
754 {
576f8b11 755 AfxMessageBox (L"It is necessary to activate the edges selection mode\n"
756 L"and select edges on an object before \nrunning this function");
5c1f974e 757 return;
758 }
759
760 myAISContext->InitSelected();
761 if (myAISContext->MoreSelected())
762 {
92efcf78 763 Handle(AIS_Shape) aSelInteractive (Handle(AIS_Shape)::DownCast (myAISContext->SelectedInteractive()));
47162471 764 if (aSelInteractive.IsNull())
5c1f974e 765 {
576f8b11 766 AfxMessageBox (L"It is necessary to activate the edges selection mode\n"
767 L"and select edges on an object before \nrunning this function");
5c1f974e 768 return;
769 }
770
47162471 771 BRepFilletAPI_MakeFillet aFillet (aSelInteractive->Shape());
5c1f974e 772
773 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
7fd59977 774 {
47162471 775 const TopoDS_Shape& aSelShape = myAISContext->SelectedShape();
5c1f974e 776 if (aSelShape.ShapeType() != TopAbs_EDGE)
777 {
576f8b11 778 AfxMessageBox (L"It is necessary to activate the edges selection mode\n"
779 L"and select edges on an object before \nrunning this function");
7fd59977 780 return;
5c1f974e 781 }
7fd59977 782 }
7fd59977 783
47162471 784 BoxRadius aDialog (NULL,10.);
785 if (aDialog.DoModal() == IDOK)
5c1f974e 786 {
787 for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
788 {
47162471 789 // Selected shape has its own location, and sub-shapes in the solid shape for fillet building
790 // don't have own locations. Fillet builder needs to know that input edge is just the same one as
791 // some sub-shape in our target solid shape, so no location is to be in input edge for fillet builder.
792 TopoDS_Shape aSelShape = myAISContext->SelectedShape().Located (TopLoc_Location());
793 aFillet.Add (aDialog.m_radius, TopoDS::Edge (aSelShape) );
5c1f974e 794 }
795 }
796 else
797 {
798 return;
799 }
800
801 TopoDS_Shape aNewShape;
802 try
803 {
804 aNewShape = aFillet.Shape();
805 }
806 catch (Standard_Failure)
807 {
576f8b11 808 AfxMessageBox (L"Error During Fillet computation");
5c1f974e 809 return;
810 }
811
47162471 812 aSelInteractive ->Set (aNewShape);
813 myAISContext->Redisplay (aSelInteractive);
5c1f974e 814 }
815
5c573e69 816 myCResultDialog.SetTitle("Make a fillet");
817 myCResultDialog.SetText(" Handle(AIS_Shape) S = Handle(AIS_Shape)::DownCast(myAISContext->Interactive()); \n"
5c1f974e 818 " \n"
819 " BRepAPI_MakeFillet aFillet(S->Shape()); \n"
820 " \n"
821 " TopoDS_Edge anEdge=TopoDS::Edge(myAISContext->SelectedShape()); \n"
822 " \n"
823 " aFillet.Add(dlg.m_radius,anEdge); \n"
824 " \n"
825 " TopoDS_Shape aNewShape = aFillet.Shape(); \n"
826 " \n"
827 " S->Set(aNewShape); \n"
828 " \n"
829 " myAISContext->Redisplay(S); \n"
830 " \n");
576f8b11 831 SetTitle (L"Make a fillet");
7fd59977 832}
5c1f974e 833
7fd59977 834// Create and display a circle with standard tools
835void CViewer3dDoc::OnCircle()
836{
5c1f974e 837 gp_Ax2 anAx2 (gp_Pnt (0., 0., 0.), gp_Dir(0., 0., -1.));
838 Handle(Geom_Circle) aGeomCircle = new Geom_Circle (anAx2, 300);
7fd59977 839
5c1f974e 840 // the lines above substitute
841 // GC_MakeCircle C(gp_Pnt(-100.,-300.,0.),gp_Pnt(-50.,-200.,0.),gp_Pnt(-10.,-250.,0.));
842 // Handle(AIS_Circle) anAISCirc = new AIS_Circle(C.Value());
843
844 Handle(AIS_Circle) anAISCirc = new AIS_Circle(aGeomCircle);
845 myAISContext->Display (anAISCirc);
7fd59977 846
5c573e69 847 myCResultDialog.SetTitle("Create a circle");
848 myCResultDialog.SetText(" GC_MakeCircle C(gp_Pnt(-100.,-300.,0.),gp_Pnt(-50.,-200.,0.),gp_Pnt(-10.,-250.,0.)); \n"
5c1f974e 849 " \n"
850 " Handle(AIS_Circle) anAISCirc = new AIS_Circle(C.Value()); \n"
851 " \n"
852 " myAISContext->Display(anAISCirc); \n"
853 " \n");
576f8b11 854 SetTitle (L"Create a circle");
7fd59977 855}
856
857void CViewer3dDoc::OnLine()
858{
5c1f974e 859 // TODO: Add your command handler code here
860 gp_Lin aGpLin (gp_Pnt (0., 0., 0.), gp_Dir(1., 0., 0.));
861 Handle(Geom_Line) aGeomLin = new Geom_Line (aGpLin);
862 Handle(AIS_Line) anAISLine = new AIS_Line (aGeomLin);
863 myAISContext->Display (anAISLine);
7fd59977 864
5c573e69 865 myCResultDialog.SetTitle("Create a line");
866 myCResultDialog.SetText(" gp_Lin L(gp_Pnt(0.,0.,0.),gp_Dir(1.,0.,0.)); \n"
5c1f974e 867 " \n"
868 " Handle(Geom_Line) aLine = new Geom_Line(L); \n"
869 " \n"
870 " Handle(AIS_Line) anAISLine = new AIS_Line(aLine); \n"
871 " \n"
872 " myAISContext->Display(anAISLine); \n"
873 " \n");
576f8b11 874 SetTitle (L"Create a line");
7fd59977 875}
876
877void CViewer3dDoc::OnNbisos()
878{
5c1f974e 879 int aNumU = myAISContext->DefaultDrawer()->UIsoAspect()->Number();
880 int aNumV = myAISContext->DefaultDrawer()->VIsoAspect()->Number();
7fd59977 881
5c1f974e 882 DlgIsos aDlg (NULL, aNumU, aNumV);
7fd59977 883
5c1f974e 884 if (aDlg.DoModal() == IDOK)
885 {
886 myAISContext->DefaultDrawer()->UIsoAspect()->SetNumber (aDlg.m_isou);
887 myAISContext->DefaultDrawer()->VIsoAspect()->SetNumber (aDlg.m_isov);
888
5c573e69 889 myCResultDialog.SetTitle("Iso Aspect");
890 myCResultDialog.SetText(" myAISContext->DefaultDrawer()->UIsoAspect()->SetNumber(dlg.m_isou); \n"
5c1f974e 891 " \n"
892 " myAISContext->DefaultDrawer()->VIsoAspect()->SetNumber(dlg.m_isov); \n"
893 " \n");
576f8b11 894 SetTitle (L"Iso Aspect");
5c1f974e 895 }
7fd59977 896}
897
898BOOL CViewer3dDoc::OnNewDocument()
899{
5c1f974e 900 if (!CDocument::OnNewDocument())
901 return FALSE;
7fd59977 902
5c1f974e 903 // TODO: add reinitialization code here
904 // (SDI documents will reuse this document)
7fd59977 905 SetTitle(myPresentation->GetName());
906
eb4320f2 907 myAISContext->EraseAll();
7fd59977 908 myAISContext->SetDisplayMode(AIS_Shaded);
909
910 POSITION pos = GetFirstViewPosition();
911 while (pos != NULL)
912 {
913 CViewer3dView* pView = (CViewer3dView*) GetNextView(pos);
914 pView->Reset();
915 }
916
5c1f974e 917 return TRUE;
7fd59977 918}
919
920void CViewer3dDoc::OnFileNew()
921{
922 OnNewDocument();
923}
924
925void CViewer3dDoc::InitViewButtons()
926{
927 POSITION pos = GetFirstViewPosition();
928 while (pos != NULL)
929 {
930 CViewer3dView* pView = (CViewer3dView*) GetNextView(pos);
931 pView->InitButtons();
932 }
933}
934
935void CViewer3dDoc::OnTextureOn()
936{
937 isTextureSampleStarted = TRUE;
938 Start();
939}
940
941void CViewer3dDoc::DoSample()
942{
943 InitViewButtons();
944
945 HCURSOR hOldCursor = ::GetCursor();
946 HCURSOR hNewCursor = AfxGetApp()->LoadStandardCursor(IDC_APPSTARTING);
947
948 SetCursor(hNewCursor);
949 {
950 try
951 {
952 myPresentation->DoSample();
953 }
954 catch (Standard_Failure)
955 {
956 Standard_SStream aSStream;
957 aSStream << "An exception was caught: " << Standard_Failure::Caught() << ends;
576f8b11 958 CString aMsg = aSStream.str().c_str();
7fd59977 959 AfxMessageBox (aMsg);
960 }
961 }
962 SetCursor(hOldCursor);
963}
964
965void CViewer3dDoc::OnBUTTONStart()
966{
eb4320f2 967 myAISContext->EraseAll();
7fd59977 968 myPresentation->FirstSample();
969 DoSample();
970}
971
972void CViewer3dDoc::OnBUTTONEnd()
973{
eb4320f2 974 myAISContext->EraseAll();
7fd59977 975 myPresentation->LastSample();
976 DoSample();
977}
978
979void CViewer3dDoc::OnBUTTONRepeat()
980{
981 DoSample();
982}
983
984void CViewer3dDoc::OnBUTTONNext()
985{
986 if (!myPresentation->AtLastSample())
987 {
988 myPresentation->NextSample();
989 DoSample();
990 }
991}
992
993void CViewer3dDoc::OnBUTTONPrev()
994{
995 if (!myPresentation->AtFirstSample())
996 {
997 myPresentation->PrevSample();
998 DoSample();
999 }
1000}
1001
1002void CViewer3dDoc::OnUpdateBUTTONNext(CCmdUI* pCmdUI)
1003{
1004 if ( isTextureSampleStarted )
1005 pCmdUI->Enable (!myPresentation->AtLastSample());
1006 else
1007 pCmdUI->Enable (FALSE);
1008}
1009
1010void CViewer3dDoc::OnUpdateBUTTONPrev(CCmdUI* pCmdUI)
1011{
1012 if ( isTextureSampleStarted )
1013 pCmdUI->Enable (!myPresentation->AtFirstSample());
1014 else
1015 pCmdUI->Enable (FALSE);
1016}
1017
1018void CViewer3dDoc::OnUpdateBUTTONStart(CCmdUI* pCmdUI)
1019{
1020 pCmdUI->Enable (isTextureSampleStarted);
1021}
1022
1023void CViewer3dDoc::OnUpdateBUTTONRepeat(CCmdUI* pCmdUI)
1024{
1025 pCmdUI->Enable (isTextureSampleStarted);
1026}
1027
1028void CViewer3dDoc::OnUpdateBUTTONEnd(CCmdUI* pCmdUI)
1029{
1030 pCmdUI->Enable (isTextureSampleStarted);
1031}
1032
12c76bee 1033void CViewer3dDoc::OnDumpView()
1034{
5c1f974e 1035 for (POSITION aPos = GetFirstViewPosition(); aPos != NULL;)
1036 {
1037 CViewer3dView* pView = (CViewer3dView* )GetNextView (aPos);
1038 pView->UpdateWindow();
7fd59977 1039 }
12c76bee 1040
5c1f974e 1041 myViewer->InitActiveViews();
1042 Handle(V3d_View) aView = myViewer->ActiveView();
12c76bee 1043 ExportView (aView);
7fd59977 1044}
1045
1046void CViewer3dDoc::Start()
1047{
1048 myPresentation->Init();
1049 OnBUTTONStart();
1050}
1051
1052void CViewer3dDoc::Fit()
1053{
1054 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
1055 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
1056 CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
1057 pView->FitAll();
1058}