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