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_Radius.h"
21 #include "SketcherPrs_Tools.h"
22 #include "SketcherPrs_DimensionStyleListener.h"
24 #include <SketchPlugin_ConstraintRadius.h>
25 #include <SketchPlugin_Constraint.h>
26 #include <SketchPlugin_Circle.h>
27 #include <SketchPlugin_Arc.h>
29 #include <GeomDataAPI_Point2D.h>
30 #include <GeomAPI_Pnt2d.h>
31 #include <GeomAPI_Circ.h>
32 #include <GeomAPI_XYZ.h>
33 #include <ModelAPI_AttributeDouble.h>
35 #include <gp_Circ.hxx>
37 /// Creates an aspect to be shown in length/radius dimension presentations
38 /// \return an instance of aspect
39 extern Handle(Prs3d_DimensionAspect) createDimensionAspect();
41 /// Update variable aspect parameters (depending on viewer scale)
42 /// \param theDimAspect an aspect to be changed
43 /// \param theDimValue an arrow value
44 /// \param theTextSize an arrow value
45 extern void updateArrows(Handle_Prs3d_DimensionAspect theDimAspect,
46 double theDimValue, double theTextSize);
49 static const gp_Circ MyDefCirc(gp_Ax2(gp_Pnt(0,0,0), gp_Dir(0,0,1)), 1);
51 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Radius, AIS_RadiusDimension);
53 SketcherPrs_Radius::SketcherPrs_Radius(ModelAPI_Feature* theConstraint,
54 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
55 : AIS_RadiusDimension(MyDefCirc), myConstraint(theConstraint), mySketcherPlane(thePlane),
57 myAnchorPoint(gp_Pnt(0, 0, 2)),
60 SetDimensionAspect(createDimensionAspect());
61 myStyleListener = new SketcherPrs_DimensionStyleListener();
64 SketcherPrs_Radius::~SketcherPrs_Radius()
66 delete myStyleListener;
69 bool SketcherPrs_Radius::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
70 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
74 return readyToDisplay(theConstraint, thePlane, aCircle, anAnchorPoint);
77 bool SketcherPrs_Radius::readyToDisplay(ModelAPI_Feature* theConstraint,
78 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
79 gp_Circ& theCircle, gp_Pnt& theAnchorPoint)
81 bool aReadyToDisplay = false;
83 DataPtr aData = theConstraint->data();
86 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
87 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
88 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
89 if (!aFlyoutAttr->isInitialized()) {
90 return aReadyToDisplay; // can not create a good presentation
94 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
95 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
96 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
98 return aReadyToDisplay;
100 std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
103 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
104 // it is possible that circle result becomes zero, in this case the presentation should disappear
105 // for example, it happens when circle radius is set to zero
107 return aReadyToDisplay;
108 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
109 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
110 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
111 AttributeDoublePtr aCircRadius =
112 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
113 aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
114 aRadius = aCircRadius->value();
116 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
117 aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
118 //std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
119 // std::dynamic_pointer_cast<GeomDataAPI_Point2D>
120 // (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
121 //theRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
122 AttributeDoublePtr aCircRadius =
123 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
124 aCyrcFeature->data()->attribute(SketchPlugin_Arc::RADIUS_ID()));
125 aRadius = aCircRadius->value();
127 std::shared_ptr<GeomAPI_Pnt> aCenter = thePlane->to3D(aCenterAttr->x(), aCenterAttr->y());
128 std::shared_ptr<GeomAPI_Dir> aNormal = thePlane->normal();
130 GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
131 std::shared_ptr<GeomAPI_Pnt> anAnchor =
132 SketcherPrs_Tools::getAnchorPoint(theConstraint, thePlane);
134 theCircle = aCircle.impl<gp_Circ>();
135 theAnchorPoint = anAnchor->impl<gp_Pnt>();
137 aReadyToDisplay = theAnchorPoint.Distance(theCircle.Location()) > 1e-7;
139 return aReadyToDisplay;
142 void SketcherPrs_Radius::Compute(
143 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
144 const Handle(Prs3d_Presentation)& thePresentation,
145 const Standard_Integer theMode)
148 gp_Pnt anAnchorPoint;
149 bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aCircle, anAnchorPoint);
150 if (aReadyToDisplay) {
152 myAnchorPoint = anAnchorPoint;
154 DataPtr aData = myConstraint->data();
155 AttributeDoublePtr anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
156 myValue.init(anAttributeValue);
159 SetMeasuredGeometry(myCircle, myAnchorPoint);
160 myStyleListener->updateDimensions(this, myValue);
162 // Update variable aspect parameters (depending on viewer scale)
163 double aTextSize = 0.0;
164 GetValueString(aTextSize);
165 updateArrows(DimensionAspect(), GetValue(), aTextSize);
168 AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
170 if (!aReadyToDisplay)
171 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
172 "An empty AIS presentation: SketcherPrs_Radius");
175 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
176 const Standard_Integer theMode)
178 // Map the application selection modes to standard ones
179 Standard_Integer aMode;
181 case 0: // we should use selection of all objects
184 case SketcherPrs_Tools::Sel_Dimension_All:
187 case SketcherPrs_Tools::Sel_Dimension_Line:
190 case SketcherPrs_Tools::Sel_Dimension_Text:
194 // there are own selection modes, so the others should be ignored
195 // otherwise, the text selection appears in the viewer
199 SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
200 AIS_RadiusDimension::ComputeSelection(aSelection, aMode);