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