1 // Copyright (C) 2014-2019 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_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>
34 #include <ModelAPI_AttributeInteger.h>
36 #include <gp_Circ.hxx>
38 /// Creates an aspect to be shown in length/radius dimension presentations
39 /// \return an instance of aspect
40 extern Handle(Prs3d_DimensionAspect) createDimensionAspect();
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);
50 static const gp_Circ MyDefCirc(gp_Ax2(gp_Pnt(0,0,0), gp_Dir(0,0,1)), 1);
52 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Radius, AIS_RadiusDimension);
54 SketcherPrs_Radius::SketcherPrs_Radius(ModelAPI_Feature* theConstraint,
55 SketchPlugin_Sketch* theSketcher)
56 : AIS_RadiusDimension(MyDefCirc), myConstraint(theConstraint), mySketcher(theSketcher),
58 myAnchorPoint(gp_Pnt(0, 0, 2)),
61 SetDimensionAspect(createDimensionAspect());
62 myStyleListener = new SketcherPrs_DimensionStyleListener();
65 SketcherPrs_Radius::~SketcherPrs_Radius()
67 delete myStyleListener;
70 bool SketcherPrs_Radius::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
71 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
75 return readyToDisplay(theConstraint, thePlane, aCircle, anAnchorPoint);
78 bool SketcherPrs_Radius::readyToDisplay(ModelAPI_Feature* theConstraint,
79 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
80 gp_Circ& theCircle, gp_Pnt& theAnchorPoint)
82 bool aReadyToDisplay = false;
84 DataPtr aData = theConstraint->data();
87 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
88 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
89 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
90 if (!aFlyoutAttr->isInitialized()) {
91 return aReadyToDisplay; // can not create a good presentation
95 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
96 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
97 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
99 return aReadyToDisplay;
101 std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
104 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
105 // it is possible that circle result becomes zero, in this case the presentation should disappear
106 // for example, it happens when circle radius is set to zero
108 return aReadyToDisplay;
109 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
110 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
111 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
112 AttributeDoublePtr aCircRadius =
113 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
114 aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
115 aRadius = aCircRadius->value();
117 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
118 aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
119 //std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
120 // std::dynamic_pointer_cast<GeomDataAPI_Point2D>
121 // (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
122 //theRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
123 AttributeDoublePtr aCircRadius =
124 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
125 aCyrcFeature->data()->attribute(SketchPlugin_Arc::RADIUS_ID()));
126 aRadius = aCircRadius->value();
128 std::shared_ptr<GeomAPI_Pnt> aCenter = thePlane->to3D(aCenterAttr->x(), aCenterAttr->y());
129 std::shared_ptr<GeomAPI_Dir> aNormal = thePlane->normal();
131 GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
132 std::shared_ptr<GeomAPI_Pnt> anAnchor =
133 SketcherPrs_Tools::getAnchorPoint(theConstraint, thePlane);
135 theCircle = aCircle.impl<gp_Circ>();
136 theAnchorPoint = anAnchor->impl<gp_Pnt>();
138 aReadyToDisplay = theAnchorPoint.Distance(theCircle.Location()) > 1e-7;
140 return aReadyToDisplay;
143 void SketcherPrs_Radius::Compute(
144 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
145 const Handle(Prs3d_Presentation)& thePresentation,
146 const Standard_Integer theMode)
151 gp_Pnt anAnchorPoint;
152 bool aReadyToDisplay = readyToDisplay(myConstraint, plane(), aCircle, anAnchorPoint);
153 if (aReadyToDisplay) {
155 myAnchorPoint = anAnchorPoint;
157 DataPtr aData = myConstraint->data();
158 AttributeDoublePtr anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
159 myValue.init(anAttributeValue);
162 SetMeasuredGeometry(myCircle, myAnchorPoint);
163 myStyleListener->updateDimensions(this, myValue);
165 // Update variable aspect parameters (depending on viewer scale)
166 double aTextSize = 0.0;
167 GetValueString(aTextSize);
169 AttributeIntegerPtr aLocAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>
170 (myConstraint->data()->attribute(SketchPlugin_ConstraintRadius::LOCATION_TYPE_ID()));
171 SketcherPrs_Tools::LocationType aLocationType = aLocAttr->isInitialized() ?
172 (SketcherPrs_Tools::LocationType)(aLocAttr->value()) : SketcherPrs_Tools::LOCATION_AUTOMATIC;
173 updateArrows(DimensionAspect(), GetValue(), aTextSize, aLocationType);
175 AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
177 if (!aReadyToDisplay)
178 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
179 "An empty AIS presentation: SketcherPrs_Radius");
182 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
183 const Standard_Integer theMode)
185 // Map the application selection modes to standard ones
186 Standard_Integer aMode;
188 case 0: // we should use selection of all objects
191 case SketcherPrs_Tools::Sel_Dimension_All:
194 case SketcherPrs_Tools::Sel_Dimension_Line:
197 case SketcherPrs_Tools::Sel_Dimension_Text:
201 // there are own selection modes, so the others should be ignored
202 // otherwise, the text selection appears in the viewer
206 SetSelToleranceForText2d(SketcherPrs_Tools::getArrowSize()/5.);
207 AIS_RadiusDimension::ComputeSelection(aSelection, aMode);