Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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> aPoint_A = getFeaturePoint(feature(),
91                                                                         CONSTRAINT_ATTR_ENTITY_A);
92       boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(feature(),
93                                                                         CONSTRAINT_ATTR_ENTITY_B);
94
95       boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>
96                                                              (new GeomAPI_Pnt2d(theX, theY));
97       boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = boost::shared_ptr<GeomAPI_Lin2d>
98                                         (new GeomAPI_Lin2d(aPoint_A->x(), aPoint_A->y(),
99                                                            aPoint_B->x(), aPoint_B->y()));
100       boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->project(aPoint);
101       double aDistance = aPoint->distance(aResult);
102
103       if (!aFeatureLin->isRight(aPoint))
104         aDistance = -aDistance;
105
106       AttributeDoublePtr aFlyoutAttr = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>
107                                       (feature()->data()->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
108       aFlyoutAttr->setValue(aDistance);
109
110       aMode = SM_DonePoint;
111     }
112     break;
113     default:
114       break;
115   }
116   return aMode;
117 }
118
119 Handle(AIS_InteractiveObject) PartSet_ConstraintDistancePrs::createPresentation(FeaturePtr theFeature,
120                                                        FeaturePtr theSketch,
121                                                        Handle(AIS_InteractiveObject) thePreviuos)
122 {
123   Handle(AIS_InteractiveObject) anAIS = thePreviuos;
124   if (!theFeature || !theSketch)
125     return anAIS;
126   boost::shared_ptr<GeomAPI_Pln> aGPlane = PartSet_Tools::sketchPlane(theSketch);
127   gp_Pln aPlane = aGPlane->impl<gp_Pln>();
128
129   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(theFeature, CONSTRAINT_ATTR_ENTITY_A);
130   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(theFeature, CONSTRAINT_ATTR_ENTITY_B);
131   if (!aPoint_A || !aPoint_B)
132     return anAIS;
133
134   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
135   boost::shared_ptr<ModelAPI_AttributeDouble> aFlyoutAttr = 
136           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
137   double aFlyout = aFlyoutAttr->value();
138
139   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
140           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
141   double aValue = aValueAttr->value();
142
143   gp_Pnt aPoint1, aPoint2;
144   PartSet_Tools::convertTo3D(aPoint_A->x(), aPoint_A->y(), theSketch, aPoint1);
145   PartSet_Tools::convertTo3D(aPoint_B->x(), aPoint_B->y(), theSketch, aPoint2);
146
147   //Build dimension here
148   gp_Pnt aP1 = aPoint1;
149   gp_Pnt aP2 = aPoint2;
150   if (aFlyout < 0) {
151     aP1 = aPoint2;
152     aP2 = aPoint1;
153   }
154
155   if (anAIS.IsNull())
156   {
157     Handle(AIS_LengthDimension) aDimAIS = new AIS_LengthDimension(aP1, aP2, aPlane);
158     aDimAIS->SetCustomValue(aValue);
159
160     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
161     anAspect->MakeArrows3d (Standard_False);
162     anAspect->MakeText3d(false);
163     anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
164     anAspect->MakeTextShaded(false);
165     aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
166     aDimAIS->SetDimensionAspect (anAspect);
167     aDimAIS->SetFlyout(aFlyout);
168     aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
169
170     anAIS = aDimAIS;
171   }
172   else {
173     // update presentation
174     Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
175     if (!aDimAIS.IsNull()) {
176       aDimAIS->SetMeasuredGeometry(aPoint1, aPoint2, aPlane);
177       aDimAIS->SetCustomValue(aValue);
178       aDimAIS->SetFlyout(aFlyout);
179
180       aDimAIS->Redisplay(Standard_True);
181     }
182   }
183   return anAIS;
184 }
185
186 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::getFeaturePoint
187                                                                (FeaturePtr theFeature,
188                                                                 const std::string& theAttribute)
189 {
190   boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
191
192   FeaturePtr aFeature = PartSet_Tools::feature(theFeature, theAttribute, SKETCH_POINT_KIND);
193   if (aFeature)
194     aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
195                                            (aFeature->data()->attribute(POINT_ATTR_COORD));
196   return aPointAttr;
197 }
198
199 std::string PartSet_ConstraintDistancePrs::getAttribute(const PartSet_SelectionMode& theMode) const
200 {
201   return "";
202 }
203
204 PartSet_SelectionMode PartSet_ConstraintDistancePrs::getNextMode(const std::string& theAttribute) const
205 {
206   return SM_FirstPoint;
207 }
208
209 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::findPoint(FeaturePtr theFeature,
210                                                                            double theX, double theY)
211 {
212   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
213   return aPoint2D;
214 }
215
216 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::featurePoint
217                                                      (const PartSet_SelectionMode& theMode)
218 {
219   return boost::shared_ptr<GeomDataAPI_Point2D>();
220 }