0028162: Draw Harness - eliminate usage of deprecated Local Context
[occt.git] / samples / mfc / standard / Common / LengthParamsEdgesPage.cpp
1 // LenghtParamsEdgesPage.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "LengthParamsEdgesPage.h"
6 #include "DimensionDlg.h"
7 #include <AIS_InteractiveContext.hxx>
8 #include <AIS_LocalContext.hxx>
9 #include <AIS_LengthDimension.hxx>
10 #include <AIS_AngleDimension.hxx>
11 #include <GC_MakePlane.hxx>
12 // CLengthParamsEdgesPage dialog
13
14 //=======================================================================
15 //function : CLengthParamsEdgesPage
16 //purpose  :
17 //=======================================================================
18
19 CLengthParamsEdgesPage::CLengthParamsEdgesPage (Handle(AIS_InteractiveContext) theAISContext,
20                                                 bool isAngleDimension /*= false*/,
21                                                 CWnd* pParent /*=NULL*/)
22 : CDialog(CLengthParamsEdgesPage::IDD, pParent)
23 {
24   myAISContext = theAISContext;
25   myIsAngleDimension = isAngleDimension;
26 }
27
28 //=======================================================================
29 //function : ~CLengthParamsEdgesPage
30 //purpose  :
31 //=======================================================================
32
33 CLengthParamsEdgesPage::~CLengthParamsEdgesPage()
34 {
35 }
36
37 //=======================================================================
38 //function : DoDataExchange
39 //purpose  :
40 //=======================================================================
41
42 void CLengthParamsEdgesPage::DoDataExchange(CDataExchange* pDX)
43 {
44   CDialog::DoDataExchange(pDX);
45 }
46
47
48 BEGIN_MESSAGE_MAP(CLengthParamsEdgesPage, CDialog)
49   ON_BN_CLICKED(IDC_BUTTON1, &CLengthParamsEdgesPage::OnBnClickedEdge1Btn)
50   ON_BN_CLICKED(IDC_BUTTON3, &CLengthParamsEdgesPage::OnBnClickedEdge2Btn)
51 END_MESSAGE_MAP()
52
53
54 //=======================================================================
55 //function : OnBnClickedEdge1Btn
56 //purpose  :
57 //=======================================================================
58
59 void CLengthParamsEdgesPage::OnBnClickedEdge1Btn()
60 {
61   myAISContext->Activate (AIS_Shape::SelectionMode (TopAbs_EDGE));
62
63   // Now it's ok, edge selection mode is activated
64   // Check if some edge is selected
65   myAISContext->InitSelected();
66   if (!myAISContext->MoreSelected() ||
67        myAISContext->SelectedShape().ShapeType() != TopAbs_EDGE)
68   {
69     AfxMessageBox(_T("Choose the edge and press the button again"),
70                     MB_ICONINFORMATION | MB_OK);
71     return;
72   }
73
74   myFirstEdge = TopoDS::Edge (myAISContext->SelectedShape());
75
76   myAISContext->ClearSelected();
77 }
78
79 //=======================================================================
80 //function : OnBnClickedEdge2Btn
81 //purpose  :
82 //=======================================================================
83
84 void CLengthParamsEdgesPage::OnBnClickedEdge2Btn()
85 {
86   myAISContext->InitSelected();
87   if (!myAISContext->MoreSelected() ||
88        myAISContext->SelectedShape().ShapeType() != TopAbs_EDGE)
89   {
90     AfxMessageBox (_T("Choose the edge and press the button again"),
91        MB_ICONINFORMATION | MB_OK);
92     return;
93   }
94
95   mySecondEdge = TopoDS::Edge (myAISContext->SelectedShape());
96
97   myAISContext->ClearSelected();
98
99   // Build plane through three points
100   BRepAdaptor_Curve aCurve1 (myFirstEdge);
101   BRepAdaptor_Curve aCurve2 (mySecondEdge);
102
103   gp_Pnt aP1=aCurve1.Value (0.1);
104   gp_Pnt aP2=aCurve1.Value (0.9);
105   gp_Pnt aP3=aCurve2.Value (0.5);
106
107   GC_MakePlane aMkPlane (aP1,aP2,aP3);
108
109   Handle(Geom_Plane) aPlane = aMkPlane.Value();
110
111   CDimensionDlg *aDimDlg = (CDimensionDlg*)(GetParentOwner());
112
113   Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
114   anAspect->MakeArrows3d (Standard_False);
115   anAspect->MakeText3d (aDimDlg->GetTextType());
116   anAspect->TextAspect()->SetHeight (aDimDlg->GetFontHeight());
117   anAspect->MakeTextShaded (aDimDlg->IsText3dShaded());
118   anAspect->SetCommonColor (aDimDlg->GetDimensionColor());
119   anAspect->MakeUnitsDisplayed (aDimDlg->IsUnitsDisplayed());
120   if (myIsAngleDimension)
121   {
122     // Build an angle dimension between two non-parallel edges
123     Handle(AIS_AngleDimension) anAngleDim = new AIS_AngleDimension (myFirstEdge, mySecondEdge);
124     anAngleDim->SetDimensionAspect (anAspect);
125     anAngleDim->DimensionAspect()->MakeUnitsDisplayed (aDimDlg->IsUnitsDisplayed());
126     if (aDimDlg->IsUnitsDisplayed())
127     {
128       anAngleDim->SetDisplayUnits (aDimDlg->GetUnits ());
129       if ((anAngleDim->GetDisplayUnits().IsEqual (TCollection_AsciiString ("deg"))))
130       {
131         anAngleDim->DimensionAspect()->MakeUnitsDisplayed (Standard_False);
132       }
133       else
134       {
135         anAngleDim->SetDisplaySpecialSymbol (AIS_DSS_No);
136       }
137     }
138
139     anAngleDim->SetFlyout (aDimDlg->GetFlyout());
140     myAISContext->Display (anAngleDim);
141   }
142   else
143   {
144     Handle(AIS_LengthDimension) aLenDim = new AIS_LengthDimension (myFirstEdge, mySecondEdge, aPlane->Pln());
145     aLenDim->SetDimensionAspect (anAspect);
146     aLenDim->SetFlyout (aDimDlg->GetFlyout());
147     if (aDimDlg->IsUnitsDisplayed())
148     {
149       aLenDim->SetDisplayUnits (aDimDlg->GetUnits());
150     }
151
152     myAISContext->Display (aLenDim);
153   }
154
155   myAISContext->Activate (AIS_Shape::SelectionMode (TopAbs_EDGE));
156 }