]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Tools.cpp
Salome HOME
Issue #332: Find point by coordinates comparison instead selection
[modules/shaper.git] / src / PartSet / PartSet_Tools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_Tools.h
4 // Created:     28 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include <PartSet_Tools.h>
8
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_AttributeRefList.h>
12 #include <ModelAPI_Document.h>
13 #include <ModelAPI_Session.h>
14 #include <ModelAPI_ResultConstruction.h>
15
16 #include <GeomDataAPI_Point.h>
17 #include <GeomDataAPI_Dir.h>
18 #include <GeomDataAPI_Point2D.h>
19 #include <GeomAPI_Pln.h>
20 #include <GeomAPI_Pnt2d.h>
21 #include <GeomAPI_Pnt.h>
22 #include <GeomAPI_Edge.h>
23 #include <GeomAPI_Vertex.h>
24
25 #include <GeomAPI_Dir.h>
26 #include <GeomAPI_XYZ.h>
27
28 #include <SketchPlugin_Feature.h>
29 #include <SketchPlugin_Sketch.h>
30 #include <SketchPlugin_ConstraintCoincidence.h>
31 #include <SketchPlugin_ConstraintDistance.h>
32 #include <SketchPlugin_ConstraintLength.h>
33 #include <SketchPlugin_ConstraintRadius.h>
34 #include <SketchPlugin_ConstraintRigid.h>
35 #include <SketchPlugin_Constraint.h>
36 #include <SketchPlugin_Circle.h>
37 #include <SketchPlugin_Arc.h>
38 #include <SketchPlugin_Line.h>
39 #include <SketchPlugin_Point.h>
40
41 #include <ModuleBase_ViewerPrs.h>
42
43 #include <V3d_View.hxx>
44 #include <gp_Pln.hxx>
45 #include <gp_Circ.hxx>
46 #include <ProjLib.hxx>
47 #include <ElSLib.hxx>
48 #include <Geom_Line.hxx>
49 #include <GeomAPI_ProjectPointOnCurve.hxx>
50 #include <BRep_Tool.hxx>
51 #include <TopoDS.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Vertex.hxx>
54
55 #ifdef _DEBUG
56 #include <QDebug>
57 #endif
58
59 const double PRECISION_TOLERANCE = 0.000001;
60
61 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
62 {
63   if (theView.IsNull())
64     return gp_Pnt();
65
66   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
67   theView->Eye(XEye, YEye, ZEye);
68
69   theView->At(XAt, YAt, ZAt);
70   gp_Pnt EyePoint(XEye, YEye, ZEye);
71   gp_Pnt AtPoint(XAt, YAt, ZAt);
72
73   gp_Vec EyeVector(EyePoint, AtPoint);
74   gp_Dir EyeDir(EyeVector);
75
76   gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
77   Standard_Real X, Y, Z;
78   theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
79   gp_Pnt ConvertedPoint(X, Y, Z);
80
81   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
82   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
83                                      PlaneOfTheView);
84   return ResultPoint;
85 }
86
87 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
88 Handle(V3d_View) theView,
89                                 double& theX, double& theY)
90 {
91   if (!theSketch)
92     return;
93
94   AttributeDoublePtr anAttr;
95   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
96
97   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
98       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
99
100   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
101       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
102   std::shared_ptr<GeomDataAPI_Dir> anY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
103       aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
104
105   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
106   gp_Vec aVec(anOriginPnt, thePoint);
107
108   if (!theView.IsNull()) {
109     V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
110     theView->Eye(XEye, YEye, ZEye);
111
112     theView->At(XAt, YAt, ZAt);
113     gp_Pnt EyePoint(XEye, YEye, ZEye);
114     gp_Pnt AtPoint(XAt, YAt, ZAt);
115
116     gp_Vec anEyeVec(EyePoint, AtPoint);
117     anEyeVec.Normalize();
118
119     std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
120         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
121     gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
122
123     double aDen = anEyeVec * aNormalVec;
124     double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
125
126     gp_Vec aDeltaVec = anEyeVec * aLVec;
127     aVec = aVec - aDeltaVec;
128   }
129   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
130   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
131 }
132
133 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch)
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_Pnt2d> aPnt2d = 
145     std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
146
147   return aPnt2d->to3D(aC->pnt(), aX->dir(), aY->dir());
148 }
149
150 ObjectPtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
151                                         FeaturePtr theSketch,
152                                         const QList<ModuleBase_ViewerPrs>& theSelected,
153                                         const QList<ModuleBase_ViewerPrs>& theHighlighted)
154 {
155   // firstly it finds the feature in the list of highlight
156   ObjectPtr aDeltaObject  = nearestFeature(thePoint, theView, theSketch, theHighlighted);
157   if (!aDeltaObject)
158     // secondly it finds the feature in the list of selected objects
159     aDeltaObject  = nearestFeature(thePoint, theView, theSketch, theSelected);
160
161   return aDeltaObject;
162 }
163
164 ObjectPtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
165                                         FeaturePtr theSketch,
166                                         const QList<ModuleBase_ViewerPrs>& thePresentations)
167 {
168   ObjectPtr aDeltaObject;
169
170   CompositeFeaturePtr aSketch = 
171       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
172   // 1. find it in the selected list by the selected point
173   if (!aDeltaObject) {
174     double aX, anY;
175     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
176     PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
177
178     FeaturePtr aFeature;
179     double aMinDelta = -1;
180     ModuleBase_ViewerPrs aPrs;
181     foreach (ModuleBase_ViewerPrs aPrs, thePresentations) {
182       if (!aPrs.object())
183         continue;
184       FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
185       std::shared_ptr<SketchPlugin_Feature> aSketchFeature = std::dynamic_pointer_cast<
186           SketchPlugin_Feature>(aFeature);
187       if (!aSketchFeature || !aSketch->isSub(aSketchFeature))
188         continue;
189
190       double aDelta = aSketchFeature->distanceToPoint(
191           std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
192       if (aMinDelta < 0 || aMinDelta > aDelta) {
193         aMinDelta = aDelta;
194         // TODO aDeltaObject = aPrs.result();
195       }
196     }
197   }
198   // 2. if the object is not found, returns the first selected sketch feature
199   if (!aDeltaObject && thePresentations.size() > 0) {
200     // there can be some highlighted objects, e.g. a result of boolean operation and a sketch point
201     foreach (ModuleBase_ViewerPrs aPrs, thePresentations) {
202       if (!aPrs.object())
203         continue;
204       FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
205       if (aFeature && aSketch->isSub(aFeature))
206         aDeltaObject = aPrs.object();
207     }
208   }
209   return aDeltaObject;
210 }
211
212 std::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
213 {
214   return ModelAPI_Session::get()->moduleDocument();
215 }
216
217 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::getFeaturePoint(FeaturePtr theFeature,
218                                                                       double theX, double theY)
219 {
220   std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
221                                                                  new GeomAPI_Pnt2d(theX, theY));
222   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes =
223                                     theFeature->data()->attributes(GeomDataAPI_Point2D::type());
224   std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
225                                                                     aLast = anAttiributes.end();
226   std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
227   for (; anIt != aLast && !aFPoint; anIt++) {
228     std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = std::dynamic_pointer_cast<
229         GeomDataAPI_Point2D>(*anIt);
230     if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
231       aFPoint = aCurPoint;
232   }
233
234   return aFPoint;
235 }
236
237 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
238                                     const std::string& theAttribute)
239 {
240   if (!theFeature)
241     return;
242   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
243   AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
244       aData->attribute(theAttribute));
245   if (anAttribute)
246     anAttribute->setValue(theValue);
247 }
248
249 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
250                                    bool& isValid)
251 {
252   isValid = false;
253   double aValue = 0;
254   if (theFeature) {
255     std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
256     AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
257         aData->attribute(theAttribute));
258     if (anAttribute) {
259       aValue = anAttribute->value();
260       isValid = true;
261     }
262   }
263   return aValue;
264 }
265
266 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
267                                   const std::string& theKind)
268 {
269   FeaturePtr aFeature;
270   if (!theFeature)
271     return aFeature;
272
273   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
274   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
275       ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
276   if (anAttr) {
277     aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
278     if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
279       aFeature = FeaturePtr();
280     }
281   }
282   return aFeature;
283 }
284
285 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
286                                      std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
287                                      std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
288 {
289   FeaturePtr aFeature;
290   if (theSketch) {
291     aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
292   } else {
293     std::shared_ptr<ModelAPI_Document> aDoc = document();
294     aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
295   }
296
297   std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
298
299   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
300       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
301   aRef1->setAttr(thePoint1);
302
303   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
304       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
305   aRef2->setAttr(thePoint2);
306
307   if (aFeature)  // TODO: generate an error if feature was not created
308     aFeature->execute();
309 }
310
311 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::
312   findAttributePoint(CompositeFeaturePtr theSketch, double theX, double theY,
313   double theTolerance, const QList<FeaturePtr>& theIgnore)
314 {
315   std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
316       new GeomAPI_Pnt2d(theX, theY));
317
318   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
319   for (int i = 0; i < theSketch->numberOfSubs(); i++) {
320     FeaturePtr aFeature = theSketch->subFeature(i);
321     if (!theIgnore.contains(aFeature)) {
322       anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
323
324       std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt;
325       for (anIt = anAttiributes.cbegin(); anIt != anAttiributes.cend(); ++anIt) {
326         std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
327           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
328         double x = aCurPoint->x();
329         double y = aCurPoint->y();
330         if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < theTolerance)) {
331           return aCurPoint;
332         }
333       }
334     }
335   }
336   return std::shared_ptr<GeomDataAPI_Point2D>();
337 }
338
339
340 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
341                                    const std::string& theAttribute, double theClickedX,
342                                    double theClickedY)
343 {
344   // find a feature point by the selection mode
345   //std::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
346   std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
347       GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
348   if (!aFeaturePoint)
349     return;
350
351   // get all sketch features. If the point with the given coordinates belong to any sketch feature,
352   // the constraint is created between the feature point and the found sketch point
353   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
354   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
355       ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
356
357   std::list<ObjectPtr> aFeatures = aRefList->list();
358   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
359   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
360   std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
361       new GeomAPI_Pnt2d(theClickedX, theClickedY));
362   for (; anIt != aLast; anIt++) {
363     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
364     // find the given point in the feature attributes
365     anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
366     std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
367         aLast = anAttiributes.end();
368     std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
369     for (; anIt != aLast && !aFPoint; anIt++) {
370       std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
371         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
372       if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())) {
373         aFPoint = aCurPoint;
374         break;
375       }
376     }
377     if (aFPoint)
378       PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
379   }
380 }
381
382 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
383 {
384   std::shared_ptr<GeomAPI_Pln> aPlane;
385
386   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
387   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
388       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
389   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
390       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
391   if (aNormal && anOrigin) {
392     double adX = aNormal->x();
393     double adY = aNormal->y();
394     double adZ = aNormal->z();
395
396     if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
397       double aX = anOrigin->x();
398       double aY = anOrigin->y();
399       double aZ = anOrigin->z();
400       gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
401       double aA, aB, aC, aD;
402       aPln.Coefficients(aA, aB, aC, aD);
403       aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
404     }
405   }
406   return aPlane;
407 }
408
409 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
410                                                       CompositeFeaturePtr theSketch)
411 {
412   std::shared_ptr<GeomAPI_Pnt> aPoint;
413   if (!theSketch || !thePoint2D)
414     return aPoint;
415
416   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
417       theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
418   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
419       theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
420   std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
421       theSketch->data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
422
423   return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
424 }
425
426 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
427 {
428   return theKind == SketchPlugin_ConstraintDistance::ID()
429       || theKind == SketchPlugin_ConstraintLength::ID()
430       || theKind == SketchPlugin_ConstraintRadius::ID()
431       || theKind == SketchPlugin_ConstraintRigid::ID();
432 }
433
434 ResultPtr PartSet_Tools::createFixedObjectByEdge(const TopoDS_Shape& theShape, 
435                                                  const ObjectPtr& theObject, 
436                                                  CompositeFeaturePtr theSketch)
437 {
438   if (theShape.ShapeType() == TopAbs_EDGE) {
439     // Check that we already have such external edge
440     std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
441     aInEdge->setImpl(new TopoDS_Shape(theShape));
442     ResultPtr aResult = findExternalEdge(theSketch, aInEdge);
443     if (aResult)
444       return aResult;
445
446     // If not found then we have to create new
447     Standard_Real aStart, aEnd;
448     Handle(V3d_View) aNullView;
449     FeaturePtr aMyFeature;
450
451     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
452     GeomAdaptor_Curve aAdaptor(aCurve);
453     if (aAdaptor.GetType() == GeomAbs_Line) {
454       // Create line
455       aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
456     } else if (aAdaptor.GetType() == GeomAbs_Circle) {
457       if (aAdaptor.IsClosed()) {
458         // Create circle
459         aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
460       } else {
461         // Create arc
462         aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
463       }
464     }
465     if (aMyFeature) {
466       DataPtr aData = aMyFeature->data();
467       AttributeSelectionPtr anAttr = 
468         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
469         (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
470
471       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
472       if (anAttr && aRes) {
473         std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
474         anEdge->setImpl(new TopoDS_Shape(theShape));
475
476         anAttr->setValue(aRes, anEdge);
477
478         aMyFeature->execute();
479
480         // fix this edge
481         FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
482         aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
483           setObject(aMyFeature->lastResult());
484
485         return aMyFeature->lastResult();
486       }
487     }
488   }
489   if (theShape.ShapeType() == TopAbs_VERTEX) {
490     std::shared_ptr<GeomAPI_Vertex> aInVert = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex());
491     aInVert->setImpl(new TopoDS_Shape(theShape));
492     ResultPtr aResult = findExternalVertex(theSketch, aInVert);
493     if (aResult)
494       return aResult;
495
496     FeaturePtr aMyFeature = theSketch->addFeature(SketchPlugin_Point::ID());
497
498     if (aMyFeature) {
499       DataPtr aData = aMyFeature->data();
500       AttributeSelectionPtr anAttr = 
501         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
502         (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
503
504       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
505       if (anAttr && aRes) {
506         std::shared_ptr<GeomAPI_Shape> aVert(new GeomAPI_Shape);
507         aVert->setImpl(new TopoDS_Shape(theShape));
508
509         anAttr->setValue(aRes, aVert);
510         aMyFeature->execute();
511
512         // fix this edge
513         FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
514         aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
515           setObject(aMyFeature->lastResult());
516
517         return aMyFeature->lastResult();
518       }
519     }
520   }
521   return ResultPtr();
522 }
523
524 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrs>& theSelected,
525                                           const ModuleBase_ViewerPrs& thePrs)
526 {
527   foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
528     if (aPrs.object() == thePrs.object())
529       return true;
530   }
531   return false;
532 }
533
534 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Edge> theEdge)
535 {
536   for (int i = 0; i < theSketch->numberOfSubs(); i++) {
537     FeaturePtr aFeature = theSketch->subFeature(i);
538     std::shared_ptr<SketchPlugin_Feature> aSketchFea = 
539       std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
540     if (aSketchFea) {
541       if (aSketchFea->isExternal()) {
542         std::list<ResultPtr> aResults = aSketchFea->results();
543         std::list<ResultPtr>::const_iterator aIt;
544         for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
545           ResultConstructionPtr aRes = 
546             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
547           if (aRes) {
548             std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
549             if (aShape) {
550               if (theEdge->isEqual(aShape))
551                 return aRes;
552             }
553           }
554         }
555       }
556     }
557   }
558   return ResultPtr();
559 }
560
561
562 ResultPtr PartSet_Tools::findExternalVertex(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Vertex> theVert)
563 {
564   for (int i = 0; i < theSketch->numberOfSubs(); i++) {
565     FeaturePtr aFeature = theSketch->subFeature(i);
566     std::shared_ptr<SketchPlugin_Feature> aSketchFea = 
567       std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
568     if (aSketchFea) {
569       if (aSketchFea->isExternal()) {
570         std::list<ResultPtr> aResults = aSketchFea->results();
571         std::list<ResultPtr>::const_iterator aIt;
572         for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
573           ResultConstructionPtr aRes = 
574             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
575           if (aRes) {
576             std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
577             if (aShape) {
578               if (theVert->isEqual(aShape))
579                 return aRes;
580             }
581           }
582         }
583       }
584     }
585   }
586   return ResultPtr();
587 }
588
589
590 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrs& thePrs, FeaturePtr theSketch,
591                                    Handle_V3d_View theView, double& theX, double& theY)
592 {
593   bool aHasVertex = false;
594
595   const TopoDS_Shape& aShape = thePrs.shape();
596   if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX)
597   {
598     const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
599     if (!aVertex.IsNull())
600     {
601       gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
602       PartSet_Tools::convertTo2D(aPoint, theSketch, theView, theX, theY);
603       aHasVertex = true;
604     }
605   }
606
607   return aHasVertex;
608 }
609
610 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj, 
611                                                    const TopoDS_Shape theShape, 
612                                                    FeaturePtr theSketch)
613 {
614
615   AttributePtr anAttribute;
616   FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
617   if (aFeature) {
618     if (theShape.ShapeType() == TopAbs_VERTEX) {
619       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
620       if (!aVertex.IsNull())  {
621         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
622         std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
623             new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
624
625         // find the given point in the feature attributes
626         std::list<AttributePtr> anAttiributes = 
627           aFeature->data()->attributes(GeomDataAPI_Point2D::type());
628         std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(), 
629                                                 aLast = anAttiributes.end();
630         for (; anIt != aLast && !anAttribute; anIt++) {
631           std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
632             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
633
634           std::shared_ptr<GeomAPI_Pnt> aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
635           if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
636             anAttribute = aCurPoint;
637             break;
638           }
639         }
640       }
641     }
642   }
643   return anAttribute;
644 }