Salome HOME
Issue #1303 Reordering menu items: Sketch action enable state
[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 #include "SketcherPrs_DimensionStyleListener.h"
10
11 #include <SketchPlugin_ConstraintAngle.h>
12 #include <SketchPlugin_Constraint.h>
13
14 #include <GeomAPI_Edge.h>
15 #include <GeomAPI_Lin.h>
16 #include <GeomDataAPI_Point2D.h>
17
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_AttributeDouble.h>
20 #include <ModelAPI_AttributeInteger.h>
21
22 #include <TopExp.hxx>
23 #include <BRep_Tool.hxx>
24
25 #include <Events_Error.h>
26
27 #define PI 3.1415926535897932
28
29 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Angle, AIS_AngleDimension);
30 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
31
32 SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint, 
33                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
34 : AIS_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)), myConstraint(theConstraint), myPlane(thePlane)
35 {
36   myAspect = new Prs3d_DimensionAspect();
37   myAspect->MakeArrows3d(false);
38   myAspect->MakeText3d(false);
39   myAspect->MakeTextShaded(false);
40   myAspect->MakeUnitsDisplayed(false);
41   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
42   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
43   
44   SetDimensionAspect(myAspect);
45   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
46
47   myStyleListener = new SketcherPrs_DimensionStyleListener();
48 }
49
50 SketcherPrs_Angle::~SketcherPrs_Angle()
51 {
52   delete myStyleListener;
53 }
54
55 bool SketcherPrs_Angle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
56                                          const std::shared_ptr<GeomAPI_Ax3>&/* thePlane*/)
57 {
58   bool aReadyToDisplay = false;
59
60   DataPtr aData = theConstraint->data();
61
62   // Flyout point
63   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
64     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
65     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
66   if (!aFlyoutAttr->isInitialized())
67     return aReadyToDisplay; // can not create a good presentation
68
69   AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
70   if (!anAttr1->isInitialized())
71     return aReadyToDisplay;
72
73   AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
74   if (!anAttr2->isInitialized())
75     return aReadyToDisplay;
76
77   // Get angle edges
78   ObjectPtr aObj1 = anAttr1->object();
79   ObjectPtr aObj2 = anAttr2->object();
80
81   std::shared_ptr<GeomAPI_Shape> aShape1 = SketcherPrs_Tools::getShape(aObj1);
82   std::shared_ptr<GeomAPI_Shape> aShape2 = SketcherPrs_Tools::getShape(aObj2);
83
84   aReadyToDisplay = aShape1.get() && aShape2.get();
85   return aReadyToDisplay;
86 }
87
88 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
89                                 const Handle(Prs3d_Presentation)& thePresentation, 
90                                 const Standard_Integer theMode)
91 {
92   DataPtr aData = myConstraint->data();
93
94   if (!IsReadyToDisplay(myConstraint, myPlane)) {
95     Events_Error::throwException("An empty AIS presentation: SketcherPrs_Angle");
96     return; // can not create a good presentation
97   }
98
99   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
100       ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
101   SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
102
103   AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
104   AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
105
106   // Get angle edges
107   ObjectPtr aObj1 = anAttr1->object();
108   ObjectPtr aObj2 = anAttr2->object();
109
110   std::shared_ptr<GeomAPI_Shape> aShape1 = SketcherPrs_Tools::getShape(aObj1);
111   std::shared_ptr<GeomAPI_Shape> aShape2 = SketcherPrs_Tools::getShape(aObj2);
112
113   TopoDS_Shape aTEdge1 = aShape1->impl<TopoDS_Shape>();
114   TopoDS_Shape aTEdge2 = aShape2->impl<TopoDS_Shape>();
115
116   TopoDS_Edge aEdge1 = TopoDS::Edge(aTEdge1);
117   TopoDS_Edge aEdge2 = TopoDS::Edge(aTEdge2);
118
119   double aDist = -1;
120
121   switch (anAngleType) {
122     case SketcherPrs_Tools::ANGLE_DIRECT: {
123       SetArrowVisible(Standard_False/*first*/, Standard_True/*second*/);
124       /*
125       std::shared_ptr<GeomAPI_Edge> anEdge1 = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aShape1));
126       std::shared_ptr<GeomAPI_Edge> anEdge2 = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aShape2));
127
128       std::shared_ptr<GeomAPI_Lin> aLin1 = anEdge1->line();
129       std::shared_ptr<GeomAPI_Lin> aLin2 = anEdge2->line();
130
131       std::shared_ptr<GeomAPI_Pnt> aCenterPnt = aLin1->intersect(aLin2);
132       std::shared_ptr<GeomAPI_Dir> aDir1 = aLin1->direction();
133       std::shared_ptr<GeomAPI_Dir> aDir2 = aLin2->direction();
134
135       const gp_Pnt& aCenterPoint = aCenterPnt->impl<gp_Pnt>();
136
137       const gp_Dir& aDirection1 = aDir1->impl<gp_Dir>();
138       const gp_Dir& aDirection2 = aDir2->impl<gp_Dir>();
139
140       gp_Pnt aFirstPoint  = aCenterPoint.Translated (gp_Vec (aDirection1));
141       gp_Pnt aSecondPoint = aCenterPoint.Translated (gp_Vec (aDirection2));
142
143       SetMeasuredGeometry(aFirstPoint, aCenterPoint, aSecondPoint);*/
144       SetMeasuredGeometry(aEdge1, aEdge2, Standard_False);
145       bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
146       SetAngleReversed(!isReversedPlanes);
147     }
148     break;
149     case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
150       SetArrowVisible(Standard_True/*first*/, Standard_False/*second*/);
151       // to calculate center, first and end points
152       SetAngleReversed(false);
153       SetMeasuredGeometry(aEdge1, aEdge2, Standard_False);
154       /// the first point will be moved, so it is necessary to find distance
155       /// after applying initial parameters of geometry but before correcting them
156       /// for the current type of angle(complementary)
157       aDist = calculateDistanceToFlyoutPoint();
158       /// invert the first edge according to the angle center
159       gp_Pnt aCenter = CenterPoint();
160       gp_Pnt aFirst = FirstPoint();
161       gp_Pnt aSecond = SecondPoint();
162       double anEdge1Length = aCenter.Distance(aFirst);
163       aFirst = aCenter.Translated (gp_Vec(aCenter, aFirst).Normalized() * (-anEdge1Length));
164       anEdge1Length = aCenter.Distance(aFirst);
165
166       SetMeasuredGeometry(aFirst, aCenter, aSecond);
167     }
168     break;
169     case SketcherPrs_Tools::ANGLE_BACKWARD: {
170       SetArrowVisible(Standard_False/*first*/, Standard_True/*second*/);
171
172       SetMeasuredGeometry(aEdge1, aEdge2, Standard_False);
173       bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
174       SetAngleReversed(isReversedPlanes);
175     }
176     break;
177     default:
178       break;
179   }
180   if (aDist < 0) /// it was not calculated yet
181     aDist = calculateDistanceToFlyoutPoint();
182   SetFlyout(aDist);
183
184   // Angle value is in degrees
185   AttributeDoublePtr aVal = aData->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
186   SetCustomValue(aVal->value());
187
188   myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
189   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
190
191   SketcherPrs_Tools::setDisplaySpecialSymbol(this, aVal->usedParameters().size() > 0);
192   myStyleListener->updateDimensions(this, aVal);
193
194   AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
195 }
196
197 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
198                                                    const Standard_Integer theMode)
199 {
200   Standard_Integer aMode;
201   switch (theMode) {
202   case 0: // we should use selection of all objects
203     aMode = 0;
204     break;
205   case SketcherPrs_Tools::Sel_Dimension_All:
206     aMode = 0;
207     break;
208   case SketcherPrs_Tools::Sel_Dimension_Line:
209     aMode = 1;
210     break;
211   case SketcherPrs_Tools::Sel_Dimension_Text:
212     aMode = 2;
213     break;
214   default: {
215     // there are own selection modes, so the others should be ignored
216     // otherwise, the text selection appears in the viewer
217     return; 
218   }
219   }
220   AIS_AngleDimension::ComputeSelection(aSelection, aMode);
221 }
222
223 bool SketcherPrs_Angle::isAnglePlaneReversedToSketchPlane()
224 {
225   bool aReversed = false;
226   if (!myPlane.get())
227     return aReversed;
228
229   gp_Pln aPlane = GetPlane();
230   gp_Dir aDir = aPlane.Axis().Direction();
231   double aDot = myPlane->normal()->dot(
232                 std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z())));
233   return aDot < 0;
234 }
235
236 double SketcherPrs_Angle::calculateDistanceToFlyoutPoint()
237 {
238   const gp_Pnt& aCenter = CenterPoint();
239   const gp_Pnt& aFirst = FirstPoint();
240   const gp_Pnt& aSecond = SecondPoint();
241
242   // Flyout point
243   DataPtr aData = myConstraint->data();
244   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
245                               std::dynamic_pointer_cast<GeomDataAPI_Point2D>
246                               (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
247   std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = myPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
248
249   gp_Dir aBisector((aFirst.XYZ() + aSecond.XYZ()) * 0.5 - aCenter.XYZ());
250   gp_Pnt aFlyPnt(aFlyoutPnt->x(), aFlyoutPnt->y(), aFlyoutPnt->z());
251   gp_XYZ aFlyDir = aFlyPnt.XYZ() - aCenter.XYZ();
252   double aDistance = aFlyDir.Dot(aBisector.XYZ());
253   // make a positive distance in order to AIS angle presentation is not reversed
254   aDistance = fabs(aDistance);
255   return aDistance;
256 }