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