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 void SketcherPrs_Radius::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
48 const Handle(Prs3d_Presentation)& thePresentation,
49 const Standard_Integer theMode)
51 DataPtr aData = myConstraint->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 Events_Error::throwException("An empty AIS presentation: SketcherPrs_Radius");
59 return; // 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()));
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
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 = myPlane->to3D(aCenterAttr->x(), aCenterAttr->y());
91 std::shared_ptr<GeomAPI_Dir> aNormal = myPlane->normal();
93 GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
95 std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(myConstraint, myPlane);
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.
101 if (anAncorPnt.Distance(aCirc.Location()) < 1e-7)
103 SetMeasuredGeometry(aCirc, anAncorPnt);
104 SetCustomValue(aRadius);
106 // Update variable aspect parameters (depending on viewer scale)
107 double anArrowLength = myAspect->ArrowAspect()->Length();
108 // This is not realy correct way to get viewer scale.
109 double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
110 double aDimensionValue = GetValue();
111 double aTextSize = 0.0;
112 GetValueString(aTextSize);
114 if(aTextSize > ((aDimensionValue - 2 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
115 myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
116 myAspect->SetArrowOrientation(Prs3d_DAO_External);
117 myAspect->SetExtensionSize(aTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
119 myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
120 myAspect->SetArrowOrientation(Prs3d_DAO_Internal);
122 myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
124 // The value of vertical aligment is sometimes changed
125 myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
127 AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
128 SketcherPrs_Tools::setDisplaySpecialSymbol(this, aValue->usedParameters().size() > 0);
130 AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
133 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
134 const Standard_Integer theMode)
136 // Map the application selection modes to standard ones
137 Standard_Integer aMode;
139 case 0: // we should use selection of all objects
142 case SketcherPrs_Tools::Sel_Dimension_All:
145 case SketcherPrs_Tools::Sel_Dimension_Line:
148 case SketcherPrs_Tools::Sel_Dimension_Text:
152 // there are own selection modes, so the others should be ignored
153 // otherwise, the text selection appears in the viewer
157 AIS_RadiusDimension::ComputeSelection(aSelection, aMode);