Salome HOME
Issue #1502 Select sub-solids in viewer : set selection priority for the owner less...
[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 = SketcherPrs_Factory::radiusConstraint(this, sketch()->coordinatePlane(),
156                                                              thePrevious);
157   return anAIS;
158 }
159
160 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
161 {
162   std::shared_ptr<ModelAPI_Data> aData = data();
163   if (!aData->isValid())
164     return;
165
166   myFlyoutUpdate = true;
167   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
168       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
169   aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
170   myFlyoutUpdate = false;
171 }
172
173 void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) {
174   if (theID == SketchPlugin_Constraint::ENTITY_A()) {
175     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
176         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
177     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
178       std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
179       double aRadius = circleRadius(aCyrcFeature);
180       if (aRadius > 0) { // set as value the radius of updated reference to another circle
181         aValueAttr->setValue(aRadius);
182       }
183     }
184   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
185     // Recalculate flyout point in local coordinates of the circle (or arc):
186     // coordinates are calculated according to center of the shape
187     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
188         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
189         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
190
191     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
192         attribute(SketchPlugin_Constraint::ENTITY_A()));
193     if (!aRefAttr || !aRefAttr->isObject())
194       return;
195     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
196     if (!aFeature || (aFeature->getKind() != SketchPlugin_Arc::ID() &&
197         aFeature->getKind() != SketchPlugin_Circle::ID()))
198       return;
199
200     std::string aCenterAttrName;
201     if (aFeature->getKind() == SketchPlugin_Circle::ID())
202       aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
203     else if (aFeature->getKind() == SketchPlugin_Arc::ID())
204       aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
205     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
206         GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
207     std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
208     std::shared_ptr<ModelAPI_AttributeDouble> aRadius = std::dynamic_pointer_cast<
209         ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
210
211     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
212     double aDist = aFlyoutPnt->distance(aCenter);
213     if (aDist < tolerance)
214       return;
215
216     myFlyoutUpdate = true;
217     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aCenter->xy());
218     aFlyoutAttr->setValue(aFlyoutDir->x(), aFlyoutDir->y());
219     myFlyoutUpdate = false;
220   }
221 }