Salome HOME
Issue #144 Crash on perpendicular constraint move
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintPerpendicular.cpp
1 // File:    SketchPlugin_ConstraintPerpendicular.cpp
2 // Created: 26 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchPlugin_ConstraintPerpendicular.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_ConstraintPerpendicular::SketchPlugin_ConstraintPerpendicular()
21 {
22 }
23
24 void SketchPlugin_ConstraintPerpendicular::initAttributes()
25 {
26   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
27   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
28 }
29
30 void SketchPlugin_ConstraintPerpendicular::execute()
31 {
32 }
33
34 AISObjectPtr SketchPlugin_ConstraintPerpendicular::getAISObject(AISObjectPtr thePrevious)
35 {
36   if (!sketch())
37     return thePrevious;
38
39   boost::shared_ptr<ModelAPI_Data> aData = data();
40   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr1 = boost::dynamic_pointer_cast<
41       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
42   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr2 = boost::dynamic_pointer_cast<
43       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
44   if (!anAttr1 || !anAttr1->isObject() || !anAttr2 || !anAttr2->isObject())
45     return thePrevious;
46
47   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr1->object());
48   if (!aFeature)
49     return thePrevious;
50   boost::shared_ptr<SketchPlugin_Line> aLine1Feature =
51       boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
52
53   aFeature = ModelAPI_Feature::feature(anAttr2->object());
54   if (!aFeature)
55     return thePrevious;
56   boost::shared_ptr<SketchPlugin_Line> aLine2Feature =
57       boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
58
59   if (!aLine1Feature || !aLine2Feature)
60     return thePrevious;
61
62   boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
63   boost::shared_ptr<GeomAPI_Shape> aLine1, aLine2;
64   boost::shared_ptr<ModelAPI_ResultConstruction> aConst1 = boost::dynamic_pointer_cast<
65       ModelAPI_ResultConstruction>(anAttr1->object());
66   if (aConst1)
67     aLine1 = aConst1->shape();
68   boost::shared_ptr<ModelAPI_ResultConstruction> aConst2 = boost::dynamic_pointer_cast<
69       ModelAPI_ResultConstruction>(anAttr2->object());
70   if (aConst2)
71     aLine2 = aConst2->shape();
72
73   AISObjectPtr anAIS = thePrevious;
74   if (!anAIS)
75     anAIS = AISObjectPtr(new GeomAPI_AISObject);
76   anAIS->createPerpendicular(aLine1, aLine2, aPlane);
77
78   // Set color from preferences
79   std::vector<int> aRGB = Config_PropManager::color("Visualization", "perpendicular_color",
80                                                     PERPENDICULAR_COLOR);
81   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
82   return anAIS;
83 }
84
85 void SketchPlugin_ConstraintPerpendicular::move(double theDeltaX, double theDeltaY)
86 {
87   //Flyout point of the constraint follows it's features
88   return;
89 }
90