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), myPlane(thePlane)
35 // Set default values of the presentation
36 myAspect = new Prs3d_DimensionAspect();
37 myAspect->MakeArrows3d(false);
38 myAspect->MakeText3d(false);
39 myAspect->MakeTextShaded(false);
40 myAspect->MakeUnitsDisplayed(false);
41 myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
42 myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
44 SetDimensionAspect(myAspect);
45 SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
47 myStyleListener = new SketcherPrs_DimensionStyleListener();
50 SketcherPrs_Radius::~SketcherPrs_Radius()
52 delete myStyleListener;
55 bool SketcherPrs_Radius::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
56 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
58 bool aReadyToDisplay = false;
60 DataPtr aData = theConstraint->data();
63 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
64 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
65 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
66 if (!aFlyoutAttr->isInitialized()) {
67 return aReadyToDisplay; // can not create a good presentation
71 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
72 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
73 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
75 return aReadyToDisplay;
77 std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
79 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
80 // it is possible that circle result becomes zero, in this case the presentation should disappear
81 // for example, it happens when circle radius is set to zero
83 return aReadyToDisplay;
84 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
85 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
86 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
87 AttributeDoublePtr aCircRadius =
88 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
89 aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
90 aRadius = aCircRadius->value();
92 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
93 aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
94 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
95 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
96 (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
97 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
99 std::shared_ptr<GeomAPI_Pnt> aCenter = thePlane->to3D(aCenterAttr->x(), aCenterAttr->y());
100 std::shared_ptr<GeomAPI_Dir> aNormal = thePlane->normal();
102 GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
104 std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(theConstraint, thePlane);
106 gp_Circ aCirc = aCircle.impl<gp_Circ>();
107 gp_Pnt anAncorPnt = anAnchor->impl<gp_Pnt>();
108 // anchor point should not coincide to the location point of the circle
109 // OCCT does not process this case.
111 aReadyToDisplay = anAncorPnt.Distance(aCirc.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)
119 if (!IsReadyToDisplay(myConstraint, myPlane)) {
120 Events_Error::throwException("An empty AIS presentation: SketcherPrs_Radius");
124 DataPtr aData = myConstraint->data();
127 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
128 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
129 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
132 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
133 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
134 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
136 std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
138 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
139 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
140 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
141 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
142 AttributeDoublePtr aCircRadius =
143 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
144 aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
145 aRadius = aCircRadius->value();
147 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
148 aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
149 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
150 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
151 (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
152 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
154 std::shared_ptr<GeomAPI_Pnt> aCenter = myPlane->to3D(aCenterAttr->x(), aCenterAttr->y());
155 std::shared_ptr<GeomAPI_Dir> aNormal = myPlane->normal();
157 GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
159 std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(myConstraint, myPlane);
161 gp_Circ aCirc = aCircle.impl<gp_Circ>();
162 gp_Pnt anAncorPnt = anAnchor->impl<gp_Pnt>();
164 SetMeasuredGeometry(aCirc, anAncorPnt);
165 SetCustomValue(aRadius);
167 // Update variable aspect parameters (depending on viewer scale)
168 double anArrowLength = myAspect->ArrowAspect()->Length();
169 // This is not realy correct way to get viewer scale.
170 double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
171 double aDimensionValue = GetValue();
172 double aTextSize = 0.0;
173 GetValueString(aTextSize);
175 if(aTextSize > ((aDimensionValue - 2 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
176 myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
177 myAspect->SetArrowOrientation(Prs3d_DAO_External);
178 myAspect->SetExtensionSize(aTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
180 myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
181 myAspect->SetArrowOrientation(Prs3d_DAO_Internal);
183 myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
185 // The value of vertical aligment is sometimes changed
186 myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
188 AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
189 SketcherPrs_Tools::setDisplaySpecialSymbol(this, aValue->usedParameters().size() > 0);
191 myStyleListener->updateDimensions(this, aValue);
193 AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
196 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
197 const Standard_Integer theMode)
199 // Map the application selection modes to standard ones
200 Standard_Integer aMode;
202 case 0: // we should use selection of all objects
205 case SketcherPrs_Tools::Sel_Dimension_All:
208 case SketcherPrs_Tools::Sel_Dimension_Line:
211 case SketcherPrs_Tools::Sel_Dimension_Text:
215 // there are own selection modes, so the others should be ignored
216 // otherwise, the text selection appears in the viewer
220 AIS_RadiusDimension::ComputeSelection(aSelection, aMode);