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