]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Tools.cpp
Salome HOME
8f3090ec5501db13516676b05c9255c1c8843e16
[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     double aDelta = distanceToPoint(aPrs.feature(), aX, anY);
198     if (aMinDelta < 0 || aMinDelta > aDelta) {
199       aMinDelta = aDelta;
200       aDeltaFeature = aPrs.feature();
201     }
202   }
203   return aDeltaFeature;
204 }
205
206 double PartSet_Tools::distanceToPoint(FeaturePtr theFeature,
207                                       double theX, double theY)
208 {
209   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
210                                            theFeature->getKind(), FeaturePtr(), theFeature);
211   double aDelta = 0;
212   if (aFeaturePrs)
213     aDelta = aFeaturePrs->distanceToPoint(theFeature, theX, theY);
214
215   return aDelta;
216 }
217
218 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
219 {
220   return ModelAPI_PluginManager::get()->rootDocument();
221 }
222
223 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
224                                     const std::string& theAttribute)
225 {
226   if (!theFeature)
227     return;
228   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
229   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
230         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
231   if (aPoint)
232     aPoint->setValue(theX, theY);
233 }
234
235 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
236                                     const std::string& theAttribute)
237 {
238   if (!theFeature)
239     return;
240   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
241   AttributeDoublePtr anAttribute =
242         boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
243   if (anAttribute)
244     anAttribute->setValue(theValue);
245 }
246
247 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
248                                    bool& isValid)
249 {
250   isValid = false;
251   double aValue;
252   if (theFeature) {
253     boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
254     AttributeDoublePtr anAttribute =
255           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
256     if (anAttribute) {
257       aValue = anAttribute->value();
258       isValid = true;
259     }
260   }
261   return aValue;
262 }
263
264 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
265                                   const std::string& theKind)
266 {
267   FeaturePtr aFeature;
268   if (!theFeature)
269     return aFeature;
270
271   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
272   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
273           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
274   if (anAttr) {
275     aFeature = anAttr->feature();
276     if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
277       aFeature = FeaturePtr();
278     }
279   }
280   return aFeature;
281 }
282
283 void PartSet_Tools::createConstraint(FeaturePtr theSketch,
284                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
285                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
286 {
287   boost::shared_ptr<ModelAPI_Document> aDoc = document();
288   FeaturePtr aFeature = aDoc->addFeature(SKETCH_CONSTRAINT_COINCIDENCE_KIND);
289
290   if (theSketch) {
291     boost::shared_ptr<SketchPlugin_Feature> aSketch = 
292                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(theSketch);
293     aSketch->addSub(aFeature);
294   }
295
296   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
297
298   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
299         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
300   aRef1->setAttr(thePoint1);
301
302   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
303         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
304   aRef2->setAttr(thePoint2);
305
306   if (aFeature) // TODO: generate an error if feature was not created
307     aFeature->execute();
308 }
309
310 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findPoint(FeaturePtr theFeature,
311                                                                 double theX, double theY)
312 {
313   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
314                                            theFeature->getKind(), FeaturePtr(), theFeature);
315
316   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
317   if (aFeaturePrs)
318     aPoint2D = aFeaturePrs->findPoint(theFeature, theX, theY);
319
320   return aPoint2D;
321 }
322
323 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(FeaturePtr theSketch)
324 {
325   boost::shared_ptr<GeomAPI_Pln> aPlane;
326   double aA, aB, aC, aD;
327
328   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
329   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
330     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
331   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
332     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
333   aA = aNormal->x();
334   aB = aNormal->y();
335   aC = aNormal->z();
336   aD = 0;
337
338   aPlane = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
339   return aPlane;
340 }
341
342 boost::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(
343                                                 boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
344                                                 FeaturePtr theSketch)
345 {
346   boost::shared_ptr<GeomAPI_Pnt> aPoint;
347   if (!theSketch || !thePoint2D)
348     return aPoint;
349
350   boost::shared_ptr<GeomDataAPI_Point> aC = 
351     boost::dynamic_pointer_cast<GeomDataAPI_Point>(theSketch->data()->attribute(SKETCH_ATTR_ORIGIN));
352   boost::shared_ptr<GeomDataAPI_Dir> aX = 
353     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theSketch->data()->attribute(SKETCH_ATTR_DIRX));
354   boost::shared_ptr<GeomDataAPI_Dir> aY = 
355     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theSketch->data()->attribute(SKETCH_ATTR_DIRY));
356
357   return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
358 }
359
360 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
361 {
362   return theKind == PartSet_ConstraintLengthPrs::getKind() ||
363          theKind == PartSet_ConstraintDistancePrs::getKind() ||
364          theKind == PartSet_ConstraintRadiusPrs::getKind();
365 }