Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintDistance.cpp
1 // File:    SketchPlugin_ConstraintDistance.cpp
2 // Created: 23 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchPlugin_ConstraintDistance.h"
6 #include <SketchPlugin_Point.h>
7
8 #include <GeomAPI_Lin2d.h>
9 #include <GeomAPI_Pnt2d.h>
10 #include <GeomDataAPI_Point2D.h>
11
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_Data.h>
14
15 /// Obtain the point object from specified constraint parameter
16 static boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(
17             DataPtr             theData,
18             const std::string&  theAttribute);
19
20
21 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
22 {
23 }
24
25 void SketchPlugin_ConstraintDistance::initAttributes()
26 {
27   data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::type());
28   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
29   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
30   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
31 }
32
33 void SketchPlugin_ConstraintDistance::execute()
34 {
35   boost::shared_ptr<ModelAPI_Data> aData = data();
36
37   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
38   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
39   if (aPoint_A && aPoint_B) {
40     AttributeDoublePtr anAttr_Value =
41       boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
42
43     if (!anAttr_Value->isInitialized()) {
44       anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
45     }
46   }
47 }
48
49 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintDistance::getAISObject(
50                     boost::shared_ptr<GeomAPI_AISObject> thePrevious)
51 {
52   if (!sketch())
53     return thePrevious;
54
55   boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
56
57   DataPtr aData = data();
58   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
59   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
60   if (!aPoint_A || !aPoint_B)
61     return thePrevious;
62
63   boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = 
64     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
65
66   boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPoint_A->x(), aPoint_A->y());
67   boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPoint_B->x(), aPoint_B->y());
68   boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = aFlyOutAttr->isInitialized() ? 
69                                               sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()) : 
70                                               boost::shared_ptr<GeomAPI_Pnt>();
71
72   // value calculation
73   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
74     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
75   double aValue = aValueAttr->value();
76
77   boost::shared_ptr<GeomAPI_AISObject> anAIS = thePrevious;
78   if (!anAIS)
79     anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject);
80   anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
81   return anAIS;
82 }
83
84 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
85 {
86   boost::shared_ptr<ModelAPI_Data> aData = data();
87   if (!aData->isValid())
88     return;
89
90   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
91         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
92   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
93 }
94
95 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
96                                                        const std::string& theAttribute)
97 {
98   boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
99
100   if (!theData)
101     return aPointAttr;
102
103   FeaturePtr aFeature;
104   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
105     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
106   if (anAttr)
107     aFeature = SketchPlugin_Sketch::getFeature(anAttr->object());
108
109   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
110     aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
111                                            (aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
112   else {
113     if (anAttr->attr())
114       aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
115   }
116   return aPointAttr;
117 }