Salome HOME
Merge branch 'Port_SALOME_8.5.0'
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Radius.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketcherPrs_Radius.h"
22 #include "SketcherPrs_Tools.h"
23 #include "SketcherPrs_DimensionStyleListener.h"
24
25 #include <SketchPlugin_ConstraintRadius.h>
26 #include <SketchPlugin_Constraint.h>
27 #include <SketchPlugin_Circle.h>
28 #include <SketchPlugin_Arc.h>
29
30 #include <GeomDataAPI_Point2D.h>
31 #include <GeomAPI_Pnt2d.h>
32 #include <GeomAPI_Circ.h>
33 #include <GeomAPI_XYZ.h>
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_AttributeInteger.h>
36
37 #include <gp_Circ.hxx>
38
39 /// Creates an aspect to be shown in length/radius dimension presentations
40 /// \return an instance of aspect
41 extern Handle(Prs3d_DimensionAspect) createDimensionAspect();
42
43 /// Update variable aspect parameters (depending on viewer scale)
44 /// \param theDimAspect an aspect to be changed
45 /// \param theDimValue an arrow value
46 /// \param theTextSize an arrow value
47 extern void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect,
48   double theDimValue, double theTextSize, SketcherPrs_Tools::LocationType theLocationType);
49
50
51 static const gp_Circ MyDefCirc(gp_Ax2(gp_Pnt(0,0,0), gp_Dir(0,0,1)), 1);
52
53 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Radius, AIS_RadiusDimension);
54
55 SketcherPrs_Radius::SketcherPrs_Radius(ModelAPI_Feature* theConstraint,
56                                        const std::shared_ptr<GeomAPI_Ax3>& thePlane)
57 : AIS_RadiusDimension(MyDefCirc), myConstraint(theConstraint), mySketcherPlane(thePlane),
58   myCircle(MyDefCirc),
59   myAnchorPoint(gp_Pnt(0, 0, 2)),
60   myValue(1, false, "")
61 {
62   SetDimensionAspect(createDimensionAspect());
63   myStyleListener = new SketcherPrs_DimensionStyleListener();
64 }
65
66 SketcherPrs_Radius::~SketcherPrs_Radius()
67 {
68   delete myStyleListener;
69 }
70
71 bool SketcherPrs_Radius::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
72                                           const std::shared_ptr<GeomAPI_Ax3>& thePlane)
73 {
74   gp_Circ aCircle;
75   gp_Pnt anAnchorPoint;
76   return readyToDisplay(theConstraint, thePlane, aCircle, anAnchorPoint);
77 }
78
79 bool SketcherPrs_Radius::readyToDisplay(ModelAPI_Feature* theConstraint,
80                                         const std::shared_ptr<GeomAPI_Ax3>& thePlane,
81                                         gp_Circ& theCircle, gp_Pnt& theAnchorPoint)
82 {
83   bool aReadyToDisplay = false;
84
85   DataPtr aData = theConstraint->data();
86
87   // Flyout point
88   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
89     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
90     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
91   if (!aFlyoutAttr->isInitialized()) {
92     return aReadyToDisplay; // can not create a good presentation
93   }
94
95   // Get circle
96   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
97     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
98     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
99   if (!anAttr)
100     return aReadyToDisplay;
101
102   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
103   //theRadius = 1;
104   double aRadius = 1;
105   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
106   // it is possible that circle result becomes zero, in this case the presentation should disappear
107   // for example, it happens when circle radius is set to zero
108   if (!aCyrcFeature)
109     return aReadyToDisplay;
110   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
111     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
112         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
113     AttributeDoublePtr aCircRadius =
114       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
115       aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
116     aRadius = aCircRadius->value();
117   } else { // arc
118     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
119         aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
120     //std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
121     //  std::dynamic_pointer_cast<GeomDataAPI_Point2D>
122     //  (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
123     //theRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
124     AttributeDoublePtr aCircRadius =
125       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
126       aCyrcFeature->data()->attribute(SketchPlugin_Arc::RADIUS_ID()));
127     aRadius = aCircRadius->value();
128   }
129   std::shared_ptr<GeomAPI_Pnt> aCenter = thePlane->to3D(aCenterAttr->x(), aCenterAttr->y());
130   std::shared_ptr<GeomAPI_Dir> aNormal = thePlane->normal();
131
132   GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
133   std::shared_ptr<GeomAPI_Pnt> anAnchor =
134     SketcherPrs_Tools::getAnchorPoint(theConstraint, thePlane);
135
136   theCircle = aCircle.impl<gp_Circ>();
137   theAnchorPoint = anAnchor->impl<gp_Pnt>();
138
139   aReadyToDisplay = theAnchorPoint.Distance(theCircle.Location()) > 1e-7;
140
141   return aReadyToDisplay;
142 }
143
144 void SketcherPrs_Radius::Compute(
145   const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
146   const Handle(Prs3d_Presentation)& thePresentation,
147   const Standard_Integer theMode)
148 {
149   gp_Circ aCircle;
150   gp_Pnt anAnchorPoint;
151   bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aCircle, anAnchorPoint);
152   if (aReadyToDisplay) {
153     myCircle = aCircle;
154     myAnchorPoint = anAnchorPoint;
155
156     DataPtr aData = myConstraint->data();
157     AttributeDoublePtr anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
158     myValue.init(anAttributeValue);
159   }
160
161   SetMeasuredGeometry(myCircle, myAnchorPoint);
162   myStyleListener->updateDimensions(this, myValue);
163
164   // Update variable aspect parameters (depending on viewer scale)
165   double aTextSize = 0.0;
166   GetValueString(aTextSize);
167
168   AttributeIntegerPtr aLocAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>
169     (myConstraint->data()->attribute(SketchPlugin_ConstraintRadius::LOCATION_TYPE_ID()));
170   SketcherPrs_Tools::LocationType aLocationType = aLocAttr->isInitialized() ?
171     (SketcherPrs_Tools::LocationType)(aLocAttr->value()) : SketcherPrs_Tools::LOCATION_AUTOMATIC;
172   updateArrows(DimensionAspect(), GetValue(), aTextSize, aLocationType);
173
174   AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
175
176   if (!aReadyToDisplay)
177     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
178                               "An empty AIS presentation: SketcherPrs_Radius");
179 }
180
181 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
182                                                    const Standard_Integer theMode)
183 {
184   // Map the application selection modes to standard ones
185   Standard_Integer aMode;
186   switch (theMode) {
187   case 0: // we should use selection of all objects
188     aMode = 0;
189     break;
190   case SketcherPrs_Tools::Sel_Dimension_All:
191     aMode = 0;
192     break;
193   case SketcherPrs_Tools::Sel_Dimension_Line:
194     aMode = 1;
195     break;
196   case SketcherPrs_Tools::Sel_Dimension_Text:
197     aMode = 2;
198     break;
199   default: {
200     // there are own selection modes, so the others should be ignored
201     // otherwise, the text selection appears in the viewer
202     return;
203   }
204   }
205   SetSelToleranceForText2d(SketcherPrs_Tools::getArrowSize()/5.);
206   AIS_RadiusDimension::ComputeSelection(aSelection, aMode);
207 }