Salome HOME
Provide preselection of Line and Point for Distance
[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 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch)
130 {
131   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
132
133   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
134       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
135   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
136       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
137   std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
138       aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
139
140   std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = 
141     std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
142
143   return aPnt2d->to3D(aC->pnt(), aX->dir(), aY->dir());
144 }
145
146 ObjectPtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
147                                         FeaturePtr theSketch,
148                                         const QList<ModuleBase_ViewerPrs>& theSelected,
149                                         const QList<ModuleBase_ViewerPrs>& theHighlighted)
150 {
151   // firstly it finds the feature in the list of highlight
152   ObjectPtr aDeltaObject  = nearestFeature(thePoint, theView, theSketch, theHighlighted);
153   if (!aDeltaObject)
154     // secondly it finds the feature in the list of selected objects
155     aDeltaObject  = nearestFeature(thePoint, theView, theSketch, theSelected);
156
157   return aDeltaObject;
158 }
159
160 ObjectPtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
161                                         FeaturePtr theSketch,
162                                         const QList<ModuleBase_ViewerPrs>& thePresentations)
163 {
164   ObjectPtr aDeltaObject;
165
166   CompositeFeaturePtr aSketch = 
167       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
168   // 1. find it in the selected list by the selected point
169   if (!aDeltaObject) {
170     double aX, anY;
171     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
172     PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
173
174     FeaturePtr aFeature;
175     double aMinDelta = -1;
176     ModuleBase_ViewerPrs aPrs;
177     foreach (ModuleBase_ViewerPrs aPrs, thePresentations) {
178       if (!aPrs.object())
179         continue;
180       FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
181       std::shared_ptr<SketchPlugin_Feature> aSketchFeature = std::dynamic_pointer_cast<
182           SketchPlugin_Feature>(aFeature);
183       if (!aSketchFeature || !aSketch->isSub(aSketchFeature))
184         continue;
185
186       double aDelta = aSketchFeature->distanceToPoint(
187           std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
188       if (aMinDelta < 0 || aMinDelta > aDelta) {
189         aMinDelta = aDelta;
190         // TODO aDeltaObject = aPrs.result();
191       }
192     }
193   }
194   // 2. if the object is not found, returns the first selected sketch feature
195   if (!aDeltaObject && thePresentations.size() > 0) {
196     // there can be some highlighted objects, e.g. a result of boolean operation and a sketch point
197     foreach (ModuleBase_ViewerPrs aPrs, thePresentations) {
198       if (!aPrs.object())
199         continue;
200       FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
201       if (aFeature && aSketch->isSub(aFeature))
202         aDeltaObject = aPrs.object();
203     }
204   }
205   return aDeltaObject;
206 }
207
208 std::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
209 {
210   return ModelAPI_Session::get()->moduleDocument();
211 }
212
213 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::getFeaturePoint(FeaturePtr theFeature,
214                                                                       double theX, double theY)
215 {
216   std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
217                                                                  new GeomAPI_Pnt2d(theX, theY));
218   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes =
219                                     theFeature->data()->attributes(GeomDataAPI_Point2D::type());
220   std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
221                                                                     aLast = anAttiributes.end();
222   std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
223   for (; anIt != aLast && !aFPoint; anIt++) {
224     std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = std::dynamic_pointer_cast<
225         GeomDataAPI_Point2D>(*anIt);
226     if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
227       aFPoint = aCurPoint;
228   }
229
230   return aFPoint;
231 }
232
233 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
234                                     const std::string& theAttribute)
235 {
236   if (!theFeature)
237     return;
238   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
239   AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
240       aData->attribute(theAttribute));
241   if (anAttribute)
242     anAttribute->setValue(theValue);
243 }
244
245 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
246                                    bool& isValid)
247 {
248   isValid = false;
249   double aValue = 0;
250   if (theFeature) {
251     std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
252     AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
253         aData->attribute(theAttribute));
254     if (anAttribute) {
255       aValue = anAttribute->value();
256       isValid = true;
257     }
258   }
259   return aValue;
260 }
261
262 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
263                                   const std::string& theKind)
264 {
265   FeaturePtr aFeature;
266   if (!theFeature)
267     return aFeature;
268
269   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
270   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
271       ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
272   if (anAttr) {
273     aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
274     if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
275       aFeature = FeaturePtr();
276     }
277   }
278   return aFeature;
279 }
280
281 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
282                                      std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
283                                      std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
284 {
285   FeaturePtr aFeature;
286   if (theSketch) {
287     aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
288   } else {
289     std::shared_ptr<ModelAPI_Document> aDoc = document();
290     aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
291   }
292
293   std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
294
295   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
296       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
297   aRef1->setAttr(thePoint1);
298
299   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
300       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
301   aRef2->setAttr(thePoint2);
302
303   if (aFeature)  // TODO: generate an error if feature was not created
304     aFeature->execute();
305 }
306
307 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
308                                    const std::string& theAttribute, double theClickedX,
309                                    double theClickedY)
310 {
311   // find a feature point by the selection mode
312   //std::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
313   std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
314       GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
315   if (!aFeaturePoint)
316     return;
317
318   // get all sketch features. If the point with the given coordinates belong to any sketch feature,
319   // the constraint is created between the feature point and the found sketch point
320   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
321   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
322       ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
323
324   std::list<ObjectPtr> aFeatures = aRefList->list();
325   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
326   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
327   std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
328       new GeomAPI_Pnt2d(theClickedX, theClickedY));
329   for (; anIt != aLast; anIt++) {
330     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
331     // find the given point in the feature attributes
332     anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
333     std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
334         aLast = anAttiributes.end();
335     std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
336     for (; anIt != aLast && !aFPoint; anIt++) {
337       std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = std::dynamic_pointer_cast<
338           GeomDataAPI_Point2D>(*anIt);
339       if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
340         aFPoint = aCurPoint;
341     }
342     if (aFPoint)
343       PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
344   }
345 }
346
347 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
348 {
349   std::shared_ptr<GeomAPI_Pln> aPlane;
350
351   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
352   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
353       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
354   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
355       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
356   if (aNormal && anOrigin) {
357     double adX = aNormal->x();
358     double adY = aNormal->y();
359     double adZ = aNormal->z();
360
361     if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
362       double aX = anOrigin->x();
363       double aY = anOrigin->y();
364       double aZ = anOrigin->z();
365       gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
366       double aA, aB, aC, aD;
367       aPln.Coefficients(aA, aB, aC, aD);
368       aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
369     }
370   }
371   return aPlane;
372 }
373
374 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
375                                                       CompositeFeaturePtr theSketch)
376 {
377   std::shared_ptr<GeomAPI_Pnt> aPoint;
378   if (!theSketch || !thePoint2D)
379     return aPoint;
380
381   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
382       theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
383   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
384       theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
385   std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
386       theSketch->data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
387
388   return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
389 }
390
391 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
392 {
393   return theKind == SketchPlugin_ConstraintDistance::ID()
394       || theKind == SketchPlugin_ConstraintLength::ID()
395       || theKind == SketchPlugin_ConstraintRadius::ID()
396       || theKind == SketchPlugin_ConstraintRigid::ID();
397 }
398
399 ResultPtr PartSet_Tools::createFixedObjectByEdge(const TopoDS_Shape& theShape, 
400                                                  const ObjectPtr& theObject, 
401                                                  CompositeFeaturePtr theSketch)
402 {
403   if (theShape.ShapeType() != TopAbs_EDGE)
404     return ResultPtr();
405
406   // Check that we already have such external edge
407   std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
408   aInEdge->setImpl(new TopoDS_Shape(theShape));
409   ResultPtr aResult = findExternalEdge(theSketch, aInEdge);
410   if (aResult)
411     return aResult;
412
413   // If not found then we have to create new
414   Standard_Real aStart, aEnd;
415   Handle(V3d_View) aNullView;
416   FeaturePtr aMyFeature;
417
418   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
419   GeomAdaptor_Curve aAdaptor(aCurve);
420   if (aAdaptor.GetType() == GeomAbs_Line) {
421     // Create line
422     aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
423   } else if (aAdaptor.GetType() == GeomAbs_Circle) {
424     if (aAdaptor.IsClosed()) {
425       // Create circle
426       aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
427     } else {
428       // Create arc
429       aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
430     }
431   }
432   if (aMyFeature) {
433     DataPtr aData = aMyFeature->data();
434     AttributeSelectionPtr anAttr = 
435       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
436       (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
437
438     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
439     if (anAttr && aRes) {
440       std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
441       anEdge->setImpl(new TopoDS_Shape(theShape));
442
443       anAttr->setValue(aRes, anEdge);
444
445       aMyFeature->execute();
446
447       // fix this edge
448       FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
449       aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
450         setObject(aMyFeature->lastResult());
451
452       return aMyFeature->lastResult();
453     }
454   }
455   return ResultPtr();
456 }
457
458 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrs>& theSelected,
459                                           const ModuleBase_ViewerPrs& thePrs)
460 {
461   foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
462     if (aPrs.object() == thePrs.object())
463       return true;
464   }
465   return false;
466 }
467
468 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Edge> theEdge)
469 {
470   for (int i = 0; i < theSketch->numberOfSubs(); i++) {
471     FeaturePtr aFeature = theSketch->subFeature(i);
472     std::shared_ptr<SketchPlugin_Feature> aSketchFea = 
473       std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
474     if (aSketchFea) {
475       if (aSketchFea->isExternal()) {
476         std::list<ResultPtr> aResults = aSketchFea->results();
477         std::list<ResultPtr>::const_iterator aIt;
478         for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
479           ResultConstructionPtr aRes = 
480             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
481           if (aRes) {
482             std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
483             if (aShape) {
484               if (theEdge->isEqual(aShape))
485                 return aRes;
486             }
487           }
488         }
489       }
490     }
491   }
492   return ResultPtr();
493 }
494
495 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrs& thePrs, FeaturePtr theSketch,
496                                    Handle_V3d_View theView, double& theX, double& theY)
497 {
498   bool aHasVertex = false;
499
500   const TopoDS_Shape& aShape = thePrs.shape();
501   if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX)
502   {
503     const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
504     if (!aVertex.IsNull())
505     {
506       gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
507       PartSet_Tools::convertTo2D(aPoint, theSketch, theView, theX, theY);
508       aHasVertex = true;
509     }
510   }
511
512   return aHasVertex;
513 }
514
515 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj, 
516                                                    const TopoDS_Shape theShape, 
517                                                    FeaturePtr theSketch)
518 {
519
520   AttributePtr anAttribute;
521   FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
522   if (aFeature) {
523     if (theShape.ShapeType() == TopAbs_VERTEX) {
524       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
525       if (!aVertex.IsNull())  {
526         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
527         std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
528             new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
529
530         // find the given point in the feature attributes
531         std::list<AttributePtr> anAttiributes = 
532           aFeature->data()->attributes(GeomDataAPI_Point2D::type());
533         std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(), 
534                                                 aLast = anAttiributes.end();
535         for (; anIt != aLast && !anAttribute; anIt++) {
536           std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
537             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
538
539           std::shared_ptr<GeomAPI_Pnt> aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
540           if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
541             anAttribute = aCurPoint;
542             break;
543           }
544         }
545       }
546     }
547   }
548   return anAttribute;
549 }