Salome HOME
Main fix of this integration is in ConstraintRigid. The AIS should be created uncondi...
[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::execute()
44 {
45   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
46       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
47   FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
48   if (aFeature) {
49     // set length value
50     std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<
51         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
52     std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<
53         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
54
55     double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
56
57     //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
58     //    ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
59     //if(!aValueAttr->isInitialized()) {
60     //  aValueAttr->setValue(aLenght);
61     //}
62
63     // the value should to be computed here, not in the getAISObject in order to change the model value
64     // inside the object transaction. This is important for creating a constraint by preselection.
65     // The display of the presentation in this case happens after the transaction commit
66     AttributePtr aFlyOutAttribute = data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
67     if (!aFlyOutAttribute->isInitialized()) {
68       compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
69     }
70   }
71 }
72
73 bool SketchPlugin_ConstraintLength::compute(const std::string& theAttributeId)
74 {
75   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
76     return false;
77
78   std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
79   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
80   if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
81     return false;
82
83   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
84       GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
85
86   std::shared_ptr<GeomAPI_Lin2d> aLine = 
87     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
88   if (fabs(aFlyOutAttr->x()) < tolerance && fabs(aFlyOutAttr->y()) < tolerance) {
89     double aDist = aPoint1->distance(aPoint2)/5.;
90     std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
91     aFlyOutAttr->setValue(aFPnt);
92   }
93
94   return true;
95 }
96
97 bool SketchPlugin_ConstraintLength::getPoints(
98   std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
99   std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
100   std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
101 {
102   if (!sketch())
103     return false;
104   std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
105   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
106       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
107   if (!anAttr)
108     return false;
109   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
110   if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
111     return false;
112   DataPtr aData = aFeature->data();
113   theStartPoint = std::dynamic_pointer_cast<
114       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
115   theEndPoint = std::dynamic_pointer_cast<
116       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
117   thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
118   thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
119   return true;
120 }
121
122 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
123 {
124   if (!sketch())
125     return thePrevious;
126
127   AISObjectPtr anAIS = thePrevious;
128   if (!anAIS) {
129     anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this, sketch()->coordinatePlane());
130   }
131   return anAIS;
132 }
133
134 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
135 {
136   std::shared_ptr<ModelAPI_Data> aData = data();
137   if (!aData->isValid())
138     return;
139
140   AttributeRefAttrPtr aLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
141       attribute(SketchPlugin_Constraint::ENTITY_A()));
142   if (!aLineAttr || !aLineAttr->isObject())
143     return;
144   FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
145   if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
146     return;
147
148   // Recalculate a shift of flyout point in terms of local coordinates
149   std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(theDeltaX, theDeltaY));
150   std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
151       aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
152   std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
153       aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
154   std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
155   double dX = aDir->dot(aLineDir->xy());
156   double dY = -aDir->cross(aLineDir->xy());
157
158   myFlyoutUpdate = true;
159   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
160       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
161   aPoint->setValue(aPoint->x() + dX, aPoint->y() + dY);
162   myFlyoutUpdate = false;
163 }
164
165 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
166   if (theID == SketchPlugin_Constraint::ENTITY_A()) 
167   {
168     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
169       ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
170     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
171       std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
172       std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
173       if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
174         double aLength = aPoint1->distance(aPoint2);
175         aValueAttr->setValue(aLength);
176       }
177     }
178   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
179     myFlyoutUpdate = true;
180     // Recalculate flyout point in local coordinates of the line:
181     // the X coordinate is a length of projection of the flyout point on the line
182     // the Y coordinate is a distance from the point to the line
183     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
184         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
185         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
186     AttributeRefAttrPtr aLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
187         attribute(SketchPlugin_Constraint::ENTITY_A()));
188     if (!aLineAttr || !aLineAttr->isObject())
189       return;
190     FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
191     if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
192       return;
193
194     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
195     std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
196         aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
197     std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
198         aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
199
200     std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
201     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
202     double X = aFlyoutDir->dot(aLineDir->xy());
203     double Y = -aFlyoutDir->cross(aLineDir->xy());
204     aFlyoutAttr->setValue(X, Y);
205     myFlyoutUpdate = false;
206   }
207 }
208
209 bool SketchPlugin_ConstraintLength::customisePresentation(ResultPtr theResult,
210                                     AISObjectPtr thePrs,
211                                     std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
212 {
213   bool isCustomized = false;
214   std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
215                                                     SKETCH_DIMENSION_COLOR);
216   isCustomized = thePrs->setColor(aRGB[0], aRGB[1], aRGB[2]);
217
218   return isCustomized;
219 }