]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Tools.cpp
Salome HOME
An attempt of porting to current OCCT.
[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     return ResultPtr();
378
379   FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
380   if (aProjection.get() && aProjection->lastResult().get())
381     return aProjection->lastResult();
382
383   return ResultPtr();
384 }
385
386 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrsPtr>& theSelected,
387                                           const ModuleBase_ViewerPrsPtr& thePrs)
388 {
389   foreach (ModuleBase_ViewerPrsPtr aPrs, theSelected) {
390     if (aPrs->object() == thePrs->object())
391       return true;
392   }
393   return false;
394 }
395
396 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
397                                                        ModuleBase_IWorkshop* theWorkshop)
398 {
399   GeomShapePtr aShape;
400   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
401   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
402
403   // 2. find visualized vertices of the attribute and if the attribute of the vertex is
404   // the same, return it
405   FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
406   // 2.1 get visualized results of the feature
407   const std::list<ResultPtr>& aResList = anAttributeFeature->results();
408   std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
409   for (; anIt != aLast; anIt++) {
410     AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
411     if (aAISObj.get() != NULL) {
412       Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
413       // 2.2 find selected owners of a visualizedd object
414       SelectMgr_IndexedMapOfOwner aSelectedOwners;
415       aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
416       for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
417         Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
418         if (!anOwner.IsNull()) {
419           Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
420           if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
421             const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
422             if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
423               // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
424               // attribute, returns the shape
425               PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
426               PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
427               AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
428                                                         aBRepShape, aSketchMgr->activeSketch());
429               if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
430                 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
431                 aShape->setImpl(new TopoDS_Shape(aBRepShape));
432                 break;
433               }
434             }
435           }
436         }
437       }
438     }
439   }
440   return aShape;
441 }
442
443 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(
444                                                   std::shared_ptr<ModelAPI_Feature>& theFeature,
445                                                   const std::string& theAttribute)
446 {
447   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = ModelGeomAlgo_Point2D::getPointOfRefAttr(
448                                           theFeature.get(), theAttribute, SketchPlugin_Point::ID(),
449                                           SketchPlugin_Point::COORD_ID());
450   if (aPointAttr.get() != NULL)
451     return aPointAttr->pnt();
452   return std::shared_ptr<GeomAPI_Pnt2d>();
453 }
454
455 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPnt2d(QMouseEvent* theEvent,
456                                                 ModuleBase_IViewWindow* theWindow,
457                                                 const FeaturePtr& theSketch)
458 {
459   gp_Pnt aPnt = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWindow->v3dView());
460   double aX, anY;
461   Handle(V3d_View) aView = theWindow->v3dView();
462   PartSet_Tools::convertTo2D(aPnt, theSketch, aView, aX, anY);
463
464   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY));
465 }
466
467 FeaturePtr findFirstCoincidenceByData(const DataPtr& theData,
468                                       std::shared_ptr<GeomAPI_Pnt2d> thePoint)
469 {
470   FeaturePtr aCoincident;
471
472   const std::set<AttributePtr>& aRefsList = theData->refsToMe();
473   std::set<AttributePtr>::const_iterator aIt;
474   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
475     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
476     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
477     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
478       std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
479         PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
480       if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
481         aCoincident = aConstrFeature;
482         break;
483       } else {
484         a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
485                                           SketchPlugin_ConstraintCoincidence::ENTITY_B());
486         if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
487           aCoincident = aConstrFeature;
488           break;
489         }
490       }
491     }
492   }
493   return aCoincident;
494 }
495
496 FeaturePtr PartSet_Tools::findFirstCoincidence(const FeaturePtr& theFeature,
497                                                std::shared_ptr<GeomAPI_Pnt2d> thePoint)
498 {
499   FeaturePtr aCoincident;
500   if (theFeature.get() == NULL)
501     return aCoincident;
502
503   const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
504   std::set<AttributePtr>::const_iterator aIt;
505   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
506     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
507     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
508     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
509       std::shared_ptr<GeomAPI_Pnt2d> a2dPnt =
510         PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
511       if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
512         aCoincident = aConstrFeature;
513         break;
514       } else {
515         a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
516                                           SketchPlugin_ConstraintCoincidence::ENTITY_B());
517         if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) {
518           aCoincident = aConstrFeature;
519           break;
520         }
521       }
522     }
523   }
524   /// Find by result
525   if (!aCoincident.get()) {
526     std::list<ResultPtr> aResults = theFeature->results();
527     std::list<ResultPtr>::const_iterator aIt;
528     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
529       ResultPtr aResult = *aIt;
530       aCoincident = findFirstCoincidenceByData(aResult->data(), thePoint);
531       if (aCoincident.get())
532         break;
533     }
534   }
535   return aCoincident;
536 }
537
538 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
539                                      QList<FeaturePtr>& theCoincidencies,
540                                      std::string theAttr, QList<bool>& theIsAttributes)
541 {
542   std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincedencePoint(theStartCoin);
543   if (aOrig.get() == NULL)
544     return;
545
546   AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
547   if (!aPnt)
548     return;
549   ObjectPtr aObj = aPnt->object();
550   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
551   if (aFeature.get()) {
552     if (!theList.contains(aFeature)) {
553       theList.append(aFeature);
554       theCoincidencies.append(theStartCoin);
555       theIsAttributes.append(true); // point attribute on a feature
556       const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
557       std::set<AttributePtr>::const_iterator aIt;
558       for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
559         std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
560         FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
561         if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
562           if (!theCoincidencies.contains(aConstrFeature)) {
563             std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
564             if (aPnt.get() && aOrig->isEqual(aPnt)) {
565               findCoincidences(aConstrFeature, theList, theCoincidencies,
566                 SketchPlugin_ConstraintCoincidence::ENTITY_A(), theIsAttributes);
567               findCoincidences(aConstrFeature, theList, theCoincidencies,
568                 SketchPlugin_ConstraintCoincidence::ENTITY_B(), theIsAttributes);
569             }
570           }
571         }
572       }
573     }
574   } else {
575     // Find by Results
576     ResultConstructionPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
577     if (aResult.get()) {
578       FeaturePtr aFeature = ModelAPI_Feature::feature(aPnt->object());
579       if (!theList.contains(aFeature))
580         theList.append(aFeature);
581       theCoincidencies.append(theStartCoin);
582       theIsAttributes.append(false); // point attribute on a feature
583
584       const std::set<AttributePtr>& aRefsList = aResult->data()->refsToMe();
585       std::set<AttributePtr>::const_iterator aIt;
586       for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
587         std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
588         FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
589         if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
590           if (!theCoincidencies.contains(aConstrFeature)) {
591             std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
592             if (aPnt.get() && aOrig->isEqual(aPnt)) {
593               findCoincidences(aConstrFeature, theList, theCoincidencies,
594                 SketchPlugin_ConstraintCoincidence::ENTITY_A(), theIsAttributes);
595               findCoincidences(aConstrFeature, theList, theCoincidencies,
596                 SketchPlugin_ConstraintCoincidence::ENTITY_B(), theIsAttributes);
597             }
598           }
599         }
600       }
601     }
602   }
603 }
604
605 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getCoincedencePoint(FeaturePtr theStartCoin)
606 {
607   std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
608                                                         SketchPlugin_Constraint::ENTITY_A());
609   if (aPnt.get() == NULL)
610     aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
611   return aPnt;
612 }
613
614 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj,
615                                                    const TopoDS_Shape theShape,
616                                                    FeaturePtr theSketch)
617 {
618
619   AttributePtr anAttribute;
620   FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
621   if (aFeature) {
622     if (theShape.ShapeType() == TopAbs_VERTEX) {
623       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
624       if (!aVertex.IsNull())  {
625         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
626         std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
627             new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
628
629         // find the given point in the feature attributes
630         std::list<AttributePtr> anAttiributes =
631           aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
632         std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(),
633                                                 aLast = anAttiributes.end();
634         for (; anIt != aLast && !anAttribute; anIt++) {
635           std::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
636             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
637           if (!aCurPoint->isInitialized())
638             continue;
639
640           std::shared_ptr<GeomAPI_Pnt> aPnt =
641             convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
642           if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
643             anAttribute = aCurPoint;
644             break;
645           }
646         }
647       }
648     }
649   }
650   return anAttribute;
651 }
652
653 void PartSet_Tools::sendSubFeaturesEvent(const CompositeFeaturePtr& theComposite,
654                                          const Events_ID theEventId)
655 {
656   if (!theComposite.get())
657     return;
658
659   static Events_Loop* aLoop = Events_Loop::loop();
660   for (int i = 0; i < theComposite->numberOfSubs(); i++) {
661     FeaturePtr aSubFeature = theComposite->subFeature(i);
662     static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
663     aECreator->sendUpdated(aSubFeature, theEventId);
664   }
665   Events_Loop::loop()->flush(theEventId);
666 }
667
668 bool PartSet_Tools::isAuxiliarySketchEntity(const ObjectPtr& theObject)
669 {
670   bool isAuxiliaryFeature = false;
671
672   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
673   std::string anAuxiliaryAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
674   AttributeBooleanPtr anAuxiliaryAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
675                                     anObjectFeature->data()->attribute(anAuxiliaryAttribute));
676   if (anAuxiliaryAttr.get())
677     isAuxiliaryFeature = anAuxiliaryAttr->value();
678
679
680   return isAuxiliaryFeature;
681 }
682
683
684 ResultPtr PartSet_Tools::createFixedByExternalCenter(
685     const ObjectPtr& theObject,
686     const std::shared_ptr<GeomAPI_Edge>& theEdge,
687     ModelAPI_AttributeSelection::CenterType theType,
688     const CompositeFeaturePtr& theSketch,
689     bool theTemporary)
690 {
691   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
692   if (!aResult.get())
693     return ResultPtr();
694
695   FeaturePtr aProjectionFeature = theSketch->addFeature(SketchPlugin_Projection::ID());
696   AttributeSelectionPtr anExternalAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
697                  aProjectionFeature->attribute(SketchPlugin_Projection::EXTERNAL_FEATURE_ID()));
698   anExternalAttr->setValueCenter(aResult, theEdge, theType, theTemporary);
699
700   AttributeBooleanPtr anIntoResult = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>
701     (aProjectionFeature->data()->attribute(SketchPlugin_Projection::INCLUDE_INTO_RESULT()));
702   anIntoResult->setValue(SKETCH_PROJECTION_INCLUDE_INTO_RESULT);
703   aProjectionFeature->execute();
704
705   // if projection feature has not been created, exit
706   AttributeRefAttrPtr aRefAttr = aProjectionFeature->data()->refattr(
707     SketchPlugin_Projection::PROJECTED_FEATURE_ID());
708   if (!aRefAttr || !aRefAttr->isInitialized())
709     return ResultPtr();
710
711   FeaturePtr aProjection = ModelAPI_Feature::feature(aRefAttr->object());
712   if (aProjection.get() && aProjection->lastResult().get())
713     return aProjection->lastResult();
714
715   return ResultPtr();
716 }