0028162: Draw Harness - eliminate usage of deprecated Local Context
[occt.git] / samples / mfc / standard / Common / RadiusParamsPage.cpp
1
2 #include "stdafx.h"
3 #include "RadiusParamsPage.h"
4 #include "DimensionDlg.h"
5
6 #include <AIS_InteractiveContext.hxx>
7 #include <AIS_LocalContext.hxx>
8 #include <AIS_RadiusDimension.hxx>
9 #include <AIS_DiameterDimension.hxx>
10 #include <ElCLib.hxx>
11 #include <TopoDS_Shape.hxx>
12
13 IMPLEMENT_DYNAMIC(CRadiusParamsPage, CDialog)
14
15 //=======================================================================
16 //function : CRadiusParamsPage
17 //purpose  :
18 //=======================================================================
19
20 CRadiusParamsPage::CRadiusParamsPage (const Handle(AIS_InteractiveContext)& theAISContext,
21                                       const Standard_Boolean isDiameterDimension /* =Standard_False*/,
22                                       CWnd* pParent /*=NULL*/)
23  : CDialog (CRadiusParamsPage::IDD, pParent)
24 {
25   myAISContext = theAISContext;
26   myIsDiameterDimension = isDiameterDimension;
27 }
28
29 //=======================================================================
30 //function : ~CRadiusParamsPage
31 //purpose  :
32 //=======================================================================
33
34 CRadiusParamsPage::~CRadiusParamsPage()
35 {
36 }
37
38 //=======================================================================
39 //function : DoDataExchange
40 //purpose  :
41 //=======================================================================
42
43 void CRadiusParamsPage::DoDataExchange(CDataExchange* pDX)
44 {
45   CDialog::DoDataExchange(pDX);
46 }
47
48
49 BEGIN_MESSAGE_MAP(CRadiusParamsPage, CDialog)
50   ON_BN_CLICKED(IDC_BUTTON1, &CRadiusParamsPage::OnBnClickedObjectBtn)
51 END_MESSAGE_MAP()
52
53 //=======================================================================
54 //function : OnBnClickedObjectBtn
55 //purpose  :
56 //=======================================================================
57
58 void CRadiusParamsPage::OnBnClickedObjectBtn()
59 {
60   //Build dimension here
61   myAISContext->InitSelected();
62   if (!myAISContext->MoreSelected() ||
63        myAISContext->SelectedShape().ShapeType() != TopAbs_EDGE)
64   {
65     AfxMessageBox (_T ("Choose the edge and press the button again"), MB_ICONINFORMATION | MB_OK);
66     return;
67   }
68
69   gp_Circ aCircle;
70   Standard_Boolean isAttachPoint = Standard_False;
71   Standard_Real aFirstPar = 0, aLastPar = 0;
72
73   TopoDS_Shape aSelShape = myAISContext->SelectedShape();
74
75   if (aSelShape.ShapeType() != TopAbs_EDGE &&
76       aSelShape.ShapeType() != TopAbs_FACE &&
77       aSelShape.ShapeType() != TopAbs_WIRE)
78     return;
79
80   if (aSelShape.ShapeType() == TopAbs_EDGE)
81   {
82     BRepAdaptor_Curve aCurve (TopoDS::Edge (aSelShape));
83     if (aCurve.GetType() != GeomAbs_Circle)
84     {
85       return;
86     }
87
88     aCircle = aCurve.Circle();
89     if (aCurve.FirstParameter() != 0 && aCurve.LastParameter() != M_PI * 2)
90     {
91       isAttachPoint = Standard_True;
92       aFirstPar = aCurve.FirstParameter();
93       aLastPar = aCurve.LastParameter();
94     }
95   }
96
97   myAISContext->ClearSelected();
98   CDimensionDlg *aDimDlg = (CDimensionDlg*)(this->GetParentOwner());
99   // Try to create dimension if it is possible
100   Handle(AIS_Dimension) aDim;
101   if (myIsDiameterDimension)
102   {
103     aDim = new AIS_DiameterDimension (aCircle);
104     Handle(AIS_DiameterDimension)::DownCast(aDim)->SetFlyout (aDimDlg->GetFlyout());
105   }
106   else
107   {
108     aDim = new AIS_RadiusDimension (aCircle);
109     Handle(AIS_RadiusDimension)::DownCast(aDim)->SetFlyout (aDimDlg->GetFlyout());
110   }
111
112   Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
113   anAspect->MakeArrows3d (Standard_False);
114   anAspect->MakeText3d (aDimDlg->GetTextType());
115   anAspect->TextAspect()->SetHeight (aDimDlg->GetFontHeight());
116   anAspect->MakeTextShaded (aDimDlg->IsText3dShaded());
117   anAspect->SetCommonColor (aDimDlg->GetDimensionColor());
118   anAspect->MakeUnitsDisplayed (aDimDlg->IsUnitsDisplayed());
119   if (aDimDlg->IsUnitsDisplayed())
120   {
121     aDim->SetDisplayUnits (aDimDlg->GetUnits());
122   }
123
124   aDim->SetDimensionAspect (anAspect);
125
126   // Display dimension in the neutral point
127
128   myAISContext->Display (aDim);
129   myAISContext->Activate (AIS_Shape::SelectionMode (TopAbs_EDGE));
130 }