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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "SketcherPrs_Angle.h"
21 #include "SketcherPrs_DimensionStyleListener.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 #define PI 3.1415926535897932
44 // #define COMPILATION_CORRECTION
47 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
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)
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());
64 SetDimensionAspect(myAspect);
66 myStyleListener = new SketcherPrs_DimensionStyleListener();
69 SketcherPrs_Angle::~SketcherPrs_Angle()
71 delete myStyleListener;
74 bool SketcherPrs_Angle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
75 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
77 gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
78 return readyToDisplay(theConstraint, thePlane, aFirstPoint, aSecondPoint, aCenterPoint);
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)
86 bool aReadyToDisplay = false;
88 DataPtr aData = theConstraint->data();
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
97 AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
98 if (!anAttr1->isInitialized())
99 return aReadyToDisplay;
101 AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
102 if (!anAttr2->isInitialized())
103 return aReadyToDisplay;
106 SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
108 SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
110 if (!aLineA.get() || !aLineB.get())
111 return aReadyToDisplay;
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()));
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()));
131 std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
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()));
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));
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>();
145 gp_Pnt2d aCenterPoint = anAng->center()->impl<gp_Pnt2d>();
146 aPoint = thePlane->to3D(aCenterPoint.X(), aCenterPoint.Y());
147 theCenterPoint = aPoint->impl<gp_Pnt>();
149 gp_Pnt2d aSecondPoint = anAng->secondPoint()->impl<gp_Pnt2d>();
150 aPoint = thePlane->to3D(aSecondPoint.X(), aSecondPoint.Y());
151 theSecondPoint = aPoint->impl<gp_Pnt>();
157 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
158 const Handle(Prs3d_Presentation)& thePresentation,
159 const Standard_Integer theMode)
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;
169 DataPtr aData = myConstraint->data();
170 AttributeDoublePtr anAttributeValue =
171 aData->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
172 myValue.init(anAttributeValue);
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>();
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());
188 switch (anAngleType) {
189 case SketcherPrs_Tools::ANGLE_DIRECT: {
190 #ifndef COMPILATION_CORRECTION
191 SetArrowsVisibility(AIS_TOAV_Second);
193 SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
194 #ifndef COMPILATION_CORRECTION
195 bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
196 SetType(!isReversedPlanes ? AIS_TOA_Exterior : AIS_TOA_Interior);
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);
211 case SketcherPrs_Tools::ANGLE_BACKWARD: {
212 #ifndef COMPILATION_CORRECTION
213 SetArrowsVisibility(AIS_TOAV_Second);
215 SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
216 bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
217 #ifndef COMPILATION_CORRECTION
218 SetType(isReversedPlanes ? AIS_TOA_Exterior : AIS_TOA_Interior);
225 if (aDist < 0) /// it was not calculated yet
226 aDist = calculateDistanceToFlyoutPoint();
229 // Update text visualization: parameter value or parameter text
230 myStyleListener->updateDimensions(this, myValue);
232 myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
233 myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
235 AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
237 if (!aReadyToDisplay)
238 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
239 "An empty AIS presentation: SketcherPrs_Angle");
242 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
243 const Standard_Integer theMode)
245 Standard_Integer aMode;
247 case 0: // we should use selection of all objects
250 case SketcherPrs_Tools::Sel_Dimension_All:
253 case SketcherPrs_Tools::Sel_Dimension_Line:
256 case SketcherPrs_Tools::Sel_Dimension_Text:
260 // there are own selection modes, so the others should be ignored
261 // otherwise, the text selection appears in the viewer
265 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
266 AIS_AngleDimension::ComputeSelection(aSelection, aMode);
269 bool SketcherPrs_Angle::isAnglePlaneReversedToSketchPlane()
271 bool aReversed = false;
272 if (!mySketcherPlane.get())
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())));
282 double SketcherPrs_Angle::calculateDistanceToFlyoutPoint()
284 return myFlyOutPoint.Distance(myCenterPoint);