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 #define PI 3.1415926535897932
45 // #define COMPILATION_CORRECTION
48 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
50 SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint,
51 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
52 : AIS_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)), myConstraint(theConstraint),
53 mySketcherPlane(thePlane),
54 myFirstPoint(gp_Pnt(0,0,0)), myCenterPoint(gp_Pnt(1,0,0)), mySecondPoint(gp_Pnt(0,1,0)),
55 myValue(90., false, ""), myFlyOutPoint(0, 0.5, 0)
57 myAspect = new Prs3d_DimensionAspect();
58 myAspect->MakeArrows3d(false);
59 myAspect->MakeText3d(false);
60 myAspect->MakeTextShaded(false);
61 myAspect->MakeUnitsDisplayed(false);
62 myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
63 myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
65 SetDimensionAspect(myAspect);
67 myStyleListener = new SketcherPrs_DimensionStyleListener();
70 SketcherPrs_Angle::~SketcherPrs_Angle()
72 delete myStyleListener;
75 bool SketcherPrs_Angle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
76 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
78 gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
79 return readyToDisplay(theConstraint, thePlane, aFirstPoint, aSecondPoint, aCenterPoint);
82 bool SketcherPrs_Angle::readyToDisplay(ModelAPI_Feature* theConstraint,
83 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
84 gp_Pnt& theFirstPoint, gp_Pnt& theSecondPoint,
85 gp_Pnt& theCenterPoint)
87 bool aReadyToDisplay = false;
89 DataPtr aData = theConstraint->data();
92 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
93 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
94 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
95 if (!aFlyoutAttr->isInitialized())
96 return aReadyToDisplay; // can not create a good presentation
98 AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
99 if (!anAttr1->isInitialized())
100 return aReadyToDisplay;
102 AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
103 if (!anAttr2->isInitialized())
104 return aReadyToDisplay;
107 SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
109 SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
111 if (!aLineA.get() || !aLineB.get())
112 return aReadyToDisplay;
114 std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
115 aLineA->attribute(SketchPlugin_Line::START_ID()));
116 std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
117 aLineA->attribute(SketchPlugin_Line::END_ID()));
118 std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
119 aLineB->attribute(SketchPlugin_Line::START_ID()));
120 std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
121 aLineB->attribute(SketchPlugin_Line::END_ID()));
123 std::shared_ptr<GeomAPI_Angle2d> anAng;
124 bool isFirstPnt = aData->attribute(
125 SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized();
126 bool isSecondPnt = aData->attribute(
127 SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized();
128 if (!isFirstPnt || !isSecondPnt)
129 anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
130 aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
132 std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
134 aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->value();
135 std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
137 aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->value();
138 anAng = std::shared_ptr<GeomAPI_Angle2d>(
139 new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
142 gp_Pnt2d aFirstPoint = anAng->firstPoint()->impl<gp_Pnt2d>();
143 std::shared_ptr<GeomAPI_Pnt> aPoint = thePlane->to3D(aFirstPoint.X(), aFirstPoint.Y());
144 theFirstPoint = aPoint->impl<gp_Pnt>();
146 gp_Pnt2d aCenterPoint = anAng->center()->impl<gp_Pnt2d>();
147 aPoint = thePlane->to3D(aCenterPoint.X(), aCenterPoint.Y());
148 theCenterPoint = aPoint->impl<gp_Pnt>();
150 gp_Pnt2d aSecondPoint = anAng->secondPoint()->impl<gp_Pnt2d>();
151 aPoint = thePlane->to3D(aSecondPoint.X(), aSecondPoint.Y());
152 theSecondPoint = aPoint->impl<gp_Pnt>();
158 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
159 const Handle(Prs3d_Presentation)& thePresentation,
160 const Standard_Integer theMode)
162 gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
163 bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane,
164 aFirstPoint, aSecondPoint, aCenterPoint);
165 if (aReadyToDisplay) {
166 myFirstPoint = aFirstPoint;
167 mySecondPoint = aSecondPoint;
168 myCenterPoint = aCenterPoint;
170 DataPtr aData = myConstraint->data();
171 AttributeDoublePtr anAttributeValue =
172 aData->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
173 myValue.init(anAttributeValue);
175 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
176 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
177 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
178 std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt =
179 mySketcherPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
180 myFlyOutPoint = aFlyoutPnt->impl<gp_Pnt>();
183 DataPtr aData = myConstraint->data();
184 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
185 ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
186 SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
189 switch (anAngleType) {
190 case SketcherPrs_Tools::ANGLE_DIRECT: {
191 #ifndef COMPILATION_CORRECTION
192 SetArrowsVisibility(AIS_TOAV_Second);
194 SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
195 #ifndef COMPILATION_CORRECTION
196 bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
197 SetType(!isReversedPlanes ? AIS_TOA_Exterior : AIS_TOA_Interior);
201 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
202 double anEdge1Length = aCenterPoint.Distance(myFirstPoint);
203 //aDist = calculateDistanceToFlyoutPoint();
204 gp_Pnt aFirstPoint = aCenterPoint.Translated(
205 gp_Vec(myCenterPoint, myFirstPoint).Normalized() * (-anEdge1Length));
206 SetMeasuredGeometry(aFirstPoint, myCenterPoint, mySecondPoint);
207 #ifndef COMPILATION_CORRECTION
208 SetType(AIS_TOA_Interior);
212 case SketcherPrs_Tools::ANGLE_BACKWARD: {
213 #ifndef COMPILATION_CORRECTION
214 SetArrowsVisibility(AIS_TOAV_Second);
216 SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
217 bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
218 #ifndef COMPILATION_CORRECTION
219 SetType(isReversedPlanes ? AIS_TOA_Exterior : AIS_TOA_Interior);
226 if (aDist < 0) /// it was not calculated yet
227 aDist = calculateDistanceToFlyoutPoint();
230 // Update text visualization: parameter value or parameter text
231 myStyleListener->updateDimensions(this, myValue);
233 myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
234 myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
236 AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
238 if (!aReadyToDisplay)
239 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
240 "An empty AIS presentation: SketcherPrs_Angle");
243 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
244 const Standard_Integer theMode)
246 Standard_Integer aMode;
248 case 0: // we should use selection of all objects
251 case SketcherPrs_Tools::Sel_Dimension_All:
254 case SketcherPrs_Tools::Sel_Dimension_Line:
257 case SketcherPrs_Tools::Sel_Dimension_Text:
261 // there are own selection modes, so the others should be ignored
262 // otherwise, the text selection appears in the viewer
266 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
267 AIS_AngleDimension::ComputeSelection(aSelection, aMode);
270 bool SketcherPrs_Angle::isAnglePlaneReversedToSketchPlane()
272 bool aReversed = false;
273 if (!mySketcherPlane.get())
276 gp_Pln aPlane = GetPlane();
277 gp_Dir aDir = aPlane.Axis().Direction();
278 double aDot = mySketcherPlane->normal()->dot(
279 std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z())));
283 double SketcherPrs_Angle::calculateDistanceToFlyoutPoint()
285 return myFlyOutPoint.Distance(myCenterPoint);