Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintParallel.cpp
1 // File:    SketchPlugin_ConstraintParallel.cpp
2 // Created: 26 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchPlugin_ConstraintParallel.h"
6
7 #include <ModelAPI_AttributeDouble.h>
8 #include <ModelAPI_Data.h>
9 #include <ModelAPI_ResultConstruction.h>
10
11 #include <SketchPlugin_Line.h>
12 #include <SketchPlugin_Sketch.h>
13
14 #include <GeomDataAPI_Point2D.h>
15 #include <GeomAPI_Pnt2d.h>
16 #include <GeomAPI_Pnt.h>
17
18 #include <Config_PropManager.h>
19
20 SketchPlugin_ConstraintParallel::SketchPlugin_ConstraintParallel()
21 {
22 }
23
24 void SketchPlugin_ConstraintParallel::initAttributes()
25 {
26   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
27   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
28   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
29 }
30
31 void SketchPlugin_ConstraintParallel::execute()
32 {
33 }
34
35 AISObjectPtr SketchPlugin_ConstraintParallel::getAISObject(AISObjectPtr thePrevious)
36 {
37   if (!sketch())
38     return thePrevious;
39
40   boost::shared_ptr<ModelAPI_Data> aData = data();
41   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr1 = boost::dynamic_pointer_cast<
42       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
43   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr2 = boost::dynamic_pointer_cast<
44       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
45   if (!anAttr1 || !anAttr1->isObject() || !anAttr2 || !anAttr2->isObject())
46     return thePrevious;
47
48   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr1->object());
49   if (!aFeature)
50     return thePrevious;
51   boost::shared_ptr<SketchPlugin_Line> aLine1Feature =
52       boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
53
54   aFeature = ModelAPI_Feature::feature(anAttr2->object());
55   if (!aFeature)
56     return thePrevious;
57   boost::shared_ptr<SketchPlugin_Line> aLine2Feature =
58       boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
59
60   if (!aLine1Feature || !aLine2Feature)
61     return thePrevious;
62
63   boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
64   boost::shared_ptr<GeomAPI_Shape> aLine1, aLine2;
65   boost::shared_ptr<ModelAPI_ResultConstruction> aConst1 = boost::dynamic_pointer_cast<
66       ModelAPI_ResultConstruction>(anAttr1->object());
67   if (aConst1)
68     aLine1 = aConst1->shape();
69   boost::shared_ptr<ModelAPI_ResultConstruction> aConst2 = boost::dynamic_pointer_cast<
70       ModelAPI_ResultConstruction>(anAttr2->object());
71   if (aConst2)
72     aLine2 = aConst2->shape();
73
74   boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = boost::dynamic_pointer_cast<
75       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
76   boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = boost::shared_ptr<GeomAPI_Pnt>();;
77   if(aFlyoutAttr->isInitialized()) {
78     aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
79   }
80
81   AISObjectPtr anAIS = thePrevious;
82   if (!anAIS)
83     anAIS = AISObjectPtr(new GeomAPI_AISObject);
84   anAIS->createParallel(aLine1, aLine2, aFlyoutPnt, aPlane);
85
86   // Set color from preferences
87   std::vector<int> aRGB = Config_PropManager::color("Visualization", "parallel_color",
88                                                     PARALLEL_COLOR);
89   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
90   return anAIS;
91 }
92
93 void SketchPlugin_ConstraintParallel::move(double theDeltaX, double theDeltaY)
94 {
95   boost::shared_ptr<ModelAPI_Data> aData = data();
96   if (!aData->isValid())
97     return;
98
99   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
100       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
101   aPoint->move(theDeltaX, theDeltaY);
102 }
103