]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelGeomAlgo/ModelGeomAlgo_Point2D.cpp
Salome HOME
Correction for restart of point create operation.
[modules/shaper.git] / src / ModelGeomAlgo / ModelGeomAlgo_Point2D.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_Tools.cpp
4 // Created:     20 Jul 2016
5 // Author:      Natalia ERMOLAEVA
6
7 #include "ModelGeomAlgo_Point2D.h"
8
9 #include <ModelAPI_Feature.h>
10 #include <ModelAPI_Result.h>
11 #include <ModelAPI_AttributeRefAttr.h>
12
13 #include <GeomAlgoAPI_ShapeTools.h>
14 #include <GeomDataAPI_Point2D.h>
15
16 #include <GeomAPI_Pnt.h>
17 #include <GeomAPI_Pnt2d.h>
18 #include <GeomAPI_Vertex.h>
19 #include <GeomAPI_Dir.h>
20 #include <GeomAPI_Edge.h>
21 #include <GeomAPI_Lin.h>
22
23 namespace ModelGeomAlgo_Point2D {
24   std::shared_ptr<GeomDataAPI_Point2D> getPointOfRefAttr(ModelAPI_Feature* theFeature,
25                                                          const std::string& theAttribute,
26                                                          const std::string& theObjectFeatureKind,
27                                                          const std::string& theObjectFeatureAttribute)
28   {
29     std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
30
31     /// essential check as it is called in openGl thread
32     if (!theFeature || !theFeature->data().get() || !theFeature->data()->isValid())
33       return std::shared_ptr<GeomDataAPI_Point2D>();
34
35     FeaturePtr aFeature;
36     std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
37         ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
38     if(anAttr.get() && anAttr->isInitialized()) {
39       aFeature = ModelAPI_Feature::feature(anAttr->object());
40       if (aFeature.get()) {
41         bool aFeatureOfObjectKind = !theObjectFeatureKind.empty() &&
42                                     !theObjectFeatureAttribute.empty() &&
43                                     aFeature->getKind() == theObjectFeatureKind;
44         if(aFeatureOfObjectKind)
45             aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
46                                     aFeature->data()->attribute(theObjectFeatureAttribute));
47         else if (anAttr->attr())
48           aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
49       }
50     }
51     return aPointAttr;
52   }
53
54   void getPointsOfReference(const std::shared_ptr<ModelAPI_Object>& theObject,
55                             const std::string& theReferenceFeatureKind,
56                             std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theAttributes,
57                             const std::string& theObjectFeatureKind,
58                             const std::string& theObjectFeatureAttribute)
59   {
60     // find by feature
61     const std::set<AttributePtr>& aRefsList = theObject->data()->refsToMe();
62     std::set<AttributePtr>::const_iterator aIt;
63     for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
64       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
65       FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
66       if (aRefFeature->getKind() == theReferenceFeatureKind) {
67         std::list<AttributePtr> anAttributes =
68                          aRefFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
69         std::list<AttributePtr>::iterator anIter = anAttributes.begin();
70         // it searches the first point of AttributeRefAtt
71         std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
72         for(; anIter != anAttributes.end() && !aPointAttr.get(); anIter++) {
73           AttributeRefAttrPtr aRefAttribute =
74             std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
75           if (aRefAttribute.get())
76             aPointAttr = getPointOfRefAttr(aRefFeature.get(), aRefAttribute->id(),
77                                            theObjectFeatureKind, theObjectFeatureAttribute);
78         }
79         if (aPointAttr.get()) {
80           theAttributes.insert(aPointAttr);
81         }
82       }
83     }
84     // find by results
85     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
86     if (aFeature.get()) {
87       const std::list<std::shared_ptr<ModelAPI_Result> > aResults = aFeature->results();
88       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
89       for (; aRIter != aResults.cend(); aRIter++) {
90         ResultPtr aResult = *aRIter;
91         getPointsOfReference(aResult, theReferenceFeatureKind, theAttributes, theObjectFeatureKind,
92                              theObjectFeatureAttribute);
93       }
94     }
95   }
96
97   void getPointsInsideShape(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
98                             const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theAttributes,
99                             const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
100                             const std::shared_ptr<GeomAPI_Dir>& theDirX,
101                             const std::shared_ptr<GeomAPI_Dir>& theDirY,
102                             std::set<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
103   {
104     std::set<std::shared_ptr<GeomDataAPI_Point2D> >::const_iterator anIt = theAttributes.begin(),
105                                                             aLast = theAttributes.end();
106     for (; anIt != aLast; anIt++) {
107       std::shared_ptr<GeomDataAPI_Point2D> anAttribute = *anIt;
108       std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = anAttribute->pnt();
109       std::shared_ptr<GeomAPI_Pnt> aPoint = aPnt2d->to3D(theOrigin, theDirX, theDirY);
110       std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
111       if (isPointOnEdge(theBaseShape, aPoint, aProjectedPoint))
112         thePoints.insert(aProjectedPoint);
113     }
114   }
115
116   bool isPointOnEdge(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
117                      const std::shared_ptr<GeomAPI_Pnt>& thePoint,
118                      std::shared_ptr<GeomAPI_Pnt>& theProjectedPoint)
119   {
120     bool isInside = false;
121     if (theBaseShape->shapeType() == GeomAPI_Shape::EDGE) {
122       std::shared_ptr<GeomAPI_Edge> aLineEdge(new GeomAPI_Edge(theBaseShape));
123       std::shared_ptr<GeomAPI_Lin> aLine = aLineEdge->line();
124       theProjectedPoint = aLine->project(thePoint);
125
126       std::shared_ptr<GeomAPI_Vertex> aVertexShape(new GeomAPI_Vertex(theProjectedPoint->x(),
127                                                 theProjectedPoint->y(), theProjectedPoint->z()));
128       isInside = GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aVertexShape, theBaseShape);
129     }
130     return isInside;
131   }
132
133 } // namespace ModelGeomAlgo_Point2D