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 #include <FeaturesPlugin_Tools.h>
25 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
29 void FeaturesPlugin_Placement::initAttributes()
32 AttributeSelectionListPtr aSelection =
33 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
34 OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
36 data()->addAttribute(START_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
37 data()->addAttribute(END_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
38 data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
39 data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
42 void FeaturesPlugin_Placement::execute()
45 ListOfShape anObjects;
46 std::list<ResultPtr> aContextes;
47 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
48 if(anObjectsSelList->size() == 0) {
51 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
52 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
53 anObjectsSelList->value(anObjectsIndex);
54 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
55 if(!anObject.get()) { // may be for not-activated parts
59 anObjects.push_back(anObject);
60 aContextes.push_back(anObjectAttr->context());
63 // Verify the start shape
64 AttributeSelectionPtr anObjRef = selection(START_SHAPE_ID());
68 std::shared_ptr<GeomAPI_Shape> aStartShape = anObjRef->value();
70 static const std::string aSelectionError = "Error: The start shape selection is bad.";
71 setError(aSelectionError);
76 std::shared_ptr<GeomAPI_Shape> aStartShapeContext;
77 ResultPtr aContextRes = anObjRef->context();
78 if (aContextRes.get()) {
79 aStartShapeContext = aContextRes->shape();
81 if(!aStartShapeContext.get()) {
82 static const std::string aContextError = "Error: The start shape selection context is bad.";
83 setError(aContextError);
87 // Verify the end shape
88 anObjRef = selection(END_SHAPE_ID());
89 std::shared_ptr<GeomAPI_Shape> anEndShape = anObjRef->value();
91 static const std::string aSelectionError = "Error: The end shape selection is bad.";
92 setError(aSelectionError);
96 std::shared_ptr<GeomAPI_Shape> anEndShapeContext;
97 aContextRes = anObjRef->context();
98 if(aContextRes.get()) {
99 anEndShapeContext = aContextRes->shape();
101 if(!anEndShapeContext.get()) {
102 static const std::string aContextError = "Error: The end shape selection context is bad.";
103 setError(aContextError);
107 // Verify planarity of faces and linearity of edges
108 std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
109 for (int i = 0; i < 2; i++) {
110 if (aShapes[i]->isFace()) {
111 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
112 if (!aFace->isPlanar()) {
113 static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
114 setError(aPlanarityError);
118 else if (aShapes[i]->isEdge()) {
119 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
120 if (!anEdge->isLine()) {
121 static const std::string aLinearityError = "Error: One of selected endges is not linear.";
122 setError(aLinearityError);
128 // Flags of the Placement
129 bool isReverse = boolean(REVERSE_ID())->value();
130 bool isCentering = boolean(CENTERING_ID())->value();
132 // Getting transformation.
133 GeomAlgoAPI_Placement aPlacementAlgo(
134 aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
135 if(!aPlacementAlgo.isDone()) {
136 static const std::string aFeatureError = "Error: Placement algorithm failed.";
137 setError(aFeatureError);
140 std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
142 // Applying transformation to each object.
143 int aResultIndex = 0;
144 std::list<ResultPtr>::iterator aContext = aContextes.begin();
145 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
146 anObjectsIt++, aContext++) {
148 // for part results just set transformation
149 if ((*aContext)->groupName() == ModelAPI_ResultPart::group()) {
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 =
177 document()->createBody(data(), aResultIndex);
178 loadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
179 setResult(aResultBody, aResultIndex);
184 // Remove the rest results if there were produced in the previous pass.
185 removeResults(aResultIndex);
188 //============================================================================
189 void FeaturesPlugin_Placement::loadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
190 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
191 std::shared_ptr<GeomAPI_Shape> theBaseShape)
194 theResultBody->storeModified(theBaseShape, theTransformAlgo.shape());
196 std::string aPlacedName = "Placed";
197 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfSubShapes();
199 FeaturesPlugin_Tools::storeModifiedShapes(theTransformAlgo, theResultBody,
200 theBaseShape, 1, 2, 3, aPlacedName,