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)
49 bool aReadyToDisplay = false;
51 DataPtr aData = theConstraint->data();
54 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
55 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
56 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
57 if (!aFlyoutAttr->isInitialized()) {
58 return aReadyToDisplay; // can not create a good presentation
62 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
63 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
64 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
66 return aReadyToDisplay;
68 std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
70 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
71 // it is possible that circle result becomes zero, in this case the presentation should disappear
72 // for example, it happens when circle radius is set to zero
74 return aReadyToDisplay;
75 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
76 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
77 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
78 AttributeDoublePtr aCircRadius =
79 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
80 aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
81 aRadius = aCircRadius->value();
83 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
84 aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
85 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
86 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
87 (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
88 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
90 std::shared_ptr<GeomAPI_Pnt> aCenter = thePlane->to3D(aCenterAttr->x(), aCenterAttr->y());
91 std::shared_ptr<GeomAPI_Dir> aNormal = thePlane->normal();
93 GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
95 std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(theConstraint, thePlane);
97 gp_Circ aCirc = aCircle.impl<gp_Circ>();
98 gp_Pnt anAncorPnt = anAnchor->impl<gp_Pnt>();
99 // anchor point should not coincide to the location point of the circle
100 // OCCT does not process this case.
102 aReadyToDisplay = anAncorPnt.Distance(aCirc.Location()) > 1e-7;
103 return aReadyToDisplay;
106 void SketcherPrs_Radius::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
107 const Handle(Prs3d_Presentation)& thePresentation,
108 const Standard_Integer theMode)
110 bool aReadyToDisplay = IsReadyToDisplay(myConstraint, mySketcherPlane);
111 if (aReadyToDisplay) {
112 //myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
114 DataPtr aData = myConstraint->data();
117 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
118 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
119 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
122 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
123 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
124 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
126 std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
128 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
129 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
130 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
131 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
132 AttributeDoublePtr aCircRadius =
133 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
134 aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
135 myRadius = aCircRadius->value();
137 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
138 aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
139 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
140 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
141 (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
142 myRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
144 std::shared_ptr<GeomAPI_Pnt> aCenter = mySketcherPlane->to3D(aCenterAttr->x(), aCenterAttr->y());
145 std::shared_ptr<GeomAPI_Dir> aNormal = mySketcherPlane->normal();
147 GeomAPI_Circ aCircle(aCenter, aNormal, myRadius);
148 std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(myConstraint, mySketcherPlane);
150 myCircle = aCircle.impl<gp_Circ>();
151 myAncorPnt = anAnchor->impl<gp_Pnt>();
153 AttributeDoublePtr anAttributeValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
154 myHasParameters = anAttributeValue->usedParameters().size() > 0;
155 myValue = anAttributeValue->text();
158 SetMeasuredGeometry(myCircle, myAncorPnt);
159 SetCustomValue(myRadius);
161 // Update variable aspect parameters (depending on viewer scale)
162 double aTextSize = 0.0;
163 GetValueString(aTextSize);
164 SketcherPrs_Tools::updateArrows(DimensionAspect(), GetValue(), aTextSize);
166 myStyleListener->updateDimensions(this, myHasParameters, myValue);
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 AIS_RadiusDimension::ComputeSelection(aSelection, aMode);