]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp
Salome HOME
[bos #37570] [CEA] SHAPER hdf tests are failing. Fix Sphere feature attributes order.
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintRadius.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 "SketchPlugin_ConstraintRadius.h"
21
22 #include <SketchPlugin_Arc.h>
23 #include <SketchPlugin_Circle.h>
24 #include <SketchPlugin_Point.h>
25 #include <SketchPlugin_Tools.h>
26
27 #include <SketcherPrs_Factory.h>
28 #include <SketcherPrs_Tools.h>
29
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_AttributeInteger.h>
32 #include <ModelAPI_Data.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Validator.h>
35
36 #include <GeomAPI_Pnt2d.h>
37 #include <GeomAPI_Circ.h>
38 #include <GeomAPI_Circ2d.h>
39 #include <GeomAPI_Dir.h>
40 #include <GeomAPI_XY.h>
41 #include <GeomAPI_XYZ.h>
42 #include <GeomDataAPI_Point2D.h>
43 #include <GeomDataAPI_Dir.h>
44
45 #include <Config_PropManager.h>
46
47 const double tolerance = 1.e-7;
48
49 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
50 {
51   myFlyoutUpdate = false;
52 }
53
54 void SketchPlugin_ConstraintRadius::initAttributes()
55 {
56   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
57   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
58   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
59
60   data()->addAttribute(SketchPlugin_ConstraintRadius::LOCATION_TYPE_ID(),
61                        ModelAPI_AttributeInteger::typeId());
62   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
63 }
64
65 void SketchPlugin_ConstraintRadius::colorConfigInfo(std::string& theSection, std::string& theName,
66                                                     std::string& theDefault)
67 {
68   theSection = "Visualization";
69   theName = "sketch_dimension_color";
70   theDefault = SKETCH_DIMENSION_COLOR;
71 }
72
73 void SketchPlugin_ConstraintRadius::execute()
74 {
75   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
76       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
77   FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
78   if (aFeature) {
79     // the value should to be computed here,
80     // not in the getAISObject in order to change the model value
81     // inside the object transaction. This is important for creating a constraint by preselection.
82     // The display of the presentation in this case happens after the transaction commit
83     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
84         GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
85     if (!aFlyoutAttr->isInitialized())
86       compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
87   }
88 }
89
90 bool SketchPlugin_ConstraintRadius::compute(const std::string& theAttributeId)
91 {
92   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
93     return false;
94
95   std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
96   double aRadius = circleRadius(aCyrcFeature);
97   if (aRadius < 0)
98     return false;
99
100   // Flyout point
101   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
102       GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
103   // Prepare a circle
104   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
105     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
106       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
107         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
108     double aShift = aRadius * 1.1;
109     std::shared_ptr<GeomAPI_Pnt2d> aPnt = aCenterAttr->pnt();
110     std::shared_ptr<GeomAPI_Pnt2d> aFPnt =
111       std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt->x() + aShift, aPnt->y() + aShift));
112     aFlyoutAttr->setValue(aFPnt);
113   } else { // arc
114     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
115       GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
116     aFlyoutAttr->setValue(aStartAttr->pnt());
117   }
118   return true;
119 }
120
121 double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc)
122 {
123   static const double kErrorResult = -1.;
124   if (!sketch())
125     return kErrorResult;
126
127   std::shared_ptr<ModelAPI_Data> aData = data();
128   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
129       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
130   if (!anAttr)
131     return kErrorResult;
132   theCirc = ModelAPI_Feature::feature(anAttr->object());
133   std::string aKind = theCirc ? theCirc->getKind() : "";
134   if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
135     return kErrorResult;
136
137   DataPtr aCircData = theCirc->data();
138   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
139   if (aKind == SketchPlugin_Circle::ID()) {
140     AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
141         aCircData->attribute(SketchPlugin_Circle::RADIUS_ID()));
142     return aCircRadius->value();
143   } else {
144     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
145         aCircData->attribute(SketchPlugin_Arc::CENTER_ID()));
146     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
147         GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID()));
148     return aCenterAttr->pnt()->distance(aStartAttr->pnt());
149   }
150   return kErrorResult;
151 }
152
153 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
154 {
155   if (!sketch())
156     return thePrevious;
157
158   AISObjectPtr anAIS = SketcherPrs_Factory::radiusConstraint(this, sketch(),
159                                                              thePrevious);
160   if (anAIS.get() && !thePrevious.get())
161     SketchPlugin_Tools::setDimensionColor(anAIS);
162   return anAIS;
163 }
164
165 void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) {
166   if (theID == SketchPlugin_Constraint::ENTITY_A()) {
167     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
168         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
169     if (!aValueAttr->isInitialized()) {
170       // only if it is not initialized, try to compute the current value
171       std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
172       double aRadius = circleRadius(aCyrcFeature);
173       if (aRadius > 0) { // set as value the radius of updated reference to another circle
174         aValueAttr->setValue(aRadius);
175       }
176     }
177   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
178     // Recalculate flyout point in local coordinates of the circle (or arc):
179     // coordinates are calculated according to center of the shape
180     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
181         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
182         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
183
184     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
185         attribute(SketchPlugin_Constraint::ENTITY_A()));
186     if (!aRefAttr || !aRefAttr->isObject())
187       return;
188     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
189     if (!aFeature || (aFeature->getKind() != SketchPlugin_Arc::ID() &&
190         aFeature->getKind() != SketchPlugin_Circle::ID()))
191       return;
192
193     std::string aCenterAttrName;
194     if (aFeature->getKind() == SketchPlugin_Circle::ID())
195       aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
196     else if (aFeature->getKind() == SketchPlugin_Arc::ID())
197       aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
198     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
199         GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
200     std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
201     std::shared_ptr<ModelAPI_AttributeDouble> aRadius = std::dynamic_pointer_cast<
202         ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
203
204     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
205     double aDist = aFlyoutPnt->distance(aCenter);
206     if (aDist < tolerance)
207       return;
208
209     myFlyoutUpdate = true;
210     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aCenter->xy());
211     aFlyoutAttr->setValue(aFlyoutDir->x(), aFlyoutDir->y());
212     myFlyoutUpdate = false;
213   }
214 }