Salome HOME
6f5c379f3c6732c51fec2a1ed44035157aed1f15
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Angle.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_Angle.cpp
4 // Created:     20 August 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_Angle.h"
8 #include "SketcherPrs_Tools.h"
9
10 #include <SketchPlugin_ConstraintAngle.h>
11 #include <SketchPlugin_Constraint.h>
12
13 #include <GeomAPI_Edge.h>
14 #include <GeomAPI_Lin.h>
15 #include <GeomDataAPI_Point2D.h>
16
17 #include <ModelAPI_AttributeRefAttr.h>
18 #include <ModelAPI_AttributeDouble.h>
19
20 #include <TopExp.hxx>
21 #include <BRep_Tool.hxx>
22
23 #define PI 3.1415926535897932
24
25 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Angle, AIS_AngleDimension);
26 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
27
28 SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint, 
29                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
30 : AIS_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)), myConstraint(theConstraint), myPlane(thePlane)
31 {
32   myAspect = new Prs3d_DimensionAspect();
33   myAspect->MakeArrows3d(false);
34   myAspect->MakeText3d(false);
35   myAspect->MakeTextShaded(false);
36   myAspect->MakeUnitsDisplayed(false);
37   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
38   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
39   
40   SetDimensionAspect(myAspect);
41   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
42 }
43
44 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
45                                 const Handle(Prs3d_Presentation)& thePresentation, 
46                                 const Standard_Integer theMode)
47 {
48   DataPtr aData = myConstraint->data();
49
50   // Flyout point
51   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
52     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
53     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
54   if (!aFlyoutAttr->isInitialized())
55     return; // can not create a good presentation
56
57   std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = myPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
58
59   AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
60   if (!anAttr1->isInitialized())
61     return;
62
63   AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
64   if (!anAttr2->isInitialized())
65     return;
66
67   // Get angle edges
68   ObjectPtr aObj1 = anAttr1->object();
69   ObjectPtr aObj2 = anAttr2->object();
70
71   std::shared_ptr<GeomAPI_Shape> aShape1 = SketcherPrs_Tools::getShape(aObj1);
72   std::shared_ptr<GeomAPI_Shape> aShape2 = SketcherPrs_Tools::getShape(aObj2);
73
74   if (!aShape1 && !aShape2)
75     return;
76
77   TopoDS_Shape aTEdge1 = aShape1->impl<TopoDS_Shape>();
78   TopoDS_Shape aTEdge2 = aShape2->impl<TopoDS_Shape>();
79
80   TopoDS_Edge aEdge1 = TopoDS::Edge(aTEdge1);
81   TopoDS_Edge aEdge2 = TopoDS::Edge(aTEdge2);
82   SetMeasuredGeometry(aEdge1, aEdge2);
83
84   const gp_Pnt& aCenter = CenterPoint();
85   const gp_Pnt& aFirst = FirstPoint();
86   const gp_Pnt& aSecond = SecondPoint();
87
88   gp_Dir aBisector((aFirst.XYZ() + aSecond.XYZ()) * 0.5 - aCenter.XYZ());
89
90   gp_Pnt aFlyPnt(aFlyoutPnt->x(), aFlyoutPnt->y(), aFlyoutPnt->z());
91   gp_XYZ aFlyDir = aFlyPnt.XYZ() - aCenter.XYZ();
92   double aDist = aFlyDir.Dot(aBisector.XYZ());
93   SetFlyout(aDist);
94
95   // Angle value is in degrees
96   AttributeDoublePtr aVal = aData->real(SketchPlugin_Constraint::VALUE());
97   SetCustomValue(aVal->value() * PI / 180.0);
98
99   myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
100   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
101
102   AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
103   SketcherPrs_Tools::setDisplaySpecialSymbol(this, aValue->usedParameters().size() > 0);
104
105   AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
106 }
107
108 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
109                                                    const Standard_Integer theMode)
110 {
111   Standard_Integer aMode;
112   switch (theMode) {
113   case 0: // we should use selection of all objects
114     aMode = 0;
115     break;
116   case SketcherPrs_Tools::Sel_Dimension_All:
117     aMode = 0;
118     break;
119   case SketcherPrs_Tools::Sel_Dimension_Line:
120     aMode = 1;
121     break;
122   case SketcherPrs_Tools::Sel_Dimension_Text:
123     aMode = 2;
124     break;
125   default: {
126     // there are own selection modes, so the others should be ignored
127     // otherwise, the text selection appears in the viewer
128     return; 
129   }
130   }
131   AIS_AngleDimension::ComputeSelection(aSelection, aMode);
132 }