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