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>
20 #include <GeomAlgoAPI_Transform.h>
22 #define _MODIFIEDF_TAG 1
23 #define _MODIFIEDE_TAG 2
24 #define _MODIFIEDV_TAG 3
26 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
30 void FeaturesPlugin_Placement::initAttributes()
33 AttributeSelectionListPtr aSelection =
34 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
35 OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
36 // extrusion works with faces always
37 aSelection->setSelectionType("SOLID");
39 data()->addAttribute(START_FACE_ID(), ModelAPI_AttributeSelection::typeId());
40 data()->addAttribute(END_FACE_ID(), ModelAPI_AttributeSelection::typeId());
41 data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
42 data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
45 void FeaturesPlugin_Placement::execute()
48 ListOfShape anObjects;
49 std::list<ResultPtr> aContextes;
50 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
51 if(anObjectsSelList->size() == 0) {
54 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
55 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
56 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
60 anObjects.push_back(anObject);
61 aContextes.push_back(anObjectAttr->context());
64 // Verify the start face
65 AttributeSelectionPtr anObjRef = selection(START_FACE_ID());
69 std::shared_ptr<GeomAPI_Shape> aStartFace = anObjRef->value();
70 if(!aStartFace || !GeomAPI_Face(aStartFace).isPlanar()) {
71 static const std::string aSelectionError = "The start face selection is bad";
72 setError(aSelectionError);
77 std::shared_ptr<GeomAPI_Shape> aStartShape;
78 ResultPtr aContextRes = anObjRef->context();
79 if (aContextRes.get()) {
80 aStartShape = aContextRes->shape();
82 if(!aStartShape.get()) {
83 static const std::string aContextError = "The start face selection context is bad";
84 setError(aContextError);
88 // Verify the end face
89 anObjRef = selection(END_FACE_ID());
90 std::shared_ptr<GeomAPI_Shape> anEndFace = anObjRef->value();
91 if(!anEndFace || !GeomAPI_Face(anEndFace).isPlanar()) {
92 static const std::string aSelectionError = "The end face selection is bad";
93 setError(aSelectionError);
97 std::shared_ptr<GeomAPI_Shape> anEndShape;
98 aContextRes = anObjRef->context();
99 if(aContextRes.get()) {
100 anEndShape = aContextRes->shape();
102 if(!anEndShape.get()) {
103 static const std::string aContextError = "The end face selection context is bad";
104 setError(aContextError);
108 // Flags of the Placement
109 bool isReverse = boolean(REVERSE_ID())->value();
110 bool isCentering = boolean(CENTERING_ID())->value();
112 bool isPart = aContextRes->groupName() == ModelAPI_ResultPart::group();
114 // Getting transformation.
115 GeomAlgoAPI_Placement aPlacementAlgo(
116 aStartShape, anEndShape, aStartFace, anEndFace, isReverse, isCentering, true);
117 if(!aPlacementAlgo.isDone()) {
118 static const std::string aFeatureError = "Placement algorithm failed";
119 setError(aFeatureError);
122 std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
124 // Applying transformation to each object.
125 int aResultIndex = 0;
126 std::list<ResultPtr>::iterator aContext = aContextes.begin();
127 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
128 anObjectsIt++, aContext++) {
130 if (isPart) { // for part results just set transformation
131 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aContextRes);
132 ResultPartPtr aResultPart = document()->copyPart(firstResult(), anOrigin);
133 aResultPart->setTrsf(aContextRes, aTrsf);
134 setResult(aResultPart);
136 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
137 GeomAlgoAPI_Transform aTransformAlgo(aBaseShape, aTrsf);
139 // Checking that the algorithm worked properly.
140 if(!aTransformAlgo.isDone()) {
141 static const std::string aFeatureError = "Transform algorithm failed";
142 setError(aFeatureError);
145 if(aTransformAlgo.shape()->isNull()) {
146 static const std::string aShapeError = "Resulting shape is Null";
147 setError(aShapeError);
150 if(!aTransformAlgo.isValid()) {
151 std::string aFeatureError = "Warning: resulting shape is not valid";
152 setError(aFeatureError);
157 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
158 LoadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
159 setResult(aResultBody, aResultIndex);
164 // Remove the rest results if there were produced in the previous pass.
165 removeResults(aResultIndex);
168 //============================================================================
169 void FeaturesPlugin_Placement::LoadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
170 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
171 std::shared_ptr<GeomAPI_Shape> theSlaveObject)
174 theResultBody->storeModified(theSlaveObject, theTransformAlgo.shape()); // the initial Slave, the resulting Slave
176 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfShapes();
178 // put modifed faces in DF
179 std::string aModName = "Modified";
180 theResultBody->loadAndOrientModifiedShapes(theTransformAlgo.makeShape().get(),
181 theSlaveObject, _FACE,
182 _MODIFIEDF_TAG, aModName, *aSubShapes.get());