Salome HOME
Copyright update 2020
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintRadius.cpp
1 // Copyright (C) 2014-2020  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 "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     double aRadius = 0;
80     std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
81     if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
82       AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
83           aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
84       if (anAttribute)
85         aRadius = anAttribute->value();
86     } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
87       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
88           GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
89       std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
90           GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
91       if (aCenterAttr && aStartAttr)
92         aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
93     }
94     //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
95     //    ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
96     //if(!aValueAttr->isInitialized()) {
97     //  aValueAttr->setValue(aRadius);
98     //}
99
100     // the value should to be computed here,
101     // not in the getAISObject in order to change the model value
102     // inside the object transaction. This is important for creating a constraint by preselection.
103     // The display of the presentation in this case happens after the transaction commit
104     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
105         GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
106     if (!aFlyoutAttr->isInitialized())
107       compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
108   }
109 }
110
111 bool SketchPlugin_ConstraintRadius::compute(const std::string& theAttributeId)
112 {
113   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
114     return false;
115
116   std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
117   double aRadius = circleRadius(aCyrcFeature);
118   if (aRadius < 0)
119     return false;
120
121   // Flyout point
122   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
123       GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
124   // Prepare a circle
125   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
126     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
127       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
128         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
129     double aShift = aRadius * 1.1;
130     std::shared_ptr<GeomAPI_Pnt2d> aPnt = aCenterAttr->pnt();
131     std::shared_ptr<GeomAPI_Pnt2d> aFPnt =
132       std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt->x() + aShift, aPnt->y() + aShift));
133     aFlyoutAttr->setValue(aFPnt);
134   } else { // arc
135     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
136       GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
137     aFlyoutAttr->setValue(aStartAttr->pnt());
138   }
139   return true;
140 }
141
142 double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc)
143 {
144   static const double kErrorResult = -1.;
145   if (!sketch())
146     return kErrorResult;
147
148   std::shared_ptr<ModelAPI_Data> aData = data();
149   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
150       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
151   if (!anAttr)
152     return kErrorResult;
153   theCirc = ModelAPI_Feature::feature(anAttr->object());
154   std::string aKind = theCirc ? theCirc->getKind() : "";
155   if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
156     return kErrorResult;
157
158   DataPtr aCircData = theCirc->data();
159   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
160   if (aKind == SketchPlugin_Circle::ID()) {
161     AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
162         aCircData->attribute(SketchPlugin_Circle::RADIUS_ID()));
163     return aCircRadius->value();
164   } else {
165     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
166         aCircData->attribute(SketchPlugin_Arc::CENTER_ID()));
167     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
168         GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID()));
169     return aCenterAttr->pnt()->distance(aStartAttr->pnt());
170   }
171   return kErrorResult;
172 }
173
174 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
175 {
176   if (!sketch())
177     return thePrevious;
178
179   AISObjectPtr anAIS = SketcherPrs_Factory::radiusConstraint(this, sketch(),
180                                                              thePrevious);
181   if (anAIS.get() && !thePrevious.get())
182     SketchPlugin_Tools::setDimensionColor(anAIS);
183   return anAIS;
184 }
185
186 void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) {
187   if (theID == SketchPlugin_Constraint::ENTITY_A()) {
188     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
189         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
190     if (!aValueAttr->isInitialized()) {
191       // only if it is not initialized, try to compute the current value
192       std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
193       double aRadius = circleRadius(aCyrcFeature);
194       if (aRadius > 0) { // set as value the radius of updated reference to another circle
195         aValueAttr->setValue(aRadius);
196       }
197     }
198   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
199     // Recalculate flyout point in local coordinates of the circle (or arc):
200     // coordinates are calculated according to center of the shape
201     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
202         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
203         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
204
205     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
206         attribute(SketchPlugin_Constraint::ENTITY_A()));
207     if (!aRefAttr || !aRefAttr->isObject())
208       return;
209     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
210     if (!aFeature || (aFeature->getKind() != SketchPlugin_Arc::ID() &&
211         aFeature->getKind() != SketchPlugin_Circle::ID()))
212       return;
213
214     std::string aCenterAttrName;
215     if (aFeature->getKind() == SketchPlugin_Circle::ID())
216       aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
217     else if (aFeature->getKind() == SketchPlugin_Arc::ID())
218       aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
219     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
220         GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
221     std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
222     std::shared_ptr<ModelAPI_AttributeDouble> aRadius = std::dynamic_pointer_cast<
223         ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
224
225     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
226     double aDist = aFlyoutPnt->distance(aCenter);
227     if (aDist < tolerance)
228       return;
229
230     myFlyoutUpdate = true;
231     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aCenter->xy());
232     aFlyoutAttr->setValue(aFlyoutDir->x(), aFlyoutDir->y());
233     myFlyoutUpdate = false;
234   }
235 }