Salome HOME
Avoid of reference to not-initialized attributes values
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintLength.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintLength.cpp
4 // Created: 30 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_ConstraintLength.h"
8 #include <SketchPlugin_Line.h>
9
10 #include <GeomDataAPI_Point2D.h>
11
12 #include <SketcherPrs_Factory.h>
13
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Result.h>
17
18 #include <GeomDataAPI_Point2D.h>
19
20 #include <GeomAPI_Dir2d.h>
21 #include <GeomAPI_Lin2d.h>
22 #include <GeomAPI_Pnt2d.h>
23 #include <GeomAPI_XY.h>
24
25 #include <Config_PropManager.h>
26
27 #include <math.h>
28
29 const double tolerance = 1e-7;
30
31 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
32 {
33   myFlyoutUpdate = false;
34 }
35
36 void SketchPlugin_ConstraintLength::initAttributes()
37 {
38   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
39   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
40   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
41 }
42
43 void SketchPlugin_ConstraintLength::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_ConstraintLength::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     // set length value
58     std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<
59         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
60     std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<
61         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
62
63     double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
64
65     //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
66     //    ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
67     //if(!aValueAttr->isInitialized()) {
68     //  aValueAttr->setValue(aLenght);
69     //}
70
71     // the value should to be computed here, not in the getAISObject in order to change the model value
72     // inside the object transaction. This is important for creating a constraint by preselection.
73     // The display of the presentation in this case happens after the transaction commit
74     AttributePtr aFlyOutAttribute = data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
75     if (!aFlyOutAttribute->isInitialized()) {
76       compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
77     }
78   }
79 }
80
81 bool SketchPlugin_ConstraintLength::compute(const std::string& theAttributeId)
82 {
83   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
84     return false;
85
86   std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
87   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
88   if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
89     return false;
90
91   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
92       GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
93
94   std::shared_ptr<GeomAPI_Lin2d> aLine = 
95     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
96   if (!aFlyOutAttr->isInitialized() || 
97       (fabs(aFlyOutAttr->x()) < tolerance && fabs(aFlyOutAttr->y()) < tolerance)) {
98     double aDist = aPoint1->distance(aPoint2)/5.;
99     std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
100     aFlyOutAttr->setValue(aFPnt);
101   }
102
103   return true;
104 }
105
106 bool SketchPlugin_ConstraintLength::getPoints(
107   std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
108   std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
109   std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
110 {
111   if (!sketch())
112     return false;
113   std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
114   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
115       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
116   if (!anAttr)
117     return false;
118   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
119   if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
120     return false;
121   DataPtr aData = aFeature->data();
122   theStartPoint = std::dynamic_pointer_cast<
123       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
124   theEndPoint = std::dynamic_pointer_cast<
125       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
126   thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
127   thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
128   return true;
129 }
130
131 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
132 {
133   if (!sketch())
134     return thePrevious;
135
136   AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this, sketch()->coordinatePlane(),
137                                                                       thePrevious);
138   return anAIS;
139 }
140
141 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
142 {
143   std::shared_ptr<ModelAPI_Data> aData = data();
144   if (!aData->isValid())
145     return;
146
147   AttributeRefAttrPtr aLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
148       attribute(SketchPlugin_Constraint::ENTITY_A()));
149   if (!aLineAttr || !aLineAttr->isObject())
150     return;
151   FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
152   if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
153     return;
154
155   // Recalculate a shift of flyout point in terms of local coordinates
156   std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(theDeltaX, theDeltaY));
157   std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
158       aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
159   std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
160       aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
161   std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
162   double dX = aDir->dot(aLineDir->xy());
163   double dY = -aDir->cross(aLineDir->xy());
164
165   myFlyoutUpdate = true;
166   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
167       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
168   aPoint->setValue(aPoint->x() + dX, aPoint->y() + dY);
169   myFlyoutUpdate = false;
170 }
171
172 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
173   if (theID == SketchPlugin_Constraint::ENTITY_A()) 
174   {
175     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
176       ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
177     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
178       std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
179       std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
180       if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
181         double aLength = aPoint1->distance(aPoint2);
182         aValueAttr->setValue(aLength);
183       }
184     }
185   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
186     myFlyoutUpdate = true;
187     // Recalculate flyout point in local coordinates of the line:
188     // the X coordinate is a length of projection of the flyout point on the line
189     // the Y coordinate is a distance from the point to the line
190     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
191         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
193     AttributeRefAttrPtr aLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
194         attribute(SketchPlugin_Constraint::ENTITY_A()));
195     if (!aLineAttr || !aLineAttr->isObject())
196       return;
197     FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
198     if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
199       return;
200
201     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
202     std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
203         aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
204     std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
205         aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
206
207     std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
208     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
209     double X = aFlyoutDir->dot(aLineDir->xy());
210     double Y = -aFlyoutDir->cross(aLineDir->xy());
211     aFlyoutAttr->setValue(X, Y);
212     myFlyoutUpdate = false;
213   }
214 }