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