Salome HOME
Fix fail on the Fillet unit test
[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 static const gp_Circ MyDefCirc(gp_Ax2(gp_Pnt(0,0,0), gp_Dir(0,0,1)), 1);
25
26 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Radius, AIS_RadiusDimension);
27 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Radius, AIS_RadiusDimension);
28
29 SketcherPrs_Radius::SketcherPrs_Radius(ModelAPI_Feature* theConstraint, 
30                                        const std::shared_ptr<GeomAPI_Ax3>& thePlane)
31 : AIS_RadiusDimension(MyDefCirc), myConstraint(theConstraint), mySketcherPlane(thePlane),
32   myCircle(MyDefCirc),
33   myAnchorPoint(gp_Pnt(0, 0, 2)),
34   myValue(1, false, "")
35 {
36   SetDimensionAspect(SketcherPrs_Tools::createDimensionAspect());
37   myStyleListener = new SketcherPrs_DimensionStyleListener();
38 }
39
40 SketcherPrs_Radius::~SketcherPrs_Radius()
41 {
42   delete myStyleListener;
43 }
44
45 bool SketcherPrs_Radius::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
46                                           const std::shared_ptr<GeomAPI_Ax3>& thePlane)
47 {
48   gp_Circ aCircle;
49   gp_Pnt anAnchorPoint;
50   return readyToDisplay(theConstraint, thePlane, aCircle, anAnchorPoint);
51 }
52
53 bool SketcherPrs_Radius::readyToDisplay(ModelAPI_Feature* theConstraint,
54                                         const std::shared_ptr<GeomAPI_Ax3>& thePlane,
55                                         gp_Circ& theCircle, gp_Pnt& theAnchorPoint)
56 {
57   bool aReadyToDisplay = false;
58
59   DataPtr aData = theConstraint->data();
60
61   // Flyout point
62   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
63     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
64     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
65   if (!aFlyoutAttr->isInitialized()) {
66     return aReadyToDisplay; // can not create a good presentation
67   }
68
69   // Get circle
70   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
71     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
72     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
73   if (!anAttr)
74     return aReadyToDisplay;
75
76   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
77   //theRadius = 1;
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     //theRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
98     AttributeDoublePtr aCircRadius = 
99       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
100       aCyrcFeature->data()->attribute(SketchPlugin_Arc::RADIUS_ID()));
101     aRadius = aCircRadius->value();
102   }
103   std::shared_ptr<GeomAPI_Pnt> aCenter = thePlane->to3D(aCenterAttr->x(), aCenterAttr->y());
104   std::shared_ptr<GeomAPI_Dir> aNormal = thePlane->normal();
105
106   GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
107   std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(theConstraint, thePlane);
108
109   theCircle = aCircle.impl<gp_Circ>();
110   theAnchorPoint = anAnchor->impl<gp_Pnt>();
111
112   aReadyToDisplay = theAnchorPoint.Distance(theCircle.Location()) > 1e-7;
113
114   return aReadyToDisplay;
115 }
116
117 void SketcherPrs_Radius::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
118                                  const Handle(Prs3d_Presentation)& thePresentation, 
119                                  const Standard_Integer theMode)
120 {
121   gp_Circ aCircle;
122   gp_Pnt anAnchorPoint;
123   bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aCircle, anAnchorPoint);
124   if (aReadyToDisplay) {
125     myCircle = aCircle;
126     myAnchorPoint = anAnchorPoint;
127
128     DataPtr aData = myConstraint->data();
129     AttributeDoublePtr anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
130     myValue.init(anAttributeValue);
131   }
132
133   SetMeasuredGeometry(myCircle, myAnchorPoint);
134   myStyleListener->updateDimensions(this, myValue);
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   
142   AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
143
144   if (!aReadyToDisplay)
145     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
146                               "An empty AIS presentation: SketcherPrs_Radius");
147 }
148
149 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
150                                                    const Standard_Integer theMode)
151 {
152   // Map the application selection modes to standard ones
153   Standard_Integer aMode;
154   switch (theMode) {
155   case 0: // we should use selection of all objects
156     aMode = 0;
157     break;
158   case SketcherPrs_Tools::Sel_Dimension_All:
159     aMode = 0;
160     break;
161   case SketcherPrs_Tools::Sel_Dimension_Line:
162     aMode = 1;
163     break;
164   case SketcherPrs_Tools::Sel_Dimension_Text:
165     aMode = 2;
166     break;
167   default: {
168     // there are own selection modes, so the others should be ignored
169     // otherwise, the text selection appears in the viewer
170     return; 
171   }
172   }
173   SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
174   AIS_RadiusDimension::ComputeSelection(aSelection, aMode);
175 }