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 =
51 anObjectsSelList->value(anObjectsIndex);
52 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
53 if(!anObject.get()) { // may be for not-activated parts
57 anObjects.push_back(anObject);
58 aContextes.push_back(anObjectAttr->context());
61 // Verify the start shape
62 AttributeSelectionPtr anObjRef = selection(START_SHAPE_ID());
66 std::shared_ptr<GeomAPI_Shape> aStartShape = anObjRef->value();
68 static const std::string aSelectionError = "Error: The start shape selection is bad.";
69 setError(aSelectionError);
74 std::shared_ptr<GeomAPI_Shape> aStartShapeContext;
75 ResultPtr aContextRes = anObjRef->context();
76 if (aContextRes.get()) {
77 aStartShapeContext = aContextRes->shape();
79 if(!aStartShapeContext.get()) {
80 static const std::string aContextError = "Error: The start shape selection context is bad.";
81 setError(aContextError);
85 // Verify the end shape
86 anObjRef = selection(END_SHAPE_ID());
87 std::shared_ptr<GeomAPI_Shape> anEndShape = anObjRef->value();
89 static const std::string aSelectionError = "Error: The end shape selection is bad.";
90 setError(aSelectionError);
94 std::shared_ptr<GeomAPI_Shape> anEndShapeContext;
95 aContextRes = anObjRef->context();
96 if(aContextRes.get()) {
97 anEndShapeContext = aContextRes->shape();
99 if(!anEndShapeContext.get()) {
100 static const std::string aContextError = "Error: The end shape selection context is bad.";
101 setError(aContextError);
105 // Verify planarity of faces and linearity of edges
106 std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
107 for (int i = 0; i < 2; i++) {
108 if (aShapes[i]->isFace()) {
109 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
110 if (!aFace->isPlanar()) {
111 static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
112 setError(aPlanarityError);
116 else if (aShapes[i]->isEdge()) {
117 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
118 if (!anEdge->isLine()) {
119 static const std::string aLinearityError = "Error: One of selected endges is not linear.";
120 setError(aLinearityError);
126 // Flags of the Placement
127 bool isReverse = boolean(REVERSE_ID())->value();
128 bool isCentering = boolean(CENTERING_ID())->value();
130 // Getting transformation.
131 GeomAlgoAPI_Placement aPlacementAlgo(
132 aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
133 if(!aPlacementAlgo.isDone()) {
134 static const std::string aFeatureError = "Error: Placement algorithm failed.";
135 setError(aFeatureError);
138 std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
140 // Applying transformation to each object.
141 int aResultIndex = 0;
142 std::list<ResultPtr>::iterator aContext = aContextes.begin();
143 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
144 anObjectsIt++, aContext++) {
146 // for part results just set transformation
147 if ((*aContext)->groupName() == ModelAPI_ResultPart::group()) {
148 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
149 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
150 aResultPart->setTrsf(aContextRes, aTrsf);
151 setResult(aResultPart, aResultIndex);
153 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
154 GeomAlgoAPI_Transform aTransformAlgo(aBaseShape, aTrsf);
156 // Checking that the algorithm worked properly.
157 if(!aTransformAlgo.isDone()) {
158 static const std::string aFeatureError = "Error: Transform algorithm failed.";
159 setError(aFeatureError);
162 if(aTransformAlgo.shape()->isNull()) {
163 static const std::string aShapeError = "Error: Resulting shape is Null.";
164 setError(aShapeError);
167 if(!aTransformAlgo.isValid()) {
168 std::string aFeatureError = "Error: Resulting shape is not valid.";
169 setError(aFeatureError);
174 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
175 document()->createBody(data(), aResultIndex);
176 loadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
177 setResult(aResultBody, aResultIndex);
182 // Remove the rest results if there were produced in the previous pass.
183 removeResults(aResultIndex);
186 //============================================================================
187 void FeaturesPlugin_Placement::loadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
188 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
189 std::shared_ptr<GeomAPI_Shape> theBaseShape)
192 theResultBody->storeModified(theBaseShape, theTransformAlgo.shape());
194 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfSubShapes();
196 // put modifed faces in DF
198 std::string aPlacedName = "Placed";
200 switch(theBaseShape->shapeType()) {
201 case GeomAPI_Shape::COMPOUND:
202 case GeomAPI_Shape::COMPSOLID:
203 case GeomAPI_Shape::SOLID:
204 case GeomAPI_Shape::SHELL:
205 theResultBody->loadAndOrientModifiedShapes(&theTransformAlgo,
206 theBaseShape, GeomAPI_Shape::FACE,
207 aPlacedTag, aPlacedName + "_Face", *aSubShapes.get());
208 case GeomAPI_Shape::FACE:
209 case GeomAPI_Shape::WIRE:
210 theResultBody->loadAndOrientModifiedShapes(&theTransformAlgo,
211 theBaseShape, GeomAPI_Shape::EDGE,
212 ++aPlacedTag, aPlacedName + "_Edge", *aSubShapes.get());
213 case GeomAPI_Shape::EDGE:
214 theResultBody->loadAndOrientModifiedShapes(&theTransformAlgo,
215 theBaseShape, GeomAPI_Shape::VERTEX,
216 ++aPlacedTag, aPlacedName + "_Vertex", *aSubShapes.get());