0024133: Development of improvement of dimensions implementation; new length, radius...
[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->LocalContext()->InitSelected();
62   if (!myAISContext->LocalContext()->MoreSelected())
63   {
64     AfxMessageBox (_T ("Choose the edge and press the button again"), MB_ICONINFORMATION | MB_OK);
65     return;
66   }
67
68   gp_Circ aCircle;
69   Standard_Boolean isAttachPoint = Standard_False;
70   Standard_Real aFirstPar = 0, aLastPar = 0;
71
72   TopoDS_Shape aSelShape = myAISContext->LocalContext()->SelectedShape();
73   if (aSelShape.ShapeType() != TopAbs_EDGE &&
74       aSelShape.ShapeType() != TopAbs_FACE &&
75       aSelShape.ShapeType() != TopAbs_WIRE)
76     return;
77
78   if (aSelShape.ShapeType() == TopAbs_EDGE)
79   {
80     BRepAdaptor_Curve aCurve (TopoDS::Edge (aSelShape));
81     if (aCurve.GetType() != GeomAbs_Circle)
82     {
83       return;
84     }
85
86     aCircle = aCurve.Circle();
87     if (aCurve.FirstParameter() != 0 && aCurve.LastParameter() != M_PI * 2)
88     {
89       isAttachPoint = Standard_True;
90       aFirstPar = aCurve.FirstParameter();
91       aLastPar = aCurve.LastParameter();
92     }
93   }
94
95   myAISContext->LocalContext()->ClearSelected();
96   CDimensionDlg *aDimDlg = (CDimensionDlg*)(this->GetParentOwner());
97   // Try to create dimension if it is possible
98   Handle(AIS_Dimension) aDim;
99   if (myIsDiameterDimension)
100   {
101     aDim = isAttachPoint ? new AIS_DiameterDimension (aCircle, ElCLib::Value ((aFirstPar + aLastPar) / 2.0, aCircle))
102                          : new AIS_DiameterDimension (aCircle);
103     Handle(AIS_DiameterDimension)::DownCast(aDim)->SetFlyout (aDimDlg->GetFlyout());
104   }
105   else
106   {
107     aDim = isAttachPoint ? new AIS_RadiusDimension (aCircle, ElCLib::Value ((aFirstPar + aLastPar) / 2.0, aCircle))
108                          : 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   aDim->MakeUnitsDisplayed (aDimDlg->IsUnitsDisplayed());
119   if (aDim->IsUnitsDisplayed())
120   {
121     aDim->SetDisplayUnits (aDimDlg->GetUnits());
122   }
123
124   aDim->SetDimensionAspect (anAspect);
125
126   // Display dimension in the neutral point
127   myAISContext->CloseAllContexts();
128
129   myAISContext->Display (aDim);
130
131   myAISContext->OpenLocalContext();
132   myAISContext->ActivateStandardMode (TopAbs_EDGE);
133 }