0029470: Samples - eliminate references to deprecated Local Context from MFC sample
[occt.git] / samples / mfc / standard / Common / AngleParamsVerticesPage.cpp
1 // AngleParamsVerticesPage.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "AngleParamsVerticesPage.h"
6 #include "DimensionDlg.h"
7
8 #include <AIS_InteractiveContext.hxx>
9 #include <AIS_LengthDimension.hxx>
10 #include <AIS_AngleDimension.hxx>
11 #include <BRep_Tool.hxx>
12 #include <GC_MakePlane.hxx>
13 #include <Prs3d_DimensionAspect.hxx>
14
15 // CAngleParamsVerticesPage dialog
16
17 IMPLEMENT_DYNAMIC(CAngleParamsVerticesPage, CDialog)
18
19 //=======================================================================
20 //function : CAngleParamsVerticesPage
21 //purpose  :
22 //=======================================================================
23
24 CAngleParamsVerticesPage::CAngleParamsVerticesPage (Handle(AIS_InteractiveContext) theAISContext,
25                                                     CWnd* pParent /*=NULL*/)
26 : CDialog(CAngleParamsVerticesPage::IDD, pParent)
27 {
28   myAISContext = theAISContext;
29 }
30
31 //=======================================================================
32 //function : ~CAngleParamsVerticesPage
33 //purpose  :
34 //=======================================================================
35
36 CAngleParamsVerticesPage::~CAngleParamsVerticesPage()
37 {
38 }
39
40 //=======================================================================
41 //function : DoDataExchange
42 //purpose  :
43 //=======================================================================
44
45 void CAngleParamsVerticesPage::DoDataExchange (CDataExchange* pDX)
46 {
47   CDialog::DoDataExchange (pDX);
48 }
49
50
51 BEGIN_MESSAGE_MAP(CAngleParamsVerticesPage, CDialog)
52   ON_BN_CLICKED(IDC_BUTTON1, &CAngleParamsVerticesPage::OnBnClickedVertex1Btn)
53   ON_BN_CLICKED(IDC_BUTTON3, &CAngleParamsVerticesPage::OnBnClickedVertex2Btn)
54   ON_BN_CLICKED(IDC_BUTTON4, &CAngleParamsVerticesPage::OnBnClickedVertex3Btn)
55 END_MESSAGE_MAP()
56
57
58 //=======================================================================
59 //function : OnBnClickedVertex1Btn
60 //purpose  :
61 //=======================================================================
62
63 void CAngleParamsVerticesPage::OnBnClickedVertex1Btn()
64 {
65   myAISContext->Activate (AIS_Shape::SelectionMode (TopAbs_VERTEX));
66
67   // Now it's ok, edge selection mode is activated
68   // Check if some vertex is selected
69   myAISContext->InitSelected();
70   if (!myAISContext->MoreSelected() ||
71        myAISContext->SelectedShape().ShapeType() != TopAbs_VERTEX)
72   {
73     AfxMessageBox (_T ("Choose the vertex and press the button again"),
74                        MB_ICONINFORMATION | MB_OK);
75     return;
76   }
77
78   myFirstVertex = TopoDS::Vertex (myAISContext->SelectedShape());
79   myAISContext->ClearSelected (Standard_True);
80 }
81
82 //=======================================================================
83 //function : OnBnClickedVertex2Btn
84 //purpose  :
85 //=======================================================================
86
87 void CAngleParamsVerticesPage::OnBnClickedVertex2Btn()
88 {
89   myAISContext->InitSelected();
90   if (!myAISContext->MoreSelected() ||
91        myAISContext->SelectedShape().ShapeType() != TopAbs_VERTEX)
92   {
93     AfxMessageBox ( _T("Choose the vertex and press the button again"), MB_ICONINFORMATION | MB_OK);
94     return;
95   }
96
97   mySecondVertex = TopoDS::Vertex (myAISContext->SelectedShape());
98
99   myAISContext->ClearSelected (Standard_True);
100 }
101
102 //=======================================================================
103 //function : OnBnClickedVertex3Btn
104 //purpose  :
105 //=======================================================================
106
107 void CAngleParamsVerticesPage::OnBnClickedVertex3Btn()
108 {
109   myAISContext->InitSelected();
110   if (!myAISContext->MoreSelected())
111   {
112     AfxMessageBox (_T ("Choose the vertex and press the button again"), MB_ICONINFORMATION | MB_OK);
113     return;
114   }
115
116   myThirdVertex = TopoDS::Vertex (myAISContext->SelectedShape());
117   myAISContext->ClearSelected (Standard_False);
118
119   //Build dimension here
120   TopoDS_Edge anEdge12 = BRepBuilderAPI_MakeEdge (myFirstVertex, mySecondVertex);
121   TopoDS_Edge anEdge23 = BRepBuilderAPI_MakeEdge (mySecondVertex, myThirdVertex);
122
123   CDimensionDlg *aDimDlg = (CDimensionDlg*)(GetParentOwner());
124
125   gp_Pnt aP1 = BRep_Tool::Pnt (myFirstVertex),
126          aP2 = BRep_Tool::Pnt (mySecondVertex),
127          aP3 = BRep_Tool::Pnt (myThirdVertex);
128   GC_MakePlane aPlaneBuilder (aP1,aP2,aP3);
129
130   Handle(Geom_Plane) aPlane = aPlaneBuilder.Value();
131   Handle(AIS_AngleDimension) anAngleDim = new AIS_AngleDimension (aP1,aP2,aP3);
132   Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
133   anAspect->MakeArrows3d (Standard_False);
134   anAspect->MakeText3d (aDimDlg->GetTextType());
135   anAspect->TextAspect()->SetHeight (aDimDlg->GetFontHeight());
136   anAspect->MakeTextShaded (aDimDlg->IsText3dShaded());
137   anAspect->SetCommonColor (aDimDlg->GetDimensionColor());
138   anAspect->MakeUnitsDisplayed (aDimDlg->IsUnitsDisplayed());
139   if (aDimDlg->IsUnitsDisplayed())
140   {
141     anAngleDim->SetDisplayUnits (aDimDlg->GetUnits());
142     if ((anAngleDim->GetDisplayUnits().IsEqual (TCollection_AsciiString ("deg"))))
143     {
144       // No units - for degree is special symbol that is enabled by default
145       anAspect->MakeUnitsDisplayed (Standard_False);
146     }
147     else // radians - no special symbol
148     {
149       anAngleDim->SetDisplaySpecialSymbol (AIS_DSS_No);
150     }
151   }
152   anAngleDim->SetDimensionAspect (anAspect);
153   myAISContext->Display (anAngleDim, Standard_True);
154   myAISContext->Activate (AIS_Shape::SelectionMode (TopAbs_VERTEX));
155 }