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