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