Salome HOME
Issue #1834: Fix length of lines
[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,
79     // not in the getAISObject in order to change the model value
80     // inside the object transaction. This is important for creating a constraint by preselection.
81     // The display of the presentation in this case happens after the transaction commit
82     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
83         GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
84     if (!aFlyoutAttr->isInitialized())
85       compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
86   }
87 }
88
89 bool SketchPlugin_ConstraintRadius::compute(const std::string& theAttributeId)
90 {
91   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
92     return false;
93
94   std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
95   double aRadius = circleRadius(aCyrcFeature);
96   if (aRadius < 0)
97     return false;
98
99   // Flyout point
100   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
101       GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
102   // Prepare a circle
103   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
104     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
105       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
106         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
107     double aShift = aRadius * 1.1;
108     std::shared_ptr<GeomAPI_Pnt2d> aPnt = aCenterAttr->pnt();
109     std::shared_ptr<GeomAPI_Pnt2d> aFPnt = 
110       std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt->x() + aShift, aPnt->y() + aShift));
111     aFlyoutAttr->setValue(aFPnt);
112   } else { // arc
113     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
114       GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));      
115     aFlyoutAttr->setValue(aStartAttr->pnt());
116   }
117   return true;
118 }
119
120 double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc)
121 {
122   static const double kErrorResult = -1.;
123   if (!sketch())
124     return kErrorResult;
125
126   std::shared_ptr<ModelAPI_Data> aData = data();
127   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
128       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
129   if (!anAttr)
130     return kErrorResult;
131   theCirc = ModelAPI_Feature::feature(anAttr->object());
132   std::string aKind = theCirc ? theCirc->getKind() : "";
133   if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
134     return kErrorResult;
135
136   DataPtr aCircData = theCirc->data();
137   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
138   if (aKind == SketchPlugin_Circle::ID()) {
139     AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
140         aCircData->attribute(SketchPlugin_Circle::RADIUS_ID()));
141     return aCircRadius->value();
142   } else {
143     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
144         aCircData->attribute(SketchPlugin_Arc::CENTER_ID()));
145     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
146         GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID()));
147     return aCenterAttr->pnt()->distance(aStartAttr->pnt());
148   }
149   return kErrorResult;
150 }
151
152 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
153 {
154   if (!sketch())
155     return thePrevious;
156
157   AISObjectPtr anAIS = SketcherPrs_Factory::radiusConstraint(this, sketch()->coordinatePlane(),
158                                                              thePrevious);
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()) { 
180       // only if it is not initialized, try to compute the current value
181       std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
182       double aRadius = circleRadius(aCyrcFeature);
183       if (aRadius > 0) { // set as value the radius of updated reference to another circle
184         aValueAttr->setValue(aRadius);
185       }
186     }
187   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
188     // Recalculate flyout point in local coordinates of the circle (or arc):
189     // coordinates are calculated according to center of the shape
190     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
191         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
193
194     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
195         attribute(SketchPlugin_Constraint::ENTITY_A()));
196     if (!aRefAttr || !aRefAttr->isObject())
197       return;
198     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
199     if (!aFeature || (aFeature->getKind() != SketchPlugin_Arc::ID() &&
200         aFeature->getKind() != SketchPlugin_Circle::ID()))
201       return;
202
203     std::string aCenterAttrName;
204     if (aFeature->getKind() == SketchPlugin_Circle::ID())
205       aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
206     else if (aFeature->getKind() == SketchPlugin_Arc::ID())
207       aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
208     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
209         GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
210     std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
211     std::shared_ptr<ModelAPI_AttributeDouble> aRadius = std::dynamic_pointer_cast<
212         ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
213
214     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
215     double aDist = aFlyoutPnt->distance(aCenter);
216     if (aDist < tolerance)
217       return;
218
219     myFlyoutUpdate = true;
220     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aCenter->xy());
221     aFlyoutAttr->setValue(aFlyoutDir->x(), aFlyoutDir->y());
222     myFlyoutUpdate = false;
223   }
224 }