Salome HOME
Move generation of AIS presentation into SketchPlugin
[modules/shaper.git] / src / PartSet / PartSet_ConstraintDistancePrs.cpp
1 // File:        PartSet_FeaturePrs.h
2 // Created:     16 Jun 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_ConstraintDistancePrs.h>
6 #include <PartSet_Tools.h>
7
8 #include <PartSet_FeatureLinePrs.h>
9
10 #include <SketchPlugin_Feature.h>
11 #include <SketchPlugin_Sketch.h>
12 #include <SketchPlugin_Line.h>
13 #include <SketchPlugin_Point.h>
14 #include <SketchPlugin_ConstraintDistance.h>
15
16 #include <GeomDataAPI_Point2D.h>
17 #include <GeomAPI_Pnt2d.h>
18 #include <GeomAPI_Lin2d.h>
19 #include <GeomAPI_Pln.h>
20
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Document.h>
23 #include <ModelAPI_AttributeRefAttr.h>
24 #include <ModelAPI_AttributeRefList.h>
25 #include <ModelAPI_AttributeDouble.h>
26
27 #include <AIS_InteractiveObject.hxx>
28 #include <AIS_LengthDimension.hxx>
29
30 #include <Precision.hxx>
31 #include <gp_Pln.hxx>
32
33 using namespace std;
34
35 PartSet_ConstraintDistancePrs::PartSet_ConstraintDistancePrs(FeaturePtr theSketch)
36 : PartSet_FeaturePrs(theSketch)
37 {
38 }
39
40 std::string PartSet_ConstraintDistancePrs::getKind()
41 {
42   return SKETCH_CONSTRAINT_DISTANCE_KIND;
43 }
44
45 PartSet_SelectionMode PartSet_ConstraintDistancePrs::setFeature(FeaturePtr theFeature, const PartSet_SelectionMode& theMode)
46 {
47   PartSet_SelectionMode aMode = theMode;
48   if (!feature() || !theFeature)
49     return aMode;
50
51   std::string anArgument;
52   if (theMode == SM_FirstPoint) {
53     anArgument = CONSTRAINT_ATTR_ENTITY_A;
54     aMode = SM_SecondPoint;
55   }
56   else if (theMode == SM_SecondPoint) {
57     anArgument = CONSTRAINT_ATTR_ENTITY_B;
58     aMode = SM_LastPoint;
59   }
60   if (theFeature->getKind() == SKETCH_POINT_KIND)
61   {
62     // set length feature
63     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
64     boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
65           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(anArgument));
66     aRef->setFeature(theFeature);
67   }
68
69   if (theMode == SM_SecondPoint) {
70     // set length value
71     boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(feature(), CONSTRAINT_ATTR_ENTITY_A);
72     boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(feature(), CONSTRAINT_ATTR_ENTITY_B);
73
74     if (aPoint_A && aPoint_B) {
75       double aLenght = aPoint_A->pnt()->distance(aPoint_B->pnt());
76       PartSet_Tools::setFeatureValue(feature(), aLenght, CONSTRAINT_ATTR_VALUE);
77     }
78   }
79
80   return aMode;
81 }
82
83 PartSet_SelectionMode PartSet_ConstraintDistancePrs::setPoint(double theX, double theY,
84                                                          const PartSet_SelectionMode& theMode)
85 {
86   PartSet_SelectionMode aMode = theMode;
87   switch (theMode)
88   {
89     case SM_LastPoint: {
90       boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = 
91         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(feature()->data()->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
92       aFlyOutAttr->setValue(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
93
94       boost::shared_ptr<GeomAPI_Pnt2d> aFlyOutPnt = aFlyOutAttr->pnt();
95
96       aMode = SM_DonePoint;
97     }
98     break;
99     default:
100       break;
101   }
102   return aMode;
103 }
104
105 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::getFeaturePoint
106                                                                (FeaturePtr theFeature,
107                                                                 const std::string& theAttribute)
108 {
109   boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
110
111   FeaturePtr aFeature = PartSet_Tools::feature(theFeature, theAttribute, SKETCH_POINT_KIND);
112   if (aFeature)
113     aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
114                                            (aFeature->data()->attribute(POINT_ATTR_COORD));
115   return aPointAttr;
116 }
117
118 std::string PartSet_ConstraintDistancePrs::getAttribute(const PartSet_SelectionMode& theMode) const
119 {
120   return "";
121 }
122
123 PartSet_SelectionMode PartSet_ConstraintDistancePrs::getNextMode(const std::string& theAttribute) const
124 {
125   return SM_FirstPoint;
126 }
127
128 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::findPoint(FeaturePtr theFeature,
129                                                                            double theX, double theY)
130 {
131   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
132   return aPoint2D;
133 }
134
135 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::featurePoint
136                                                      (const PartSet_SelectionMode& theMode)
137 {
138   return boost::shared_ptr<GeomDataAPI_Point2D>();
139 }