Salome HOME
Issue #780: Make Arrow Tail size and extension line size adaptive to current viewer...
[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
29 SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint, 
30                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
31 : AIS_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)), myConstraint(theConstraint), myPlane(thePlane)
32 {
33   myAspect = new Prs3d_DimensionAspect();
34   myAspect->MakeArrows3d(false);
35   myAspect->MakeText3d(false);
36   myAspect->MakeTextShaded(false);
37   myAspect->MakeUnitsDisplayed(false);
38   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
39   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
40   
41   SetDimensionAspect(myAspect);
42   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
43 }
44
45 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
46                                 const Handle(Prs3d_Presentation)& thePresentation, 
47                                 const Standard_Integer theMode)
48 {
49   DataPtr aData = myConstraint->data();
50
51   // Flyout point
52   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
53     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
54     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
55   if (!aFlyoutAttr->isInitialized())
56     return; // can not create a good presentation
57
58   std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = myPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
59
60   AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
61   if (!anAttr1->isInitialized())
62     return;
63
64   AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
65   if (!anAttr2->isInitialized())
66     return;
67
68   // Get angle edges
69   ObjectPtr aObj1 = anAttr1->object();
70   ObjectPtr aObj2 = anAttr2->object();
71
72   std::shared_ptr<GeomAPI_Shape> aShape1 = SketcherPrs_Tools::getShape(aObj1);
73   std::shared_ptr<GeomAPI_Shape> aShape2 = SketcherPrs_Tools::getShape(aObj2);
74
75   if (!aShape1 && !aShape2)
76     return;
77
78   TopoDS_Shape aTEdge1 = aShape1->impl<TopoDS_Shape>();
79   TopoDS_Shape aTEdge2 = aShape2->impl<TopoDS_Shape>();
80
81   TopoDS_Edge aEdge1 = TopoDS::Edge(aTEdge1);
82   TopoDS_Edge aEdge2 = TopoDS::Edge(aTEdge2);
83   SetMeasuredGeometry(aEdge1, aEdge2);
84
85   // Compute intersection point
86   TopoDS_Vertex aV1, aV2;
87   TopExp::Vertices(aEdge1, aV1, aV2);
88   gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
89   gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
90   std::shared_ptr<GeomAPI_Lin> aL1(new GeomAPI_Lin(aPnt1.X(), aPnt1.Y(), aPnt1.Z(),
91                                                    aPnt2.X(), aPnt2.Y(), aPnt2.Z()));
92   TopExp::Vertices(aEdge2, aV1, aV2);
93   aPnt1 = BRep_Tool::Pnt(aV1);
94   aPnt2 = BRep_Tool::Pnt(aV2);
95   std::shared_ptr<GeomAPI_Lin> aL2(new GeomAPI_Lin(aPnt1.X(), aPnt1.Y(), aPnt1.Z(),
96                                                    aPnt2.X(), aPnt2.Y(), aPnt2.Z()));
97
98   std::shared_ptr<GeomAPI_Pnt> aInter = aL1->intersect(aL2);
99   gp_Pnt aCenter = aInter->impl<gp_Pnt>();
100
101   gp_Pnt aFLyPnt(aFlyoutPnt->x(), aFlyoutPnt->y(), aFlyoutPnt->z());
102   double aDist = aCenter.Distance(aFLyPnt);
103   SetFlyout(aDist);
104
105   // Angle value is in degrees
106   AttributeDoublePtr aVal = aData->real(SketchPlugin_Constraint::VALUE());
107   SetCustomValue(aVal->value() * PI / 180.0);
108
109   myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
110   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
111
112   AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
113 }
114
115 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
116                                                    const Standard_Integer theMode)
117 {
118   Standard_Integer aMode;
119   switch (theMode) {
120   case 0: // we should use selection of all objects
121     aMode = 0;
122     break;
123   case SketcherPrs_Tools::Sel_Dimension_All:
124     aMode = 0;
125     break;
126   case SketcherPrs_Tools::Sel_Dimension_Line:
127     aMode = 1;
128     break;
129   case SketcherPrs_Tools::Sel_Dimension_Text:
130     aMode = 2;
131     break;
132   default: {
133     // there are own selection modes, so the others should be ignored
134     // otherwise, the text selection appears in the viewer
135     return; 
136   }
137   }
138   AIS_AngleDimension::ComputeSelection(aSelection, aMode);
139 }