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
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_ConstraintParallel::SketchPlugin_ConstraintParallel()
18 {
19 }
20
21 void SketchPlugin_ConstraintParallel::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_ConstraintParallel::execute()
29 {
30 }
31
32 const boost::shared_ptr<GeomAPI_Shape>&  SketchPlugin_ConstraintParallel::preview()
33 {
34   /// \todo Preview for parallel constraint
35   return getPreview();
36 }
37
38
39 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintParallel::getAISObject(
40                     boost::shared_ptr<GeomAPI_AISObject> thePrevious)
41 {
42   if (!sketch())
43     return thePrevious;
44
45   boost::shared_ptr<ModelAPI_Data> aData = data();
46   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr1 = 
47     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
48   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr2 = 
49     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
50   if (!anAttr1 || !anAttr1->isFeature() || 
51       !anAttr2 || !anAttr2->isFeature())
52     return thePrevious;
53   boost::shared_ptr<SketchPlugin_Line> aLine1Feature = 
54     boost::dynamic_pointer_cast<SketchPlugin_Line>(anAttr1->feature());
55   boost::shared_ptr<SketchPlugin_Line> aLine2Feature = 
56     boost::dynamic_pointer_cast<SketchPlugin_Line>(anAttr2->feature());
57   if (!aLine1Feature || !aLine2Feature)
58     return thePrevious;
59
60   boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
61   boost::shared_ptr<GeomAPI_Shape> aLine1 = aLine1Feature->preview();
62   boost::shared_ptr<GeomAPI_Shape> aLine2 = aLine2Feature->preview();
63
64   boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
65     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
66   boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
67
68   boost::shared_ptr<GeomAPI_AISObject> anAIS = thePrevious;
69   if (!anAIS)
70     anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject);
71   anAIS->createParallel(aLine1, aLine2, aFlyoutPnt, aPlane);
72   return anAIS;
73 }
74
75 void SketchPlugin_ConstraintParallel::move(double theDeltaX, double theDeltaY)
76 {
77   boost::shared_ptr<ModelAPI_Data> aData = data();
78   if (!aData->isValid())
79     return;
80
81   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
82         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
83   aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
84 }
85