1 // Copyright (C) 2014-2022 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 email : webmaster.salome@opencascade.com
20 #include "SketcherPrs_Angle.h"
21 #include "SketcherPrs_DimensionStyle.h"
22 #include "SketcherPrs_Tools.h"
24 #include <SketchPlugin_ConstraintAngle.h>
25 #include <SketchPlugin_Constraint.h>
26 #include <SketchPlugin_Line.h>
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>
34 #include <ModelAPI_AttributeRefAttr.h>
35 #include <ModelAPI_AttributeDouble.h>
36 #include <ModelAPI_AttributeInteger.h>
39 #include <BRep_Tool.hxx>
41 #include <Standard_Version.hxx>
43 #if OCC_VERSION_HEX > 0x070400
44 #include <PrsDim_TypeOfAngleArrowVisibility.hxx>
47 /// Update variable aspect parameters (depending on viewer scale)
48 /// \param theDimAspect an aspect to be changed
49 /// \param theDimValue an arrow value
50 /// \param theTextSize an arrow value
51 extern void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect,
52 double theDimValue, double theTextSize, SketcherPrs_Tools::LocationType theLocationType);
54 #define PI 3.1415926535897932
57 // #define COMPILATION_CORRECTION
60 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, PrsDim_AngleDimension);
62 SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint,
63 SketchPlugin_Sketch* theSketcher)
64 : PrsDim_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)),
65 myConstraint(theConstraint),
66 mySketcher(theSketcher),
67 myFirstPoint(gp_Pnt(0,0,0)),
68 mySecondPoint(gp_Pnt(0,1,0)),
69 myCenterPoint(gp_Pnt(1, 0, 0)),
70 myFlyOutPoint(0, 0.5, 0),
71 myValue(90., false, "")
73 myAspect = new Prs3d_DimensionAspect();
74 myAspect->MakeArrows3d(false);
75 myAspect->MakeText3d(false);
76 myAspect->MakeTextShaded(false);
77 myAspect->MakeUnitsDisplayed(false);
78 myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getConfigTextHeight());
79 myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
81 SetDimensionAspect(myAspect);
83 myStyleListener = new SketcherPrs_DimensionStyle();
86 SketcherPrs_Angle::~SketcherPrs_Angle()
88 delete myStyleListener;
91 bool SketcherPrs_Angle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
92 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
94 gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
95 return readyToDisplay(theConstraint, thePlane, aFirstPoint, aSecondPoint, aCenterPoint);
98 bool SketcherPrs_Angle::readyToDisplay(ModelAPI_Feature* theConstraint,
99 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
100 gp_Pnt& theFirstPoint, gp_Pnt& theSecondPoint,
101 gp_Pnt& theCenterPoint)
103 bool aReadyToDisplay = false;
105 return aReadyToDisplay;
107 DataPtr aData = theConstraint->data();
110 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
111 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
112 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
113 if (!aFlyoutAttr->isInitialized())
114 return aReadyToDisplay; // can not create a good presentation
116 AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
117 if (!anAttr1->isInitialized())
118 return aReadyToDisplay;
120 AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
121 if (!anAttr2->isInitialized())
122 return aReadyToDisplay;
125 SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
127 SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
129 if (!aLineA.get() || !aLineB.get())
130 return aReadyToDisplay;
132 std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
133 aLineA->attribute(SketchPlugin_Line::START_ID()));
134 std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
135 aLineA->attribute(SketchPlugin_Line::END_ID()));
136 std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
137 aLineB->attribute(SketchPlugin_Line::START_ID()));
138 std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
139 aLineB->attribute(SketchPlugin_Line::END_ID()));
141 std::shared_ptr<GeomAPI_Angle2d> anAng;
142 bool isFirstPnt = aData->attribute(
143 SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized();
144 bool isSecondPnt = aData->attribute(
145 SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized();
146 if (!isFirstPnt || !isSecondPnt)
147 anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
148 aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
150 std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
152 aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->value();
153 std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
155 aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->value();
157 if (aData->integer(SketchPlugin_ConstraintAngle::TYPE_ID())->value() ==
158 (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY)
159 isReversed1 = !isReversed1;
161 anAng = std::shared_ptr<GeomAPI_Angle2d>(
162 new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
165 gp_Pnt2d aCenterPoint = anAng->center()->impl<gp_Pnt2d>();
166 gp_Pnt2d aFirstPoint, aSecondPoint;
168 if (anAng->angleRadian() > 0.0) {
169 aFirstPoint = anAng->firstPoint()->impl<gp_Pnt2d>();
170 aSecondPoint = anAng->secondPoint()->impl<gp_Pnt2d>();
173 aFirstPoint = anAng->secondPoint()->impl<gp_Pnt2d>();
174 aSecondPoint = anAng->firstPoint()->impl<gp_Pnt2d>();
178 // one of angle lines is degenerated, the presentation cannot be shown
182 if (aData->integer(SketchPlugin_ConstraintAngle::TYPE_ID())->value() ==
183 (int)SketcherPrs_Tools::ANGLE_BACKWARD)
184 std::swap(aFirstPoint, aSecondPoint);
186 std::shared_ptr<GeomAPI_Pnt> aPoint = thePlane->to3D(aFirstPoint.X(), aFirstPoint.Y());
187 theFirstPoint = aPoint->impl<gp_Pnt>();
189 aPoint = thePlane->to3D(aCenterPoint.X(), aCenterPoint.Y());
190 theCenterPoint = aPoint->impl<gp_Pnt>();
192 aPoint = thePlane->to3D(aSecondPoint.X(), aSecondPoint.Y());
193 theSecondPoint = aPoint->impl<gp_Pnt>();
199 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
200 const Handle(Prs3d_Presentation)& thePresentation,
201 const Standard_Integer theMode)
206 DataPtr aData = myConstraint->data();
208 gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
209 bool aReadyToDisplay = readyToDisplay(myConstraint, plane(),
210 aFirstPoint, aSecondPoint, aCenterPoint);
211 if (aReadyToDisplay) {
212 myFirstPoint = aFirstPoint;
213 mySecondPoint = aSecondPoint;
214 myCenterPoint = aCenterPoint;
216 AttributeDoublePtr anAttributeValue =
217 aData->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
218 myValue.init(anAttributeValue);
220 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
221 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
222 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
223 std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt =
224 plane()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
225 myFlyOutPoint = aFlyoutPnt->impl<gp_Pnt>();
229 #ifndef COMPILATION_CORRECTION
230 #if OCC_VERSION_HEX > 0x070400
231 SetArrowsVisibility(PrsDim_TypeOfAngleArrowVisibility_Both);
233 SetArrowsVisibility(AIS_TOAV_Both);
236 SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
237 #ifndef COMPILATION_CORRECTION
238 bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
239 #if OCC_VERSION_HEX > 0x070400
240 SetType(!isReversedPlanes ? PrsDim_TypeOfAngle_Exterior : PrsDim_TypeOfAngle_Interior);
242 SetType(!isReversedPlanes ? AIS_TOA_Exterior : AIS_TOA_Interior);
245 if (aDist < 0) /// it was not calculated yet
246 aDist = calculateDistanceToFlyoutPoint();
249 // Update text visualization: parameter value or parameter text
250 myStyleListener->updateDimensions(this, myValue);
252 double aTextSize = 0.0;
253 GetValueString(aTextSize);
254 AttributeIntegerPtr aLocAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>
255 (myConstraint->data()->attribute(SketchPlugin_ConstraintAngle::LOCATION_TYPE_ID()));
256 SketcherPrs_Tools::LocationType aLocationType = aLocAttr->isInitialized() ?
257 (SketcherPrs_Tools::LocationType)(aLocAttr->value()) : SketcherPrs_Tools::LOCATION_AUTOMATIC;
259 double aRadius = myCenterPoint.Translated(
260 gp_Vec(myCenterPoint, myFirstPoint).Normalized()*aDist).Distance(myCenterPoint);
261 double anAngleValue = myValue.myDoubleValue;
262 double anAngleCircleLength = aRadius * anAngleValue * PI / 180.;
264 updateArrows(myAspect, anAngleCircleLength, aTextSize, aLocationType);
266 PrsDim_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
268 if (!aReadyToDisplay)
269 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
270 "An empty AIS presentation: SketcherPrs_Angle");
273 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
274 const Standard_Integer theMode)
276 Standard_Integer aMode;
278 case 0: // we should use selection of all objects
281 case SketcherPrs_Tools::Sel_Dimension_All:
284 case SketcherPrs_Tools::Sel_Dimension_Line:
287 case SketcherPrs_Tools::Sel_Dimension_Text:
291 // there are own selection modes, so the others should be ignored
292 // otherwise, the text selection appears in the viewer
296 SetSelToleranceForText2d(SketcherPrs_Tools::getArrowSize()/5.);
297 PrsDim_AngleDimension::ComputeSelection(aSelection, aMode);
300 bool SketcherPrs_Angle::isAnglePlaneReversedToSketchPlane()
302 bool aReversed = false;
306 gp_Pln aPlane = GetPlane();
307 gp_Dir aDir = aPlane.Axis().Direction();
308 double aDot = plane()->normal()->dot(
309 std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z())));
313 double SketcherPrs_Angle::calculateDistanceToFlyoutPoint()
315 return myFlyOutPoint.Distance(myCenterPoint);