]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_ConstraintDistancePrs.cpp
Salome HOME
d63e98c26f90ad2df3ba9fd6638b45792e243f4f
[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_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 <AIS_InteractiveObject.hxx>
26 #include <Precision.hxx>
27
28 using namespace std;
29
30 PartSet_ConstraintDistancePrs::PartSet_ConstraintDistancePrs(FeaturePtr theSketch)
31 : PartSet_FeaturePrs(theSketch)
32 {
33 }
34
35 std::string PartSet_ConstraintDistancePrs::getKind()
36 {
37   return SKETCH_CONSTRAINT_DISTANCE_KIND;
38 }
39
40 bool PartSet_ConstraintDistancePrs::setFeature(FeaturePtr theFeature, const PartSet_SelectionMode& theMode)
41 {
42   bool aResult = false;
43   if (feature() && theFeature && theFeature->getKind() == SKETCH_LINE_KIND && theMode == SM_FirstPoint)
44   {
45     // set length feature
46     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
47     boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
48           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
49     aRef->setFeature(theFeature);
50
51     // set length value
52     aData = theFeature->data();
53     boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
54           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
55     boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
56           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
57
58     double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
59     PartSet_Tools::setFeatureValue(feature(), aLenght, CONSTRAINT_ATTR_VALUE);
60     aResult = true;
61   }
62   return aResult;
63 }
64
65 PartSet_SelectionMode PartSet_ConstraintDistancePrs::setPoint(double theX, double theY,
66                                                          const PartSet_SelectionMode& theMode)
67 {
68   PartSet_SelectionMode aMode = theMode;
69   switch (theMode)
70   {
71     case SM_SecondPoint: {
72       boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
73       boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
74               boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
75       FeaturePtr aFeature;
76       if (anAttr) {
77         aFeature = anAttr->feature();
78         if (aFeature->getKind() != SKETCH_LINE_KIND) {
79           aFeature = FeaturePtr();
80         }
81       }
82       boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>
83                                                              (new GeomAPI_Pnt2d(theX, theY));
84       boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = PartSet_FeatureLinePrs::createLin2d(aFeature);
85       boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->project(aPoint);
86       double aDistance = aPoint->distance(aResult);
87
88       double aStartX, aStartY;
89       PartSet_FeatureLinePrs::getLinePoint(aFeature, LINE_ATTR_START, aStartX, aStartY);
90
91       if (!aFeatureLin->isRight(aPoint))
92         aDistance = -aDistance;
93
94       AttributeDoublePtr aFlyoutAttr = 
95           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
96       aFlyoutAttr->setValue(aDistance);
97
98       aMode = SM_DonePoint;
99     }
100     break;
101     default:
102       break;
103   }
104   return aMode;
105 }
106
107 Handle(AIS_InteractiveObject) PartSet_ConstraintDistancePrs::createPresentation(FeaturePtr theFeature,
108                                                        FeaturePtr theSketch,
109                                                        Handle(AIS_InteractiveObject) thePreviuos)
110 {
111   Handle(AIS_InteractiveObject) anAIS = thePreviuos;
112   if (!theFeature || !theSketch)
113     return anAIS;
114   /*
115   boost::shared_ptr<GeomAPI_Pln> aGPlane = PartSet_Tools::sketchPlane(theSketch);
116   gp_Pln aPlane = aGPlane->impl<gp_Pln>();
117
118   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
119   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
120           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
121   if (!anAttr)
122     return thePreviuos;
123   FeaturePtr aFeature = anAttr->feature();
124   if (!aFeature || aFeature->getKind() != SKETCH_LINE_KIND)
125     return thePreviuos;
126
127   boost::shared_ptr<ModelAPI_AttributeDouble> aFlyoutAttr = 
128           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
129   double aFlyout = aFlyoutAttr->value();
130
131   aData = aFeature->data();
132   if (!aData->isValid())
133     return thePreviuos;
134
135   boost::shared_ptr<GeomDataAPI_Point2D> aPointStart =
136         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
137   boost::shared_ptr<GeomDataAPI_Point2D> aPointEnd =
138         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
139
140   gp_Pnt aPoint1, aPoint2;
141   PartSet_Tools::convertTo3D(aPointStart->x(), aPointStart->y(), theSketch, aPoint1);
142   PartSet_Tools::convertTo3D(aPointEnd->x(), aPointEnd->y(), theSketch, aPoint2);
143
144   //Build dimension here
145   gp_Pnt aP1 = aPoint1;
146   gp_Pnt aP2 = aPoint2;
147   if (aFlyout < 0) {
148     aP1 = aPoint2;
149     aP2 = aPoint1;
150   }
151
152   Handle(AIS_InteractiveObject) anAIS = thePreviuos;
153   if (anAIS.IsNull())
154   {
155     Handle(AIS_LengthDimension) aDimAIS = new AIS_LengthDimension (aP1, aP2, aPlane);
156
157     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
158     anAspect->MakeArrows3d (Standard_False);
159     anAspect->MakeText3d(false);
160     anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
161     anAspect->MakeTextShaded(false);
162     aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
163     //if (isUnitsDisplayed)
164     //{
165     //  aDimAIS->SetDisplayUnits (aDimDlg->GetUnits ());
166     //}
167     aDimAIS->SetDimensionAspect (anAspect);
168     aDimAIS->SetFlyout(aFlyout);
169     aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
170
171     anAIS = aDimAIS;
172   }
173   else {
174     // update presentation
175     Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
176     if (!aDimAIS.IsNull()) {
177       aDimAIS->SetMeasuredGeometry(aPoint1, aPoint2, aPlane);
178       aDimAIS->SetFlyout(aFlyout);
179
180       aDimAIS->Redisplay(Standard_True);
181     }
182   }
183 */
184   return anAIS;
185 }
186
187 std::string PartSet_ConstraintDistancePrs::getAttribute(const PartSet_SelectionMode& theMode) const
188 {
189   return "";
190 }
191
192 PartSet_SelectionMode PartSet_ConstraintDistancePrs::getNextMode(const std::string& theAttribute) const
193 {
194   return SM_FirstPoint;
195 }
196
197 void PartSet_ConstraintDistancePrs::move(double theDeltaX, double theDeltaY)
198 {
199 }
200
201 double PartSet_ConstraintDistancePrs::distanceToPoint(FeaturePtr theFeature,
202                                                  double theX, double theY)
203 {
204   return 0;
205 }
206
207 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::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_ConstraintDistancePrs::featurePoint
215                                                      (const PartSet_SelectionMode& theMode)
216 {
217   return boost::shared_ptr<GeomDataAPI_Point2D>();
218 }