1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModelAPI_Tools.cpp
4 // Created: 20 Jul 2016
5 // Author: Natalia ERMOLAEVA
7 #include "ModelGeomAlgo_Point2D.h"
9 #include <ModelAPI_Feature.h>
10 #include <ModelAPI_Result.h>
11 #include <ModelAPI_AttributeRefAttr.h>
13 #include <GeomAlgoAPI_ShapeTools.h>
14 #include <GeomDataAPI_Point2D.h>
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 #include <GeomAPI_Circ.h>
24 namespace ModelGeomAlgo_Point2D {
25 std::shared_ptr<GeomDataAPI_Point2D> getPointOfRefAttr(ModelAPI_Feature* theFeature,
26 const std::string& theAttribute,
27 const std::string& theObjectFeatureKind,
28 const std::string& theObjectFeatureAttribute)
30 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
32 /// essential check as it is called in openGl thread
33 if (!theFeature || !theFeature->data().get() || !theFeature->data()->isValid())
34 return std::shared_ptr<GeomDataAPI_Point2D>();
37 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
38 ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
39 if(anAttr.get() && anAttr->isInitialized()) {
40 aFeature = ModelAPI_Feature::feature(anAttr->object());
42 bool aFeatureOfObjectKind = !theObjectFeatureKind.empty() &&
43 !theObjectFeatureAttribute.empty() &&
44 aFeature->getKind() == theObjectFeatureKind;
45 if(aFeatureOfObjectKind)
46 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
47 aFeature->data()->attribute(theObjectFeatureAttribute));
48 else if (anAttr->attr())
49 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
55 void getPointsOfReference(const std::shared_ptr<ModelAPI_Object>& theObject,
56 const std::string& theReferenceFeatureKind,
57 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theAttributes,
58 const std::string& theObjectFeatureKind,
59 const std::string& theObjectFeatureAttribute,
60 const bool isSkipFeatureAttributes)
63 FeaturePtr aSourceFeature = ModelAPI_Feature::feature(theObject);
65 const std::set<AttributePtr>& aRefsList = theObject->data()->refsToMe();
66 std::set<AttributePtr>::const_iterator aIt;
67 for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
68 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
69 FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
70 if (aRefFeature->getKind() == theReferenceFeatureKind) {
71 std::list<AttributePtr> anAttributes =
72 aRefFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
73 std::list<AttributePtr>::iterator anIter = anAttributes.begin(), aLast = anAttributes.end();
74 bool isSkippedAttribute = false;
75 if (isSkipFeatureAttributes) {
76 for(anIter = anAttributes.begin(); anIter != aLast && !isSkippedAttribute; anIter++) {
77 AttributeRefAttrPtr aRefAttribute =
78 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
79 if (aRefAttribute.get() && !aRefAttribute->isObject()) {
80 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
81 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttribute->attr());
82 FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(aPointAttr->owner());
83 isSkippedAttribute = aSourceFeature == anAttributeFeature;
87 if (isSkippedAttribute)
90 // it searches the first point of AttributeRefAtt
91 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
92 for(anIter = anAttributes.begin(); anIter != aLast && !aPointAttr.get(); anIter++) {
93 AttributeRefAttrPtr aRefAttribute =
94 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
95 if (aRefAttribute.get()) {
96 aPointAttr = getPointOfRefAttr(aRefFeature.get(), aRefAttribute->id(),
97 theObjectFeatureKind, theObjectFeatureAttribute);
100 if (aPointAttr.get()) {
101 theAttributes.insert(aPointAttr);
106 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
107 if (aFeature.get()) {
108 const std::list<std::shared_ptr<ModelAPI_Result> > aResults = aFeature->results();
109 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
110 for (; aRIter != aResults.cend(); aRIter++) {
111 ResultPtr aResult = *aRIter;
112 getPointsOfReference(aResult, theReferenceFeatureKind, theAttributes, theObjectFeatureKind,
113 theObjectFeatureAttribute);
118 void getPointsInsideShape(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
119 const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theAttributes,
120 const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
121 const std::shared_ptr<GeomAPI_Dir>& theDirX,
122 const std::shared_ptr<GeomAPI_Dir>& theDirY,
123 std::set<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
124 std::map<std::shared_ptr<GeomDataAPI_Point2D>,
125 std::shared_ptr<GeomAPI_Pnt> >& theAttributeToPoint)
127 std::set<std::shared_ptr<GeomDataAPI_Point2D> >::const_iterator anIt = theAttributes.begin(),
128 aLast = theAttributes.end();
129 for (; anIt != aLast; anIt++) {
130 std::shared_ptr<GeomDataAPI_Point2D> anAttribute = *anIt;
131 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = anAttribute->pnt();
132 std::shared_ptr<GeomAPI_Pnt> aPoint = aPnt2d->to3D(theOrigin, theDirX, theDirY);
133 std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
134 if (isPointOnEdge(theBaseShape, aPoint, aProjectedPoint)) {
135 thePoints.insert(aProjectedPoint);
136 theAttributeToPoint[anAttribute] = aProjectedPoint;
141 bool isPointOnEdge(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
142 const std::shared_ptr<GeomAPI_Pnt>& thePoint,
143 std::shared_ptr<GeomAPI_Pnt>& theProjectedPoint)
145 bool isInside = false;
146 if (theBaseShape->shapeType() == GeomAPI_Shape::EDGE) {
147 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theBaseShape));
148 if (anEdge->isLine()) {
149 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
150 theProjectedPoint = aLine->project(thePoint);
152 else if (anEdge->isCircle() || anEdge->isArc()) {
153 std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
154 theProjectedPoint = aCircle->project(thePoint);
156 if (theProjectedPoint.get()) {
157 std::shared_ptr<GeomAPI_Vertex> aVertexShape(new GeomAPI_Vertex(theProjectedPoint->x(),
158 theProjectedPoint->y(), theProjectedPoint->z()));
159 isInside = GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aVertexShape, theBaseShape);
165 } // namespace ModelGeomAlgo_Point2D