Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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
13 #include <GeomDataAPI_Point2D.h>
14
15 #include <GeomAPI_Lin2d.h>
16 #include <GeomAPI_Pnt2d.h>
17
18 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
19 {
20 }
21
22 void SketchPlugin_ConstraintLength::initAttributes()
23 {
24   data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::type());
25   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
26   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
27 }
28
29 void SketchPlugin_ConstraintLength::execute()
30 {
31   if (data()->attribute(SketchPlugin_Constraint::ENTITY_A())->isInitialized() &&
32       !data()->attribute(SketchPlugin_Constraint::VALUE())->isInitialized()) {
33
34     boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
35       boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
36     ObjectPtr aFeature = aRef->object();
37     if (aFeature) {
38       // set length value
39       boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
40         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
41       boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
42         boost::dynamic_pointer_cast<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 = 
47         boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
48       aValueAttr->setValue(aLenght);
49     }
50   }
51 }
52
53 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintLength::getAISObject(
54                     boost::shared_ptr<GeomAPI_AISObject> thePrevious)
55 {
56   if (!sketch())
57     return thePrevious;
58
59   boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
60
61   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
62     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
63   if (!anAttr)
64     return thePrevious;
65   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
66   if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
67     return thePrevious;
68
69   boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = 
70     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
71
72   DataPtr aData = aFeature->data();
73   boost::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
74     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
75   boost::shared_ptr<GeomDataAPI_Point2D> anEndPoint =
76       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
77
78   boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aStartPoint->x(), aStartPoint->y());
79   boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(anEndPoint->x(),  anEndPoint->y());
80   boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = aFlyOutAttr->isInitialized() ? 
81                                               sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()) : 
82                                               boost::shared_ptr<GeomAPI_Pnt>();
83
84   // value calculation
85   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
86     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
87   double aValue = aValueAttr->value();
88
89   boost::shared_ptr<GeomAPI_AISObject> anAIS = thePrevious;
90   if (!anAIS)
91     anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject);
92   anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
93   return anAIS;
94 }
95
96 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
97 {
98   boost::shared_ptr<ModelAPI_Data> aData = data();
99   if (!aData->isValid())
100     return;
101
102   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
103         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
104   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
105 }
106