Salome HOME
29a0d19c27f4508a707f9a9a8e689c38c7869445
[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<ModelAPI_Data> aData = theSketch->data();
116   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
117     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
118   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
119     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
120   gp_Pln aPlane(aNormal->x(), aNormal->y(), aNormal->z(), 0);
121
122   aData = theFeature->data();
123   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
124           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
125   if (!anAttr)
126     return thePreviuos;
127   FeaturePtr aFeature = anAttr->feature();
128   if (!aFeature || aFeature->getKind() != SKETCH_LINE_KIND)
129     return thePreviuos;
130
131   boost::shared_ptr<ModelAPI_AttributeDouble> aFlyoutAttr = 
132           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
133   double aFlyout = aFlyoutAttr->value();
134
135   aData = aFeature->data();
136   if (!aData->isValid())
137     return thePreviuos;
138
139   boost::shared_ptr<GeomDataAPI_Point2D> aPointStart =
140         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
141   boost::shared_ptr<GeomDataAPI_Point2D> aPointEnd =
142         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
143
144   gp_Pnt aPoint1, aPoint2;
145   PartSet_Tools::convertTo3D(aPointStart->x(), aPointStart->y(), theSketch, aPoint1);
146   PartSet_Tools::convertTo3D(aPointEnd->x(), aPointEnd->y(), theSketch, aPoint2);
147
148   //Build dimension here
149   gp_Pnt aP1 = aPoint1;
150   gp_Pnt aP2 = aPoint2;
151   if (aFlyout < 0) {
152     aP1 = aPoint2;
153     aP2 = aPoint1;
154   }
155
156   Handle(AIS_InteractiveObject) anAIS = thePreviuos;
157   if (anAIS.IsNull())
158   {
159     Handle(AIS_LengthDimension) aDimAIS = new AIS_LengthDimension (aP1, aP2, aPlane);
160
161     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
162     anAspect->MakeArrows3d (Standard_False);
163     anAspect->MakeText3d(false);
164     anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
165     anAspect->MakeTextShaded(false);
166     aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
167     //if (isUnitsDisplayed)
168     //{
169     //  aDimAIS->SetDisplayUnits (aDimDlg->GetUnits ());
170     //}
171     aDimAIS->SetDimensionAspect (anAspect);
172     aDimAIS->SetFlyout(aFlyout);
173     aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
174
175     anAIS = aDimAIS;
176   }
177   else {
178     // update presentation
179     Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
180     if (!aDimAIS.IsNull()) {
181       aDimAIS->SetMeasuredGeometry(aPoint1, aPoint2, aPlane);
182       aDimAIS->SetFlyout(aFlyout);
183
184       aDimAIS->Redisplay(Standard_True);
185     }
186   }
187 */
188   return anAIS;
189 }
190
191 std::string PartSet_ConstraintDistancePrs::getAttribute(const PartSet_SelectionMode& theMode) const
192 {
193   return "";
194 }
195
196 PartSet_SelectionMode PartSet_ConstraintDistancePrs::getNextMode(const std::string& theAttribute) const
197 {
198   return SM_FirstPoint;
199 }
200
201 void PartSet_ConstraintDistancePrs::move(double theDeltaX, double theDeltaY)
202 {
203 }
204
205 double PartSet_ConstraintDistancePrs::distanceToPoint(FeaturePtr theFeature,
206                                                  double theX, double theY)
207 {
208   return 0;
209 }
210
211 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::findPoint(FeaturePtr theFeature,
212                                                                            double theX, double theY)
213 {
214   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
215   return aPoint2D;
216 }
217
218 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::featurePoint
219                                                      (const PartSet_SelectionMode& theMode)
220 {
221   return boost::shared_ptr<GeomDataAPI_Point2D>();
222 }