Salome HOME
8d1329046f89eca5481f2a05b29576a091d15350
[modules/shaper.git] / src / PartSet / PartSet_Tools.cpp
1 // File:        PartSet_Tools.h
2 // Created:     28 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_Tools.h>
6
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_AttributeDouble.h>
9 #include <ModelAPI_Document.h>
10
11 #include <GeomDataAPI_Point.h>
12 #include <GeomDataAPI_Dir.h>
13 #include <GeomDataAPI_Point2D.h>
14 #include <GeomAPI_Pln.h>
15 #include <GeomAPI_Pnt2d.h>
16 #include <GeomAPI_Pnt.h>
17
18 #include <GeomAPI_Dir.h>
19 #include <GeomAPI_XYZ.h>
20
21 #include <SketchPlugin_Feature.h>
22 #include <SketchPlugin_Sketch.h>
23 #include <SketchPlugin_ConstraintCoincidence.h>
24 #include <SketchPlugin_Constraint.h>
25
26 #include <PartSet_FeatureLinePrs.h>
27 #include <PartSet_FeaturePointPrs.h>
28 #include <PartSet_FeatureCirclePrs.h>
29 #include <PartSet_FeatureArcPrs.h>
30
31 #include <PartSet_ConstraintLengthPrs.h>
32 #include <PartSet_ConstraintRadiusPrs.h>
33 #include <PartSet_ConstraintDistancePrs.h>
34
35 #include <XGUI_ViewerPrs.h>
36
37 #include <V3d_View.hxx>
38 #include <gp_Pln.hxx>
39 #include <ProjLib.hxx>
40 #include <ElSLib.hxx>
41 #include <Geom_Line.hxx>
42 #include <GeomAPI_ProjectPointOnCurve.hxx>
43
44 #ifdef _DEBUG
45 #include <QDebug>
46 #endif
47
48 const double PRECISION_TOLERANCE = 0.000001;
49
50 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
51 {
52   if (theView.IsNull())
53     return gp_Pnt();
54
55   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
56   theView->Eye(XEye, YEye, ZEye);
57
58   theView->At(XAt, YAt, ZAt);
59   gp_Pnt EyePoint(XEye, YEye, ZEye);
60   gp_Pnt AtPoint(XAt, YAt, ZAt);
61
62   gp_Vec EyeVector(EyePoint, AtPoint);
63   gp_Dir EyeDir(EyeVector);
64
65   gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
66   Standard_Real X, Y, Z;
67   theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
68   gp_Pnt ConvertedPoint(X, Y, Z);
69
70   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
71   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
72   return ResultPoint;
73 }
74
75 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
76                                 Handle(V3d_View) theView, double& theX, double& theY)
77 {
78   if (!theSketch)
79     return;
80
81   AttributeDoublePtr anAttr;
82   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
83
84   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
85     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
86
87   boost::shared_ptr<GeomDataAPI_Dir> aX = 
88     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
89   boost::shared_ptr<GeomDataAPI_Dir> anY = 
90     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
91
92   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
93   gp_Vec aVec(anOriginPnt, thePoint);
94
95   if (!theView.IsNull())
96   {
97     V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
98     theView->Eye(XEye, YEye, ZEye);
99
100     theView->At(XAt, YAt, ZAt);
101     gp_Pnt EyePoint(XEye, YEye, ZEye);
102     gp_Pnt AtPoint(XAt, YAt, ZAt);
103
104     gp_Vec anEyeVec(EyePoint, AtPoint);
105     anEyeVec.Normalize();
106
107     boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
108                   boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
109     gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
110
111     double aDen = anEyeVec * aNormalVec;
112     double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
113
114     gp_Vec aDeltaVec = anEyeVec*aLVec;
115     aVec = aVec - aDeltaVec;
116   }
117   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
118   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
119 }
120
121 void PartSet_Tools::convertTo3D(const double theX, const double theY,
122                                 FeaturePtr theSketch,
123                                 gp_Pnt& thePoint)
124 {
125   if (!theSketch)
126     return;
127
128   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
129
130   boost::shared_ptr<GeomDataAPI_Point> aC = 
131     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
132   boost::shared_ptr<GeomDataAPI_Dir> aX = 
133     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
134   boost::shared_ptr<GeomDataAPI_Dir> aY = 
135     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
136
137   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
138     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
139
140   boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
141   thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
142 }
143
144 boost::shared_ptr<PartSet_FeaturePrs> PartSet_Tools::createFeaturePrs(const std::string& theKind,
145                                                                       FeaturePtr theSketch,
146                                                                       FeaturePtr theFeature)
147 {
148   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs;
149
150   if (theKind == PartSet_FeaturePointPrs::getKind()) {
151     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeaturePointPrs(theSketch));
152   }
153   else if (theKind == PartSet_FeatureLinePrs::getKind()) {
154     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureLinePrs(theSketch));
155   }
156   else if (theKind == PartSet_FeatureCirclePrs::getKind()) {
157     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureCirclePrs(theSketch));
158   }
159   else if (theKind == PartSet_FeatureArcPrs::getKind()) {
160     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureArcPrs(theSketch));
161   }
162   else if (theKind == PartSet_ConstraintLengthPrs::getKind()) {
163     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_ConstraintLengthPrs(theSketch));
164   }
165   else if (theKind == PartSet_ConstraintRadiusPrs::getKind()) {
166     aFeaturePrs = boost::shared_ptr<PartSet_ConstraintRadiusPrs>(new PartSet_ConstraintRadiusPrs(theSketch));
167   }
168   else if (theKind == PartSet_ConstraintDistancePrs::getKind()) {
169     aFeaturePrs = boost::shared_ptr<PartSet_ConstraintDistancePrs>(new PartSet_ConstraintDistancePrs(theSketch));
170   }
171
172
173   if (theFeature && aFeaturePrs)
174     aFeaturePrs->init(theFeature);
175
176   return aFeaturePrs;
177 }
178
179 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
180                                          FeaturePtr theSketch,
181                                          const std::list<XGUI_ViewerPrs>& theFeatures)
182 {
183   double aX, anY;
184   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
185   PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
186
187   FeaturePtr aFeature;
188   std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
189
190   FeaturePtr aDeltaFeature;   
191   double aMinDelta = -1;
192   XGUI_ViewerPrs aPrs;
193   for (; anIt != aLast; anIt++) {
194     aPrs = *anIt;
195     if (!aPrs.feature())
196       continue;
197     boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
198                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(aPrs.feature());
199     if (!aSketchFeature)
200       continue;
201     double aDelta = aSketchFeature->distanceToPoint(
202                                boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
203     if (aMinDelta < 0 || aMinDelta > aDelta) {
204       aMinDelta = aDelta;
205       aDeltaFeature = aPrs.feature();
206     }
207   }
208   return aDeltaFeature;
209 }
210
211 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
212 {
213   return ModelAPI_PluginManager::get()->rootDocument();
214 }
215
216 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
217                                     const std::string& theAttribute)
218 {
219   if (!theFeature)
220     return;
221   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
222   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
223         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
224   if (aPoint)
225     aPoint->setValue(theX, theY);
226 }
227
228 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
229                                     const std::string& theAttribute)
230 {
231   if (!theFeature)
232     return;
233   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
234   AttributeDoublePtr anAttribute =
235         boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
236   if (anAttribute)
237     anAttribute->setValue(theValue);
238 }
239
240 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
241                                    bool& isValid)
242 {
243   isValid = false;
244   double aValue;
245   if (theFeature) {
246     boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
247     AttributeDoublePtr anAttribute =
248           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
249     if (anAttribute) {
250       aValue = anAttribute->value();
251       isValid = true;
252     }
253   }
254   return aValue;
255 }
256
257 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
258                                   const std::string& theKind)
259 {
260   FeaturePtr aFeature;
261   if (!theFeature)
262     return aFeature;
263
264   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
265   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
266           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
267   if (anAttr) {
268     aFeature = anAttr->feature();
269     if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
270       aFeature = FeaturePtr();
271     }
272   }
273   return aFeature;
274 }
275
276 void PartSet_Tools::createConstraint(FeaturePtr theSketch,
277                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
278                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
279 {
280   boost::shared_ptr<ModelAPI_Document> aDoc = document();
281   FeaturePtr aFeature = aDoc->addFeature(SKETCH_CONSTRAINT_COINCIDENCE_KIND);
282
283   if (theSketch) {
284     boost::shared_ptr<SketchPlugin_Feature> aSketch = 
285                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(theSketch);
286     aSketch->addSub(aFeature);
287   }
288
289   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
290
291   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
292         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
293   aRef1->setAttr(thePoint1);
294
295   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
296         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
297   aRef2->setAttr(thePoint2);
298
299   if (aFeature) // TODO: generate an error if feature was not created
300     aFeature->execute();
301 }
302
303 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findPoint(FeaturePtr theFeature,
304                                                                 double theX, double theY)
305 {
306   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
307                                            theFeature->getKind(), FeaturePtr(), theFeature);
308
309   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
310   if (aFeaturePrs)
311     aPoint2D = aFeaturePrs->findPoint(theFeature, theX, theY);
312
313   return aPoint2D;
314 }
315
316 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(FeaturePtr theSketch)
317 {
318   boost::shared_ptr<GeomAPI_Pln> aPlane;
319   double aA, aB, aC, aD;
320
321   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
322   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
323     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
324   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
325     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
326   aA = aNormal->x();
327   aB = aNormal->y();
328   aC = aNormal->z();
329   aD = 0;
330
331   aPlane = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
332   return aPlane;
333 }
334
335 boost::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(
336                                                 boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
337                                                 FeaturePtr theSketch)
338 {
339   boost::shared_ptr<GeomAPI_Pnt> aPoint;
340   if (!theSketch || !thePoint2D)
341     return aPoint;
342
343   boost::shared_ptr<GeomDataAPI_Point> aC = 
344     boost::dynamic_pointer_cast<GeomDataAPI_Point>(theSketch->data()->attribute(SKETCH_ATTR_ORIGIN));
345   boost::shared_ptr<GeomDataAPI_Dir> aX = 
346     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theSketch->data()->attribute(SKETCH_ATTR_DIRX));
347   boost::shared_ptr<GeomDataAPI_Dir> aY = 
348     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theSketch->data()->attribute(SKETCH_ATTR_DIRY));
349
350   return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
351 }
352
353 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
354 {
355   return theKind == PartSet_ConstraintLengthPrs::getKind() ||
356          theKind == PartSet_ConstraintDistancePrs::getKind() ||
357          theKind == PartSet_ConstraintRadiusPrs::getKind();
358 }