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>
15 #include <ModelAPI_BodyBuilder.h>
17 #include <GeomAPI_Edge.h>
18 #include <GeomAPI_Face.h>
19 #include <GeomAPI_Pln.h>
20 #include <GeomAlgoAPI_Placement.h>
21 #include <GeomAlgoAPI_Transform.h>
23 #define _MODIFIEDF_TAG 1
24 #define _MODIFIEDE_TAG 2
25 #define _MODIFIEDV_TAG 3
27 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
31 void FeaturesPlugin_Placement::initAttributes()
34 AttributeSelectionListPtr aSelection =
35 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
36 OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
37 // extrusion works with faces always
38 aSelection->setSelectionType("SOLID");
40 data()->addAttribute(START_FACE_ID(), ModelAPI_AttributeSelection::typeId());
41 data()->addAttribute(END_FACE_ID(), ModelAPI_AttributeSelection::typeId());
42 data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
43 data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
46 void FeaturesPlugin_Placement::execute()
49 ListOfShape anObjects;
50 std::list<ResultPtr> aContextes;
51 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
52 if(anObjectsSelList->size() == 0) {
55 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
56 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
57 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
61 anObjects.push_back(anObject);
62 aContextes.push_back(anObjectAttr->context());
65 // Verify the start face
66 AttributeSelectionPtr anObjRef = selection(START_FACE_ID());
70 std::shared_ptr<GeomAPI_Shape> aStartFace = anObjRef->value();
71 if(!aStartFace || !GeomAPI_Face(aStartFace).isPlanar()) {
72 static const std::string aSelectionError = "The start face selection is bad";
73 setError(aSelectionError);
78 std::shared_ptr<GeomAPI_Shape> aStartShape;
79 ResultPtr aContextRes = anObjRef->context();
80 if (aContextRes.get()) {
81 aStartShape = aContextRes->shape();
83 if(!aStartShape.get()) {
84 static const std::string aContextError = "The start face selection context is bad";
85 setError(aContextError);
89 // Verify the end face
90 anObjRef = selection(END_FACE_ID());
91 std::shared_ptr<GeomAPI_Shape> anEndFace = anObjRef->value();
92 if(!anEndFace || !GeomAPI_Face(anEndFace).isPlanar()) {
93 static const std::string aSelectionError = "The end face selection is bad";
94 setError(aSelectionError);
98 std::shared_ptr<GeomAPI_Shape> anEndShape;
99 aContextRes = anObjRef->context();
100 if(aContextRes.get()) {
101 anEndShape = aContextRes->shape();
103 if(!anEndShape.get()) {
104 static const std::string aContextError = "The end face selection context is bad";
105 setError(aContextError);
109 // Flags of the Placement
110 bool isReverse = boolean(REVERSE_ID())->value();
111 bool isCentering = boolean(CENTERING_ID())->value();
113 bool isPart = aContextRes->groupName() == ModelAPI_ResultPart::group();
115 // Getting transformation.
116 GeomAlgoAPI_Placement aPlacementAlgo(
117 aStartShape, anEndShape, aStartFace, anEndFace, isReverse, isCentering, true);
118 if(!aPlacementAlgo.isDone()) {
119 static const std::string aFeatureError = "Placement algorithm failed";
120 setError(aFeatureError);
123 std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
125 // Applying transformation to each object.
126 int aResultIndex = 0;
127 std::list<ResultPtr>::iterator aContext = aContextes.begin();
128 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
129 anObjectsIt++, aContext++) {
131 if (isPart) { // for part results just set transformation
132 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aContextRes);
133 ResultPartPtr aResultPart = document()->copyPart(firstResult(), anOrigin);
134 aResultPart->setTrsf(aContextRes, aTrsf);
135 setResult(aResultPart);
137 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
138 GeomAlgoAPI_Transform aTransformAlgo(aBaseShape, aTrsf);
140 // Checking that the algorithm worked properly.
141 if(!aTransformAlgo.isDone()) {
142 static const std::string aFeatureError = "Transform algorithm failed";
143 setError(aFeatureError);
146 if(aTransformAlgo.shape()->isNull()) {
147 static const std::string aShapeError = "Resulting shape is Null";
148 setError(aShapeError);
151 if(!aTransformAlgo.isValid()) {
152 std::string aFeatureError = "Warning: resulting shape is not valid";
153 setError(aFeatureError);
158 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
159 LoadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
160 setResult(aResultBody, aResultIndex);
165 // Remove the rest results if there were produced in the previous pass.
166 removeResults(aResultIndex);
169 //============================================================================
170 void FeaturesPlugin_Placement::LoadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
171 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
172 std::shared_ptr<GeomAPI_Shape> theSlaveObject)
174 ModelAPI_BodyBuilder* aResultBuilder = theResultBody->getBodyBuilder();
176 aResultBuilder->storeModified(theSlaveObject, theTransformAlgo.shape()); // the initial Slave, the resulting Slave
178 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfShapes();
180 // put modifed faces in DF
181 std::string aModName = "Modified";
182 aResultBuilder->loadAndOrientModifiedShapes(theTransformAlgo.makeShape().get(),
183 theSlaveObject, _FACE,
184 _MODIFIEDF_TAG, aModName, *aSubShapes.get());