]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_Radius.cpp
Salome HOME
#1404 Random crash with Shaper: AIS presentations: coincidence/radius constraints...
[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   bool aReadyToDisplay = false;
50
51   DataPtr aData = theConstraint->data();
52
53   // Flyout point
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     return aReadyToDisplay; // can not create a good presentation
59   }
60
61   // Get circle
62   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
63     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
64     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
65   if (!anAttr)
66     return aReadyToDisplay;
67
68   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
69   double aRadius = 1;
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
73   if (!aCyrcFeature)
74     return aReadyToDisplay;
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();
82   } else { // arc
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());
89   }
90   std::shared_ptr<GeomAPI_Pnt> aCenter = thePlane->to3D(aCenterAttr->x(), aCenterAttr->y());
91   std::shared_ptr<GeomAPI_Dir> aNormal = thePlane->normal();
92
93   GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
94     
95   std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(theConstraint, thePlane);
96
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
102   aReadyToDisplay = anAncorPnt.Distance(aCirc.Location()) > 1e-7;
103   return aReadyToDisplay;
104 }
105
106 void SketcherPrs_Radius::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
107                                  const Handle(Prs3d_Presentation)& thePresentation, 
108                                  const Standard_Integer theMode)
109 {
110   bool aReadyToDisplay = IsReadyToDisplay(myConstraint, mySketcherPlane);
111   if (aReadyToDisplay) {
112     //myDistance = SketcherPrs_Tools::getFlyoutDistance(myConstraint);
113
114     DataPtr aData = myConstraint->data();
115
116     // Flyout point
117     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
118       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
119       (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
120
121     // Get circle
122     std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
123       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
124       (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
125
126     std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
127     myRadius = 1;
128     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
129     if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
130       aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
131           aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
132       AttributeDoublePtr aCircRadius = 
133         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
134         aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
135       myRadius = aCircRadius->value();
136     } else { // arc
137       aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
138           aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
139       std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
140         std::dynamic_pointer_cast<GeomDataAPI_Point2D>
141         (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
142       myRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
143     }
144     std::shared_ptr<GeomAPI_Pnt> aCenter = mySketcherPlane->to3D(aCenterAttr->x(), aCenterAttr->y());
145     std::shared_ptr<GeomAPI_Dir> aNormal = mySketcherPlane->normal();
146
147     GeomAPI_Circ aCircle(aCenter, aNormal, myRadius);
148     std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(myConstraint, mySketcherPlane);
149
150     myCircle = aCircle.impl<gp_Circ>();
151     myAncorPnt = anAnchor->impl<gp_Pnt>();
152
153     AttributeDoublePtr anAttributeValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
154     myHasParameters = anAttributeValue->usedParameters().size() > 0;
155     myValue = anAttributeValue->text();
156   }
157
158   SetMeasuredGeometry(myCircle, myAncorPnt);
159   SetCustomValue(myRadius);
160
161   // Update variable aspect parameters (depending on viewer scale)
162   double aTextSize = 0.0;
163   GetValueString(aTextSize);
164   SketcherPrs_Tools::updateArrows(DimensionAspect(), GetValue(), aTextSize);
165
166   myStyleListener->updateDimensions(this, myHasParameters, myValue);
167   
168   AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
169
170   if (!aReadyToDisplay)
171     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
172                               "An empty AIS presentation: SketcherPrs_Radius");
173 }
174
175 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
176                                                    const Standard_Integer theMode)
177 {
178   // Map the application selection modes to standard ones
179   Standard_Integer aMode;
180   switch (theMode) {
181   case 0: // we should use selection of all objects
182     aMode = 0;
183     break;
184   case SketcherPrs_Tools::Sel_Dimension_All:
185     aMode = 0;
186     break;
187   case SketcherPrs_Tools::Sel_Dimension_Line:
188     aMode = 1;
189     break;
190   case SketcherPrs_Tools::Sel_Dimension_Text:
191     aMode = 2;
192     break;
193   default: {
194     // there are own selection modes, so the others should be ignored
195     // otherwise, the text selection appears in the viewer
196     return; 
197   }
198   }
199   AIS_RadiusDimension::ComputeSelection(aSelection, aMode);
200 }