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