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