Salome HOME
Issue #1608 : Dimension in sketcher is not always correctly updated
[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   myValue(1, false, "")
35 {
36   SetDimensionAspect(SketcherPrs_Tools::createDimensionAspect());
37   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
38
39   myStyleListener = new SketcherPrs_DimensionStyleListener();
40 }
41
42 SketcherPrs_Radius::~SketcherPrs_Radius()
43 {
44   delete myStyleListener;
45 }
46
47 bool SketcherPrs_Radius::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
48                                           const std::shared_ptr<GeomAPI_Ax3>& thePlane)
49 {
50   gp_Circ aCircle;
51   gp_Pnt anAnchorPoint;
52   return readyToDisplay(theConstraint, thePlane, aCircle, anAnchorPoint);
53 }
54
55 bool SketcherPrs_Radius::readyToDisplay(ModelAPI_Feature* theConstraint,
56                                         const std::shared_ptr<GeomAPI_Ax3>& thePlane,
57                                         gp_Circ& theCircle, gp_Pnt& theAnchorPoint)
58 {
59   bool aReadyToDisplay = false;
60
61   DataPtr aData = theConstraint->data();
62
63   // Flyout point
64   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
65     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
66     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
67   if (!aFlyoutAttr->isInitialized()) {
68     return aReadyToDisplay; // can not create a good presentation
69   }
70
71   // Get circle
72   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
73     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
74     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
75   if (!anAttr)
76     return aReadyToDisplay;
77
78   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
79   //theRadius = 1;
80   double aRadius = 1;
81   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
82   // it is possible that circle result becomes zero, in this case the presentation should disappear
83   // for example, it happens when circle radius is set to zero
84   if (!aCyrcFeature)
85     return aReadyToDisplay;
86   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
87     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
88         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
89     AttributeDoublePtr aCircRadius = 
90       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
91       aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
92     aRadius = aCircRadius->value();
93   } else { // arc
94     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
95         aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
96     //std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
97     //  std::dynamic_pointer_cast<GeomDataAPI_Point2D>
98     //  (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
99     //theRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
100     AttributeDoublePtr aCircRadius = 
101       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
102       aCyrcFeature->data()->attribute(SketchPlugin_Arc::RADIUS_ID()));
103     aRadius = aCircRadius->value();
104   }
105   std::shared_ptr<GeomAPI_Pnt> aCenter = thePlane->to3D(aCenterAttr->x(), aCenterAttr->y());
106   std::shared_ptr<GeomAPI_Dir> aNormal = thePlane->normal();
107
108   GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
109   std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(theConstraint, thePlane);
110
111   theCircle = aCircle.impl<gp_Circ>();
112   theAnchorPoint = anAnchor->impl<gp_Pnt>();
113
114   aReadyToDisplay = theAnchorPoint.Distance(theCircle.Location()) > 1e-7;
115
116   return aReadyToDisplay;
117 }
118
119 void SketcherPrs_Radius::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
120                                  const Handle(Prs3d_Presentation)& thePresentation, 
121                                  const Standard_Integer theMode)
122 {
123   gp_Circ aCircle;
124   gp_Pnt anAnchorPoint;
125   bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aCircle, anAnchorPoint);
126   if (aReadyToDisplay) {
127     myCircle = aCircle;
128     myAnchorPoint = anAnchorPoint;
129
130     DataPtr aData = myConstraint->data();
131     AttributeDoublePtr anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
132     myValue.init(anAttributeValue);
133   }
134
135   SetMeasuredGeometry(myCircle, myAnchorPoint);
136   myStyleListener->updateDimensions(this, myValue);
137
138   // Update variable aspect parameters (depending on viewer scale)
139   double aTextSize = 0.0;
140   GetValueString(aTextSize);
141   SketcherPrs_Tools::updateArrows(DimensionAspect(), GetValue(), aTextSize);
142
143   
144   AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
145
146   if (!aReadyToDisplay)
147     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
148                               "An empty AIS presentation: SketcherPrs_Radius");
149 }
150
151 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
152                                                    const Standard_Integer theMode)
153 {
154   // Map the application selection modes to standard ones
155   Standard_Integer aMode;
156   switch (theMode) {
157   case 0: // we should use selection of all objects
158     aMode = 0;
159     break;
160   case SketcherPrs_Tools::Sel_Dimension_All:
161     aMode = 0;
162     break;
163   case SketcherPrs_Tools::Sel_Dimension_Line:
164     aMode = 1;
165     break;
166   case SketcherPrs_Tools::Sel_Dimension_Text:
167     aMode = 2;
168     break;
169   default: {
170     // there are own selection modes, so the others should be ignored
171     // otherwise, the text selection appears in the viewer
172     return; 
173   }
174   }
175   AIS_RadiusDimension::ComputeSelection(aSelection, aMode);
176 }