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