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