Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[modules/shaper.git] / src / PartSet / PartSet_ConstraintLengthPrs.cpp
1 // File:        PartSet_FeaturePrs.h
2 // Created:     16 Jun 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_ConstraintLengthPrs.h>
6 #include <PartSet_Tools.h>
7 #include <PartSet_Constants.h>
8
9 #include <PartSet_FeatureLinePrs.h>
10
11 #include <SketchPlugin_Feature.h>
12 #include <SketchPlugin_Sketch.h>
13 #include <SketchPlugin_Line.h>
14 #include <SketchPlugin_ConstraintLength.h>
15
16 #include <GeomDataAPI_Point.h>
17 #include <GeomDataAPI_Point2D.h>
18 #include <GeomDataAPI_Dir.h>
19 #include <GeomAPI_Pnt2d.h>
20 #include <GeomAPI_Lin2d.h>
21 #include <GeomAPI_Pln.h>
22
23 #include <AIS_InteractiveObject.hxx>
24 #include <AIS_LengthDimension.hxx>
25
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Document.h>
28 #include <ModelAPI_AttributeRefAttr.h>
29 #include <ModelAPI_AttributeRefList.h>
30 #include <ModelAPI_AttributeDouble.h>
31
32 #include <AIS_InteractiveObject.hxx>
33 #include <gp_Pln.hxx>
34 #include <gp_Pnt.hxx>
35 #include <Precision.hxx>
36
37 using namespace std;
38
39 PartSet_ConstraintLengthPrs::PartSet_ConstraintLengthPrs(FeaturePtr theSketch)
40 : PartSet_FeaturePrs(theSketch)
41 {
42 }
43
44 std::string PartSet_ConstraintLengthPrs::getKind()
45 {
46   return SKETCH_CONSTRAINT_LENGTH_KIND;
47 }
48
49 PartSet_SelectionMode PartSet_ConstraintLengthPrs::setFeature(FeaturePtr theFeature, const PartSet_SelectionMode& theMode)
50 {
51   PartSet_SelectionMode aMode = theMode;
52   if (feature() && theFeature && theFeature->getKind() == SKETCH_LINE_KIND && theMode == SM_FirstPoint)
53   {
54     // set length feature
55     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
56     boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
57           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
58     aRef->setFeature(theFeature);
59
60     // set length value
61     aData = theFeature->data();
62     boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
63           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
64     boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
65           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
66
67     double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
68     PartSet_Tools::setFeatureValue(feature(), aLenght, CONSTRAINT_ATTR_VALUE);
69     aMode = SM_LastPoint;
70   }
71   return aMode;
72 }
73
74 PartSet_SelectionMode PartSet_ConstraintLengthPrs::setPoint(double theX, double theY,
75                                                          const PartSet_SelectionMode& theMode)
76 {
77   PartSet_SelectionMode aMode = theMode;
78   switch (theMode)
79   {
80     case SM_LastPoint: {
81       boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
82       boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
83               boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
84       FeaturePtr aFeature;
85       if (anAttr) {
86         aFeature = anAttr->feature();
87         if (aFeature->getKind() != SKETCH_LINE_KIND) {
88           aFeature = FeaturePtr();
89         }
90       }
91       boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>
92                                                              (new GeomAPI_Pnt2d(theX, theY));
93       boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = PartSet_FeatureLinePrs::createLin2d(aFeature);
94       boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->project(aPoint);
95       double aDistance = aPoint->distance(aResult);
96
97       if (!aFeatureLin->isRight(aPoint))
98         aDistance = -aDistance;
99
100       AttributeDoublePtr aFlyoutAttr = 
101           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
102       aFlyoutAttr->setValue(aDistance);
103
104       aMode = SM_DonePoint;
105     }
106     break;
107     default:
108       break;
109   }
110   return aMode;
111 }
112
113 Handle(AIS_InteractiveObject) PartSet_ConstraintLengthPrs::createPresentation(FeaturePtr theFeature,
114                                                        FeaturePtr theSketch,
115                                                        Handle(AIS_InteractiveObject) thePreviuos)
116 {
117   if (!theFeature || !theSketch)
118     return thePreviuos;
119
120   boost::shared_ptr<GeomAPI_Pln> aGPlane = PartSet_Tools::sketchPlane(theSketch);
121   gp_Pln aPlane = aGPlane->impl<gp_Pln>();
122
123   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
124   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
125           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
126   if (!anAttr)
127     return thePreviuos;
128   FeaturePtr aFeature = anAttr->feature();
129   if (!aFeature || aFeature->getKind() != SKETCH_LINE_KIND)
130     return thePreviuos;
131
132   boost::shared_ptr<ModelAPI_AttributeDouble> aFlyoutAttr = 
133           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
134   double aFlyout = aFlyoutAttr->value();
135
136   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
137           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
138   double aValue = aValueAttr->value();
139
140   aData = aFeature->data();
141   if (!aData->isValid())
142     return thePreviuos;
143
144   boost::shared_ptr<GeomDataAPI_Point2D> aPointStart =
145         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
146   boost::shared_ptr<GeomDataAPI_Point2D> aPointEnd =
147         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
148
149   gp_Pnt aPoint1, aPoint2;
150   PartSet_Tools::convertTo3D(aPointStart->x(), aPointStart->y(), theSketch, aPoint1);
151   PartSet_Tools::convertTo3D(aPointEnd->x(), aPointEnd->y(), theSketch, aPoint2);
152
153   //Build dimension here
154   gp_Pnt aP1 = aPoint1;
155   gp_Pnt aP2 = aPoint2;
156   if (aFlyout < 0) {
157     aP1 = aPoint2;
158     aP2 = aPoint1;
159   }
160
161   Handle(AIS_InteractiveObject) anAIS = thePreviuos;
162   if (anAIS.IsNull())
163   {
164     Handle(AIS_LengthDimension) aDimAIS = new AIS_LengthDimension (aP1, aP2, aPlane);
165     aDimAIS->SetCustomValue(aValue);
166
167     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
168     anAspect->MakeArrows3d (Standard_False);
169     anAspect->MakeText3d(false/*is text 3d*/);
170     anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
171     anAspect->MakeTextShaded(false/*is test shaded*/);
172     aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false/*is units displayed*/);
173     /*if (isUnitsDisplayed)
174     {
175       aDimAIS->SetDisplayUnits (aDimDlg->GetUnits ());
176     }*/
177     aDimAIS->SetDimensionAspect (anAspect);
178     aDimAIS->SetFlyout(aFlyout);
179     aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
180
181     anAIS = aDimAIS;
182   }
183   else {
184     // update presentation
185     Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
186     if (!aDimAIS.IsNull()) {
187       aDimAIS->SetMeasuredGeometry(aPoint1, aPoint2, aPlane);
188       aDimAIS->SetCustomValue(aValue);
189       aDimAIS->SetFlyout(aFlyout);
190
191       aDimAIS->Redisplay(Standard_True);
192     }
193   }
194   return anAIS;
195 }
196
197 std::string PartSet_ConstraintLengthPrs::getAttribute(const PartSet_SelectionMode& theMode) const
198 {
199   return "";
200 }
201
202 PartSet_SelectionMode PartSet_ConstraintLengthPrs::getNextMode(const std::string& theAttribute) const
203 {
204   return SM_FirstPoint;
205 }
206
207 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintLengthPrs::findPoint(FeaturePtr theFeature,
208                                                                            double theX, double theY)
209 {
210   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
211   return aPoint2D;
212 }
213
214 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintLengthPrs::featurePoint
215                                                      (const PartSet_SelectionMode& theMode)
216 {
217   return boost::shared_ptr<GeomDataAPI_Point2D>();
218 }