Salome HOME
dfd39512dfd04867ed34e3b81b158e5a0a81b25a
[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_AttributeRefList.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_Session.h>
12 #include <ModelAPI_ResultConstruction.h>
13
14 #include <GeomDataAPI_Point.h>
15 #include <GeomDataAPI_Dir.h>
16 #include <GeomDataAPI_Point2D.h>
17 #include <GeomAPI_Pln.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomAPI_Pnt.h>
20 #include <GeomAPI_Edge.h>
21
22 #include <GeomAPI_Dir.h>
23 #include <GeomAPI_XYZ.h>
24
25 #include <SketchPlugin_Feature.h>
26 #include <SketchPlugin_Sketch.h>
27 #include <SketchPlugin_ConstraintCoincidence.h>
28 #include <SketchPlugin_ConstraintDistance.h>
29 #include <SketchPlugin_ConstraintLength.h>
30 #include <SketchPlugin_ConstraintRadius.h>
31 #include <SketchPlugin_ConstraintRigid.h>
32 #include <SketchPlugin_Constraint.h>
33 #include <SketchPlugin_Circle.h>
34 #include <SketchPlugin_Arc.h>
35 #include <SketchPlugin_Line.h>
36
37 #include <ModuleBase_ViewerPrs.h>
38
39 #include <V3d_View.hxx>
40 #include <gp_Pln.hxx>
41 #include <gp_Circ.hxx>
42 #include <ProjLib.hxx>
43 #include <ElSLib.hxx>
44 #include <Geom_Line.hxx>
45 #include <GeomAPI_ProjectPointOnCurve.hxx>
46 #include <BRep_Tool.hxx>
47 #include <TopoDS.hxx>
48 #include <TopoDS_Edge.hxx>
49
50 #ifdef _DEBUG
51 #include <QDebug>
52 #endif
53
54 const double PRECISION_TOLERANCE = 0.000001;
55
56 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
57 {
58   if (theView.IsNull())
59     return gp_Pnt();
60
61   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
62   theView->Eye(XEye, YEye, ZEye);
63
64   theView->At(XAt, YAt, ZAt);
65   gp_Pnt EyePoint(XEye, YEye, ZEye);
66   gp_Pnt AtPoint(XAt, YAt, ZAt);
67
68   gp_Vec EyeVector(EyePoint, AtPoint);
69   gp_Dir EyeDir(EyeVector);
70
71   gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
72   Standard_Real X, Y, Z;
73   theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
74   gp_Pnt ConvertedPoint(X, Y, Z);
75
76   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
77   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
78                                      PlaneOfTheView);
79   return ResultPoint;
80 }
81
82 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
83 Handle(V3d_View) theView,
84                                 double& theX, double& theY)
85 {
86   if (!theSketch)
87     return;
88
89   AttributeDoublePtr anAttr;
90   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
91
92   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
93       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
94
95   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
96       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
97   boost::shared_ptr<GeomDataAPI_Dir> anY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
98       aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
99
100   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
101   gp_Vec aVec(anOriginPnt, thePoint);
102
103   if (!theView.IsNull()) {
104     V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
105     theView->Eye(XEye, YEye, ZEye);
106
107     theView->At(XAt, YAt, ZAt);
108     gp_Pnt EyePoint(XEye, YEye, ZEye);
109     gp_Pnt AtPoint(XAt, YAt, ZAt);
110
111     gp_Vec anEyeVec(EyePoint, AtPoint);
112     anEyeVec.Normalize();
113
114     boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
115         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
116     gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
117
118     double aDen = anEyeVec * aNormalVec;
119     double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
120
121     gp_Vec aDeltaVec = anEyeVec * aLVec;
122     aVec = aVec - aDeltaVec;
123   }
124   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
125   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
126 }
127
128 void PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch,
129                                 gp_Pnt& thePoint)
130 {
131   if (!theSketch)
132     return;
133
134   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
135
136   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
137       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
138   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
139       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
140   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
141       aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
142
143   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
144       ->added(aY->dir()->xyz()->multiplied(theY));
145
146   boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
147   thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
148 }
149
150 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
151                                          FeaturePtr theSketch,
152                                          const QList<ModuleBase_ViewerPrs>& theFeatures)
153 {
154   double aX, anY;
155   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
156   PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
157
158   FeaturePtr aFeature;
159   FeaturePtr aDeltaFeature;
160   double aMinDelta = -1;
161   ModuleBase_ViewerPrs aPrs;
162   foreach (ModuleBase_ViewerPrs aPrs, theFeatures) {
163     if (!aPrs.object())
164       continue;
165     boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = boost::dynamic_pointer_cast<
166         SketchPlugin_Feature>(aPrs.object());
167     if (!aSketchFeature)
168       continue;
169     double aDelta = aSketchFeature->distanceToPoint(
170         boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
171     if (aMinDelta < 0 || aMinDelta > aDelta) {
172       aMinDelta = aDelta;
173       // TODO aDeltaFeature = aPrs.result();
174     }
175   }
176   return aDeltaFeature;
177 }
178
179 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
180 {
181   return ModelAPI_Session::get()->moduleDocument();
182 }
183
184 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::getFeaturePoint(FeaturePtr theFeature,
185                                                                       double theX, double theY)
186 {
187   boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>(
188                                                                  new GeomAPI_Pnt2d(theX, theY));
189   std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes =
190                                     theFeature->data()->attributes(GeomDataAPI_Point2D::type());
191   std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
192                                                                     aLast = anAttiributes.end();
193   boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
194   for (; anIt != aLast && !aFPoint; anIt++) {
195     boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
196         GeomDataAPI_Point2D>(*anIt);
197     if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
198       aFPoint = aCurPoint;
199   }
200
201   return aFPoint;
202 }
203
204 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
205                                     const std::string& theAttribute)
206 {
207   if (!theFeature)
208     return;
209   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
210   AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
211       aData->attribute(theAttribute));
212   if (anAttribute)
213     anAttribute->setValue(theValue);
214 }
215
216 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
217                                    bool& isValid)
218 {
219   isValid = false;
220   double aValue;
221   if (theFeature) {
222     boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
223     AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
224         aData->attribute(theAttribute));
225     if (anAttribute) {
226       aValue = anAttribute->value();
227       isValid = true;
228     }
229   }
230   return aValue;
231 }
232
233 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
234                                   const std::string& theKind)
235 {
236   FeaturePtr aFeature;
237   if (!theFeature)
238     return aFeature;
239
240   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
241   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
242       ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
243   if (anAttr) {
244     aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
245     if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
246       aFeature = FeaturePtr();
247     }
248   }
249   return aFeature;
250 }
251
252 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
253                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
254                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
255 {
256   FeaturePtr aFeature;
257   if (theSketch) {
258     aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
259   } else {
260     boost::shared_ptr<ModelAPI_Document> aDoc = document();
261     aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
262   }
263
264   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
265
266   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = boost::dynamic_pointer_cast<
267       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
268   aRef1->setAttr(thePoint1);
269
270   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = boost::dynamic_pointer_cast<
271       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
272   aRef2->setAttr(thePoint2);
273
274   if (aFeature)  // TODO: generate an error if feature was not created
275     aFeature->execute();
276 }
277
278 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
279                                    const std::string& theAttribute, double theClickedX,
280                                    double theClickedY)
281 {
282   // find a feature point by the selection mode
283   //boost::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
284   boost::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = boost::dynamic_pointer_cast<
285       GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
286   if (!aFeaturePoint)
287     return;
288
289   // get all sketch features. If the point with the given coordinates belong to any sketch feature,
290   // the constraint is created between the feature point and the found sketch point
291   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
292   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
293       ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
294
295   std::list<ObjectPtr> aFeatures = aRefList->list();
296   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
297   std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes;
298   boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>(
299       new GeomAPI_Pnt2d(theClickedX, theClickedY));
300   for (; anIt != aLast; anIt++) {
301     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
302     // find the given point in the feature attributes
303     anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
304     std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
305         aLast = anAttiributes.end();
306     boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
307     for (; anIt != aLast && !aFPoint; anIt++) {
308       boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
309           GeomDataAPI_Point2D>(*anIt);
310       if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
311         aFPoint = aCurPoint;
312     }
313     if (aFPoint)
314       PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
315   }
316 }
317
318 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
319 {
320   boost::shared_ptr<GeomAPI_Pln> aPlane;
321   double aA, aB, aC, aD;
322
323   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
324   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
325       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
326   boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
327       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
328   aA = aNormal->x();
329   aB = aNormal->y();
330   aC = aNormal->z();
331   aD = 0;
332
333   aPlane = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
334   return aPlane;
335 }
336
337 boost::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
338                                                       CompositeFeaturePtr theSketch)
339 {
340   boost::shared_ptr<GeomAPI_Pnt> aPoint;
341   if (!theSketch || !thePoint2D)
342     return aPoint;
343
344   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
345       theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
346   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
347       theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
348   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
349       theSketch->data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
350
351   return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
352 }
353
354 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
355 {
356   return theKind == SketchPlugin_ConstraintDistance::ID()
357       || theKind == SketchPlugin_ConstraintLength::ID()
358       || theKind == SketchPlugin_ConstraintRadius::ID()
359       || theKind == SketchPlugin_ConstraintRigid::ID();
360 }
361
362 ResultPtr PartSet_Tools::createFixedObjectByEdge(const ModuleBase_ViewerPrs& thePrs, CompositeFeaturePtr theSketch)
363 {
364   TopoDS_Shape aShape = thePrs.shape();
365   if (aShape.ShapeType() != TopAbs_EDGE)
366     return ResultPtr();
367
368   // Check that we already have such external edge
369   boost::shared_ptr<GeomAPI_Edge> aInEdge = boost::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
370   aInEdge->setImpl(new TopoDS_Shape(aShape));
371   ResultPtr aResult = findExternalEdge(theSketch, aInEdge);
372   if (aResult)
373     return aResult;
374
375   // If not found then we have to create new
376   Standard_Real aStart, aEnd;
377   Handle(V3d_View) aNullView;
378   FeaturePtr aMyFeature;
379
380   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aStart, aEnd);
381   GeomAdaptor_Curve aAdaptor(aCurve);
382   if (aAdaptor.GetType() == GeomAbs_Line) {
383     // Create line
384     aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
385
386     //DataPtr aData = myFeature->data();
387     //boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
388     //  boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
389
390     //double aX, aY;
391     //gp_Pnt Pnt1 = aAdaptor.Value(aStart);
392     //convertTo2D(Pnt1, theSketch, aNullView, aX, aY);
393     //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::START_ID());
394
395     //gp_Pnt Pnt2 = aAdaptor.Value(aEnd);
396     //convertTo2D(Pnt2, theSketch, aNullView, aX, aY);
397     //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::END_ID());
398   } else if (aAdaptor.GetType() == GeomAbs_Circle) {
399     if (aAdaptor.IsClosed()) {
400       // Create circle
401       aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
402       //gp_Circ aCirc = aAdaptor.Circle();
403       //gp_Pnt aCenter = aCirc.Location();
404
405       //double aX, aY;
406       //convertTo2D(aCenter, theSketch, aNullView, aX, aY);
407       //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Circle::CENTER_ID());
408       //setFeatureValue(myFeature, aCirc.Radius(), SketchPlugin_Circle::RADIUS_ID());
409     } else {
410       // Create arc
411       aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
412     }
413   }
414   if (aMyFeature) {
415     DataPtr aData = aMyFeature->data();
416     AttributeSelectionPtr anAttr = 
417       boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>
418       (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
419
420     ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
421     if (anAttr && aRes) {
422       boost::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
423       anEdge->setImpl(new TopoDS_Shape(aShape));
424
425       anAttr->setValue(aRes, anEdge);
426
427       aMyFeature->execute();
428
429       // fix this edge
430       FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
431       aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
432         setObject(aMyFeature->lastResult());
433
434       return aMyFeature->lastResult();
435     }
436   }
437   return ResultPtr();
438 }
439
440 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrs>& theSelected,
441                                           const ModuleBase_ViewerPrs& thePrs)
442 {
443   foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
444     if (aPrs.object() == thePrs.object())
445       return true;
446   }
447   return false;
448 }
449
450 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, boost::shared_ptr<GeomAPI_Edge> theEdge)
451 {
452   for (int i = 0; i < theSketch->numberOfSubs(); i++) {
453     FeaturePtr aFeature = theSketch->subFeature(i);
454     boost::shared_ptr<SketchPlugin_Feature> aSketchFea = 
455       boost::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
456     if (aSketchFea) {
457       if (aSketchFea->isExternal()) {
458         std::list<ResultPtr> aResults = aSketchFea->results();
459         std::list<ResultPtr>::const_iterator aIt;
460         for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
461           ResultConstructionPtr aRes = 
462             boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
463           if (aRes) {
464             boost::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
465             if (aShape) {
466               if (theEdge->isEqual(aShape))
467                 return aRes;
468             }
469           }
470         }
471       }
472     }
473   }
474   return ResultPtr();
475 }