Salome HOME
#1404 Random crash with Shaper: AIS presentations: operation prs, result sketch prs...
[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), myPlane(thePlane)
34 {
35   // Set default values of the presentation
36   myAspect = new Prs3d_DimensionAspect();
37   myAspect->MakeArrows3d(false);
38   myAspect->MakeText3d(false);
39   myAspect->MakeTextShaded(false);
40   myAspect->MakeUnitsDisplayed(false);
41   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
42   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
43   
44   SetDimensionAspect(myAspect);
45   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
46
47   myStyleListener = new SketcherPrs_DimensionStyleListener();
48 }
49
50 SketcherPrs_Radius::~SketcherPrs_Radius()
51 {
52   delete myStyleListener;
53 }
54
55 bool SketcherPrs_Radius::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
56                                           const std::shared_ptr<GeomAPI_Ax3>& thePlane)
57 {
58   bool aReadyToDisplay = false;
59
60   DataPtr aData = theConstraint->data();
61
62   // Flyout point
63   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
64     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
65     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
66   if (!aFlyoutAttr->isInitialized()) {
67     return aReadyToDisplay; // can not create a good presentation
68   }
69
70   // Get circle
71   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
72     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
73     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
74   if (!anAttr)
75     return aReadyToDisplay;
76
77   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
78   double aRadius = 1;
79   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
80   // it is possible that circle result becomes zero, in this case the presentation should disappear
81   // for example, it happens when circle radius is set to zero
82   if (!aCyrcFeature)
83     return aReadyToDisplay;
84   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
85     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
86         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
87     AttributeDoublePtr aCircRadius = 
88       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
89       aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
90     aRadius = aCircRadius->value();
91   } else { // arc
92     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
93         aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
94     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
95       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
96       (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
97     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
98   }
99   std::shared_ptr<GeomAPI_Pnt> aCenter = thePlane->to3D(aCenterAttr->x(), aCenterAttr->y());
100   std::shared_ptr<GeomAPI_Dir> aNormal = thePlane->normal();
101
102   GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
103     
104   std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(theConstraint, thePlane);
105
106   gp_Circ aCirc = aCircle.impl<gp_Circ>();
107   gp_Pnt anAncorPnt = anAnchor->impl<gp_Pnt>();
108   // anchor point should not coincide to the location point of the circle
109   // OCCT does not process this case.
110
111   aReadyToDisplay = anAncorPnt.Distance(aCirc.Location()) > 1e-7;
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   if (!IsReadyToDisplay(myConstraint, myPlane)) {
120     Events_Error::throwException("An empty AIS presentation: SketcherPrs_Radius");
121     return;
122   }
123
124   DataPtr aData = myConstraint->data();
125
126   // Flyout point
127   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
128     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
129     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
130
131   // Get circle
132   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
133     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
134     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
135
136   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
137   double aRadius = 1;
138   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
139   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
140     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
141         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
142     AttributeDoublePtr aCircRadius = 
143       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
144       aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
145     aRadius = aCircRadius->value();
146   } else { // arc
147     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
148         aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
149     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
150       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
151       (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
152     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
153   }
154   std::shared_ptr<GeomAPI_Pnt> aCenter = myPlane->to3D(aCenterAttr->x(), aCenterAttr->y());
155   std::shared_ptr<GeomAPI_Dir> aNormal = myPlane->normal();
156
157   GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
158     
159   std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(myConstraint, myPlane);
160
161   gp_Circ aCirc = aCircle.impl<gp_Circ>();
162   gp_Pnt anAncorPnt = anAnchor->impl<gp_Pnt>();
163
164   SetMeasuredGeometry(aCirc, anAncorPnt);
165   SetCustomValue(aRadius);
166
167   // Update variable aspect parameters (depending on viewer scale)
168   double anArrowLength = myAspect->ArrowAspect()->Length();
169    // This is not realy correct way to get viewer scale.
170   double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
171   double aDimensionValue = GetValue();
172   double aTextSize = 0.0;
173   GetValueString(aTextSize);
174
175   if(aTextSize > ((aDimensionValue - 2 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
176     myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
177     myAspect->SetArrowOrientation(Prs3d_DAO_External);
178     myAspect->SetExtensionSize(aTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
179   } else {
180     myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
181     myAspect->SetArrowOrientation(Prs3d_DAO_Internal);
182   }
183   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
184
185   // The value of vertical aligment is sometimes changed
186   myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
187
188   AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
189   SketcherPrs_Tools::setDisplaySpecialSymbol(this, aValue->usedParameters().size() > 0);
190
191   myStyleListener->updateDimensions(this, aValue);
192
193   AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
194 }
195
196 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
197                                                    const Standard_Integer theMode)
198 {
199   // Map the application selection modes to standard ones
200   Standard_Integer aMode;
201   switch (theMode) {
202   case 0: // we should use selection of all objects
203     aMode = 0;
204     break;
205   case SketcherPrs_Tools::Sel_Dimension_All:
206     aMode = 0;
207     break;
208   case SketcherPrs_Tools::Sel_Dimension_Line:
209     aMode = 1;
210     break;
211   case SketcherPrs_Tools::Sel_Dimension_Text:
212     aMode = 2;
213     break;
214   default: {
215     // there are own selection modes, so the others should be ignored
216     // otherwise, the text selection appears in the viewer
217     return; 
218   }
219   }
220   AIS_RadiusDimension::ComputeSelection(aSelection, aMode);
221 }