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