Salome HOME
Issue #1629 : it is not anymore possible to select entities first in the sketcher...
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Radius.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_Radius.cpp
4 // Created:     26 March 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_Radius.h"
8 #include "SketcherPrs_Tools.h"
9 #include "SketcherPrs_DimensionStyleListener.h"
10
11 #include <SketchPlugin_ConstraintRadius.h>
12 #include <SketchPlugin_Constraint.h>
13 #include <SketchPlugin_Circle.h>
14 #include <SketchPlugin_Arc.h>
15
16 #include <GeomDataAPI_Point2D.h>
17 #include <GeomAPI_Pnt2d.h>
18 #include <GeomAPI_Circ.h>
19 #include <GeomAPI_XYZ.h>
20 #include <ModelAPI_AttributeDouble.h>
21
22 #include <gp_Circ.hxx>
23
24 static const gp_Circ MyDefCirc(gp_Ax2(gp_Pnt(0,0,0), gp_Dir(0,0,1)), 1);
25
26 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Radius, AIS_RadiusDimension);
27 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Radius, AIS_RadiusDimension);
28
29 SketcherPrs_Radius::SketcherPrs_Radius(ModelAPI_Feature* theConstraint, 
30                                        const std::shared_ptr<GeomAPI_Ax3>& thePlane)
31 : AIS_RadiusDimension(MyDefCirc), myConstraint(theConstraint), mySketcherPlane(thePlane),
32   myCircle(MyDefCirc),
33   myAnchorPoint(gp_Pnt(0, 0, 2)),
34   myHasParameters(false),
35   myValue(""),
36   myRadius(1)
37 {
38   SetDimensionAspect(SketcherPrs_Tools::createDimensionAspect());
39   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
40
41   myStyleListener = new SketcherPrs_DimensionStyleListener();
42 }
43
44 SketcherPrs_Radius::~SketcherPrs_Radius()
45 {
46   delete myStyleListener;
47 }
48
49 bool SketcherPrs_Radius::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
50                                           const std::shared_ptr<GeomAPI_Ax3>& thePlane)
51 {
52   gp_Circ aCircle;
53   gp_Pnt anAnchorPoint;
54   double aRadius;
55   return readyToDisplay(theConstraint, thePlane, aCircle, anAnchorPoint, aRadius);
56 }
57
58 bool SketcherPrs_Radius::readyToDisplay(ModelAPI_Feature* theConstraint,
59                                         const std::shared_ptr<GeomAPI_Ax3>& thePlane,
60                                         gp_Circ& theCircle, gp_Pnt& theAnchorPoint,
61                                         double& theRadius)
62 {
63   bool aReadyToDisplay = false;
64
65   DataPtr aData = theConstraint->data();
66
67   // Flyout point
68   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
69     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
70     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
71   if (!aFlyoutAttr->isInitialized()) {
72     return aReadyToDisplay; // can not create a good presentation
73   }
74
75   // Get circle
76   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
77     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
78     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
79   if (!anAttr)
80     return aReadyToDisplay;
81
82   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
83   theRadius = 1;
84   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
85   // it is possible that circle result becomes zero, in this case the presentation should disappear
86   // for example, it happens when circle radius is set to zero
87   if (!aCyrcFeature)
88     return aReadyToDisplay;
89   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
90     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
91         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
92     AttributeDoublePtr aCircRadius = 
93       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
94       aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
95     theRadius = aCircRadius->value();
96   } else { // arc
97     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
98         aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
99     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
100       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
101       (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
102     theRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
103   }
104   std::shared_ptr<GeomAPI_Pnt> aCenter = thePlane->to3D(aCenterAttr->x(), aCenterAttr->y());
105   std::shared_ptr<GeomAPI_Dir> aNormal = thePlane->normal();
106
107   GeomAPI_Circ aCircle(aCenter, aNormal, theRadius);
108   std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(theConstraint, thePlane);
109
110   theCircle = aCircle.impl<gp_Circ>();
111   theAnchorPoint = anAnchor->impl<gp_Pnt>();
112
113   aReadyToDisplay = theAnchorPoint.Distance(theCircle.Location()) > 1e-7;
114
115   return aReadyToDisplay;
116 }
117
118 void SketcherPrs_Radius::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
119                                  const Handle(Prs3d_Presentation)& thePresentation, 
120                                  const Standard_Integer theMode)
121 {
122   gp_Circ aCircle;
123   gp_Pnt anAnchorPoint;
124   double aRadius;
125   bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aCircle, anAnchorPoint, aRadius);
126   if (aReadyToDisplay) {
127     myCircle = aCircle;
128     myAnchorPoint = anAnchorPoint;
129     myRadius = aRadius;
130
131     AttributeDoublePtr anAttributeValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
132     myHasParameters = anAttributeValue->usedParameters().size() > 0;
133     myValue = anAttributeValue->text();
134   }
135
136   SetMeasuredGeometry(myCircle, myAnchorPoint);
137   SetCustomValue(myRadius);
138
139   // Update variable aspect parameters (depending on viewer scale)
140   double aTextSize = 0.0;
141   GetValueString(aTextSize);
142   SketcherPrs_Tools::updateArrows(DimensionAspect(), GetValue(), aTextSize);
143
144   myStyleListener->updateDimensions(this, myHasParameters, myValue);
145   
146   AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
147
148   if (!aReadyToDisplay)
149     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
150                               "An empty AIS presentation: SketcherPrs_Radius");
151 }
152
153 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
154                                                    const Standard_Integer theMode)
155 {
156   // Map the application selection modes to standard ones
157   Standard_Integer aMode;
158   switch (theMode) {
159   case 0: // we should use selection of all objects
160     aMode = 0;
161     break;
162   case SketcherPrs_Tools::Sel_Dimension_All:
163     aMode = 0;
164     break;
165   case SketcherPrs_Tools::Sel_Dimension_Line:
166     aMode = 1;
167     break;
168   case SketcherPrs_Tools::Sel_Dimension_Text:
169     aMode = 2;
170     break;
171   default: {
172     // there are own selection modes, so the others should be ignored
173     // otherwise, the text selection appears in the viewer
174     return; 
175   }
176   }
177   AIS_RadiusDimension::ComputeSelection(aSelection, aMode);
178 }