Salome HOME
Merge branch 'Dev_0.6' of newgeom:newgeom.git into Dev_0.6
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintLength.cpp
1 // File:    SketchPlugin_ConstraintLength.cpp
2 // Created: 30 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchPlugin_ConstraintLength.h"
6 #include <SketchPlugin_Line.h>
7
8 #include <GeomDataAPI_Point2D.h>
9
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Result.h>
13
14 #include <GeomDataAPI_Point2D.h>
15
16 #include <GeomAPI_Lin2d.h>
17 #include <GeomAPI_Pnt2d.h>
18
19 #include <Config_PropManager.h>
20
21 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
22 {
23 }
24
25 void SketchPlugin_ConstraintLength::initAttributes()
26 {
27   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
28   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
29   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
30 }
31
32 void SketchPlugin_ConstraintLength::execute()
33 {
34   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
35       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
36   FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
37   if (aFeature) {
38     // set length value
39     std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<
40         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
41     std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<
42         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
43
44     double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
45
46     //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
47     //    ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
48     //if(!aValueAttr->isInitialized()) {
49     //  aValueAttr->setValue(aLenght);
50     //}
51   }
52 }
53
54 bool SketchPlugin_ConstraintLength::getPoints(
55   std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
56   std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
57   std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
58 {
59   if (!sketch())
60     return false;
61   std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
62   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
63       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
64   if (!anAttr)
65     return false;
66   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
67   if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
68     return false;
69   DataPtr aData = aFeature->data();
70   theStartPoint = std::dynamic_pointer_cast<
71       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
72   theEndPoint = std::dynamic_pointer_cast<
73       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
74   thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
75   thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
76   return true;
77 }
78
79 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
80 {
81   std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
82   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
83   if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
84     return thePrevious; // not possible to show length because points are not defined
85
86   std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
87   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
88       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
89   std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = std::shared_ptr<GeomAPI_Pnt>();
90   if (aFlyOutAttr->isInitialized()) {
91     aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y());
92   } else {
93     std::shared_ptr<GeomAPI_Lin2d> aLine = 
94       std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
95     double aDist = aPoint1->distance(aPoint2)/5.;
96     std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
97     aFlyOutAttr->setValue(aFPnt);
98     aFlyoutPnt = sketch()->to3D(aFPnt->x(), aFPnt->y());
99   }
100   // value calculation
101   // TODO: has to be calculated on definition of reference object
102   double aDistance = aPoint1->distance(aPoint2);
103   std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
104       ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
105   double aValue = aDistance;
106   if (aValueAttr->isInitialized())
107     aValue = aValueAttr->value();
108   else 
109     aValueAttr->setValue(aValue);
110   // End TODO
111
112   AISObjectPtr anAIS = thePrevious;
113   if (!anAIS)
114     anAIS = AISObjectPtr(new GeomAPI_AISObject);
115   anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
116
117   // Set color from preferences
118   std::vector<int> aRGB = 
119     Config_PropManager::color("Visualization", "length_color", LENGTH_COLOR);
120   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
121   return anAIS;
122 }
123
124 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
125 {
126   std::shared_ptr<ModelAPI_Data> aData = data();
127   if (!aData->isValid())
128     return;
129
130   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
131       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
132   aPoint->move(theDeltaX, theDeltaY);
133 }
134
135 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
136   if (theID == SketchPlugin_Constraint::ENTITY_A()) 
137   {
138     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
139       ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
140     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
141       std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
142       std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
143       if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
144         double aLength = aPoint1->distance(aPoint2);
145         aValueAttr->setValue(aLength);
146       }
147     }
148   }
149 }