]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_FeatureLengthPrs.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[modules/shaper.git] / src / PartSet / PartSet_FeatureLengthPrs.cpp
1 // File:        PartSet_FeaturePrs.h
2 // Created:     16 Jun 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_FeatureLengthPrs.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
22 #include <AIS_InteractiveObject.hxx>
23 #include <AIS_LengthDimension.hxx>
24
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Document.h>
27 #include <ModelAPI_AttributeRefAttr.h>
28 #include <ModelAPI_AttributeRefList.h>
29 #include <ModelAPI_AttributeDouble.h>
30
31 #include <gp_Pln.hxx>
32 #include <gp_Pnt.hxx>
33 #include <Precision.hxx>
34
35 using namespace std;
36
37 PartSet_FeatureLengthPrs::PartSet_FeatureLengthPrs(FeaturePtr theSketch)
38 : PartSet_FeaturePrs(theSketch)
39 {
40 }
41
42 std::string PartSet_FeatureLengthPrs::getKind()
43 {
44   return SKETCH_CONSTRAINT_LENGTH_KIND;
45 }
46
47 bool PartSet_FeatureLengthPrs::setFeature(FeaturePtr theFeature, const PartSet_SelectionMode& theMode)
48 {
49   bool aResult = false;
50   if (feature() && theFeature && theFeature->getKind() == SKETCH_LINE_KIND && theMode == SM_FirstPoint)
51   {
52     // set length feature
53     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
54     boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
55           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
56     aRef->setFeature(theFeature);
57
58     // set length value
59     aData = theFeature->data();
60     boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
61           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
62     boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
63           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
64
65     double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
66     PartSet_Tools::setFeatureValue(feature(), aLenght, CONSTRAINT_ATTR_VALUE);
67     aResult = true;
68   }
69   return aResult;
70 }
71
72 PartSet_SelectionMode PartSet_FeatureLengthPrs::setPoint(double theX, double theY,
73                                                          const PartSet_SelectionMode& theMode)
74 {
75   PartSet_SelectionMode aMode = theMode;
76   switch (theMode)
77   {
78     case SM_SecondPoint: {
79       boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
80       boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
81               boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
82       FeaturePtr aFeature;
83       if (anAttr) {
84         aFeature = anAttr->feature();
85         if (aFeature->getKind() != SKETCH_LINE_KIND) {
86           aFeature = FeaturePtr();
87         }
88       }
89       boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>
90                                                              (new GeomAPI_Pnt2d(theX, theY));
91       boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = PartSet_FeatureLinePrs::createLin2d(aFeature);
92       boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->project(aPoint);
93       double aDistance = aPoint->distance(aResult);
94
95       double aStartX, aStartY;
96       PartSet_FeatureLinePrs::getLinePoint(aFeature, LINE_ATTR_START, aStartX, aStartY);
97
98       if (!aFeatureLin->isRight(aPoint))
99         aDistance = -aDistance;
100
101       AttributeDoublePtr aFlyoutAttr = 
102           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
103       aFlyoutAttr->setValue(aDistance);
104
105       aMode = SM_DonePoint;
106     }
107     break;
108     default:
109       break;
110   }
111   return aMode;
112 }
113
114 Handle(AIS_InteractiveObject) PartSet_FeatureLengthPrs::createPresentation(FeaturePtr theFeature,
115                                                        FeaturePtr theSketch,
116                                                        Handle(AIS_InteractiveObject) thePreviuos)
117 {
118   if (!theFeature || !theSketch)
119     return thePreviuos;
120
121   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
122   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
123     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
124   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
125     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
126   gp_Pln aPlane(aNormal->x(), aNormal->y(), aNormal->z(), 0/*D*/);
127
128   aData = theFeature->data();
129   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
130           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
131   if (!anAttr)
132     return thePreviuos;
133   FeaturePtr aFeature = anAttr->feature();
134   if (!aFeature || aFeature->getKind() != SKETCH_LINE_KIND)
135     return thePreviuos;
136
137   boost::shared_ptr<ModelAPI_AttributeDouble> aFlyoutAttr = 
138           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
139   double aFlyout = aFlyoutAttr->value();
140
141   aData = aFeature->data();
142   if (!aData->isValid())
143     return thePreviuos;
144
145   boost::shared_ptr<GeomDataAPI_Point2D> aPointStart =
146         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
147   boost::shared_ptr<GeomDataAPI_Point2D> aPointEnd =
148         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
149
150   gp_Pnt aPoint1, aPoint2;
151   PartSet_Tools::convertTo3D(aPointStart->x(), aPointStart->y(), theSketch, aPoint1);
152   PartSet_Tools::convertTo3D(aPointEnd->x(), aPointEnd->y(), theSketch, aPoint2);
153
154   //Build dimension here
155   gp_Pnt aP1 = aPoint1;
156   gp_Pnt aP2 = aPoint2;
157   if (aFlyout < 0) {
158     aP1 = aPoint2;
159     aP2 = aPoint1;
160   }
161
162   Handle(AIS_InteractiveObject) anAIS = thePreviuos;
163   if (anAIS.IsNull())
164   {
165     Handle(AIS_LengthDimension) aDimAIS = new AIS_LengthDimension (aP1, aP2, aPlane);
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->SetFlyout(aFlyout);
189
190       aDimAIS->Redisplay(Standard_True);
191     }
192   }
193   return anAIS;
194 }
195
196 std::string PartSet_FeatureLengthPrs::getAttribute(const PartSet_SelectionMode& theMode) const
197 {
198   return "";
199 }
200
201 PartSet_SelectionMode PartSet_FeatureLengthPrs::getNextMode(const std::string& theAttribute) const
202 {
203   return SM_FirstPoint;
204 }
205
206 void PartSet_FeatureLengthPrs::move(double theDeltaX, double theDeltaY)
207 {
208 }
209
210 double PartSet_FeatureLengthPrs::distanceToPoint(FeaturePtr theFeature,
211                                                  double theX, double theY)
212 {
213   return 0;
214 }
215
216 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLengthPrs::findPoint(FeaturePtr theFeature,
217                                                                            double theX, double theY)
218 {
219   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
220   return aPoint2D;
221 }
222
223 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLengthPrs::featurePoint
224                                                      (const PartSet_SelectionMode& theMode)
225 {
226   return boost::shared_ptr<GeomDataAPI_Point2D>();
227 }