]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Tools.cpp
Salome HOME
Issue #2440 Can't validate coincidence between a line and an imported line
[modules/shaper.git] / src / PartSet / PartSet_Tools.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <PartSet_Tools.h>
22 #include <PartSet_Module.h>
23 #include <PartSet_SketcherMgr.h>
24
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeRefList.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_ResultConstruction.h>
31 #include <ModelAPI_Events.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_Tools.h>
34
35 #include <ModuleBase_IViewWindow.h>
36
37 #include <ModelGeomAlgo_Point2D.h>
38
39 #include <Events_Loop.h>
40
41 #include <SketcherPrs_Tools.h>
42
43 #include <XGUI_ModuleConnector.h>
44 #include <XGUI_Displayer.h>
45 #include <XGUI_Workshop.h>
46 #include <XGUI_SelectionMgr.h>
47 #include <XGUI_Selection.h>
48
49 #include <GeomDataAPI_Point.h>
50 #include <GeomDataAPI_Dir.h>
51 #include <GeomDataAPI_Point2D.h>
52 #include <GeomAPI_Pln.h>
53 #include <GeomAPI_Pnt2d.h>
54 #include <GeomAPI_Pnt.h>
55 #include <GeomAPI_Edge.h>
56 #include <GeomAPI_Vertex.h>
57
58 #include <GeomAPI_Dir.h>
59 #include <GeomAPI_XYZ.h>
60
61 #include <SketchPlugin_Feature.h>
62 #include <SketchPlugin_Sketch.h>
63 #include <SketchPlugin_ConstraintCoincidence.h>
64 #include <SketchPlugin_ConstraintDistance.h>
65 #include <SketchPlugin_ConstraintLength.h>
66 #include <SketchPlugin_ConstraintRadius.h>
67 #include <SketchPlugin_ConstraintRigid.h>
68 #include <SketchPlugin_Constraint.h>
69 #include <SketchPlugin_Circle.h>
70 #include <SketchPlugin_Arc.h>
71 #include <SketchPlugin_Line.h>
72 #include <SketchPlugin_Point.h>
73 #include <SketchPlugin_Projection.h>
74
75 #include <ModuleBase_IWorkshop.h>
76 #include <ModuleBase_ViewerPrs.h>
77 #include <ModuleBase_Tools.h>
78
79 #include <V3d_View.hxx>
80 #include <gp_Pln.hxx>
81 #include <gp_Circ.hxx>
82 #include <ProjLib.hxx>
83 #include <ElSLib.hxx>
84 #include <Geom_Line.hxx>
85 #include <GeomAPI_ProjectPointOnCurve.hxx>
86 #include <BRep_Tool.hxx>
87 #include <TopoDS.hxx>
88 #include <TopoDS_Edge.hxx>
89 #include <TopoDS_Vertex.hxx>
90 #include <AIS_InteractiveObject.hxx>
91 #include <StdSelect_BRepOwner.hxx>
92 #include <SelectMgr_IndexedMapOfOwner.hxx>
93 #include <V3d_Coordinate.hxx>
94
95 #include <QMouseEvent>
96
97 #ifdef _DEBUG
98 #include <QDebug>
99 #endif
100
101 const double PRECISION_TOLERANCE = 0.000001;
102 const int AIS_DEFAULT_WIDTH = 2;
103 const bool SKETCH_PROJECTION_INCLUDE_INTO_RESULT = false; // by default, it is not presented
104
105 int PartSet_Tools::getAISDefaultWidth()
106 {
107   return AIS_DEFAULT_WIDTH;
108 }
109
110 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
111 {
112   if (theView.IsNull())
113     return gp_Pnt();
114
115   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
116   theView->Eye(XEye, YEye, ZEye);
117
118   theView->At(XAt, YAt, ZAt);
119   gp_Pnt EyePoint(XEye, YEye, ZEye);
120   gp_Pnt AtPoint(XAt, YAt, ZAt);
121
122   gp_Vec EyeVector(EyePoint, AtPoint);
123   gp_Dir EyeDir(EyeVector);
124
125   gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
126   Standard_Real X, Y, Z;
127   theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
128   gp_Pnt ConvertedPoint(X, Y, Z);
129
130   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
131   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
132                                      PlaneOfTheView);
133   return ResultPoint;
134 }
135
136 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
137 Handle(V3d_View) theView,
138                                 double& theX, double& theY)
139 {
140   if (!theSketch)
141     return;
142
143   AttributeDoublePtr anAttr;
144   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
145
146   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
147       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
148
149   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
150       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
151   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
152       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
153   std::shared_ptr<GeomAPI_XYZ> anY = aNorm->xyz()->cross(aX->xyz());
154
155   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
156   gp_Vec aVec(anOriginPnt, thePoint);
157
158   if (!theView.IsNull()) {
159     V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
160     theView->Eye(XEye, YEye, ZEye);
161
162     theView->At(XAt, YAt, ZAt);
163     gp_Pnt EyePoint(XEye, YEye, ZEye);
164     gp_Pnt AtPoint(XAt, YAt, ZAt);
165
166     gp_Vec anEyeVec(EyePoint, AtPoint);
167     anEyeVec.Normalize();
168
169     std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
170         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
171     gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
172
173     double aDen = anEyeVec * aNormalVec;
174     double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
175
176     gp_Vec aDeltaVec = anEyeVec * aLVec;
177     aVec = aVec - aDeltaVec;
178   }
179   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
180   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
181 }
182
183 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::convertTo2D(FeaturePtr theSketch,
184                                                     const std::shared_ptr<GeomAPI_Pnt>& thePnt)
185 {
186   std::shared_ptr<GeomAPI_Pnt2d> aRes;
187   if (theSketch->getKind() != SketchPlugin_Sketch::ID())
188     return aRes;
189   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
190       theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
191   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
192       theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
193   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
194       theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
195   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
196   return thePnt->to2D(aC->pnt(), aX->dir(), aY);
197 }
198
199
200 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const double theY,
201                                                         FeaturePtr theSketch)
202 {
203   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
204
205   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
206       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
207   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
208       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
209   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
210       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
211   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
212
213   std::shared_ptr<GeomAPI_Pnt2d> aPnt2d =
214     std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
215
216   return aPnt2d->to3D(aC->pnt(), aX->dir(), aY);
217 }
218
219 std::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
220 {
221   return ModelAPI_Session::get()->moduleDocument();
222 }
223
224 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
225                                     const std::string& theAttribute)
226 {
227   if (!theFeature)
228     return;
229   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
230   AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
231       aData->attribute(theAttribute));
232   if (anAttribute)
233     anAttribute->setValue(theValue);
234 }
235
236 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
237                                    bool& isValid)
238 {
239   isValid = false;
240   double aValue = 0;
241   if (theFeature) {
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       aValue = anAttribute->value();
247       isValid = true;
248     }
249   }
250   return aValue;
251 }
252
253 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
254                                   const std::string& theKind)
255 {
256   FeaturePtr aFeature;
257   if (!theFeature)
258     return aFeature;
259
260   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
261   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
262       ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
263   if (anAttr) {
264     aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
265     if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
266       aFeature = FeaturePtr();
267     }
268   }
269   return aFeature;
270 }
271
272 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
273                                      std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
274                                      std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
275 {
276   FeaturePtr aFeature;
277   if (theSketch) {
278     aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
279   } else {
280     std::shared_ptr<ModelAPI_Document> aDoc = document();
281     aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
282   }
283
284   std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
285
286   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
287       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
288   aRef1->setAttr(thePoint1);
289
290   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
291       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
292   aRef2->setAttr(thePoint2);
293
294   // we need to flush created signal in order to coincidence is processed by solver
295   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
296 }
297
298 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
299 {
300   std::shared_ptr<GeomAPI_Pln> aPlane;
301
302   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
303       theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
304   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
305       theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
306
307   if (aNormal.get() && aNormal->isInitialized() &&
308       anOrigin.get() && anOrigin->isInitialized())
309     aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNormal->dir()));
310
311   return aPlane;
312 }
313
314 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
315                                                       CompositeFeaturePtr theSketch)
316 {
317   std::shared_ptr<GeomAPI_Pnt> aPoint;
318   if (!theSketch || !thePoint2D)
319     return aPoint;
320
321   DataPtr aData = theSketch->data();
322   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
323       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
324   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
325       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
326   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
327       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
328   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
329
330   return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
331 }
332
333 ResultPtr PartSet_Tools::findFixedObjectByExternal(const TopoDS_Shape& theShape,
334                                                    const ObjectPtr& theObject,
335                                                    CompositeFeaturePtr theSketch)
336 {
337   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
338   if (!aResult.get())
339     return ResultPtr();
340
341   for (int i = 0, aNbSubs = theSketch->numberOfSubs(); i < aNbSubs; i++) {
342     FeaturePtr aFeature = theSketch->subFeature(i);
343     if (aFeature->getKind() != SketchPlugin_Projection::PROJECTED_FEATURE_ID())
344       continue;
345     if (aFeature->lastResult() == aResult)
346       return aResult;
347   }
348   return ResultPtr();
349 }
350
351 ResultPtr PartSet_Tools::createFixedObjectByExternal(
352                                    const std::shared_ptr<GeomAPI_Shape>& theShape,
353                                    const ObjectPtr& theObject,
354                                    CompositeFeaturePtr theSketch,
355                                    const bool theTemporary,
356                                    FeaturePtr& theCreatedFeature)
357 {
358   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
359   if (!aResult.get())
360     return ResultPtr();
361
362   FeaturePtr aProjectionFeature = theSketch->addFeature(SketchPlugin_Projection::ID());
363   theCreatedFeature = aProjectionFeature;
364   AttributeSelectionPtr anExternalAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
365                  aProjectionFeature->attribute(SketchPlugin_Projection::EXTERNAL_FEATURE_ID()));
366   anExternalAttr->setValue(aResult, theShape);
367
368   AttributeBooleanPtr anIntoResult = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>
369     (aProjectionFeature->data()->attribute(SketchPlugin_Projection::INCLUDE_INTO_RESULT()));
370   anIntoResult->setValue(SKETCH_PROJECTION_INCLUDE_INTO_RESULT);
371   aProjectionFeature->execute();
372
373   // if projection feature has not been created, exit
374   AttributeRefAttrPtr aRefAttr = aProjectionFeature->data()->refattr(
375     SketchPlugin_Projection::PROJECTED_FEATURE_ID());
376   if (!aRefAttr || !aRefAttr->isInitialized())
377   {
378     // remove external feature if the attribute is not filled
379     std::set<FeaturePtr> aFeatures;
380     aFeatures.insert(aProjectionFeature);
381     ModelAPI_Tools::removeFeaturesAndReferences(aFeatures);
382     return ResultPtr();
383   }
384
385   FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
386   if (aProjection.get() && aProjection->lastResult().get())
387     return aProjection->lastResult();
388
389   return ResultPtr();
390 }
391
392 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrsPtr>& theSelected,
393                                           const ModuleBase_ViewerPrsPtr& thePrs)
394 {
395   foreach (ModuleBase_ViewerPrsPtr aPrs, theSelected) {
396     if (aPrs->object() == thePrs->object())
397       return true;
398   }
399   return false;
400 }
401
402 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
403                                                        ModuleBase_IWorkshop* theWorkshop)
404 {
405   GeomShapePtr aShape;
406   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
407   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
408
409   // 2. find visualized vertices of the attribute and if the attribute of the vertex is
410   // the same, return it
411   FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
412   // 2.1 get visualized results of the feature
413   const std::list<ResultPtr>& aResList = anAttributeFeature->results();
414   std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
415   for (; anIt != aLast; anIt++) {
416     AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
417     if (aAISObj.get() != NULL) {
418       Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
419       // 2.2 find selected owners of a visualizedd object
420       SelectMgr_IndexedMapOfOwner aSelectedOwners;
421       aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
422       for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
423         Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
424         if (!anOwner.IsNull()) {
425           Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
426           if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
427             const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
428             if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
429               // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
430               // attribute, returns the shape
431               PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
432               PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
433               AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
434                                                         aBRepShape, aSketchMgr->activeSketch());
435               if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
436                 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
437                 aShape->setImpl(new TopoDS_Shape(aBRepShape));
438                 break;
439               }
440             }
441           }
442         }
443       }
444     }
445   }
446   return aShape;
447 }
448
449 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(
450                                                   std::shared_ptr<ModelAPI_Feature>& theFeature,
451                                                   const std::string& theAttribute)
452 {
453   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr(
454                                           theFeature.get(), theAttribute, SketchPlugin_Point::ID(),
455                                           SketchPlugin_Point::COORD_ID());
456   if (aPointAttr.get() != NULL)
457     return aPointAttr->pnt();
458   return std::shared_ptr<GeomAPI_Pnt2d>();
459 }
460
461 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPnt2d(QMouseEvent* theEvent,
462                                                 ModuleBase_IViewWindow* theWindow,
463                                                 const FeaturePtr& theSketch)
464 {
465   gp_Pnt aPnt = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
466   double aX, anY;
467   Handle(V3d_View) aView = theWindow->v3dView();
468   PartSet_Tools::convertTo2D(aPnt, theSketch, aView, aX, anY);
469
470   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY));
471 }
472
473 FeaturePtr findFirstCoincidenceByData(const DataPtr& theData,
474                                       std::shared_ptr<GeomAPI_Pnt2d> thePoint)
475 {
476   FeaturePtr aCoincident;
477
478   const std::set<AttributePtr>& aRefsList = theData->refsToMe();
479   std::set<AttributePtr>::const_iterator aIt;
480   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
481     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
482     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
483     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
484       std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
485         PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
486       if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
487         aCoincident = aConstrFeature;
488         break;
489       } else {
490         a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
491                                           SketchPlugin_ConstraintCoincidence::ENTITY_B());
492         if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
493           aCoincident = aConstrFeature;
494           break;
495         }
496       }
497     }
498   }
499   return aCoincident;
500 }
501
502 FeaturePtr PartSet_Tools::findFirstCoincidence(const FeaturePtr& theFeature,
503                                                std::shared_ptr<GeomAPI_Pnt2d> thePoint)
504 {
505   FeaturePtr aCoincident;
506   if (theFeature.get() == NULL)
507     return aCoincident;
508
509   const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
510   std::set<AttributePtr>::const_iterator aIt;
511   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
512     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
513     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
514     if (aConstrFeature && aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
515       std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
516         PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
517       if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
518         aCoincident = aConstrFeature;
519         break;
520       } else {
521         a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
522                                           SketchPlugin_ConstraintCoincidence::ENTITY_B());
523         if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
524           aCoincident = aConstrFeature;
525           break;
526         }
527       }
528     }
529   }
530   /// Find by result
531   if (!aCoincident.get()) {
532     std::list<ResultPtr> aResults = theFeature->results();
533     std::list<ResultPtr>::const_iterator aIt;
534     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
535       ResultPtr aResult = *aIt;
536       aCoincident = findFirstCoincidenceByData(aResult->data(), thePoint);
537       if (aCoincident.get())
538         break;
539     }
540   }
541   return aCoincident;
542 }
543
544 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
545                                      QList<FeaturePtr>& theCoincidencies,
546                                      std::string theAttr, QList<bool>& theIsAttributes)
547 {
548   std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincedencePoint(theStartCoin);
549   if (aOrig.get() == NULL)
550     return;
551
552   AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
553   if (!aPnt)
554     return;
555   ObjectPtr aObj = aPnt->object();
556   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
557   if (aFeature.get()) {
558     if (!theList.contains(aFeature)) {
559       theList.append(aFeature);
560       theCoincidencies.append(theStartCoin);
561       theIsAttributes.append(true); // point attribute on a feature
562       const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
563       std::set<AttributePtr>::const_iterator aIt;
564       for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
565         std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
566         FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
567         if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
568           if (!theCoincidencies.contains(aConstrFeature)) {
569             std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
570             if (aPnt.get() && aOrig->isEqual(aPnt)) {
571               findCoincidences(aConstrFeature, theList, theCoincidencies,
572                 SketchPlugin_ConstraintCoincidence::ENTITY_A(), theIsAttributes);
573               findCoincidences(aConstrFeature, theList, theCoincidencies,
574                 SketchPlugin_ConstraintCoincidence::ENTITY_B(), theIsAttributes);
575             }
576           }
577         }
578       }
579     }
580   } else {
581     // Find by Results
582     ResultConstructionPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
583     if (aResult.get()) {
584       FeaturePtr aFeature = ModelAPI_Feature::feature(aPnt->object());
585       if (!theList.contains(aFeature))
586         theList.append(aFeature);
587       theCoincidencies.append(theStartCoin);
588       theIsAttributes.append(false); // point attribute on a feature
589
590       const std::set<AttributePtr>& aRefsList = aResult->data()->refsToMe();
591       std::set<AttributePtr>::const_iterator aIt;
592       for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
593         std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
594         FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
595         if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
596           if (!theCoincidencies.contains(aConstrFeature)) {
597             std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
598             if (aPnt.get() && aOrig->isEqual(aPnt)) {
599               findCoincidences(aConstrFeature, theList, theCoincidencies,
600                 SketchPlugin_ConstraintCoincidence::ENTITY_A(), theIsAttributes);
601               findCoincidences(aConstrFeature, theList, theCoincidencies,
602                 SketchPlugin_ConstraintCoincidence::ENTITY_B(), theIsAttributes);
603             }
604           }
605         }
606       }
607     }
608   }
609 }
610
611 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getCoincedencePoint(FeaturePtr theStartCoin)
612 {
613   std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
614                                                         SketchPlugin_Constraint::ENTITY_A());
615   if (aPnt.get() == NULL)
616     aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
617   return aPnt;
618 }
619
620 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
621                                                    const TopoDS_Shape theShape,
622                                                    FeaturePtr theSketch)
623 {
624
625   AttributePtr anAttribute;
626   FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
627   if (aFeature) {
628     if (theShape.ShapeType() == TopAbs_VERTEX) {
629       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
630       if (!aVertex.IsNull())  {
631         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
632         std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
633             new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
634
635         // find the given point in the feature attributes
636         std::list<AttributePtr> anAttiributes =
637           aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
638         std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(),
639                                                 aLast = anAttiributes.end();
640         for (; anIt != aLast && !anAttribute; anIt++) {
641           std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
642             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
643           if (!aCurPoint->isInitialized())
644             continue;
645
646           std::shared_ptr<GeomAPI_Pnt> aPnt =
647             convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
648           if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
649             anAttribute = aCurPoint;
650             break;
651           }
652         }
653       }
654     }
655   }
656   return anAttribute;
657 }
658
659 void PartSet_Tools::sendSubFeaturesEvent(const CompositeFeaturePtr& theComposite,
660                                          const Events_ID theEventId)
661 {
662   if (!theComposite.get())
663     return;
664
665   static Events_Loop* aLoop = Events_Loop::loop();
666   for (int i = 0; i < theComposite->numberOfSubs(); i++) {
667     FeaturePtr aSubFeature = theComposite->subFeature(i);
668     static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
669     aECreator->sendUpdated(aSubFeature, theEventId);
670   }
671   Events_Loop::loop()->flush(theEventId);
672 }
673
674 bool PartSet_Tools::isAuxiliarySketchEntity(const ObjectPtr& theObject)
675 {
676   bool isAuxiliaryFeature = false;
677
678   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
679   std::string anAuxiliaryAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
680   AttributeBooleanPtr anAuxiliaryAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
681                                     anObjectFeature->data()->attribute(anAuxiliaryAttribute));
682   if (anAuxiliaryAttr.get())
683     isAuxiliaryFeature = anAuxiliaryAttr->value();
684
685
686   return isAuxiliaryFeature;
687 }
688
689
690 ResultPtr PartSet_Tools::createFixedByExternalCenter(
691     const ObjectPtr& theObject,
692     const std::shared_ptr<GeomAPI_Edge>& theEdge,
693     ModelAPI_AttributeSelection::CenterType theType,
694     const CompositeFeaturePtr& theSketch,
695     bool theTemporary,
696     FeaturePtr& theCreatedFeature)
697 {
698   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
699   if (!aResult.get())
700     return ResultPtr();
701
702   FeaturePtr aProjectionFeature = theSketch->addFeature(SketchPlugin_Projection::ID());
703   theCreatedFeature = aProjectionFeature;
704   AttributeSelectionPtr anExternalAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
705                  aProjectionFeature->attribute(SketchPlugin_Projection::EXTERNAL_FEATURE_ID()));
706   anExternalAttr->setValueCenter(aResult, theEdge, theType, theTemporary);
707
708   AttributeBooleanPtr anIntoResult = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>
709     (aProjectionFeature->data()->attribute(SketchPlugin_Projection::INCLUDE_INTO_RESULT()));
710   anIntoResult->setValue(SKETCH_PROJECTION_INCLUDE_INTO_RESULT);
711   aProjectionFeature->execute();
712
713   // if projection feature has not been created, exit
714   AttributeRefAttrPtr aRefAttr = aProjectionFeature->data()->refattr(
715     SketchPlugin_Projection::PROJECTED_FEATURE_ID());
716   if (!aRefAttr || !aRefAttr->isInitialized())
717     return ResultPtr();
718
719   FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
720   if (aProjection.get() && aProjection->lastResult().get())
721     return aProjection->lastResult();
722
723   return ResultPtr();
724 }