1 // File: FeaturesPlugin_Placement.cpp
3 // Author: Artem ZHIDKOV
5 #include "FeaturesPlugin_Placement.h"
7 #include <ModelAPI_ResultConstruction.h>
8 #include <ModelAPI_ResultBody.h>
9 #include <ModelAPI_AttributeSelection.h>
11 #include <GeomAPI_Face.h>
12 #include <GeomAPI_Pln.h>
13 #include <GeomAlgoAPI_Placement.h>
15 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
19 void FeaturesPlugin_Placement::initAttributes()
21 data()->addAttribute(FeaturesPlugin_Placement::BASE_FACE_ID(), ModelAPI_AttributeSelection::type());
22 data()->addAttribute(FeaturesPlugin_Placement::ATTRACT_FACE_ID(), ModelAPI_AttributeSelection::type());
25 void FeaturesPlugin_Placement::execute()
27 // Verify the base face
28 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = std::dynamic_pointer_cast<
29 ModelAPI_AttributeSelection>(data()->attribute(FeaturesPlugin_Placement::BASE_FACE_ID()));
33 std::shared_ptr<GeomAPI_Shape> aBaseFace =
34 std::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
38 std::shared_ptr<GeomAPI_Shape> aBaseFaceContext;
39 ResultPtr aContextRes = aFaceRef->context();
41 if (aContextRes->groupName() == ModelAPI_ResultBody::group())
42 aBaseFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
43 else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
44 aBaseFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
46 if (!aBaseFaceContext) {
47 static const std::string aContextError = "The selection context is bad";
48 setError(aContextError);
52 // Verify the attractive face
53 aFaceRef = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
54 data()->attribute(FeaturesPlugin_Placement::ATTRACT_FACE_ID()));
56 std::shared_ptr<GeomAPI_Shape> anAttractiveFace =
57 std::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
58 if (!anAttractiveFace)
61 std::shared_ptr<GeomAPI_Shape> anAttractiveFaceContext;
62 aContextRes = aFaceRef->context();
64 if (aContextRes->groupName() == ModelAPI_ResultBody::group())
65 anAttractiveFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
66 else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
67 anAttractiveFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
69 if (!anAttractiveFaceContext) {
70 static const std::string aContextError = "The selection context is bad";
71 setError(aContextError);
75 // Verify faces planarity
76 std::shared_ptr<GeomAPI_Face> aBaseFace1(new GeomAPI_Face(aBaseFace));
77 std::shared_ptr<GeomAPI_Face> anAttractFace1(new GeomAPI_Face(anAttractiveFace));
78 if (!aBaseFace1->isPlanar() || !anAttractFace1->isPlanar()) {
79 static const std::string aPlanarityError = "One of selected face is not planar";
80 setError(aPlanarityError);
84 std::shared_ptr<GeomAPI_Pln> aBasePlane = aBaseFace1->getPlane();
85 std::shared_ptr<GeomAPI_Pln> anAttractivePlane = anAttractFace1->getPlane();
87 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
88 GeomAlgoAPI_Placement aFeature(anAttractiveFaceContext, anAttractivePlane, aBasePlane);
89 if(!aFeature.isDone()) {
90 static const std::string aFeatureError = "Placement algorithm failed";
91 setError(aFeatureError);
95 // Check if shape is valid
96 if (aFeature.shape()->isNull()) {
97 static const std::string aShapeError = "Resulting shape is Null";
98 setError(aShapeError);
101 if(!aFeature.isValid()) {
102 std::string aFeatureError = "Warning: resulting shape is not valid";
103 setError(aFeatureError);
107 LoadNamingDS(aFeature, aResultBody, anAttractiveFace, anAttractiveFaceContext);
109 setResult(aResultBody);
112 //============================================================================
113 void FeaturesPlugin_Placement::LoadNamingDS(
114 GeomAlgoAPI_Placement& theFeature,
115 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
116 std::shared_ptr<GeomAPI_Shape> theBasis,
117 std::shared_ptr<GeomAPI_Shape> theContext)
119 theResultBody->store(theFeature.shape());
124 if(theBasis->isEqual(theContext))
125 theResultBody->store(theFeature.shape());
127 theResultBody->storeGenerated(theContext, theFeature.shape());
129 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
130 theFeature.mapOfShapes(*aSubShapes);
132 //Insert lateral face : Face from Edge
133 theResultBody->loadAndOrientGeneratedShapes(theFeature.makeShape(), theBasis, EDGE,_LATERAL_TAG, *aSubShapes);
136 std::shared_ptr<GeomAPI_Shape> aBottomFace = theFeature.firstShape();
137 if (!aBottomFace->isNull()) {
138 if (aSubShapes->isBound(aBottomFace)) {
139 aBottomFace = aSubShapes->find(aBottomFace);
141 theResultBody->generated(aBottomFace, _FIRST_TAG);
147 std::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
148 if (!aTopFace->isNull()) {
149 if (aSubShapes->isBound(aTopFace)) {
150 aTopFace = aSubShapes->find(aTopFace);
152 theResultBody->generated(aTopFace, _LAST_TAG);