Salome HOME
Draw AIS presentations for perpendicular and parallel constraints
[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 #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 SketchPlugin_ConstraintParallel::SketchPlugin_ConstraintParallel()
19 {
20 }
21
22 void SketchPlugin_ConstraintParallel::initAttributes()
23 {
24   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
25   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
26   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
27 }
28
29 void SketchPlugin_ConstraintParallel::execute()
30 {
31 }
32
33 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintParallel::getAISObject(
34                     boost::shared_ptr<GeomAPI_AISObject> thePrevious)
35 {
36   if (!sketch())
37     return thePrevious;
38
39   boost::shared_ptr<ModelAPI_Data> aData = data();
40   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr1 = 
41     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
42   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr2 = 
43     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
44   if (!anAttr1 || !anAttr1->isObject() || 
45       !anAttr2 || !anAttr2->isObject())
46     return thePrevious;
47
48   FeaturePtr aFeature = SketchPlugin_Sketch::getFeature(anAttr1->object());
49   if (!aFeature)
50     return thePrevious;
51   boost::shared_ptr<SketchPlugin_Line> aLine1Feature = 
52     boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
53
54   aFeature = SketchPlugin_Sketch::getFeature(anAttr2->object());
55   if (!aFeature)
56     return thePrevious;
57   boost::shared_ptr<SketchPlugin_Line> aLine2Feature = 
58     boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
59
60   if (!aLine1Feature || !aLine2Feature)
61     return thePrevious;
62
63   boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
64   boost::shared_ptr<GeomAPI_Shape> aLine1, aLine2;
65   boost::shared_ptr<ModelAPI_ResultConstruction> aConst1 = 
66     boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anAttr1->object());
67   if (aConst1) aLine1 = aConst1->shape();
68   boost::shared_ptr<ModelAPI_ResultConstruction> aConst2 = 
69     boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anAttr2->object());
70   if (aConst2) aLine2 = aConst2->shape();
71
72   boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
73     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
74   boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
75
76   boost::shared_ptr<GeomAPI_AISObject> anAIS = thePrevious;
77   if (!anAIS)
78     anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject);
79   anAIS->createParallel(aLine1, aLine2, aFlyoutPnt, aPlane);
80   return anAIS;
81 }
82
83 void SketchPlugin_ConstraintParallel::move(double theDeltaX, double theDeltaY)
84 {
85   boost::shared_ptr<ModelAPI_Data> aData = data();
86   if (!aData->isValid())
87     return;
88
89   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
90         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
91   aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
92 }
93