1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketcherPrs_Radius.cpp
4 // Created: 26 March 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "SketcherPrs_Radius.h"
8 #include "SketcherPrs_Tools.h"
9 #include "SketcherPrs_DimensionStyleListener.h"
11 #include <SketchPlugin_ConstraintRadius.h>
12 #include <SketchPlugin_Constraint.h>
13 #include <SketchPlugin_Circle.h>
14 #include <SketchPlugin_Arc.h>
16 #include <Events_Error.h>
18 #include <GeomDataAPI_Point2D.h>
19 #include <GeomAPI_Pnt2d.h>
20 #include <GeomAPI_Circ.h>
21 #include <GeomAPI_XYZ.h>
22 #include <ModelAPI_AttributeDouble.h>
24 #include <gp_Circ.hxx>
26 static const gp_Circ MyDefCirc(gp_Ax2(gp_Pnt(0,0,0), gp_Dir(0,0,1)), 1);
28 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Radius, AIS_RadiusDimension);
29 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Radius, AIS_RadiusDimension);
31 SketcherPrs_Radius::SketcherPrs_Radius(ModelAPI_Feature* theConstraint,
32 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
33 : AIS_RadiusDimension(MyDefCirc), myConstraint(theConstraint), mySketcherPlane(thePlane)
35 SetDimensionAspect(SketcherPrs_Tools::createDimensionAspect());
36 SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
38 myStyleListener = new SketcherPrs_DimensionStyleListener();
41 SketcherPrs_Radius::~SketcherPrs_Radius()
43 delete myStyleListener;
46 bool SketcherPrs_Radius::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
47 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
52 return readyToDisplay(theConstraint, thePlane, aCircle, anAnchorPoint, aRadius);
55 bool SketcherPrs_Radius::readyToDisplay(ModelAPI_Feature* theConstraint,
56 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
57 gp_Circ& theCircle, gp_Pnt& theAnchorPoint,
60 bool aReadyToDisplay = false;
62 DataPtr aData = theConstraint->data();
65 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
66 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
67 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
68 if (!aFlyoutAttr->isInitialized()) {
69 return aReadyToDisplay; // can not create a good presentation
73 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
74 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
75 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
77 return aReadyToDisplay;
79 std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
81 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
82 // it is possible that circle result becomes zero, in this case the presentation should disappear
83 // for example, it happens when circle radius is set to zero
85 return aReadyToDisplay;
86 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
87 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
88 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
89 AttributeDoublePtr aCircRadius =
90 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
91 aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
92 theRadius = aCircRadius->value();
94 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
95 aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
96 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
97 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
98 (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
99 theRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
101 std::shared_ptr<GeomAPI_Pnt> aCenter = thePlane->to3D(aCenterAttr->x(), aCenterAttr->y());
102 std::shared_ptr<GeomAPI_Dir> aNormal = thePlane->normal();
104 GeomAPI_Circ aCircle(aCenter, aNormal, theRadius);
105 std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(theConstraint, thePlane);
107 theCircle = aCircle.impl<gp_Circ>();
108 theAnchorPoint = anAnchor->impl<gp_Pnt>();
110 aReadyToDisplay = theAnchorPoint.Distance(theCircle.Location()) > 1e-7;
112 return aReadyToDisplay;
115 void SketcherPrs_Radius::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
116 const Handle(Prs3d_Presentation)& thePresentation,
117 const Standard_Integer theMode)
120 gp_Pnt anAnchorPoint;
122 bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aCircle, anAnchorPoint, aRadius);
123 if (aReadyToDisplay) {
125 myAnchorPoint = anAnchorPoint;
128 AttributeDoublePtr anAttributeValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
129 myHasParameters = anAttributeValue->usedParameters().size() > 0;
130 myValue = anAttributeValue->text();
133 SetMeasuredGeometry(myCircle, myAnchorPoint);
134 SetCustomValue(myRadius);
136 // Update variable aspect parameters (depending on viewer scale)
137 double aTextSize = 0.0;
138 GetValueString(aTextSize);
139 SketcherPrs_Tools::updateArrows(DimensionAspect(), GetValue(), aTextSize);
141 myStyleListener->updateDimensions(this, myHasParameters, myValue);
143 AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
145 if (!aReadyToDisplay)
146 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
147 "An empty AIS presentation: SketcherPrs_Radius");
150 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
151 const Standard_Integer theMode)
153 // Map the application selection modes to standard ones
154 Standard_Integer aMode;
156 case 0: // we should use selection of all objects
159 case SketcherPrs_Tools::Sel_Dimension_All:
162 case SketcherPrs_Tools::Sel_Dimension_Line:
165 case SketcherPrs_Tools::Sel_Dimension_Text:
169 // there are own selection modes, so the others should be ignored
170 // otherwise, the text selection appears in the viewer
174 AIS_RadiusDimension::ComputeSelection(aSelection, aMode);