]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_Angle.cpp
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Angle.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "SketcherPrs_Angle.h"
21 #include "SketcherPrs_DimensionStyleListener.h"
22 #include "SketcherPrs_Tools.h"
23
24 #include <SketchPlugin_ConstraintAngle.h>
25 #include <SketchPlugin_Constraint.h>
26 #include <SketchPlugin_Line.h>
27
28 #include <GeomAPI_Edge.h>
29 #include <GeomAPI_Lin.h>
30 #include <GeomDataAPI_Point2D.h>
31 #include <GeomAPI_Angle2d.h>
32 #include <GeomAPI_Lin2d.h>
33
34 #include <ModelAPI_AttributeRefAttr.h>
35 #include <ModelAPI_AttributeDouble.h>
36 #include <ModelAPI_AttributeInteger.h>
37
38 #include <TopExp.hxx>
39 #include <BRep_Tool.hxx>
40
41 #define PI 3.1415926535897932
42
43 //#ifndef WNT
44 //  #define COMPILATION_CORRECTION
45 //#endif
46
47 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
48
49 SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint,
50                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
51 : AIS_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)), myConstraint(theConstraint),
52   mySketcherPlane(thePlane),
53   myFirstPoint(gp_Pnt(0,0,0)), myCenterPoint(gp_Pnt(1,0,0)), mySecondPoint(gp_Pnt(0,1,0)),
54   myValue(90., false, ""), myFlyOutPoint(0, 0.5, 0)
55 {
56   myAspect = new Prs3d_DimensionAspect();
57   myAspect->MakeArrows3d(false);
58   myAspect->MakeText3d(false);
59   myAspect->MakeTextShaded(false);
60   myAspect->MakeUnitsDisplayed(false);
61   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
62   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
63
64   SetDimensionAspect(myAspect);
65
66   myStyleListener = new SketcherPrs_DimensionStyleListener();
67 }
68
69 SketcherPrs_Angle::~SketcherPrs_Angle()
70 {
71   delete myStyleListener;
72 }
73
74 bool SketcherPrs_Angle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
75                                          const std::shared_ptr<GeomAPI_Ax3>& thePlane)
76 {
77   gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
78   return readyToDisplay(theConstraint, thePlane, aFirstPoint, aSecondPoint, aCenterPoint);
79 }
80
81 bool SketcherPrs_Angle::readyToDisplay(ModelAPI_Feature* theConstraint,
82                                        const std::shared_ptr<GeomAPI_Ax3>& thePlane,
83                                        gp_Pnt& theFirstPoint, gp_Pnt& theSecondPoint,
84                                        gp_Pnt& theCenterPoint)
85 {
86   bool aReadyToDisplay = false;
87
88   DataPtr aData = theConstraint->data();
89
90   // Flyout point
91   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
92     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
93     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
94   if (!aFlyoutAttr->isInitialized())
95     return aReadyToDisplay; // can not create a good presentation
96
97   AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
98   if (!anAttr1->isInitialized())
99     return aReadyToDisplay;
100
101   AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
102   if (!anAttr2->isInitialized())
103     return aReadyToDisplay;
104
105   FeaturePtr aLineA =
106     SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
107   FeaturePtr aLineB =
108     SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
109
110   if (!aLineA.get() || !aLineB.get())
111     return aReadyToDisplay;
112
113   std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
114       aLineA->attribute(SketchPlugin_Line::START_ID()));
115   std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
116       aLineA->attribute(SketchPlugin_Line::END_ID()));
117   std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
118       aLineB->attribute(SketchPlugin_Line::START_ID()));
119   std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
120       aLineB->attribute(SketchPlugin_Line::END_ID()));
121
122   std::shared_ptr<GeomAPI_Angle2d> anAng;
123   bool isFirstPnt = aData->attribute(
124     SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized();
125   bool isSecondPnt = aData->attribute(
126     SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized();
127   if (!isFirstPnt || !isSecondPnt)
128     anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
129         aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
130   else {
131     std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
132     bool isReversed1 =
133       aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->value();
134     std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
135     bool isReversed2 =
136       aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->value();
137     anAng = std::shared_ptr<GeomAPI_Angle2d>(
138       new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
139   }
140
141   gp_Pnt2d aFirstPoint = anAng->firstPoint()->impl<gp_Pnt2d>();
142   std::shared_ptr<GeomAPI_Pnt> aPoint = thePlane->to3D(aFirstPoint.X(), aFirstPoint.Y());
143   theFirstPoint = aPoint->impl<gp_Pnt>();
144
145   gp_Pnt2d aCenterPoint = anAng->center()->impl<gp_Pnt2d>();
146   aPoint = thePlane->to3D(aCenterPoint.X(), aCenterPoint.Y());
147   theCenterPoint = aPoint->impl<gp_Pnt>();
148
149   gp_Pnt2d aSecondPoint = anAng->secondPoint()->impl<gp_Pnt2d>();
150   aPoint = thePlane->to3D(aSecondPoint.X(), aSecondPoint.Y());
151   theSecondPoint = aPoint->impl<gp_Pnt>();
152
153   return true;
154 }
155
156
157 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
158                                 const Handle(Prs3d_Presentation)& thePresentation,
159                                 const Standard_Integer theMode)
160 {
161   gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
162   bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane,
163                                         aFirstPoint, aSecondPoint, aCenterPoint);
164   if (aReadyToDisplay) {
165     myFirstPoint = aFirstPoint;
166     mySecondPoint = aSecondPoint;
167     myCenterPoint = aCenterPoint;
168
169     DataPtr aData = myConstraint->data();
170     AttributeDoublePtr anAttributeValue =
171       aData->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
172     myValue.init(anAttributeValue);
173
174     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
175                                 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
176                                 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
177     std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt =
178       mySketcherPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
179     myFlyOutPoint = aFlyoutPnt->impl<gp_Pnt>();
180   }
181
182   DataPtr aData = myConstraint->data();
183   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
184       ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
185   SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
186
187   double aDist = -1;
188   switch (anAngleType) {
189     case SketcherPrs_Tools::ANGLE_DIRECT: {
190 #ifndef COMPILATION_CORRECTION
191       SetArrowsVisibility(AIS_TOAV_Second);
192 #endif
193       SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
194 #ifndef COMPILATION_CORRECTION
195       bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
196       SetType(!isReversedPlanes ? AIS_TOA_Exterior : AIS_TOA_Interior);
197 #endif
198     }
199     break;
200     case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
201       double anEdge1Length = aCenterPoint.Distance(myFirstPoint);
202       //aDist = calculateDistanceToFlyoutPoint();
203       gp_Pnt aFirstPoint = aCenterPoint.Translated(
204                           gp_Vec(myCenterPoint, myFirstPoint).Normalized() * (-anEdge1Length));
205       SetMeasuredGeometry(aFirstPoint, myCenterPoint, mySecondPoint);
206 #ifndef COMPILATION_CORRECTION
207       SetType(AIS_TOA_Interior);
208 #endif
209     }
210     break;
211     case SketcherPrs_Tools::ANGLE_BACKWARD: {
212 #ifndef COMPILATION_CORRECTION
213       SetArrowsVisibility(AIS_TOAV_Second);
214 #endif
215       SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
216       bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
217 #ifndef COMPILATION_CORRECTION
218       SetType(isReversedPlanes ? AIS_TOA_Exterior : AIS_TOA_Interior);
219 #endif
220     }
221     break;
222     default:
223       break;
224   }
225   if (aDist < 0) /// it was not calculated yet
226     aDist = calculateDistanceToFlyoutPoint();
227   SetFlyout(aDist);
228
229   // Update text visualization: parameter value or parameter text
230   myStyleListener->updateDimensions(this, myValue);
231
232   myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
233   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
234
235   AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
236
237   if (!aReadyToDisplay)
238     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
239                               "An empty AIS presentation: SketcherPrs_Angle");
240 }
241
242 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
243                                                    const Standard_Integer theMode)
244 {
245   Standard_Integer aMode;
246   switch (theMode) {
247   case 0: // we should use selection of all objects
248     aMode = 0;
249     break;
250   case SketcherPrs_Tools::Sel_Dimension_All:
251     aMode = 0;
252     break;
253   case SketcherPrs_Tools::Sel_Dimension_Line:
254     aMode = 1;
255     break;
256   case SketcherPrs_Tools::Sel_Dimension_Text:
257     aMode = 2;
258     break;
259   default: {
260     // there are own selection modes, so the others should be ignored
261     // otherwise, the text selection appears in the viewer
262     return;
263   }
264   }
265   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
266   AIS_AngleDimension::ComputeSelection(aSelection, aMode);
267 }
268
269 bool SketcherPrs_Angle::isAnglePlaneReversedToSketchPlane()
270 {
271   bool aReversed = false;
272   if (!mySketcherPlane.get())
273     return aReversed;
274
275   gp_Pln aPlane = GetPlane();
276   gp_Dir aDir = aPlane.Axis().Direction();
277   double aDot = mySketcherPlane->normal()->dot(
278                 std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z())));
279   return aDot < 0;
280 }
281
282 double SketcherPrs_Angle::calculateDistanceToFlyoutPoint()
283 {
284   return myFlyOutPoint.Distance(myCenterPoint);
285 }