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