Salome HOME
Temporary modification to check whether the compilation on Linux is correct. OCCT...
[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 #include <SketchPlugin_Line.h>
14
15 #include <GeomAPI_Edge.h>
16 #include <GeomAPI_Lin.h>
17 #include <GeomDataAPI_Point2D.h>
18 #include <GeomAPI_Angle2d.h>
19 #include <GeomAPI_Lin2d.h>
20
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeInteger.h>
24
25 #include <TopExp.hxx>
26 #include <BRep_Tool.hxx>
27
28 #define PI 3.1415926535897932
29
30 #define COMPILATION_CORRECTION
31
32 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Angle, AIS_AngleDimension);
33 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
34
35 SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint, 
36                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
37 : AIS_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)), myConstraint(theConstraint),
38   mySketcherPlane(thePlane),
39   myFirstPoint(gp_Pnt(0,0,0)), myCenterPoint(gp_Pnt(1,0,0)), mySecondPoint(gp_Pnt(0,1,0)),
40   myValue(90., false, ""), myFlyOutPoint(0, 0.5, 0)
41 {
42   myAspect = new Prs3d_DimensionAspect();
43   myAspect->MakeArrows3d(false);
44   myAspect->MakeText3d(false);
45   myAspect->MakeTextShaded(false);
46   myAspect->MakeUnitsDisplayed(false);
47   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
48   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
49   
50   SetDimensionAspect(myAspect);
51   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
52
53   myStyleListener = new SketcherPrs_DimensionStyleListener();
54 }
55
56 SketcherPrs_Angle::~SketcherPrs_Angle()
57 {
58   delete myStyleListener;
59 }
60
61 bool SketcherPrs_Angle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
62                                          const std::shared_ptr<GeomAPI_Ax3>& thePlane)
63 {
64   gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
65   return readyToDisplay(theConstraint, thePlane, aFirstPoint, aSecondPoint, aCenterPoint);
66 }
67
68 bool SketcherPrs_Angle::readyToDisplay(ModelAPI_Feature* theConstraint,
69                                        const std::shared_ptr<GeomAPI_Ax3>& thePlane,
70                                        gp_Pnt& theFirstPoint, gp_Pnt& theSecondPoint,
71                                        gp_Pnt& theCenterPoint)
72 {
73   bool aReadyToDisplay = false;
74
75   DataPtr aData = theConstraint->data();
76
77   // Flyout point
78   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
79     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
80     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
81   if (!aFlyoutAttr->isInitialized())
82     return aReadyToDisplay; // can not create a good presentation
83
84   AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
85   if (!anAttr1->isInitialized())
86     return aReadyToDisplay;
87
88   AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
89   if (!anAttr2->isInitialized())
90     return aReadyToDisplay;
91
92   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
93   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
94
95   if (!aLineA.get() || !aLineB.get())
96     return aReadyToDisplay;
97
98   std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
99       aLineA->attribute(SketchPlugin_Line::START_ID()));
100   std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
101       aLineA->attribute(SketchPlugin_Line::END_ID()));
102   std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
103       aLineB->attribute(SketchPlugin_Line::START_ID()));
104   std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
105       aLineB->attribute(SketchPlugin_Line::END_ID()));
106
107   std::shared_ptr<GeomAPI_Angle2d> anAng;
108   if (!aData->attribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized() ||
109       !aData->attribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized())
110     anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
111         aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
112   else {
113     std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
114     bool isReversed1 = aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->value();
115     std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
116     bool isReversed2 = aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->value();
117     anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
118   }
119
120   gp_Pnt2d aFirstPoint = anAng->firstPoint()->impl<gp_Pnt2d>();
121   std::shared_ptr<GeomAPI_Pnt> aPoint = thePlane->to3D(aFirstPoint.X(), aFirstPoint.Y());
122   theFirstPoint = aPoint->impl<gp_Pnt>();
123
124   gp_Pnt2d aCenterPoint = anAng->center()->impl<gp_Pnt2d>();
125   aPoint = thePlane->to3D(aCenterPoint.X(), aCenterPoint.Y());
126   theCenterPoint = aPoint->impl<gp_Pnt>();
127
128   gp_Pnt2d aSecondPoint = anAng->secondPoint()->impl<gp_Pnt2d>();
129   aPoint = thePlane->to3D(aSecondPoint.X(), aSecondPoint.Y());
130   theSecondPoint = aPoint->impl<gp_Pnt>();
131
132   return true;
133 }
134
135
136 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
137                                 const Handle(Prs3d_Presentation)& thePresentation, 
138                                 const Standard_Integer theMode)
139 {
140   gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
141   bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aFirstPoint, aSecondPoint, aCenterPoint);
142   if (aReadyToDisplay) {
143     myFirstPoint = aFirstPoint;
144     mySecondPoint = aSecondPoint;
145     myCenterPoint = aCenterPoint;
146
147     DataPtr aData = myConstraint->data();
148     AttributeDoublePtr anAttributeValue = aData->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
149     myValue.init(anAttributeValue);
150
151     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
152                                 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
153                                 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
154     std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = mySketcherPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
155     myFlyOutPoint = aFlyoutPnt->impl<gp_Pnt>();
156   }
157
158   DataPtr aData = myConstraint->data();
159   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
160       ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
161   SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
162
163   double aDist = -1;
164   switch (anAngleType) {
165     case SketcherPrs_Tools::ANGLE_DIRECT: {
166 #ifndef COMPILATION_CORRECTION
167       SetArrowVisible(Standard_False/*first*/, Standard_True/*second*/);
168 #endif
169       SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
170 #ifndef COMPILATION_CORRECTION
171       bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
172       SetAngleReversed(!isReversedPlanes);
173 #endif
174     }
175     break;
176     case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
177       double anEdge1Length = aCenterPoint.Distance(myFirstPoint);
178       //aDist = calculateDistanceToFlyoutPoint();
179       gp_Pnt aFirstPoint = aCenterPoint.Translated(
180                           gp_Vec(myCenterPoint, myFirstPoint).Normalized() * (-anEdge1Length));
181       SetMeasuredGeometry(aFirstPoint, myCenterPoint, mySecondPoint);
182 #ifndef COMPILATION_CORRECTION
183       SetAngleReversed(false);
184 #endif
185     }
186     break;
187     case SketcherPrs_Tools::ANGLE_BACKWARD: {
188 #ifndef COMPILATION_CORRECTION
189       SetArrowVisible(Standard_False/*first*/, Standard_True/*second*/);
190 #endif
191       SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
192       bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
193 #ifndef COMPILATION_CORRECTION
194       SetAngleReversed(isReversedPlanes);
195 #endif
196     }
197     break;
198     default:
199       break;
200   }
201   if (aDist < 0) /// it was not calculated yet
202     aDist = calculateDistanceToFlyoutPoint();
203   SetFlyout(aDist);
204
205   // Update text visualization: parameter value or parameter text
206   myStyleListener->updateDimensions(this, myValue);
207
208   myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
209   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
210
211   AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
212
213   if (!aReadyToDisplay)
214     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
215                               "An empty AIS presentation: SketcherPrs_Angle");
216 }
217
218 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
219                                                    const Standard_Integer theMode)
220 {
221   Standard_Integer aMode;
222   switch (theMode) {
223   case 0: // we should use selection of all objects
224     aMode = 0;
225     break;
226   case SketcherPrs_Tools::Sel_Dimension_All:
227     aMode = 0;
228     break;
229   case SketcherPrs_Tools::Sel_Dimension_Line:
230     aMode = 1;
231     break;
232   case SketcherPrs_Tools::Sel_Dimension_Text:
233     aMode = 2;
234     break;
235   default: {
236     // there are own selection modes, so the others should be ignored
237     // otherwise, the text selection appears in the viewer
238     return; 
239   }
240   }
241   AIS_AngleDimension::ComputeSelection(aSelection, aMode);
242 }
243
244 bool SketcherPrs_Angle::isAnglePlaneReversedToSketchPlane()
245 {
246   bool aReversed = false;
247   if (!mySketcherPlane.get())
248     return aReversed;
249
250   gp_Pln aPlane = GetPlane();
251   gp_Dir aDir = aPlane.Axis().Direction();
252   double aDot = mySketcherPlane->normal()->dot(
253                 std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z())));
254   return aDot < 0;
255 }
256
257 double SketcherPrs_Angle::calculateDistanceToFlyoutPoint()
258 {
259   gp_Dir aBisector((myFirstPoint.XYZ() + mySecondPoint.XYZ()) * 0.5 - myCenterPoint.XYZ());
260   //gp_Pnt aFlyPnt(aFlyoutPnt->x(), aFlyoutPnt->y(), aFlyoutPnt->z());
261   gp_XYZ aFlyDir = myFlyOutPoint.XYZ() - myCenterPoint.XYZ();
262   double aDistance = aFlyDir.Dot(aBisector.XYZ());
263   // make a positive distance in order to AIS angle presentation is not reversed
264   aDistance = fabs(aDistance);
265   return aDistance;
266 }