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