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