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_ResultPart.h>
12 #include <ModelAPI_AttributeSelection.h>
13 #include <ModelAPI_AttributeBoolean.h>
14 #include <ModelAPI_AttributeSelectionList.h>
16 #include <GeomAPI_Edge.h>
17 #include <GeomAPI_Face.h>
18 #include <GeomAPI_Pln.h>
19 #include <GeomAlgoAPI_Placement.h>
21 #define _MODIFIEDF_TAG 1
22 #define _MODIFIEDE_TAG 2
23 #define _MODIFIEDV_TAG 3
25 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
29 void FeaturesPlugin_Placement::initAttributes()
31 /* Modification for specification of 1.3.0
32 AttributeSelectionListPtr aSelection =
33 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
34 FeaturesPlugin_Placement::LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
35 // extrusion works with faces always
36 aSelection->setSelectionType("SOLID");
38 data()->addAttribute(FeaturesPlugin_Placement::BASE_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
39 data()->addAttribute(FeaturesPlugin_Placement::ATTRACT_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
40 data()->addAttribute(FeaturesPlugin_Placement::REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
41 data()->addAttribute(FeaturesPlugin_Placement::CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
44 void FeaturesPlugin_Placement::execute()
46 // Verify the base face
47 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = std::dynamic_pointer_cast<
48 ModelAPI_AttributeSelection>(data()->attribute(FeaturesPlugin_Placement::BASE_OBJECT_ID()));
52 std::shared_ptr<GeomAPI_Shape> aBaseShape =
53 std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
57 std::shared_ptr<GeomAPI_Shape> aBaseObject;
58 ResultPtr aContextRes = anObjRef->context();
59 if (aContextRes.get()) {
60 aBaseObject = aContextRes->shape();
62 if (!aBaseObject.get()) {
63 static const std::string aContextError = "The base selection context is bad";
64 setError(aContextError);
68 // Verify the attractive face
69 anObjRef = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(
70 data()->attribute(FeaturesPlugin_Placement::ATTRACT_OBJECT_ID()));
72 std::shared_ptr<GeomAPI_Shape> aSlaveShape =
73 std::dynamic_pointer_cast<GeomAPI_Shape>(anObjRef->value());
77 std::shared_ptr<GeomAPI_Shape> aSlaveObject;
78 aContextRes = anObjRef->context();
79 if (aContextRes.get()) {
80 aSlaveObject = aContextRes->shape();
82 if (!aSlaveObject.get()) {
83 static const std::string aContextError = "The tool selection context is bad";
84 setError(aContextError);
88 // Verify planarity of faces and linearity of edges
89 std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aBaseShape, aSlaveShape};
90 for (int i = 0; i < 2; i++) {
91 if (aShapes[i]->isFace()) {
92 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
93 if (!aFace->isPlanar()) {
94 static const std::string aPlanarityError = "One of selected faces is not planar";
95 setError(aPlanarityError);
99 else if (aShapes[i]->isEdge()) {
100 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
101 if (!anEdge->isLine()) {
102 static const std::string aLinearityError = "One of selected endges is not linear";
103 setError(aLinearityError);
109 // Flags of the Placement
110 AttributeBooleanPtr aBoolAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
111 data()->attribute(FeaturesPlugin_Placement::REVERSE_ID()));
112 bool isReverse = aBoolAttr->value();
113 aBoolAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
114 data()->attribute(FeaturesPlugin_Placement::CENTERING_ID()));
115 bool isCentering = aBoolAttr->value();
117 bool isPart = aContextRes->groupName() == ModelAPI_ResultPart::group();
119 std::shared_ptr<ModelAPI_ResultBody> aResultBody;
121 aResultBody = document()->createBody(data());
122 GeomAlgoAPI_Placement aFeature(
123 aSlaveObject, aBaseObject, aSlaveShape, aBaseShape, isReverse, isCentering, isPart);
124 if(!aFeature.isDone()) {
125 static const std::string aFeatureError = "Placement algorithm failed";
126 setError(aFeatureError);
130 // Check if shape is valid
131 if (aFeature.shape()->isNull()) {
132 static const std::string aShapeError = "Resulting shape is Null";
133 setError(aShapeError);
136 if(!aFeature.isValid()) {
137 std::string aFeatureError = "Warning: resulting shape is not valid";
138 setError(aFeatureError);
142 if (isPart) { // for part results just set transformation
143 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aContextRes);
144 ResultPartPtr aResultPart = document()->copyPart(firstResult(), anOrigin);
145 aResultPart->setTrsf(aContextRes, aFeature.transformation());
146 setResult(aResultPart);
149 LoadNamingDS(aFeature, aResultBody, aSlaveObject);
151 setResult(aResultBody);
155 //============================================================================
156 void FeaturesPlugin_Placement::LoadNamingDS(
157 GeomAlgoAPI_Placement& theFeature,
158 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
159 std::shared_ptr<GeomAPI_Shape> theSlaveObject)
162 theResultBody->storeModified(theSlaveObject, theFeature.shape()); // the initial Slave, the resulting Slave
164 GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
165 theFeature.mapOfShapes(*aSubShapes);
167 // put modifed faces in DF
168 std::string aModName = "Modified";
169 theResultBody->loadAndOrientModifiedShapes(theFeature.makeShape(), theSlaveObject, _FACE, _MODIFIEDF_TAG, aModName, *aSubShapes);