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()));
38 data()->addAttribute(START_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
39 data()->addAttribute(END_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
40 data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
41 data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
44 void FeaturesPlugin_Placement::execute()
47 ListOfShape anObjects;
48 std::list<ResultPtr> aContextes;
49 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
50 if(anObjectsSelList->size() == 0) {
53 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
54 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
55 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
56 if(!anObject.get()) { // may be for not-activated parts
60 anObjects.push_back(anObject);
61 aContextes.push_back(anObjectAttr->context());
64 // Verify the start shape
65 AttributeSelectionPtr anObjRef = selection(START_SHAPE_ID());
69 std::shared_ptr<GeomAPI_Shape> aStartShape = anObjRef->value();
71 static const std::string aSelectionError = "Error: The start shape selection is bad.";
72 setError(aSelectionError);
77 std::shared_ptr<GeomAPI_Shape> aStartShapeContext;
78 ResultPtr aContextRes = anObjRef->context();
79 if (aContextRes.get()) {
80 aStartShapeContext = aContextRes->shape();
82 if(!aStartShapeContext.get()) {
83 static const std::string aContextError = "Error: The start shape selection context is bad.";
84 setError(aContextError);
88 // Verify the end shape
89 anObjRef = selection(END_SHAPE_ID());
90 std::shared_ptr<GeomAPI_Shape> anEndShape = anObjRef->value();
92 static const std::string aSelectionError = "Error: The end shape selection is bad.";
93 setError(aSelectionError);
97 std::shared_ptr<GeomAPI_Shape> anEndShapeContext;
98 aContextRes = anObjRef->context();
99 if(aContextRes.get()) {
100 anEndShapeContext = aContextRes->shape();
102 if(!anEndShapeContext.get()) {
103 static const std::string aContextError = "Error: The end shape selection context is bad.";
104 setError(aContextError);
108 // Verify planarity of faces and linearity of edges
109 std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
110 for (int i = 0; i < 2; i++) {
111 if (aShapes[i]->isFace()) {
112 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
113 if (!aFace->isPlanar()) {
114 static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
115 setError(aPlanarityError);
119 else if (aShapes[i]->isEdge()) {
120 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
121 if (!anEdge->isLine()) {
122 static const std::string aLinearityError = "Error: One of selected endges is not linear.";
123 setError(aLinearityError);
129 // Flags of the Placement
130 bool isReverse = boolean(REVERSE_ID())->value();
131 bool isCentering = boolean(CENTERING_ID())->value();
133 // Getting transformation.
134 GeomAlgoAPI_Placement aPlacementAlgo(
135 aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
136 if(!aPlacementAlgo.isDone()) {
137 static const std::string aFeatureError = "Error: Placement algorithm failed.";
138 setError(aFeatureError);
141 std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
143 // Applying transformation to each object.
144 int aResultIndex = 0;
145 std::list<ResultPtr>::iterator aContext = aContextes.begin();
146 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
147 anObjectsIt++, aContext++) {
149 if ((*aContext)->groupName() == ModelAPI_ResultPart::group()) { // for part results just set transformation
150 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
151 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
152 aResultPart->setTrsf(aContextRes, aTrsf);
153 setResult(aResultPart, aResultIndex);
155 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
156 GeomAlgoAPI_Transform aTransformAlgo(aBaseShape, aTrsf);
158 // Checking that the algorithm worked properly.
159 if(!aTransformAlgo.isDone()) {
160 static const std::string aFeatureError = "Error: Transform algorithm failed.";
161 setError(aFeatureError);
164 if(aTransformAlgo.shape()->isNull()) {
165 static const std::string aShapeError = "Error: Resulting shape is Null.";
166 setError(aShapeError);
169 if(!aTransformAlgo.isValid()) {
170 std::string aFeatureError = "Error: Resulting shape is not valid.";
171 setError(aFeatureError);
176 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
177 LoadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
178 setResult(aResultBody, aResultIndex);
183 // Remove the rest results if there were produced in the previous pass.
184 removeResults(aResultIndex);
187 //============================================================================
188 void FeaturesPlugin_Placement::LoadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
189 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
190 std::shared_ptr<GeomAPI_Shape> theSlaveObject)
193 theResultBody->storeModified(theSlaveObject, theTransformAlgo.shape()); // the initial Slave, the resulting Slave
195 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfSubShapes();
197 // put modifed faces in DF
198 std::string aModName = "Modified";
199 theResultBody->loadAndOrientModifiedShapes(&theTransformAlgo,
200 theSlaveObject, _FACE,
201 _MODIFIEDF_TAG, aModName, *aSubShapes.get());