Salome HOME
Merge branch 'master' of newgeom:newgeom
[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   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::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     boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<
40         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
41     boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<
42         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
43
44     double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
45
46     boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = boost::dynamic_pointer_cast<
47         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
48     aValueAttr->setValue(aLenght);
49   }
50 }
51
52 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
53 {
54   if (!sketch())
55     return thePrevious;
56
57   boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
58
59   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
60       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
61   if (!anAttr)
62     return thePrevious;
63   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
64   if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
65     return thePrevious;
66
67   boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = boost::dynamic_pointer_cast<
68       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
69
70   DataPtr aData = aFeature->data();
71   boost::shared_ptr<GeomDataAPI_Point2D> aStartPoint = boost::dynamic_pointer_cast<
72       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
73   boost::shared_ptr<GeomDataAPI_Point2D> anEndPoint = boost::dynamic_pointer_cast<
74       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
75
76   boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aStartPoint->x(), aStartPoint->y());
77   boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(anEndPoint->x(), anEndPoint->y());
78   boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt =
79       aFlyOutAttr->isInitialized() ?
80           sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()) : boost::shared_ptr<GeomAPI_Pnt>();
81
82   // value calculation
83   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = boost::dynamic_pointer_cast<
84       ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
85   double aValue = aValueAttr->value();
86
87   AISObjectPtr anAIS = thePrevious;
88   if (!anAIS)
89     anAIS = AISObjectPtr(new GeomAPI_AISObject);
90   anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
91
92   // Set color from preferences
93   std::vector<int> aRGB = Config_PropManager::color("Visualization", "length_color", LENGTH_COLOR);
94   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
95   return anAIS;
96 }
97
98 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
99 {
100   boost::shared_ptr<ModelAPI_Data> aData = data();
101   if (!aData->isValid())
102     return;
103
104   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
105       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
106   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
107 }
108