Salome HOME
e656da498df4a10476eafd90a2a4f0b6305352fc
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Radius.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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_DimensionStyle.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, PrsDim_RadiusDimension);
53
54 SketcherPrs_Radius::SketcherPrs_Radius(ModelAPI_Feature* theConstraint,
55   SketchPlugin_Sketch* theSketcher)
56 : PrsDim_RadiusDimension(MyDefCirc), myConstraint(theConstraint), mySketcher(theSketcher),
57   myCircle(MyDefCirc),
58   myAnchorPoint(gp_Pnt(0, 0, 2)),
59   myValue(1, false, "")
60 {
61   SetDimensionAspect(createDimensionAspect());
62   myStyleListener = new SketcherPrs_DimensionStyle();
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   if (!plane().get())
149     return;
150   gp_Circ aCircle;
151   gp_Pnt anAnchorPoint;
152   bool aReadyToDisplay = readyToDisplay(myConstraint, plane(), aCircle, anAnchorPoint);
153   if (aReadyToDisplay) {
154     myCircle = aCircle;
155     myAnchorPoint = anAnchorPoint;
156
157     DataPtr aData = myConstraint->data();
158     AttributeDoublePtr anAttributeValue = aData->real(SketchPlugin_Constraint::VALUE());
159     myValue.init(anAttributeValue);
160   }
161
162   SetMeasuredGeometry(myCircle, myAnchorPoint);
163   myStyleListener->updateDimensions(this, myValue);
164
165   // Update variable aspect parameters (depending on viewer scale)
166   double aTextSize = 0.0;
167   GetValueString(aTextSize);
168
169   AttributeIntegerPtr aLocAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>
170     (myConstraint->data()->attribute(SketchPlugin_ConstraintRadius::LOCATION_TYPE_ID()));
171   SketcherPrs_Tools::LocationType aLocationType = aLocAttr->isInitialized() ?
172     (SketcherPrs_Tools::LocationType)(aLocAttr->value()) : SketcherPrs_Tools::LOCATION_AUTOMATIC;
173   updateArrows(DimensionAspect(), GetValue(), aTextSize, aLocationType);
174
175   PrsDim_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
176
177   if (!aReadyToDisplay)
178     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
179                               "An empty AIS presentation: SketcherPrs_Radius");
180 }
181
182 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
183                                                    const Standard_Integer theMode)
184 {
185   // Map the application selection modes to standard ones
186   Standard_Integer aMode;
187   switch (theMode) {
188   case 0: // we should use selection of all objects
189     aMode = 0;
190     break;
191   case SketcherPrs_Tools::Sel_Dimension_All:
192     aMode = 0;
193     break;
194   case SketcherPrs_Tools::Sel_Dimension_Line:
195     aMode = 1;
196     break;
197   case SketcherPrs_Tools::Sel_Dimension_Text:
198     aMode = 2;
199     break;
200   default: {
201     // there are own selection modes, so the others should be ignored
202     // otherwise, the text selection appears in the viewer
203     return;
204   }
205   }
206   SetSelToleranceForText2d(SketcherPrs_Tools::getArrowSize()/5.);
207   PrsDim_RadiusDimension::ComputeSelection(aSelection, aMode);
208 }