]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_FeatureDistancePrs.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[modules/shaper.git] / src / PartSet / PartSet_FeatureDistancePrs.cpp
1 // File:        PartSet_FeaturePrs.h
2 // Created:     16 Jun 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_FeatureDistancePrs.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_ConstraintDistance.h>
14
15 #include <GeomDataAPI_Point2D.h>
16 #include <GeomAPI_Pnt2d.h>
17 #include <GeomAPI_Lin2d.h>
18
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_AttributeRefList.h>
23 #include <ModelAPI_AttributeDouble.h>
24
25 #include <Precision.hxx>
26
27 using namespace std;
28
29 PartSet_FeatureDistancePrs::PartSet_FeatureDistancePrs(FeaturePtr theSketch)
30 : PartSet_FeaturePrs(theSketch)
31 {
32 }
33
34 std::string PartSet_FeatureDistancePrs::getKind()
35 {
36   return SKETCH_CONSTRAINT_DISTANCE_KIND;
37 }
38
39 bool PartSet_FeatureDistancePrs::setFeature(FeaturePtr theFeature, const PartSet_SelectionMode& theMode)
40 {
41   bool aResult = false;
42   if (feature() && theFeature && theFeature->getKind() == SKETCH_LINE_KIND && theMode == SM_FirstPoint)
43   {
44     // set length feature
45     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
46     boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
47           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
48     aRef->setFeature(theFeature);
49
50     // set length value
51     aData = theFeature->data();
52     boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
53           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
54     boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
55           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
56
57     double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
58     PartSet_Tools::setFeatureValue(feature(), aLenght, CONSTRAINT_ATTR_VALUE);
59     aResult = true;
60   }
61   return aResult;
62 }
63
64 PartSet_SelectionMode PartSet_FeatureDistancePrs::setPoint(double theX, double theY,
65                                                          const PartSet_SelectionMode& theMode)
66 {
67   PartSet_SelectionMode aMode = theMode;
68   switch (theMode)
69   {
70     case SM_SecondPoint: {
71       boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
72       boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
73               boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
74       FeaturePtr aFeature;
75       if (anAttr) {
76         aFeature = anAttr->feature();
77         if (aFeature->getKind() != SKETCH_LINE_KIND) {
78           aFeature = FeaturePtr();
79         }
80       }
81       boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>
82                                                              (new GeomAPI_Pnt2d(theX, theY));
83       boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = PartSet_FeatureLinePrs::createLin2d(aFeature);
84       boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->project(aPoint);
85       double aDistance = aPoint->distance(aResult);
86
87       double aStartX, aStartY;
88       PartSet_FeatureLinePrs::getLinePoint(aFeature, LINE_ATTR_START, aStartX, aStartY);
89
90       if (!aFeatureLin->isRight(aPoint))
91         aDistance = -aDistance;
92
93       AttributeDoublePtr aFlyoutAttr = 
94           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
95       aFlyoutAttr->setValue(aDistance);
96
97       aMode = SM_DonePoint;
98     }
99     break;
100     default:
101       break;
102   }
103   return aMode;
104 }
105
106 std::string PartSet_FeatureDistancePrs::getAttribute(const PartSet_SelectionMode& theMode) const
107 {
108   return "";
109 }
110
111 PartSet_SelectionMode PartSet_FeatureDistancePrs::getNextMode(const std::string& theAttribute) const
112 {
113   return SM_FirstPoint;
114 }
115
116 void PartSet_FeatureDistancePrs::move(double theDeltaX, double theDeltaY)
117 {
118 }
119
120 double PartSet_FeatureDistancePrs::distanceToPoint(FeaturePtr theFeature,
121                                                  double theX, double theY)
122 {
123   return 0;
124 }
125
126 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureDistancePrs::findPoint(FeaturePtr theFeature,
127                                                                            double theX, double theY)
128 {
129   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
130   return aPoint2D;
131 }
132
133 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureDistancePrs::featurePoint
134                                                      (const PartSet_SelectionMode& theMode)
135 {
136   return boost::shared_ptr<GeomDataAPI_Point2D>();
137 }