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