0031456: Visualization - move out Dimensions and Relations from package AIS to PrsDims
[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 <PrsDim_LengthDimension.hxx>
9 #include <PrsDim_AngleDimension.hxx>
10 #include <GC_MakePlane.hxx>
11 // CLengthParamsEdgesPage dialog
12
13 //=======================================================================
14 //function : CLengthParamsEdgesPage
15 //purpose  :
16 //=======================================================================
17
18 CLengthParamsEdgesPage::CLengthParamsEdgesPage (Handle(AIS_InteractiveContext) theAISContext,
19                                                 bool isAngleDimension /*= false*/,
20                                                 CWnd* pParent /*=NULL*/)
21 : CDialog(CLengthParamsEdgesPage::IDD, pParent)
22 {
23   myAISContext = theAISContext;
24   myIsAngleDimension = isAngleDimension;
25 }
26
27 //=======================================================================
28 //function : ~CLengthParamsEdgesPage
29 //purpose  :
30 //=======================================================================
31
32 CLengthParamsEdgesPage::~CLengthParamsEdgesPage()
33 {
34 }
35
36 //=======================================================================
37 //function : DoDataExchange
38 //purpose  :
39 //=======================================================================
40
41 void CLengthParamsEdgesPage::DoDataExchange(CDataExchange* pDX)
42 {
43   CDialog::DoDataExchange(pDX);
44 }
45
46
47 BEGIN_MESSAGE_MAP(CLengthParamsEdgesPage, CDialog)
48   ON_BN_CLICKED(IDC_BUTTON1, &CLengthParamsEdgesPage::OnBnClickedEdge1Btn)
49   ON_BN_CLICKED(IDC_BUTTON3, &CLengthParamsEdgesPage::OnBnClickedEdge2Btn)
50 END_MESSAGE_MAP()
51
52
53 //=======================================================================
54 //function : OnBnClickedEdge1Btn
55 //purpose  :
56 //=======================================================================
57
58 void CLengthParamsEdgesPage::OnBnClickedEdge1Btn()
59 {
60   myAISContext->Activate (AIS_Shape::SelectionMode (TopAbs_EDGE));
61
62   // Now it's ok, edge selection mode is activated
63   // Check if some edge is selected
64   myAISContext->InitSelected();
65   if (!myAISContext->MoreSelected() ||
66        myAISContext->SelectedShape().ShapeType() != TopAbs_EDGE)
67   {
68     AfxMessageBox(_T("Choose the edge and press the button again"),
69                     MB_ICONINFORMATION | MB_OK);
70     return;
71   }
72
73   myFirstEdge = TopoDS::Edge (myAISContext->SelectedShape());
74
75   myAISContext->ClearSelected (Standard_True);
76 }
77
78 //=======================================================================
79 //function : OnBnClickedEdge2Btn
80 //purpose  :
81 //=======================================================================
82
83 void CLengthParamsEdgesPage::OnBnClickedEdge2Btn()
84 {
85   myAISContext->InitSelected();
86   if (!myAISContext->MoreSelected() ||
87        myAISContext->SelectedShape().ShapeType() != TopAbs_EDGE)
88   {
89     AfxMessageBox (_T("Choose the edge and press the button again"),
90        MB_ICONINFORMATION | MB_OK);
91     return;
92   }
93
94   mySecondEdge = TopoDS::Edge (myAISContext->SelectedShape());
95
96   myAISContext->ClearSelected (Standard_True);
97
98   // Build plane through three points
99   BRepAdaptor_Curve aCurve1 (myFirstEdge);
100   BRepAdaptor_Curve aCurve2 (mySecondEdge);
101
102   gp_Pnt aP1=aCurve1.Value (0.1);
103   gp_Pnt aP2=aCurve1.Value (0.9);
104   gp_Pnt aP3=aCurve2.Value (0.5);
105
106   GC_MakePlane aMkPlane (aP1,aP2,aP3);
107
108   Handle(Geom_Plane) aPlane = aMkPlane.Value();
109
110   CDimensionDlg *aDimDlg = (CDimensionDlg*)(GetParentOwner());
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 (myIsAngleDimension)
120   {
121     // Build an angle dimension between two non-parallel edges
122     Handle(PrsDim_AngleDimension) anAngleDim = new PrsDim_AngleDimension (myFirstEdge, mySecondEdge);
123     anAngleDim->SetDimensionAspect (anAspect);
124     anAngleDim->DimensionAspect()->MakeUnitsDisplayed (aDimDlg->IsUnitsDisplayed());
125     if (aDimDlg->IsUnitsDisplayed())
126     {
127       anAngleDim->SetDisplayUnits (aDimDlg->GetUnits ());
128       if ((anAngleDim->GetDisplayUnits().IsEqual (TCollection_AsciiString ("deg"))))
129       {
130         anAngleDim->DimensionAspect()->MakeUnitsDisplayed (Standard_False);
131       }
132       else
133       {
134         anAngleDim->SetDisplaySpecialSymbol (PrsDim_DisplaySpecialSymbol_No);
135       }
136     }
137
138     anAngleDim->SetFlyout (aDimDlg->GetFlyout());
139     myAISContext->Display (anAngleDim, Standard_True);
140   }
141   else
142   {
143     Handle(PrsDim_LengthDimension) aLenDim = new PrsDim_LengthDimension (myFirstEdge, mySecondEdge, aPlane->Pln());
144     aLenDim->SetDimensionAspect (anAspect);
145     aLenDim->SetFlyout (aDimDlg->GetFlyout());
146     if (aDimDlg->IsUnitsDisplayed())
147     {
148       aLenDim->SetDisplayUnits (aDimDlg->GetUnits());
149     }
150
151     myAISContext->Display (aLenDim, Standard_True);
152   }
153
154   myAISContext->Activate (AIS_Shape::SelectionMode (TopAbs_EDGE));
155 }