]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Tools.cpp
Salome HOME
1. Circle creation - when 'internal' edit operation is active, the first click should...
[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     if (aAdaptor.GetType() == GeomAbs_Line) {
435       // Create line
436       aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
437     } else if (aAdaptor.GetType() == GeomAbs_Circle) {
438       std::shared_ptr<GeomAPI_Edge> anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
439       anEdge->setImpl(new TopoDS_Shape(theShape));
440       if (anEdge->isArc()) {
441         // Create arc
442         aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
443       }
444       else {
445         // Create circle
446         aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
447       }
448     }
449     if (aMyFeature) {
450       DataPtr aData = aMyFeature->data();
451       AttributeSelectionPtr anAttr = 
452         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
453         (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
454
455       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
456       if (anAttr && aRes) {
457         std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
458         anEdge->setImpl(new TopoDS_Shape(theShape));
459
460         anAttr->setValue(aRes, anEdge);
461         //if (!theTemporary) {
462           aMyFeature->execute();
463
464           // fix this edge
465           FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
466           aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
467             setObject(aMyFeature->lastResult());
468         //}
469         return aMyFeature->lastResult();
470       }
471     }
472   }
473   if (theShape.ShapeType() == TopAbs_VERTEX) {
474     FeaturePtr aMyFeature = theSketch->addFeature(SketchPlugin_Point::ID());
475
476     if (aMyFeature) {
477       DataPtr aData = aMyFeature->data();
478       AttributeSelectionPtr anAttr = 
479         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
480         (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
481
482       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
483       if (anAttr && aRes) {
484         std::shared_ptr<GeomAPI_Shape> aVert(new GeomAPI_Shape);
485         aVert->setImpl(new TopoDS_Shape(theShape));
486
487         anAttr->setValue(aRes, aVert);
488         //if (theTemporary) {
489           aMyFeature->execute();
490
491           // fix this edge
492           FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
493           aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
494             setObject(aMyFeature->lastResult());
495         //}
496         return aMyFeature->lastResult();
497       }
498     }
499   }
500   return ResultPtr();
501 }
502
503 bool PartSet_Tools::isContainPresentation(const QList<ModuleBase_ViewerPrs>& theSelected,
504                                           const ModuleBase_ViewerPrs& thePrs)
505 {
506   foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
507     if (aPrs.object() == thePrs.object())
508       return true;
509   }
510   return false;
511 }
512
513 ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Edge> theEdge)
514 {
515   for (int i = 0; i < theSketch->numberOfSubs(); i++) {
516     FeaturePtr aFeature = theSketch->subFeature(i);
517     std::shared_ptr<SketchPlugin_Feature> aSketchFea = 
518       std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
519     if (aSketchFea) {
520       if (aSketchFea->isExternal()) {
521         std::list<ResultPtr> aResults = aSketchFea->results();
522         std::list<ResultPtr>::const_iterator aIt;
523         for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
524           ResultConstructionPtr aRes = 
525             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
526           if (aRes) {
527             std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
528             if (aShape) {
529               if (theEdge->isEqual(aShape))
530                 return aRes;
531             }
532           }
533         }
534       }
535     }
536   }
537   return ResultPtr();
538 }
539
540
541 ResultPtr PartSet_Tools::findExternalVertex(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Vertex> theVert)
542 {
543   for (int i = 0; i < theSketch->numberOfSubs(); i++) {
544     FeaturePtr aFeature = theSketch->subFeature(i);
545     std::shared_ptr<SketchPlugin_Feature> aSketchFea = 
546       std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
547     if (aSketchFea) {
548       if (aSketchFea->isExternal()) {
549         std::list<ResultPtr> aResults = aSketchFea->results();
550         std::list<ResultPtr>::const_iterator aIt;
551         for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
552           ResultConstructionPtr aRes = 
553             std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
554           if (aRes) {
555             std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
556             if (aShape) {
557               if (theVert->isEqual(aShape))
558                 return aRes;
559             }
560           }
561         }
562       }
563     }
564   }
565   return ResultPtr();
566 }
567
568
569 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrs& thePrs, FeaturePtr theSketch,
570                                    Handle_V3d_View theView, double& theX, double& theY)
571 {
572   bool aHasVertex = false;
573
574   const TopoDS_Shape& aShape = thePrs.shape();
575   if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX)
576   {
577     const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
578     if (!aVertex.IsNull())
579     {
580       gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
581       PartSet_Tools::convertTo2D(aPoint, theSketch, theView, theX, theY);
582       aHasVertex = true;
583     }
584   }
585
586   return aHasVertex;
587 }
588
589 GeomShapePtr PartSet_Tools::findShapeBy2DPoint(const AttributePtr& theAttribute,
590                                                ModuleBase_IWorkshop* theWorkshop)
591 {
592   // 1. find an attribute value in attribute reference attribute value
593   GeomShapePtr aShape;
594   AttributeRefAttrPtr aRefAttr =
595     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
596   if (aRefAttr) {
597     if (!aRefAttr->isObject()) {
598       AttributePtr theAttribute = aRefAttr->attr();
599       if (theAttribute.get()) {
600         XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
601         XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
602
603         // 2. find visualized vertices of the attribute and if the attribute of the vertex is
604         // the same, return it
605         FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(theAttribute->owner());
606         // 2.1 get visualized results of the feature
607         const std::list<ResultPtr>& aResList = anAttributeFeature->results();
608         std::list<ResultPtr>::const_iterator anIt = aResList.begin(), aLast = aResList.end();
609         for (; anIt != aLast; anIt++) {
610           AISObjectPtr aAISObj = aDisplayer->getAISObject(*anIt);
611           if (aAISObj.get() != NULL) {
612             Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
613             // 2.2 find selected owners of a visualizedd object
614             SelectMgr_IndexedMapOfOwner aSelectedOwners;
615             aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
616             for (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
617               Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
618               if (!anOwner.IsNull()) {
619                 Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
620                 if (!aBRepOwner.IsNull() && aBRepOwner->HasShape()) {
621                   const TopoDS_Shape& aBRepShape = aBRepOwner->Shape();
622                   if (aBRepShape.ShapeType() == TopAbs_VERTEX) {
623                     // 2.3 if the owner is vertex and an attribute of the vertex is equal to the initial
624                     // attribute, returns the shape
625                     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(theWorkshop->module());
626                     PartSet_SketcherMgr* aSketchMgr = aModule->sketchMgr();
627                     AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(anAttributeFeature,
628                                                              aBRepShape, aSketchMgr->activeSketch());
629                     if (aPntAttr.get() != NULL && aPntAttr == theAttribute) {
630                       aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
631                       aShape->setImpl(new TopoDS_Shape(aBRepShape));
632                       break;
633                     }
634                   }
635                 }
636               }
637             }
638           }
639         }
640       }
641     }
642   }
643   return aShape;
644 }
645
646 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getPoint(std::shared_ptr<ModelAPI_Feature>& theFeature,
647                                                        const std::string& theAttribute)
648 {
649   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
650
651   if (!theFeature->data())
652     return std::shared_ptr<GeomAPI_Pnt2d>();
653
654   FeaturePtr aFeature;
655   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
656       ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
657   if (!anAttr)
658     return std::shared_ptr<GeomAPI_Pnt2d>();
659
660   aFeature = ModelAPI_Feature::feature(anAttr->object());
661
662   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
663     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
664         aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
665
666   else if (anAttr->attr()) {
667     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
668   }
669   if (aPointAttr.get() != NULL)
670     return aPointAttr->pnt();
671   return std::shared_ptr<GeomAPI_Pnt2d>();
672 }
673
674 void PartSet_Tools::findCoincidences(FeaturePtr theStartCoin, QList<FeaturePtr>& theList,
675                                      std::string theAttr)
676 {
677   AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
678   if (!aPnt) return;
679   FeaturePtr aObj = ModelAPI_Feature::feature(aPnt->object());
680   if (!theList.contains(aObj)) {
681     std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincedencePoint(theStartCoin);
682     if (aOrig.get() == NULL)
683       return;
684     theList.append(aObj);
685     const std::set<AttributePtr>& aRefsList = aObj->data()->refsToMe();
686     std::set<AttributePtr>::const_iterator aIt;
687     for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
688       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
689       FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
690       if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) { 
691         std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincedencePoint(aConstrFeature);
692         if (aPnt.get() && aOrig->isEqual(aPnt)) {
693           findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_A());
694           findCoincidences(aConstrFeature, theList, SketchPlugin_ConstraintCoincidence::ENTITY_B());
695         }
696       }
697     }
698   }
699 }
700
701 std::shared_ptr<GeomAPI_Pnt2d> PartSet_Tools::getCoincedencePoint(FeaturePtr theStartCoin)
702 {
703   std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), 
704                                                                     SketchPlugin_Constraint::ENTITY_A());
705   if (aPnt.get() == NULL)
706     aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
707   return aPnt;
708 }
709
710 AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj, 
711                                                    const TopoDS_Shape theShape, 
712                                                    FeaturePtr theSketch)
713 {
714
715   AttributePtr anAttribute;
716   FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
717   if (aFeature) {
718     if (theShape.ShapeType() == TopAbs_VERTEX) {
719       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
720       if (!aVertex.IsNull())  {
721         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
722         std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
723             new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
724
725         // find the given point in the feature attributes
726         std::list<AttributePtr> anAttiributes = 
727           aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
728         std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(), 
729                                                 aLast = anAttiributes.end();
730         for (; anIt != aLast && !anAttribute; anIt++) {
731           std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
732             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
733
734           std::shared_ptr<GeomAPI_Pnt> aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
735           if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
736             anAttribute = aCurPoint;
737             break;
738           }
739         }
740       }
741     }
742   }
743   return anAttribute;
744 }