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"
10 #include <SketchPlugin_ConstraintRadius.h>
11 #include <SketchPlugin_Constraint.h>
12 #include <SketchPlugin_Circle.h>
13 #include <SketchPlugin_Arc.h>
15 #include <Events_Error.h>
17 #include <GeomDataAPI_Point2D.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomAPI_Circ.h>
20 #include <GeomAPI_XYZ.h>
21 #include <ModelAPI_AttributeDouble.h>
23 #include <gp_Circ.hxx>
25 static const gp_Circ MyDefCirc(gp_Ax2(gp_Pnt(0,0,0), gp_Dir(0,0,1)), 1);
27 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Radius, AIS_RadiusDimension);
28 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Radius, AIS_RadiusDimension);
30 SketcherPrs_Radius::SketcherPrs_Radius(ModelAPI_Feature* theConstraint,
31 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
32 : AIS_RadiusDimension(MyDefCirc), myConstraint(theConstraint), myPlane(thePlane)
34 // Set default values of the presentation
35 myAspect = new Prs3d_DimensionAspect();
36 myAspect->MakeArrows3d(false);
37 myAspect->MakeText3d(false);
38 myAspect->MakeTextShaded(false);
39 myAspect->MakeUnitsDisplayed(false);
40 myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
41 myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
43 SetDimensionAspect(myAspect);
44 SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
47 bool SketcherPrs_Radius::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
48 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
50 bool aReadyToDisplay = false;
52 DataPtr aData = theConstraint->data();
55 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
56 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
57 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
58 if (!aFlyoutAttr->isInitialized()) {
59 return aReadyToDisplay; // can not create a good presentation
63 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
64 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
65 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
67 return aReadyToDisplay;
69 std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
71 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
72 // it is possible that circle result becomes zero, in this case the presentation should disappear
73 // for example, it happens when circle radius is set to zero
75 return aReadyToDisplay;
76 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
77 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
78 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
79 AttributeDoublePtr aCircRadius =
80 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
81 aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
82 aRadius = aCircRadius->value();
84 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
85 aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
86 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
87 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
88 (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
89 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
91 std::shared_ptr<GeomAPI_Pnt> aCenter = thePlane->to3D(aCenterAttr->x(), aCenterAttr->y());
92 std::shared_ptr<GeomAPI_Dir> aNormal = thePlane->normal();
94 GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
96 std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(theConstraint, thePlane);
98 gp_Circ aCirc = aCircle.impl<gp_Circ>();
99 gp_Pnt anAncorPnt = anAnchor->impl<gp_Pnt>();
100 // anchor point should not coincide to the location point of the circle
101 // OCCT does not process this case.
103 aReadyToDisplay = anAncorPnt.Distance(aCirc.Location()) > 1e-7;
104 return aReadyToDisplay;
107 void SketcherPrs_Radius::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
108 const Handle(Prs3d_Presentation)& thePresentation,
109 const Standard_Integer theMode)
111 if (!IsReadyToDisplay(myConstraint, myPlane)) {
112 Events_Error::throwException("An empty AIS presentation: SketcherPrs_Radius");
116 DataPtr aData = myConstraint->data();
119 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
120 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
121 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
124 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
125 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
126 (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
128 std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
130 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
131 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
132 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
133 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
134 AttributeDoublePtr aCircRadius =
135 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
136 aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
137 aRadius = aCircRadius->value();
139 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
140 aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
141 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
142 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
143 (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
144 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
146 std::shared_ptr<GeomAPI_Pnt> aCenter = myPlane->to3D(aCenterAttr->x(), aCenterAttr->y());
147 std::shared_ptr<GeomAPI_Dir> aNormal = myPlane->normal();
149 GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
151 std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(myConstraint, myPlane);
153 gp_Circ aCirc = aCircle.impl<gp_Circ>();
154 gp_Pnt anAncorPnt = anAnchor->impl<gp_Pnt>();
156 SetMeasuredGeometry(aCirc, anAncorPnt);
157 SetCustomValue(aRadius);
159 // Update variable aspect parameters (depending on viewer scale)
160 double anArrowLength = myAspect->ArrowAspect()->Length();
161 // This is not realy correct way to get viewer scale.
162 double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
163 double aDimensionValue = GetValue();
164 double aTextSize = 0.0;
165 GetValueString(aTextSize);
167 if(aTextSize > ((aDimensionValue - 2 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
168 myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
169 myAspect->SetArrowOrientation(Prs3d_DAO_External);
170 myAspect->SetExtensionSize(aTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
172 myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
173 myAspect->SetArrowOrientation(Prs3d_DAO_Internal);
175 myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
177 // The value of vertical aligment is sometimes changed
178 myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
180 AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
181 SketcherPrs_Tools::setDisplaySpecialSymbol(this, aValue->usedParameters().size() > 0);
183 AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
186 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
187 const Standard_Integer theMode)
189 // Map the application selection modes to standard ones
190 Standard_Integer aMode;
192 case 0: // we should use selection of all objects
195 case SketcherPrs_Tools::Sel_Dimension_All:
198 case SketcherPrs_Tools::Sel_Dimension_Line:
201 case SketcherPrs_Tools::Sel_Dimension_Text:
205 // there are own selection modes, so the others should be ignored
206 // otherwise, the text selection appears in the viewer
210 AIS_RadiusDimension::ComputeSelection(aSelection, aMode);