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