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