Salome HOME
Changes in the presentations of features
[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
10 #include <SketchPlugin_Line.h>
11 #include <SketchPlugin_Sketch.h>
12
13 #include <GeomDataAPI_Point2D.h>
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomAPI_Pnt.h>
16
17 SketchPlugin_ConstraintPerpendicular::SketchPlugin_ConstraintPerpendicular()
18 {
19 }
20
21 void SketchPlugin_ConstraintPerpendicular::initAttributes()
22 {
23   data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
24   data()->addAttribute(CONSTRAINT_ATTR_ENTITY_B, ModelAPI_AttributeRefAttr::type());
25   data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
26 }
27
28 void SketchPlugin_ConstraintPerpendicular::execute()
29 {
30 }
31
32 const boost::shared_ptr<GeomAPI_Shape>&  SketchPlugin_ConstraintPerpendicular::preview()
33 {
34   /// \todo Preview for perpendicular constraint
35   return getPreview();
36 }
37
38 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintPerpendicular::getAISObject(
39                     boost::shared_ptr<GeomAPI_AISObject> thePrevious)
40 {
41   if (!sketch())
42     return thePrevious;
43
44   boost::shared_ptr<ModelAPI_Data> aData = data();
45   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr1 = 
46     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
47   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr2 = 
48     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
49   if (!anAttr1 || !anAttr1->isFeature() || 
50       !anAttr2 || !anAttr2->isFeature())
51     return thePrevious;
52   boost::shared_ptr<SketchPlugin_Line> aLine1Feature = 
53     boost::dynamic_pointer_cast<SketchPlugin_Line>(anAttr1->feature());
54   boost::shared_ptr<SketchPlugin_Line> aLine2Feature = 
55     boost::dynamic_pointer_cast<SketchPlugin_Line>(anAttr2->feature());
56   if (!aLine1Feature || !aLine2Feature)
57     return thePrevious;
58
59   boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
60   boost::shared_ptr<GeomAPI_Shape> aLine1 = aLine1Feature->preview();
61   boost::shared_ptr<GeomAPI_Shape> aLine2 = aLine2Feature->preview();
62
63   boost::shared_ptr<GeomAPI_AISObject> anAIS = thePrevious;
64   if (!anAIS)
65     anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject);
66   anAIS->createPerpendicular(aLine1, aLine2, aPlane);
67   return anAIS;
68 }
69
70 void SketchPlugin_ConstraintPerpendicular::move(double theDeltaX, double theDeltaY)
71 {
72   boost::shared_ptr<ModelAPI_Data> aData = data();
73   if (!aData->isValid())
74     return;
75
76   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
77         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
78   aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
79 }
80