1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: FeaturesPlugin_Placement.cpp
5 // Author: Artem ZHIDKOV
7 #include "FeaturesPlugin_Placement.h"
9 #include <ModelAPI_ResultConstruction.h>
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_AttributeSelection.h>
12 #include <ModelAPI_AttributeBoolean.h>
14 #include <GeomAPI_Face.h>
15 #include <GeomAPI_Pln.h>
16 #include <GeomAlgoAPI_Placement.h>
18 #define _MODIFIEDF_TAG 1
19 #define _MODIFIEDE_TAG 2
20 #define _MODIFIEDV_TAG 3
22 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
26 void FeaturesPlugin_Placement::initAttributes()
28 data()->addAttribute(FeaturesPlugin_Placement::BASE_FACE_ID(), ModelAPI_AttributeSelection::type());
29 data()->addAttribute(FeaturesPlugin_Placement::ATTRACT_FACE_ID(), ModelAPI_AttributeSelection::type());
30 data()->addAttribute(FeaturesPlugin_Placement::REVERSE_ID(), ModelAPI_AttributeBoolean::type());
31 data()->addAttribute(FeaturesPlugin_Placement::CENTERING_ID(), ModelAPI_AttributeBoolean::type());
34 void FeaturesPlugin_Placement::execute()
36 // Verify the base face
37 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = std::dynamic_pointer_cast<
38 ModelAPI_AttributeSelection>(data()->attribute(FeaturesPlugin_Placement::BASE_FACE_ID()));
42 std::shared_ptr<GeomAPI_Shape> aBaseFace =
43 std::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
47 std::shared_ptr<GeomAPI_Shape> aBaseFaceContext;
48 ResultPtr aContextRes = aFaceRef->context();
50 if (aContextRes->groupName() == ModelAPI_ResultBody::group())
51 aBaseFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
52 else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
53 aBaseFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
55 if (!aBaseFaceContext) {
56 static const std::string aContextError = "The selection context is bad";
57 setError(aContextError);
61 // Verify the attractive face
62 aFaceRef = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
63 data()->attribute(FeaturesPlugin_Placement::ATTRACT_FACE_ID()));
65 std::shared_ptr<GeomAPI_Shape> aSlaveObjectFace =
66 std::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
67 if (!aSlaveObjectFace)
70 std::shared_ptr<GeomAPI_Shape> aSlaveObject;
71 aContextRes = aFaceRef->context();
73 if (aContextRes->groupName() == ModelAPI_ResultBody::group())
74 aSlaveObject = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
75 else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
76 aSlaveObject = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
79 static const std::string aContextError = "The selection context is bad";
80 setError(aContextError);
84 // Verify faces planarity
85 std::shared_ptr<GeomAPI_Face> aBaseFace1(new GeomAPI_Face(aBaseFace));
86 std::shared_ptr<GeomAPI_Face> aSlaveFace1(new GeomAPI_Face(aSlaveObjectFace));
87 if (!aBaseFace1->isPlanar() || !aSlaveFace1->isPlanar()) {
88 static const std::string aPlanarityError = "One of selected face is not planar";
89 setError(aPlanarityError);
93 // Flags of the Placement
94 AttributeBooleanPtr aBoolAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
95 data()->attribute(FeaturesPlugin_Placement::REVERSE_ID()));
96 bool isReverse = aBoolAttr->value();
97 aBoolAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
98 data()->attribute(FeaturesPlugin_Placement::CENTERING_ID()));
99 bool isCentering = aBoolAttr->value();
101 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
102 GeomAlgoAPI_Placement aFeature(aSlaveObject, aBaseFaceContext, aSlaveFace1, aBaseFace1, isReverse, isCentering);
103 if(!aFeature.isDone()) {
104 static const std::string aFeatureError = "Placement algorithm failed";
105 setError(aFeatureError);
109 // Check if shape is valid
110 if (aFeature.shape()->isNull()) {
111 static const std::string aShapeError = "Resulting shape is Null";
112 setError(aShapeError);
115 if(!aFeature.isValid()) {
116 std::string aFeatureError = "Warning: resulting shape is not valid";
117 setError(aFeatureError);
121 LoadNamingDS(aFeature, aResultBody, aSlaveObject);
123 setResult(aResultBody);
126 //============================================================================
127 void FeaturesPlugin_Placement::LoadNamingDS(
128 GeomAlgoAPI_Placement& theFeature,
129 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
130 std::shared_ptr<GeomAPI_Shape> theSlaveObject)
133 theResultBody->storeModified(theSlaveObject, theFeature.shape()); // the initial Slave, the resulting Slave
135 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
136 theFeature.mapOfShapes(*aSubShapes);
138 // put modifed faces in DF
139 std::string aModName = "Modified";
140 theResultBody->loadAndOrientModifiedShapes(theFeature.makeShape(), theSlaveObject, _FACE, _MODIFIEDF_TAG, aModName, *aSubShapes);