]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_Radius.cpp
Salome HOME
#1404 Random crash with Shaper: AIS presentations: avoid IsReadyToDisplay calling...
[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 <Events_Error.h>
17
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>
23
24 #include <gp_Circ.hxx>
25
26 static const gp_Circ MyDefCirc(gp_Ax2(gp_Pnt(0,0,0), gp_Dir(0,0,1)), 1);
27
28 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Radius, AIS_RadiusDimension);
29 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Radius, AIS_RadiusDimension);
30
31 SketcherPrs_Radius::SketcherPrs_Radius(ModelAPI_Feature* theConstraint, 
32                                        const std::shared_ptr<GeomAPI_Ax3>& thePlane)
33 : AIS_RadiusDimension(MyDefCirc), myConstraint(theConstraint), mySketcherPlane(thePlane)
34 {
35   SetDimensionAspect(SketcherPrs_Tools::createDimensionAspect());
36   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
37
38   myStyleListener = new SketcherPrs_DimensionStyleListener();
39 }
40
41 SketcherPrs_Radius::~SketcherPrs_Radius()
42 {
43   delete myStyleListener;
44 }
45
46 bool SketcherPrs_Radius::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
47                                           const std::shared_ptr<GeomAPI_Ax3>& thePlane)
48 {
49   gp_Circ aCircle;
50   gp_Pnt anAnchorPoint;
51   double aRadius;
52   return readyToDisplay(theConstraint, thePlane, aCircle, anAnchorPoint, aRadius);
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                                         double& theRadius)
59 {
60   bool aReadyToDisplay = false;
61
62   DataPtr aData = theConstraint->data();
63
64   // Flyout point
65   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
66     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
67     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
68   if (!aFlyoutAttr->isInitialized()) {
69     return aReadyToDisplay; // can not create a good presentation
70   }
71
72   // Get circle
73   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
74     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
75     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
76   if (!anAttr)
77     return aReadyToDisplay;
78
79   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
80   theRadius = 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     theRadius = 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   }
101   std::shared_ptr<GeomAPI_Pnt> aCenter = thePlane->to3D(aCenterAttr->x(), aCenterAttr->y());
102   std::shared_ptr<GeomAPI_Dir> aNormal = thePlane->normal();
103
104   GeomAPI_Circ aCircle(aCenter, aNormal, theRadius);
105   std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(theConstraint, thePlane);
106
107   theCircle = aCircle.impl<gp_Circ>();
108   theAnchorPoint = anAnchor->impl<gp_Pnt>();
109
110   aReadyToDisplay = theAnchorPoint.Distance(theCircle.Location()) > 1e-7;
111
112   return aReadyToDisplay;
113 }
114
115 void SketcherPrs_Radius::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
116                                  const Handle(Prs3d_Presentation)& thePresentation, 
117                                  const Standard_Integer theMode)
118 {
119   gp_Circ aCircle;
120   gp_Pnt anAnchorPoint;
121   double aRadius;
122   bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aCircle, anAnchorPoint, aRadius);
123   if (aReadyToDisplay) {
124     myCircle = aCircle;
125     myAnchorPoint = anAnchorPoint;
126     myRadius = aRadius;
127
128     AttributeDoublePtr anAttributeValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
129     myHasParameters = anAttributeValue->usedParameters().size() > 0;
130     myValue = anAttributeValue->text();
131   }
132
133   SetMeasuredGeometry(myCircle, myAnchorPoint);
134   SetCustomValue(myRadius);
135
136   // Update variable aspect parameters (depending on viewer scale)
137   double aTextSize = 0.0;
138   GetValueString(aTextSize);
139   SketcherPrs_Tools::updateArrows(DimensionAspect(), GetValue(), aTextSize);
140
141   myStyleListener->updateDimensions(this, myHasParameters, myValue);
142   
143   AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
144
145   if (!aReadyToDisplay)
146     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
147                               "An empty AIS presentation: SketcherPrs_Radius");
148 }
149
150 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
151                                                    const Standard_Integer theMode)
152 {
153   // Map the application selection modes to standard ones
154   Standard_Integer aMode;
155   switch (theMode) {
156   case 0: // we should use selection of all objects
157     aMode = 0;
158     break;
159   case SketcherPrs_Tools::Sel_Dimension_All:
160     aMode = 0;
161     break;
162   case SketcherPrs_Tools::Sel_Dimension_Line:
163     aMode = 1;
164     break;
165   case SketcherPrs_Tools::Sel_Dimension_Text:
166     aMode = 2;
167     break;
168   default: {
169     // there are own selection modes, so the others should be ignored
170     // otherwise, the text selection appears in the viewer
171     return; 
172   }
173   }
174   AIS_RadiusDimension::ComputeSelection(aSelection, aMode);
175 }