Salome HOME
1. Correction for perfomance problem by Apply button state update: do not listen...
[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 #include <ModelAPI_Events.h>
18
19 #include <Events_Loop.h>
20
21 #include <SketcherPrs_Tools.h>
22
23 #include <XGUI_ModuleConnector.h>
24 #include <XGUI_Displayer.h>
25 #include <XGUI_Workshop.h>
26 #include <XGUI_SelectionMgr.h>
27 #include <XGUI_Selection.h>
28
29 #include <GeomDataAPI_Point.h>
30 #include <GeomDataAPI_Dir.h>
31 #include <GeomDataAPI_Point2D.h>
32 #include <GeomAPI_Pln.h>
33 #include <GeomAPI_Pnt2d.h>
34 #include <GeomAPI_Pnt.h>
35 #include <GeomAPI_Edge.h>
36 #include <GeomAPI_Vertex.h>
37
38 #include <GeomAPI_Dir.h>
39 #include <GeomAPI_XYZ.h>
40
41 #include <SketchPlugin_Feature.h>
42 #include <SketchPlugin_Sketch.h>
43 #include <SketchPlugin_ConstraintCoincidence.h>
44 #include <SketchPlugin_ConstraintDistance.h>
45 #include <SketchPlugin_ConstraintLength.h>
46 #include <SketchPlugin_ConstraintRadius.h>
47 #include <SketchPlugin_ConstraintRigid.h>
48 #include <SketchPlugin_Constraint.h>
49 #include <SketchPlugin_Circle.h>
50 #include <SketchPlugin_Arc.h>
51 #include <SketchPlugin_Line.h>
52 #include <SketchPlugin_Point.h>
53
54 #include <ModuleBase_IWorkshop.h>
55 #include <ModuleBase_ViewerPrs.h>
56 #include <ModuleBase_Tools.h>
57
58 #include <V3d_View.hxx>
59 #include <gp_Pln.hxx>
60 #include <gp_Circ.hxx>
61 #include <ProjLib.hxx>
62 #include <ElSLib.hxx>
63 #include <Geom_Line.hxx>
64 #include <GeomAPI_ProjectPointOnCurve.hxx>
65 #include <BRep_Tool.hxx>
66 #include <TopoDS.hxx>
67 #include <TopoDS_Edge.hxx>
68 #include <TopoDS_Vertex.hxx>
69 #include <AIS_InteractiveObject.hxx>
70 #include <StdSelect_BRepOwner.hxx>
71 #include <SelectMgr_IndexedMapOfOwner.hxx>
72
73 #ifdef _DEBUG
74 #include <QDebug>
75 #endif
76
77 const double PRECISION_TOLERANCE = 0.000001;
78 const int AIS_DEFAULT_WIDTH = 2;
79
80 int PartSet_Tools::getAISDefaultWidth()
81 {
82   return AIS_DEFAULT_WIDTH;
83 }
84
85 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
86 {
87   if (theView.IsNull())
88     return gp_Pnt();
89
90   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
91   theView->Eye(XEye, YEye, ZEye);
92
93   theView->At(XAt, YAt, ZAt);
94   gp_Pnt EyePoint(XEye, YEye, ZEye);
95   gp_Pnt AtPoint(XAt, YAt, ZAt);
96
97   gp_Vec EyeVector(EyePoint, AtPoint);
98   gp_Dir EyeDir(EyeVector);
99
100   gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
101   Standard_Real X, Y, Z;
102   theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
103   gp_Pnt ConvertedPoint(X, Y, Z);
104
105   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
106   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
107                                      PlaneOfTheView);
108   return ResultPoint;
109 }
110
111 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
112 Handle(V3d_View) theView,
113                                 double& theX, double& theY)
114 {
115   if (!theSketch)
116     return;
117
118   AttributeDoublePtr anAttr;
119   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
120
121   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
122       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
123
124   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
125       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
126   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
127       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
128   std::shared_ptr<GeomAPI_XYZ> anY = aNorm->xyz()->cross(aX->xyz());
129
130   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
131   gp_Vec aVec(anOriginPnt, thePoint);
132
133   if (!theView.IsNull()) {
134     V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
135     theView->Eye(XEye, YEye, ZEye);
136
137     theView->At(XAt, YAt, ZAt);
138     gp_Pnt EyePoint(XEye, YEye, ZEye);
139     gp_Pnt AtPoint(XAt, YAt, ZAt);
140
141     gp_Vec anEyeVec(EyePoint, AtPoint);
142     anEyeVec.Normalize();
143
144     std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
145         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
146     gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
147
148     double aDen = anEyeVec * aNormalVec;
149     double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
150
151     gp_Vec aDeltaVec = anEyeVec * aLVec;
152     aVec = aVec - aDeltaVec;
153   }
154   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
155   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
156 }
157
158 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::convertTo2D(FeaturePtr theSketch, 
159                                                     const std::shared_ptr<GeomAPI_Pnt>& thePnt)
160 {
161   std::shared_ptr<GeomAPI_Pnt2d> aRes;
162   if (theSketch->getKind() != SketchPlugin_Sketch::ID())
163     return aRes;
164   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
165       theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
166   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
167       theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
168   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
169       theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
170   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
171   return thePnt->to2D(aC->pnt(), aX->dir(), aY);
172 }
173
174
175 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch)
176 {
177   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
178
179   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
180       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
181   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
182       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
183   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
184       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
185   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
186
187   std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = 
188     std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
189
190   return aPnt2d->to3D(aC->pnt(), aX->dir(), aY);
191 }
192
193 std::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
194 {
195   return ModelAPI_Session::get()->moduleDocument();
196 }
197
198 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
199                                     const std::string& theAttribute)
200 {
201   if (!theFeature)
202     return;
203   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
204   AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
205       aData->attribute(theAttribute));
206   if (anAttribute)
207     anAttribute->setValue(theValue);
208 }
209
210 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
211                                    bool& isValid)
212 {
213   isValid = false;
214   double aValue = 0;
215   if (theFeature) {
216     std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
217     AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
218         aData->attribute(theAttribute));
219     if (anAttribute) {
220       aValue = anAttribute->value();
221       isValid = true;
222     }
223   }
224   return aValue;
225 }
226
227 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
228                                   const std::string& theKind)
229 {
230   FeaturePtr aFeature;
231   if (!theFeature)
232     return aFeature;
233
234   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
235   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
236       ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
237   if (anAttr) {
238     aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
239     if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
240       aFeature = FeaturePtr();
241     }
242   }
243   return aFeature;
244 }
245
246 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
247                                      std::shared_ptr<GeomDataAPI_Point2D> thePoint1,
248                                      std::shared_ptr<GeomDataAPI_Point2D> thePoint2)
249 {
250   FeaturePtr aFeature;
251   if (theSketch) {
252     aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
253   } else {
254     std::shared_ptr<ModelAPI_Document> aDoc = document();
255     aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
256   }
257
258   std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
259
260   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
261       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
262   aRef1->setAttr(thePoint1);
263
264   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
265       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
266   aRef2->setAttr(thePoint2);
267
268   // we need to flush created signal in order to coincidence is processed by solver
269   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
270 }
271
272 /*std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::
273   findAttributePoint(CompositeFeaturePtr theSketch, double theX, double theY,
274   double theTolerance, const QList<FeaturePtr>& theIgnore)
275 {
276   std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
277       new GeomAPI_Pnt2d(theX, theY));
278
279   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
280   for (int i = 0; i < theSketch->numberOfSubs(); i++) {
281     FeaturePtr aFeature = theSketch->subFeature(i);
282     if (!theIgnore.contains(aFeature)) {
283       anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
284
285       std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt;
286       for (anIt = anAttiributes.cbegin(); anIt != anAttiributes.cend(); ++anIt) {
287         std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
288           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
289         double x = aCurPoint->x();
290         double y = aCurPoint->y();
291         if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < theTolerance)) {
292           return aCurPoint;
293         }
294       }
295     }
296   }
297   return std::shared_ptr<GeomDataAPI_Point2D>();
298 }*/
299
300
301 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findFirstEqualPointInArgumentFeatures(
302                   const FeaturePtr& theFeature, const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
303 {
304   std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
305
306   // may be feature is not updated yet, execute is not performed and references features
307   // are not created. Case: rectangle macro feature
308   ModuleBase_Tools::flushUpdated(theFeature);
309
310   std::list<AttributePtr> anAttributes = theFeature->data()->attributes(
311                                           ModelAPI_AttributeRefList::typeId());
312   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
313   for (; anIt != aLast && !aFeaturePoint.get(); anIt++) {
314     std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
315                                       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
316     for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFeaturePoint.get(); i++) {
317       ObjectPtr anObject = aCurSelList->object(i);
318       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
319       if (aFeature.get())
320         aFeaturePoint = PartSet_Tools::findFirstEqualPoint(aFeature, thePoint);
321     }
322   }
323   return aFeaturePoint;
324 }
325
326 std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findFirstEqualPoint(const FeaturePtr& theFeature,
327                                                       const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
328 {
329   std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
330
331   // find the given point in the feature attributes
332   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes =
333                                     theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
334   std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
335       aLast = anAttiributes.end();
336   for (; anIt != aLast && !aFPoint; anIt++) {
337     std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
338       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
339     if (aCurPoint && (aCurPoint->pnt()->distance(thePoint) < Precision::Confusion())) {
340       aFPoint = aCurPoint;
341       break;
342     }
343   }
344   return aFPoint;
345 }
346
347 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
348                                    const std::string& theAttribute, double theClickedX,
349                                    double theClickedY)
350 {
351   if (!theFeature.get())
352     return;
353
354   std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
355       new GeomAPI_Pnt2d(theClickedX, theClickedY));
356
357   // find a feature point by the selection mode
358   std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
359   if (theFeature->isMacro()) {
360     // the macro feature will be removed after the operation is stopped, so we need to build
361     // coicidence to possible sub-features
362     aFeaturePoint = PartSet_Tools::findFirstEqualPointInArgumentFeatures(theFeature, aClickedPoint);
363   }
364   else {
365     aFeaturePoint = std::dynamic_pointer_cast<
366         GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
367   }
368   if (!aFeaturePoint)
369     return;
370
371   // get all sketch features. If the point with the given coordinates belong to any sketch feature,
372   // the constraint is created between the feature point and the found sketch point
373   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
374   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
375       ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
376
377   std::list<ObjectPtr> aFeatures = aRefList->list();
378   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
379   std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
380   for (; anIt != aLast; anIt++) {
381     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
382     if (!aFeature.get() || theFeature == aFeature)
383       continue;
384     std::shared_ptr<GeomDataAPI_Point2D> aFPoint = PartSet_Tools::findFirstEqualPoint(aFeature,
385                                                                                 aClickedPoint);
386     if (aFPoint)
387       PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
388   }
389 }
390
391 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
392 {
393   std::shared_ptr<GeomAPI_Pln> aPlane;
394
395   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
396   if (aData) {
397     std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
398         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
399     std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
400         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
401     if (aNormal.get() && aNormal->isInitialized() &&
402         anOrigin.get() && anOrigin->isInitialized()) {
403       double adX = aNormal->x();
404       double adY = aNormal->y();
405       double adZ = aNormal->z();
406
407       if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
408         double aX = anOrigin->x();
409         double aY = anOrigin->y();
410         double aZ = anOrigin->z();
411         gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
412         double aA, aB, aC, aD;
413         aPln.Coefficients(aA, aB, aC, aD);
414         aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
415       }
416     }
417   }
418   return aPlane;
419 }
420
421 std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
422                                                       CompositeFeaturePtr theSketch)
423 {
424   std::shared_ptr<GeomAPI_Pnt> aPoint;
425   if (!theSketch || !thePoint2D)
426     return aPoint;
427
428   DataPtr aData = theSketch->data();
429   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
430       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
431   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
432       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
433   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
434       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
435   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
436
437   return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
438 }
439
440 ResultPtr PartSet_Tools::findFixedObjectByExternal(const TopoDS_Shape& theShape,
441                                                    const ObjectPtr& theObject,
442                                                    CompositeFeaturePtr theSketch)
443 {
444   ResultPtr aResult;
445   if (theShape.ShapeType() == TopAbs_EDGE) {
446     // Check that we already have such external edge
447     std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
448     aInEdge->setImpl(new TopoDS_Shape(theShape));
449     aResult = findExternalEdge(theSketch, aInEdge);
450   }
451   if (theShape.ShapeType() == TopAbs_VERTEX) {
452     std::shared_ptr<GeomAPI_Vertex> aInVert = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex());
453     aInVert->setImpl(new TopoDS_Shape(theShape));
454     aResult = findExternalVertex(theSketch, aInVert);
455   }
456   return aResult;
457 }
458
459 ResultPtr PartSet_Tools::createFixedObjectByExternal(const TopoDS_Shape& theShape, 
460                                                      const ObjectPtr& theObject, 
461                                                      CompositeFeaturePtr theSketch,
462                                                      const bool theTemporary)
463 {
464   if (theShape.ShapeType() == TopAbs_EDGE) {
465     Standard_Real aStart, aEnd;
466     Handle(V3d_View) aNullView;
467     FeaturePtr aMyFeature;
468
469     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
470     GeomAdaptor_Curve aAdaptor(aCurve);
471     std::shared_ptr<GeomAPI_Edge> anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
472     anEdge->setImpl(new TopoDS_Shape(theShape));
473     if (aAdaptor.GetType() == GeomAbs_Line) {
474       // Create line
475       aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
476       if (!theObject.get()) {
477         // There is no selected result
478         std::shared_ptr<GeomAPI_Pnt> aPnt1 = anEdge->firstPoint();
479         std::shared_ptr<GeomAPI_Pnt> aPnt2 = anEdge->lastPoint();
480         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d1 = convertTo2D(theSketch, aPnt1);
481         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d2 = convertTo2D(theSketch, aPnt2);
482
483         std::shared_ptr<ModelAPI_Data> aData = aMyFeature->data();
484         std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = 
485           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
486         std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = 
487           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
488
489         aPoint1->setValue(aPnt2d1);
490         aPoint2->setValue(aPnt2d2);
491
492         // If this is an axis then its name has to be changed correspondently
493         std::string aSuffix = "";
494         bool aXdir = fabs(aPnt1->x() - aPnt2->x()) > Precision::Confusion();
495         bool aYdir = fabs(aPnt1->y() - aPnt2->y()) > Precision::Confusion();
496         bool aZdir = fabs(aPnt1->z() - aPnt2->z()) > Precision::Confusion();
497         if (aXdir && (!aYdir) && (!aZdir))
498           aSuffix = "X";
499         else if ((!aXdir) && aYdir && (!aZdir))
500           aSuffix = "Y";
501         else if ((!aXdir) && (!aYdir) && aZdir)
502           aSuffix = "Z";
503         if (aSuffix.length() > 0)
504           aData->setName("Axis_" + aSuffix);
505         aMyFeature->execute();
506
507       }
508     } else if (aAdaptor.GetType() == GeomAbs_Circle) {
509       if (anEdge->isArc()) {
510         // Create arc
511         aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
512       }
513       else {
514         // Create circle
515         aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
516       }
517     }
518     if (aMyFeature) {
519       DataPtr aData = aMyFeature->data();
520       AttributeSelectionPtr anAttr = 
521         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
522         (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
523
524       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
525       // selection shape has no result owner => the trihedron axis
526       // TODO: make reference to the real axes when they are implemented in the initialization plugin
527       if (!aRes.get()) {
528         ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()->objectByName(
529           ModelAPI_ResultConstruction::group(), "Origin");
530         if (aPointObj.get()) { // if initialization plugin performed well
531           aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
532         }     
533       }
534       if (!aRes.get()) {
535         aRes = aMyFeature->firstResult();
536       }
537       if (anAttr.get() && aRes.get()) {
538         std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
539         anEdge->setImpl(new TopoDS_Shape(theShape));
540
541         anAttr->setValue(aRes, anEdge);
542         //if (!theTemporary) {
543           aMyFeature->execute();
544
545           // fix this edge
546           FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
547           aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
548             setObject(aMyFeature->lastResult());
549         //}
550         return aMyFeature->lastResult();
551       }
552     }
553   }
554   if (theShape.ShapeType() == TopAbs_VERTEX) {
555     FeaturePtr aMyFeature = theSketch->addFeature(SketchPlugin_Point::ID());
556
557     if (aMyFeature) {
558       DataPtr aData = aMyFeature->data();
559       AttributeSelectionPtr anAttr = 
560         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
561         (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
562
563       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
564       // if there is no object, it means that this is the origin point: search it in the module document
565       if (!aRes.get()) {
566         ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()->objectByName(
567           ModelAPI_ResultConstruction::group(), "Origin");
568         if (aPointObj.get()) { // if initialization plugin performed well
569           aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
570         }     
571       }
572       // reference to itself with name "Origin" (but this may cause the infinitive cycling)
573       if (!aRes.get()) {
574         // If the point is selected not from Result object
575         std::shared_ptr<GeomAPI_Shape> aShape = 
576           std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
577         aShape->setImpl(new TopoDS_Shape(theShape));
578
579         std::shared_ptr<GeomAPI_Vertex> aVertex = 
580           std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aShape));
581         std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
582
583         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = convertTo2D(theSketch, aPnt);
584         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
585           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Point::COORD_ID()));
586         aPoint->setValue(aPnt2d);
587         if ((aPnt->x() < Precision::Confusion()) && 
588             (aPnt->y() < Precision::Confusion()) &&
589             (aPnt->z() < Precision::Confusion()))
590           aData->setName("Origin");
591
592         aMyFeature->execute();
593         aRes = aMyFeature->firstResult();
594       }
595       if (anAttr.get() && aRes.get()) {
596         std::shared_ptr<GeomAPI_Shape> aVert(new GeomAPI_Shape);
597         aVert->setImpl(new TopoDS_Shape(theShape));
598
599         anAttr->setValue(aRes, aVert);
600         //if (theTemporary) {
601           aMyFeature->execute();
602
603           // fix this edge
604           FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
605           aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
606             setObject(aMyFeature->lastResult());
607         //}
608         return aMyFeature->lastResult();
609       }
610     }
611   }
612   return ResultPtr();
613 }
614
615 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrsPtr>& theSelected,
616                                           const ModuleBase_ViewerPrsPtr& thePrs)
617 {
618   foreach (ModuleBase_ViewerPrsPtr aPrs, theSelected) {
619     if (aPrs->object() == thePrs->object())
620       return true;
621   }
622   return false;
623 }
624
625 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Edge> theEdge)
626 {
627   for (int i = 0; i < theSketch->numberOfSubs(); i++) {
628     FeaturePtr aFeature = theSketch->subFeature(i);
629     std::shared_ptr<SketchPlugin_Feature> aSketchFea = 
630       std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
631     if (aSketchFea) {
632       if (aSketchFea->isExternal()) {
633         std::list<ResultPtr> aResults = aSketchFea->results();
634         std::list<ResultPtr>::const_iterator aIt;
635         for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
636           ResultConstructionPtr aRes = 
637             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
638           if (aRes) {
639             std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
640             if (aShape) {
641               if (theEdge->isEqual(aShape))
642                 return aRes;
643             }
644           }
645         }
646       }
647     }
648   }
649   return ResultPtr();
650 }
651
652
653 ResultPtr PartSet_Tools::findExternalVertex(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Vertex> theVert)
654 {
655   for (int i = 0; i < theSketch->numberOfSubs(); i++) {
656     FeaturePtr aFeature = theSketch->subFeature(i);
657     std::shared_ptr<SketchPlugin_Feature> aSketchFea = 
658       std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
659     if (aSketchFea) {
660       if (aSketchFea->isExternal()) {
661         std::list<ResultPtr> aResults = aSketchFea->results();
662         std::list<ResultPtr>::const_iterator aIt;
663         for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
664           ResultConstructionPtr aRes = 
665             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
666           if (aRes) {
667             std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
668             if (aShape) {
669               if (theVert->isEqual(aShape))
670                 return aRes;
671             }
672           }
673         }
674       }
675     }
676   }
677   return ResultPtr();
678 }
679
680
681 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrsPtr& thePrs, FeaturePtr theSketch,
682                                    Handle_V3d_View theView, double& theX, double& theY)
683 {
684   bool aHasVertex = false;
685
686   const GeomShapePtr& aShape = thePrs->shape();
687   if (aShape.get() && !aShape->isNull() && aShape->shapeType() == GeomAPI_Shape::VERTEX)
688   {
689     const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
690     const TopoDS_Vertex& aVertex = TopoDS::Vertex(aTDShape);
691     if (!aVertex.IsNull())
692     {
693       gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
694       PartSet_Tools::convertTo2D(aPoint, theSketch, theView, theX, theY);
695       aHasVertex = true;
696     }
697   }
698
699   return aHasVertex;
700 }
701
702 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
703                                                        ModuleBase_IWorkshop* theWorkshop)
704 {
705   GeomShapePtr aShape;
706   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
707   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
708
709   // 2. find visualized vertices of the attribute and if the attribute of the vertex is
710   // the same, return it
711   FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
712   // 2.1 get visualized results of the feature
713   const std::list<ResultPtr>& aResList = anAttributeFeature->results();
714   std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
715   for (; anIt != aLast; anIt++) {
716     AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
717     if (aAISObj.get() != NULL) {
718       Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
719       // 2.2 find selected owners of a visualizedd object
720       SelectMgr_IndexedMapOfOwner aSelectedOwners;
721       aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
722       for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
723         Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
724         if (!anOwner.IsNull()) {
725           Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
726           if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
727             const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
728             if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
729               // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
730               // attribute, returns the shape
731               PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
732               PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
733               AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
734                                                         aBRepShape, aSketchMgr->activeSketch());
735               if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
736                 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
737                 aShape->setImpl(new TopoDS_Shape(aBRepShape));
738                 break;
739               }
740             }
741           }
742         }
743       }
744     }
745   }
746   return aShape;
747 }
748
749 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
750                                                        const std::string& theAttribute)
751 {
752   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
753
754   if (!theFeature->data())
755     return std::shared_ptr<GeomAPI_Pnt2d>();
756
757   FeaturePtr aFeature;
758   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
759       ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
760   if (!anAttr)
761     return std::shared_ptr<GeomAPI_Pnt2d>();
762
763   aFeature = ModelAPI_Feature::feature(anAttr->object());
764
765   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
766     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
767         aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
768
769   else if (anAttr->attr()) {
770     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
771   }
772   if (aPointAttr.get() != NULL)
773     return aPointAttr->pnt();
774   return std::shared_ptr<GeomAPI_Pnt2d>();
775 }
776
777 FeaturePtr findFirstCoincidenceByData(const DataPtr& theData, std::shared_ptr<GeomAPI_Pnt2d> thePoint)
778 {
779   FeaturePtr aCoincident;
780
781   const std::set<AttributePtr>& aRefsList = theData->refsToMe();
782   std::set<AttributePtr>::const_iterator aIt;
783   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
784     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
785     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
786     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) { 
787       std::shared_ptr<GeomAPI_Pnt2d> a2dPnt = 
788         PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
789       if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) { 
790         aCoincident = aConstrFeature;
791         break;
792       } else {
793         a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
794                                           SketchPlugin_ConstraintCoincidence::ENTITY_B());
795         if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) { 
796           aCoincident = aConstrFeature;
797           break;
798         }
799       }
800     }
801   }
802   return aCoincident;
803 }
804
805 FeaturePtr PartSet_Tools::findFirstCoincidence(const FeaturePtr& theFeature,
806                                                std::shared_ptr<GeomAPI_Pnt2d> thePoint)
807 {
808   FeaturePtr aCoincident;
809   if (theFeature.get() == NULL)
810     return aCoincident;
811
812   const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
813   std::set<AttributePtr>::const_iterator aIt;
814   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
815     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
816     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
817     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) { 
818       std::shared_ptr<GeomAPI_Pnt2d> a2dPnt = 
819         PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
820       if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) { 
821         aCoincident = aConstrFeature;
822         break;
823       } else {
824         a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
825                                           SketchPlugin_ConstraintCoincidence::ENTITY_B());
826         if (a2dPnt.get() && thePoint->isEqual(a2dPnt)) { 
827           aCoincident = aConstrFeature;
828           break;
829         }
830       }
831     }
832   }
833   /// Find by result
834   if (!aCoincident.get()) {
835     std::list<ResultPtr> aResults = theFeature->results();
836     std::list<ResultPtr>::const_iterator aIt;
837     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
838       ResultPtr aResult = *aIt;
839       aCoincident = findFirstCoincidenceByData(aResult->data(), thePoint);
840       if (aCoincident.get())
841         break;
842     }
843   }
844   return aCoincident;
845 }
846
847 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
848                                      QList<FeaturePtr>& theCoincidencies,
849                                      std::string theAttr)
850 {
851   std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincedencePoint(theStartCoin);
852   if (aOrig.get() == NULL)
853     return;
854
855   AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
856   if (!aPnt) 
857     return;
858   ObjectPtr aObj = aPnt->object();
859   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
860   if (aFeature.get()) {
861     if (!theList.contains(aFeature)) {
862       theList.append(aFeature);
863       theCoincidencies.append(theStartCoin);
864       const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
865       std::set<AttributePtr>::const_iterator aIt;
866       for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
867         std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
868         FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
869         if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) { 
870           if (!theCoincidencies.contains(aConstrFeature)) {
871             std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
872             if (aPnt.get() && aOrig->isEqual(aPnt)) {
873               findCoincidences(aConstrFeature, theList, theCoincidencies, 
874                 SketchPlugin_ConstraintCoincidence::ENTITY_A());
875               findCoincidences(aConstrFeature, theList, theCoincidencies, 
876                 SketchPlugin_ConstraintCoincidence::ENTITY_B());
877             }
878           }
879         }
880       }
881     }
882   } else {
883     // Find by Results
884     ResultConstructionPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aObj);
885     if (aResult.get()) {
886       FeaturePtr aFeature = ModelAPI_Feature::feature(aPnt->object());
887       if (!theList.contains(aFeature)) 
888         theList.append(aFeature);
889       theCoincidencies.append(theStartCoin);
890
891       const std::set<AttributePtr>& aRefsList = aResult->data()->refsToMe();
892       std::set<AttributePtr>::const_iterator aIt;
893       for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
894         std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
895         FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
896         if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) { 
897           if (!theCoincidencies.contains(aConstrFeature)) {
898             std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
899             if (aPnt.get() && aOrig->isEqual(aPnt)) {
900               findCoincidences(aConstrFeature, theList, theCoincidencies, 
901                 SketchPlugin_ConstraintCoincidence::ENTITY_A());
902               findCoincidences(aConstrFeature, theList, theCoincidencies, 
903                 SketchPlugin_ConstraintCoincidence::ENTITY_B());
904             }
905           }
906         }
907       }
908     }
909   }
910 }
911
912 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getCoincedencePoint(FeaturePtr theStartCoin)
913 {
914   std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), 
915                                                                     SketchPlugin_Constraint::ENTITY_A());
916   if (aPnt.get() == NULL)
917     aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
918   return aPnt;
919 }
920
921 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj, 
922                                                    const TopoDS_Shape theShape, 
923                                                    FeaturePtr theSketch)
924 {
925
926   AttributePtr anAttribute;
927   FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
928   if (aFeature) {
929     if (theShape.ShapeType() == TopAbs_VERTEX) {
930       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
931       if (!aVertex.IsNull())  {
932         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
933         std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
934             new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
935
936         // find the given point in the feature attributes
937         std::list<AttributePtr> anAttiributes = 
938           aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
939         std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(), 
940                                                 aLast = anAttiributes.end();
941         for (; anIt != aLast && !anAttribute; anIt++) {
942           std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
943             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
944
945           std::shared_ptr<GeomAPI_Pnt> aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
946           if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
947             anAttribute = aCurPoint;
948             break;
949           }
950         }
951       }
952     }
953   }
954   return anAttribute;
955 }
956
957 void PartSet_Tools::sendSubFeaturesEvent(const CompositeFeaturePtr& theComposite,
958                                          const Events_ID theEventId)
959 {
960   if (!theComposite.get())
961     return;
962
963   static Events_Loop* aLoop = Events_Loop::loop();
964   for (int i = 0; i < theComposite->numberOfSubs(); i++) {
965     FeaturePtr aSubFeature = theComposite->subFeature(i);
966     static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
967     aECreator->sendUpdated(aSubFeature, theEventId);
968   }
969   Events_Loop::loop()->flush(theEventId);
970 }
971
972 bool PartSet_Tools::isAuxiliarySketchEntity(const ObjectPtr& theObject)
973 {
974   bool isAuxiliaryFeature = false;
975
976   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
977   std::string anAuxiliaryAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
978   AttributeBooleanPtr anAuxiliaryAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
979                                     anObjectFeature->data()->attribute(anAuxiliaryAttribute));
980   if (anAuxiliaryAttr.get())
981     isAuxiliaryFeature = anAuxiliaryAttr->value();
982
983
984   return isAuxiliaryFeature;
985 }