]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_Angle.cpp
Salome HOME
Porting to SALOME_8.2.0
[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_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
35
36 SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint,
37                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
38 : AIS_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)), myConstraint(theConstraint),
39   mySketcherPlane(thePlane),
40   myFirstPoint(gp_Pnt(0,0,0)), myCenterPoint(gp_Pnt(1,0,0)), mySecondPoint(gp_Pnt(0,1,0)),
41   myValue(90., false, ""), myFlyOutPoint(0, 0.5, 0)
42 {
43   myAspect = new Prs3d_DimensionAspect();
44   myAspect->MakeArrows3d(false);
45   myAspect->MakeText3d(false);
46   myAspect->MakeTextShaded(false);
47   myAspect->MakeUnitsDisplayed(false);
48   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
49   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
50
51   SetDimensionAspect(myAspect);
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 =
93     SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
94   FeaturePtr aLineB =
95     SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
96
97   if (!aLineA.get() || !aLineB.get())
98     return aReadyToDisplay;
99
100   std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
101       aLineA->attribute(SketchPlugin_Line::START_ID()));
102   std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
103       aLineA->attribute(SketchPlugin_Line::END_ID()));
104   std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
105       aLineB->attribute(SketchPlugin_Line::START_ID()));
106   std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
107       aLineB->attribute(SketchPlugin_Line::END_ID()));
108
109   std::shared_ptr<GeomAPI_Angle2d> anAng;
110   bool isFirstPnt = aData->attribute(
111     SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized();
112   bool isSecondPnt = aData->attribute(
113     SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized();
114   if (!isFirstPnt || !isSecondPnt)
115     anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
116         aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
117   else {
118     std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
119     bool isReversed1 =
120       aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->value();
121     std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
122     bool isReversed2 =
123       aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->value();
124     anAng = std::shared_ptr<GeomAPI_Angle2d>(
125       new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
126   }
127
128   gp_Pnt2d aFirstPoint = anAng->firstPoint()->impl<gp_Pnt2d>();
129   std::shared_ptr<GeomAPI_Pnt> aPoint = thePlane->to3D(aFirstPoint.X(), aFirstPoint.Y());
130   theFirstPoint = aPoint->impl<gp_Pnt>();
131
132   gp_Pnt2d aCenterPoint = anAng->center()->impl<gp_Pnt2d>();
133   aPoint = thePlane->to3D(aCenterPoint.X(), aCenterPoint.Y());
134   theCenterPoint = aPoint->impl<gp_Pnt>();
135
136   gp_Pnt2d aSecondPoint = anAng->secondPoint()->impl<gp_Pnt2d>();
137   aPoint = thePlane->to3D(aSecondPoint.X(), aSecondPoint.Y());
138   theSecondPoint = aPoint->impl<gp_Pnt>();
139
140   return true;
141 }
142
143
144 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
145                                 const Handle(Prs3d_Presentation)& thePresentation,
146                                 const Standard_Integer theMode)
147 {
148   gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
149   bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane,
150                                         aFirstPoint, aSecondPoint, aCenterPoint);
151   if (aReadyToDisplay) {
152     myFirstPoint = aFirstPoint;
153     mySecondPoint = aSecondPoint;
154     myCenterPoint = aCenterPoint;
155
156     DataPtr aData = myConstraint->data();
157     AttributeDoublePtr anAttributeValue =
158       aData->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
159     myValue.init(anAttributeValue);
160
161     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
162                                 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
163                                 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
164     std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt =
165       mySketcherPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
166     myFlyOutPoint = aFlyoutPnt->impl<gp_Pnt>();
167   }
168
169   DataPtr aData = myConstraint->data();
170   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
171       ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
172   SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
173
174   double aDist = -1;
175   switch (anAngleType) {
176     case SketcherPrs_Tools::ANGLE_DIRECT: {
177 #ifndef COMPILATION_CORRECTION
178       SetArrowsVisibility(AIS_TOAV_Second);
179 #endif
180       SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
181 #ifndef COMPILATION_CORRECTION
182       bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
183       SetType(!isReversedPlanes ? AIS_TypeOfAngle::AIS_TOA_Exterior
184                                 : AIS_TypeOfAngle::AIS_TOA_Interior);
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       SetType(AIS_TypeOfAngle::AIS_TOA_Interior);
196 #endif
197     }
198     break;
199     case SketcherPrs_Tools::ANGLE_BACKWARD: {
200 #ifndef COMPILATION_CORRECTION
201       SetArrowsVisibility(AIS_TypeOfAngleArrowVisibility::AIS_TOAV_Second);
202 #endif
203       SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
204       bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
205 #ifndef COMPILATION_CORRECTION
206       SetType(isReversedPlanes ? AIS_TypeOfAngle::AIS_TOA_Exterior
207                                : AIS_TypeOfAngle::AIS_TOA_Interior);
208 #endif
209     }
210     break;
211     default:
212       break;
213   }
214   if (aDist < 0) /// it was not calculated yet
215     aDist = calculateDistanceToFlyoutPoint();
216   SetFlyout(aDist);
217
218   // Update text visualization: parameter value or parameter text
219   myStyleListener->updateDimensions(this, myValue);
220
221   myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
222   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
223
224   AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
225
226   if (!aReadyToDisplay)
227     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
228                               "An empty AIS presentation: SketcherPrs_Angle");
229 }
230
231 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
232                                                    const Standard_Integer theMode)
233 {
234   Standard_Integer aMode;
235   switch (theMode) {
236   case 0: // we should use selection of all objects
237     aMode = 0;
238     break;
239   case SketcherPrs_Tools::Sel_Dimension_All:
240     aMode = 0;
241     break;
242   case SketcherPrs_Tools::Sel_Dimension_Line:
243     aMode = 1;
244     break;
245   case SketcherPrs_Tools::Sel_Dimension_Text:
246     aMode = 2;
247     break;
248   default: {
249     // there are own selection modes, so the others should be ignored
250     // otherwise, the text selection appears in the viewer
251     return;
252   }
253   }
254   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
255   AIS_AngleDimension::ComputeSelection(aSelection, aMode);
256 }
257
258 bool SketcherPrs_Angle::isAnglePlaneReversedToSketchPlane()
259 {
260   bool aReversed = false;
261   if (!mySketcherPlane.get())
262     return aReversed;
263
264   gp_Pln aPlane = GetPlane();
265   gp_Dir aDir = aPlane.Axis().Direction();
266   double aDot = mySketcherPlane->normal()->dot(
267                 std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z())));
268   return aDot < 0;
269 }
270
271 double SketcherPrs_Angle::calculateDistanceToFlyoutPoint()
272 {
273   return myFlyOutPoint.Distance(myCenterPoint);
274 }