1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketcherPrs_Angle.h"
22 #include "SketcherPrs_DimensionStyleListener.h"
23 #include "SketcherPrs_Tools.h"
25 #include <SketchPlugin_ConstraintAngle.h>
26 #include <SketchPlugin_Constraint.h>
27 #include <SketchPlugin_Line.h>
29 #include <GeomAPI_Edge.h>
30 #include <GeomAPI_Lin.h>
31 #include <GeomDataAPI_Point2D.h>
32 #include <GeomAPI_Angle2d.h>
33 #include <GeomAPI_Lin2d.h>
35 #include <ModelAPI_AttributeRefAttr.h>
36 #include <ModelAPI_AttributeDouble.h>
37 #include <ModelAPI_AttributeInteger.h>
40 #include <BRep_Tool.hxx>
42 /// Update variable aspect parameters (depending on viewer scale)
43 /// \param theDimAspect an aspect to be changed
44 /// \param theDimValue an arrow value
45 /// \param theTextSize an arrow value
46 extern void updateArrows(Handle_Prs3d_DimensionAspect theDimAspect,
47 double theDimValue, double theTextSize, SketcherPrs_Tools::LocationType theLocationType);
49 #define PI 3.1415926535897932
52 // #define COMPILATION_CORRECTION
55 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
57 SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint,
58 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
59 : AIS_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)), myConstraint(theConstraint),
60 mySketcherPlane(thePlane),
61 myFirstPoint(gp_Pnt(0,0,0)), myCenterPoint(gp_Pnt(1,0,0)), mySecondPoint(gp_Pnt(0,1,0)),
62 myValue(90., false, ""), myFlyOutPoint(0, 0.5, 0)
64 myAspect = new Prs3d_DimensionAspect();
65 myAspect->MakeArrows3d(false);
66 myAspect->MakeText3d(false);
67 myAspect->MakeTextShaded(false);
68 myAspect->MakeUnitsDisplayed(false);
69 myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getConfigTextHeight());
70 myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
72 SetDimensionAspect(myAspect);
74 myStyleListener = new SketcherPrs_DimensionStyleListener();
77 SketcherPrs_Angle::~SketcherPrs_Angle()
79 delete myStyleListener;
82 bool SketcherPrs_Angle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
83 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
85 gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
86 return readyToDisplay(theConstraint, thePlane, aFirstPoint, aSecondPoint, aCenterPoint);
89 bool SketcherPrs_Angle::readyToDisplay(ModelAPI_Feature* theConstraint,
90 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
91 gp_Pnt& theFirstPoint, gp_Pnt& theSecondPoint,
92 gp_Pnt& theCenterPoint)
94 bool aReadyToDisplay = false;
96 DataPtr aData = theConstraint->data();
99 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
100 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
101 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
102 if (!aFlyoutAttr->isInitialized())
103 return aReadyToDisplay; // can not create a good presentation
105 AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
106 if (!anAttr1->isInitialized())
107 return aReadyToDisplay;
109 AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
110 if (!anAttr2->isInitialized())
111 return aReadyToDisplay;
114 SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
116 SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
118 if (!aLineA.get() || !aLineB.get())
119 return aReadyToDisplay;
121 std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
122 aLineA->attribute(SketchPlugin_Line::START_ID()));
123 std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
124 aLineA->attribute(SketchPlugin_Line::END_ID()));
125 std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
126 aLineB->attribute(SketchPlugin_Line::START_ID()));
127 std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
128 aLineB->attribute(SketchPlugin_Line::END_ID()));
130 std::shared_ptr<GeomAPI_Angle2d> anAng;
131 bool isFirstPnt = aData->attribute(
132 SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized();
133 bool isSecondPnt = aData->attribute(
134 SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized();
135 if (!isFirstPnt || !isSecondPnt)
136 anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
137 aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
139 std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
141 aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->value();
142 std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
144 aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->value();
145 anAng = std::shared_ptr<GeomAPI_Angle2d>(
146 new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
149 gp_Pnt2d aFirstPoint = anAng->firstPoint()->impl<gp_Pnt2d>();
150 std::shared_ptr<GeomAPI_Pnt> aPoint = thePlane->to3D(aFirstPoint.X(), aFirstPoint.Y());
151 theFirstPoint = aPoint->impl<gp_Pnt>();
153 gp_Pnt2d aCenterPoint = anAng->center()->impl<gp_Pnt2d>();
154 aPoint = thePlane->to3D(aCenterPoint.X(), aCenterPoint.Y());
155 theCenterPoint = aPoint->impl<gp_Pnt>();
157 gp_Pnt2d aSecondPoint = anAng->secondPoint()->impl<gp_Pnt2d>();
158 aPoint = thePlane->to3D(aSecondPoint.X(), aSecondPoint.Y());
159 theSecondPoint = aPoint->impl<gp_Pnt>();
165 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
166 const Handle(Prs3d_Presentation)& thePresentation,
167 const Standard_Integer theMode)
169 gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
170 bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane,
171 aFirstPoint, aSecondPoint, aCenterPoint);
172 if (aReadyToDisplay) {
173 myFirstPoint = aFirstPoint;
174 mySecondPoint = aSecondPoint;
175 myCenterPoint = aCenterPoint;
177 DataPtr aData = myConstraint->data();
178 AttributeDoublePtr anAttributeValue =
179 aData->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
180 myValue.init(anAttributeValue);
182 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
183 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
184 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
185 std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt =
186 mySketcherPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
187 myFlyOutPoint = aFlyoutPnt->impl<gp_Pnt>();
190 DataPtr aData = myConstraint->data();
191 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
192 ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
193 SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
196 switch (anAngleType) {
197 case SketcherPrs_Tools::ANGLE_DIRECT: {
198 #ifndef COMPILATION_CORRECTION
199 SetArrowsVisibility(AIS_TOAV_Second);
201 SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
202 #ifndef COMPILATION_CORRECTION
203 bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
204 SetType(!isReversedPlanes ? AIS_TOA_Exterior : AIS_TOA_Interior);
208 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
209 double anEdge1Length = aCenterPoint.Distance(myFirstPoint);
210 //aDist = calculateDistanceToFlyoutPoint();
211 gp_Pnt aFirstPoint = aCenterPoint.Translated(
212 gp_Vec(myCenterPoint, myFirstPoint).Normalized() * (-anEdge1Length));
213 SetMeasuredGeometry(aFirstPoint, myCenterPoint, mySecondPoint);
214 #ifndef COMPILATION_CORRECTION
215 SetType(AIS_TOA_Interior);
219 case SketcherPrs_Tools::ANGLE_BACKWARD: {
220 #ifndef COMPILATION_CORRECTION
221 SetArrowsVisibility(AIS_TOAV_Second);
223 SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
224 bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
225 #ifndef COMPILATION_CORRECTION
226 SetType(isReversedPlanes ? AIS_TOA_Exterior : AIS_TOA_Interior);
233 if (aDist < 0) /// it was not calculated yet
234 aDist = calculateDistanceToFlyoutPoint();
237 // Update text visualization: parameter value or parameter text
238 myStyleListener->updateDimensions(this, myValue);
240 double aTextSize = 0.0;
241 GetValueString(aTextSize);
242 AttributeIntegerPtr aLocationTypeAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>
243 (myConstraint->data()->attribute(SketchPlugin_ConstraintAngle::LOCATION_TYPE_ID()));
244 updateArrows(myAspect, GetValue(), aTextSize,
245 (SketcherPrs_Tools::LocationType)(aLocationTypeAttr->value()));
247 AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
249 if (!aReadyToDisplay)
250 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
251 "An empty AIS presentation: SketcherPrs_Angle");
254 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
255 const Standard_Integer theMode)
257 Standard_Integer aMode;
259 case 0: // we should use selection of all objects
262 case SketcherPrs_Tools::Sel_Dimension_All:
265 case SketcherPrs_Tools::Sel_Dimension_Line:
268 case SketcherPrs_Tools::Sel_Dimension_Text:
272 // there are own selection modes, so the others should be ignored
273 // otherwise, the text selection appears in the viewer
277 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
278 AIS_AngleDimension::ComputeSelection(aSelection, aMode);
281 bool SketcherPrs_Angle::isAnglePlaneReversedToSketchPlane()
283 bool aReversed = false;
284 if (!mySketcherPlane.get())
287 gp_Pln aPlane = GetPlane();
288 gp_Dir aDir = aPlane.Axis().Direction();
289 double aDot = mySketcherPlane->normal()->dot(
290 std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z())));
294 double SketcherPrs_Angle::calculateDistanceToFlyoutPoint()
296 return myFlyOutPoint.Distance(myCenterPoint);