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 #define _MODIFIEDF_TAG 1
16 #define _MODIFIEDE_TAG 2
17 #define _MODIFIEDV_TAG 3
19 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
23 void FeaturesPlugin_Placement::initAttributes()
25 data()->addAttribute(FeaturesPlugin_Placement::BASE_FACE_ID(), ModelAPI_AttributeSelection::type());
26 data()->addAttribute(FeaturesPlugin_Placement::ATTRACT_FACE_ID(), ModelAPI_AttributeSelection::type());
29 void FeaturesPlugin_Placement::execute()
31 // Verify the base face
32 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = std::dynamic_pointer_cast<
33 ModelAPI_AttributeSelection>(data()->attribute(FeaturesPlugin_Placement::BASE_FACE_ID()));
37 std::shared_ptr<GeomAPI_Shape> aBaseFace =
38 std::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
42 std::shared_ptr<GeomAPI_Shape> aBaseFaceContext;
43 ResultPtr aContextRes = aFaceRef->context();
45 if (aContextRes->groupName() == ModelAPI_ResultBody::group())
46 aBaseFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
47 else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
48 aBaseFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
50 if (!aBaseFaceContext) {
51 static const std::string aContextError = "The selection context is bad";
52 setError(aContextError);
56 // Verify the attractive face
57 aFaceRef = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
58 data()->attribute(FeaturesPlugin_Placement::ATTRACT_FACE_ID()));
60 std::shared_ptr<GeomAPI_Shape> aSlaveObjectFace =
61 std::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
62 if (!aSlaveObjectFace)
65 std::shared_ptr<GeomAPI_Shape> aSlaveObject;
66 aContextRes = aFaceRef->context();
68 if (aContextRes->groupName() == ModelAPI_ResultBody::group())
69 aSlaveObject = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
70 else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
71 aSlaveObject = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
74 static const std::string aContextError = "The selection context is bad";
75 setError(aContextError);
79 // Verify faces planarity
80 std::shared_ptr<GeomAPI_Face> aBaseFace1(new GeomAPI_Face(aBaseFace));
81 std::shared_ptr<GeomAPI_Face> aSlaveFace1(new GeomAPI_Face(aSlaveObjectFace));
82 if (!aBaseFace1->isPlanar() || !aSlaveFace1->isPlanar()) {
83 static const std::string aPlanarityError = "One of selected face is not planar";
84 setError(aPlanarityError);
88 std::shared_ptr<GeomAPI_Pln> aBasePlane = aBaseFace1->getPlane();
89 std::shared_ptr<GeomAPI_Pln> aSlavePlane = aSlaveFace1->getPlane();
91 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
92 GeomAlgoAPI_Placement aFeature(aSlaveObject, aSlavePlane, aBasePlane);
93 if(!aFeature.isDone()) {
94 static const std::string aFeatureError = "Placement algorithm failed";
95 setError(aFeatureError);
99 // Check if shape is valid
100 if (aFeature.shape()->isNull()) {
101 static const std::string aShapeError = "Resulting shape is Null";
102 setError(aShapeError);
105 if(!aFeature.isValid()) {
106 std::string aFeatureError = "Warning: resulting shape is not valid";
107 setError(aFeatureError);
111 LoadNamingDS(aFeature, aResultBody, aSlaveObject);
113 setResult(aResultBody);
116 //============================================================================
117 void FeaturesPlugin_Placement::LoadNamingDS(
118 GeomAlgoAPI_Placement& theFeature,
119 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
120 std::shared_ptr<GeomAPI_Shape> theSlaveObject)
123 theResultBody->storeModified(theSlaveObject, theFeature.shape()); // the initial Slave, the resulting Slave
125 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
126 theFeature.mapOfShapes(*aSubShapes);
128 // put modifed faces in DF
129 theResultBody->loadAndOrientModifiedShapes(theFeature.makeShape(), theSlaveObject, _FACE, _MODIFIEDF_TAG, *aSubShapes);