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