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 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
27 void FeaturesPlugin_Placement::initAttributes()
30 AttributeSelectionListPtr aSelection =
31 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
32 OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
34 data()->addAttribute(START_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
35 data()->addAttribute(END_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
36 data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
37 data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
40 void FeaturesPlugin_Placement::execute()
43 ListOfShape anObjects;
44 std::list<ResultPtr> aContextes;
45 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
46 if(anObjectsSelList->size() == 0) {
49 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
50 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
51 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
52 if(!anObject.get()) { // may be for not-activated parts
56 anObjects.push_back(anObject);
57 aContextes.push_back(anObjectAttr->context());
60 // Verify the start shape
61 AttributeSelectionPtr anObjRef = selection(START_SHAPE_ID());
65 std::shared_ptr<GeomAPI_Shape> aStartShape = anObjRef->value();
67 static const std::string aSelectionError = "Error: The start shape selection is bad.";
68 setError(aSelectionError);
73 std::shared_ptr<GeomAPI_Shape> aStartShapeContext;
74 ResultPtr aContextRes = anObjRef->context();
75 if (aContextRes.get()) {
76 aStartShapeContext = aContextRes->shape();
78 if(!aStartShapeContext.get()) {
79 static const std::string aContextError = "Error: The start shape selection context is bad.";
80 setError(aContextError);
84 // Verify the end shape
85 anObjRef = selection(END_SHAPE_ID());
86 std::shared_ptr<GeomAPI_Shape> anEndShape = anObjRef->value();
88 static const std::string aSelectionError = "Error: The end shape selection is bad.";
89 setError(aSelectionError);
93 std::shared_ptr<GeomAPI_Shape> anEndShapeContext;
94 aContextRes = anObjRef->context();
95 if(aContextRes.get()) {
96 anEndShapeContext = aContextRes->shape();
98 if(!anEndShapeContext.get()) {
99 static const std::string aContextError = "Error: The end shape selection context is bad.";
100 setError(aContextError);
104 // Verify planarity of faces and linearity of edges
105 std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
106 for (int i = 0; i < 2; i++) {
107 if (aShapes[i]->isFace()) {
108 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
109 if (!aFace->isPlanar()) {
110 static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
111 setError(aPlanarityError);
115 else if (aShapes[i]->isEdge()) {
116 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
117 if (!anEdge->isLine()) {
118 static const std::string aLinearityError = "Error: One of selected endges is not linear.";
119 setError(aLinearityError);
125 // Flags of the Placement
126 bool isReverse = boolean(REVERSE_ID())->value();
127 bool isCentering = boolean(CENTERING_ID())->value();
129 // Getting transformation.
130 GeomAlgoAPI_Placement aPlacementAlgo(
131 aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
132 if(!aPlacementAlgo.isDone()) {
133 static const std::string aFeatureError = "Error: Placement algorithm failed.";
134 setError(aFeatureError);
137 std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
139 // Applying transformation to each object.
140 int aResultIndex = 0;
141 std::list<ResultPtr>::iterator aContext = aContextes.begin();
142 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
143 anObjectsIt++, aContext++) {
145 if ((*aContext)->groupName() == ModelAPI_ResultPart::group()) { // for part results just set transformation
146 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
147 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
148 aResultPart->setTrsf(aContextRes, aTrsf);
149 setResult(aResultPart, aResultIndex);
151 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
152 GeomAlgoAPI_Transform aTransformAlgo(aBaseShape, aTrsf);
154 // Checking that the algorithm worked properly.
155 if(!aTransformAlgo.isDone()) {
156 static const std::string aFeatureError = "Error: Transform algorithm failed.";
157 setError(aFeatureError);
160 if(aTransformAlgo.shape()->isNull()) {
161 static const std::string aShapeError = "Error: Resulting shape is Null.";
162 setError(aShapeError);
165 if(!aTransformAlgo.isValid()) {
166 std::string aFeatureError = "Error: Resulting shape is not valid.";
167 setError(aFeatureError);
172 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
173 loadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
174 setResult(aResultBody, aResultIndex);
179 // Remove the rest results if there were produced in the previous pass.
180 removeResults(aResultIndex);
183 //============================================================================
184 void FeaturesPlugin_Placement::loadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
185 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
186 std::shared_ptr<GeomAPI_Shape> theBaseShape)
189 theResultBody->storeModified(theBaseShape, theTransformAlgo.shape());
191 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfSubShapes();
193 // put modifed faces in DF
195 std::string aPlacedName = "Placed";
197 switch(theBaseShape->shapeType()) {
198 case GeomAPI_Shape::COMPOUND:
199 case GeomAPI_Shape::COMPSOLID:
200 case GeomAPI_Shape::SOLID:
201 case GeomAPI_Shape::SHELL:
202 theResultBody->loadAndOrientModifiedShapes(&theTransformAlgo,
203 theBaseShape, GeomAPI_Shape::FACE,
204 aPlacedTag, aPlacedName + "_Face", *aSubShapes.get());
205 case GeomAPI_Shape::FACE:
206 case GeomAPI_Shape::WIRE:
207 theResultBody->loadAndOrientModifiedShapes(&theTransformAlgo,
208 theBaseShape, GeomAPI_Shape::EDGE,
209 ++aPlacedTag, aPlacedName + "_Edge", *aSubShapes.get());
210 case GeomAPI_Shape::EDGE:
211 theResultBody->loadAndOrientModifiedShapes(&theTransformAlgo,
212 theBaseShape, GeomAPI_Shape::VERTEX,
213 ++aPlacedTag, aPlacedName + "_Vertex", *aSubShapes.get());