Salome HOME
a92364d0594b30c0c7d950c77e94bfc4995f720a
[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> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
103       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
104   std::shared_ptr<GeomAPI_XYZ> anY = aNorm->xyz()->cross(aX->xyz());
105
106   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
107   gp_Vec aVec(anOriginPnt, thePoint);
108
109   if (!theView.IsNull()) {
110     V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
111     theView->Eye(XEye, YEye, ZEye);
112
113     theView->At(XAt, YAt, ZAt);
114     gp_Pnt EyePoint(XEye, YEye, ZEye);
115     gp_Pnt AtPoint(XAt, YAt, ZAt);
116
117     gp_Vec anEyeVec(EyePoint, AtPoint);
118     anEyeVec.Normalize();
119
120     std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
121         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
122     gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
123
124     double aDen = anEyeVec * aNormalVec;
125     double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
126
127     gp_Vec aDeltaVec = anEyeVec * aLVec;
128     aVec = aVec - aDeltaVec;
129   }
130   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
131   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
132 }
133
134 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch)
135 {
136   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
137
138   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
139       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
140   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
141       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
142   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
143       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
144   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
145
146   std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = 
147     std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
148
149   return aPnt2d->to3D(aC->pnt(), aX->dir(), aY);
150 }
151
152 ObjectPtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
153                                         FeaturePtr theSketch,
154                                         const QList<ModuleBase_ViewerPrs>& theSelected,
155                                         const QList<ModuleBase_ViewerPrs>& theHighlighted)
156 {
157   // firstly it finds the feature in the list of highlight
158   ObjectPtr aDeltaObject  = nearestFeature(thePoint, theView, theSketch, theHighlighted);
159   if (!aDeltaObject)
160     // secondly it finds the feature in the list of selected objects
161     aDeltaObject  = nearestFeature(thePoint, theView, theSketch, theSelected);
162
163   return aDeltaObject;
164 }
165
166 ObjectPtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
167                                         FeaturePtr theSketch,
168                                         const QList<ModuleBase_ViewerPrs>& thePresentations)
169 {
170   ObjectPtr aDeltaObject;
171
172   CompositeFeaturePtr aSketch = 
173       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
174   // 1. find it in the selected list by the selected point
175   if (!aDeltaObject) {
176     double aX, anY;
177     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
178     PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
179
180     FeaturePtr aFeature;
181     double aMinDelta = -1;
182     ModuleBase_ViewerPrs aPrs;
183     foreach (ModuleBase_ViewerPrs aPrs, thePresentations) {
184       if (!aPrs.object())
185         continue;
186       FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
187       std::shared_ptr<SketchPlugin_Feature> aSketchFeature = std::dynamic_pointer_cast<
188           SketchPlugin_Feature>(aFeature);
189       if (!aSketchFeature || !aSketch->isSub(aSketchFeature))
190         continue;
191
192       double aDelta = aSketchFeature->distanceToPoint(
193           std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
194       if (aMinDelta < 0 || aMinDelta > aDelta) {
195         aMinDelta = aDelta;
196         // TODO aDeltaObject = aPrs.result();
197       }
198     }
199   }
200   // 2. if the object is not found, returns the first selected sketch feature
201   if (!aDeltaObject && thePresentations.size() > 0) {
202     // there can be some highlighted objects, e.g. a result of boolean operation and a sketch point
203     foreach (ModuleBase_ViewerPrs aPrs, thePresentations) {
204       if (!aPrs.object())
205         continue;
206       FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
207       if (aFeature && aSketch->isSub(aFeature))
208         aDeltaObject = aPrs.object();
209     }
210   }
211   return aDeltaObject;
212 }
213
214 std::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
215 {
216   return ModelAPI_Session::get()->moduleDocument();
217 }
218
219 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::getFeaturePoint(FeaturePtr theFeature,
220                                                                       double theX, double theY)
221 {
222   std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
223                                                                  new GeomAPI_Pnt2d(theX, theY));
224   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes =
225                                     theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
226   std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
227                                                                     aLast = anAttiributes.end();
228   std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
229   for (; anIt != aLast && !aFPoint; anIt++) {
230     std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = std::dynamic_pointer_cast<
231         GeomDataAPI_Point2D>(*anIt);
232     if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
233       aFPoint = aCurPoint;
234   }
235
236   return aFPoint;
237 }
238
239 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
240                                     const std::string& theAttribute)
241 {
242   if (!theFeature)
243     return;
244   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
245   AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
246       aData->attribute(theAttribute));
247   if (anAttribute)
248     anAttribute->setValue(theValue);
249 }
250
251 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
252                                    bool& isValid)
253 {
254   isValid = false;
255   double aValue = 0;
256   if (theFeature) {
257     std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
258     AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
259         aData->attribute(theAttribute));
260     if (anAttribute) {
261       aValue = anAttribute->value();
262       isValid = true;
263     }
264   }
265   return aValue;
266 }
267
268 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
269                                   const std::string& theKind)
270 {
271   FeaturePtr aFeature;
272   if (!theFeature)
273     return aFeature;
274
275   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
276   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
277       ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
278   if (anAttr) {
279     aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
280     if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
281       aFeature = FeaturePtr();
282     }
283   }
284   return aFeature;
285 }
286
287 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
288                                      std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
289                                      std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
290 {
291   FeaturePtr aFeature;
292   if (theSketch) {
293     aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
294   } else {
295     std::shared_ptr<ModelAPI_Document> aDoc = document();
296     aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
297   }
298
299   std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
300
301   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
302       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
303   aRef1->setAttr(thePoint1);
304
305   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
306       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
307   aRef2->setAttr(thePoint2);
308
309   if (aFeature)  // TODO: generate an error if feature was not created
310     aFeature->execute();
311 }
312
313 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::
314   findAttributePoint(CompositeFeaturePtr theSketch, double theX, double theY,
315   double theTolerance, const QList<FeaturePtr>& theIgnore)
316 {
317   std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
318       new GeomAPI_Pnt2d(theX, theY));
319
320   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
321   for (int i = 0; i < theSketch->numberOfSubs(); i++) {
322     FeaturePtr aFeature = theSketch->subFeature(i);
323     if (!theIgnore.contains(aFeature)) {
324       anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
325
326       std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt;
327       for (anIt = anAttiributes.cbegin(); anIt != anAttiributes.cend(); ++anIt) {
328         std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
329           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
330         double x = aCurPoint->x();
331         double y = aCurPoint->y();
332         if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < theTolerance)) {
333           return aCurPoint;
334         }
335       }
336     }
337   }
338   return std::shared_ptr<GeomDataAPI_Point2D>();
339 }
340
341
342 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
343                                    const std::string& theAttribute, double theClickedX,
344                                    double theClickedY)
345 {
346   // find a feature point by the selection mode
347   //std::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
348   std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
349       GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
350   if (!aFeaturePoint)
351     return;
352
353   // get all sketch features. If the point with the given coordinates belong to any sketch feature,
354   // the constraint is created between the feature point and the found sketch point
355   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
356   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
357       ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
358
359   std::list<ObjectPtr> aFeatures = aRefList->list();
360   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
361   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
362   std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
363       new GeomAPI_Pnt2d(theClickedX, theClickedY));
364   for (; anIt != aLast; anIt++) {
365     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
366     if (theFeature == aFeature)
367       continue;
368     // find the given point in the feature attributes
369     anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
370     std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
371         aLast = anAttiributes.end();
372     std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
373     for (; anIt != aLast && !aFPoint; anIt++) {
374       std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
375         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
376       if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())) {
377         aFPoint = aCurPoint;
378         break;
379       }
380     }
381     if (aFPoint)
382       PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
383   }
384 }
385
386 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
387 {
388   std::shared_ptr<GeomAPI_Pln> aPlane;
389
390   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
391   if (aData) {
392     std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
393         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
394     std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
395         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
396     if (aNormal && anOrigin) {
397       double adX = aNormal->x();
398       double adY = aNormal->y();
399       double adZ = aNormal->z();
400
401       if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
402         double aX = anOrigin->x();
403         double aY = anOrigin->y();
404         double aZ = anOrigin->z();
405         gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
406         double aA, aB, aC, aD;
407         aPln.Coefficients(aA, aB, aC, aD);
408         aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
409       }
410     }
411   }
412   return aPlane;
413 }
414
415 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
416                                                       CompositeFeaturePtr theSketch)
417 {
418   std::shared_ptr<GeomAPI_Pnt> aPoint;
419   if (!theSketch || !thePoint2D)
420     return aPoint;
421
422   DataPtr aData = theSketch->data();
423   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
424       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
425   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
426       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
427   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
428       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
429   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
430
431   return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
432 }
433
434 ResultPtr PartSet_Tools::createFixedObjectByExternal(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_SketchEntity::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_SketchEntity::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::typeId());
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 }