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