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