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>
13 #include <GeomAPI_Face.h>
14 #include <GeomAPI_Pln.h>
15 #include <GeomAlgoAPI_Placement.h>
17 #define _MODIFIEDF_TAG 1
18 #define _MODIFIEDE_TAG 2
19 #define _MODIFIEDV_TAG 3
21 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
25 void FeaturesPlugin_Placement::initAttributes()
27 data()->addAttribute(FeaturesPlugin_Placement::BASE_FACE_ID(), ModelAPI_AttributeSelection::type());
28 data()->addAttribute(FeaturesPlugin_Placement::ATTRACT_FACE_ID(), ModelAPI_AttributeSelection::type());
31 void FeaturesPlugin_Placement::execute()
33 // Verify the base face
34 std::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = std::dynamic_pointer_cast<
35 ModelAPI_AttributeSelection>(data()->attribute(FeaturesPlugin_Placement::BASE_FACE_ID()));
39 std::shared_ptr<GeomAPI_Shape> aBaseFace =
40 std::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
44 std::shared_ptr<GeomAPI_Shape> aBaseFaceContext;
45 ResultPtr aContextRes = aFaceRef->context();
47 if (aContextRes->groupName() == ModelAPI_ResultBody::group())
48 aBaseFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
49 else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
50 aBaseFaceContext = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
52 if (!aBaseFaceContext) {
53 static const std::string aContextError = "The selection context is bad";
54 setError(aContextError);
58 // Verify the attractive face
59 aFaceRef = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
60 data()->attribute(FeaturesPlugin_Placement::ATTRACT_FACE_ID()));
62 std::shared_ptr<GeomAPI_Shape> aSlaveObjectFace =
63 std::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
64 if (!aSlaveObjectFace)
67 std::shared_ptr<GeomAPI_Shape> aSlaveObject;
68 aContextRes = aFaceRef->context();
70 if (aContextRes->groupName() == ModelAPI_ResultBody::group())
71 aSlaveObject = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
72 else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
73 aSlaveObject = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
76 static const std::string aContextError = "The selection context is bad";
77 setError(aContextError);
81 // Verify faces planarity
82 std::shared_ptr<GeomAPI_Face> aBaseFace1(new GeomAPI_Face(aBaseFace));
83 std::shared_ptr<GeomAPI_Face> aSlaveFace1(new GeomAPI_Face(aSlaveObjectFace));
84 if (!aBaseFace1->isPlanar() || !aSlaveFace1->isPlanar()) {
85 static const std::string aPlanarityError = "One of selected face is not planar";
86 setError(aPlanarityError);
90 std::shared_ptr<GeomAPI_Pln> aBasePlane = aBaseFace1->getPlane();
91 std::shared_ptr<GeomAPI_Pln> aSlavePlane = aSlaveFace1->getPlane();
93 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
94 GeomAlgoAPI_Placement aFeature(aSlaveObject, aSlavePlane, aBasePlane);
95 if(!aFeature.isDone()) {
96 static const std::string aFeatureError = "Placement algorithm failed";
97 setError(aFeatureError);
101 // Check if shape is valid
102 if (aFeature.shape()->isNull()) {
103 static const std::string aShapeError = "Resulting shape is Null";
104 setError(aShapeError);
107 if(!aFeature.isValid()) {
108 std::string aFeatureError = "Warning: resulting shape is not valid";
109 setError(aFeatureError);
113 LoadNamingDS(aFeature, aResultBody, aSlaveObject);
115 setResult(aResultBody);
118 //============================================================================
119 void FeaturesPlugin_Placement::LoadNamingDS(
120 GeomAlgoAPI_Placement& theFeature,
121 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
122 std::shared_ptr<GeomAPI_Shape> theSlaveObject)
125 theResultBody->storeModified(theSlaveObject, theFeature.shape()); // the initial Slave, the resulting Slave
127 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
128 theFeature.mapOfShapes(*aSubShapes);
130 // put modifed faces in DF
131 std::string aModName = "Modified";
132 theResultBody->loadAndOrientModifiedShapes(theFeature.makeShape(), theSlaveObject, _FACE, _MODIFIEDF_TAG, aModName, *aSubShapes);