1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "FeaturesPlugin_Placement.h"
23 #include <ModelAPI_ResultConstruction.h>
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_ResultPart.h>
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_AttributeBoolean.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_BodyBuilder.h>
31 #include <GeomAPI_Edge.h>
32 #include <GeomAPI_Face.h>
33 #include <GeomAPI_Pln.h>
34 #include <GeomAPI_ShapeIterator.h>
35 #include <GeomAlgoAPI_Placement.h>
36 #include <GeomAlgoAPI_Transform.h>
38 #include <FeaturesPlugin_Tools.h>
40 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
44 void FeaturesPlugin_Placement::initAttributes()
47 AttributeSelectionListPtr aSelection =
48 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
49 OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
51 data()->addAttribute(START_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
52 data()->addAttribute(END_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
53 data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
54 data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
57 void FeaturesPlugin_Placement::execute()
60 ListOfShape anObjects;
61 std::list<ResultPtr> aContextes;
62 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
63 if(anObjectsSelList->size() == 0) {
66 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
67 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
68 anObjectsSelList->value(anObjectsIndex);
69 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
70 if(!anObject.get()) { // may be for not-activated parts
74 anObjects.push_back(anObject);
75 aContextes.push_back(anObjectAttr->context());
78 // Verify the start shape
79 AttributeSelectionPtr anObjRef = selection(START_SHAPE_ID());
83 std::shared_ptr<GeomAPI_Shape> aStartShape = anObjRef->value();
85 static const std::string aSelectionError = "Error: The start shape selection is bad.";
86 setError(aSelectionError);
91 std::shared_ptr<GeomAPI_Shape> aStartShapeContext;
92 ResultPtr aContextRes = anObjRef->context();
93 if (aContextRes.get()) {
94 aStartShapeContext = aContextRes->shape();
96 if(!aStartShapeContext.get()) {
97 static const std::string aContextError = "Error: The start shape selection context is bad.";
98 setError(aContextError);
102 // Verify the end shape
103 anObjRef = selection(END_SHAPE_ID());
104 std::shared_ptr<GeomAPI_Shape> anEndShape = anObjRef->value();
106 static const std::string aSelectionError = "Error: The end shape selection is bad.";
107 setError(aSelectionError);
111 std::shared_ptr<GeomAPI_Shape> anEndShapeContext;
112 aContextRes = anObjRef->context();
113 if(aContextRes.get()) {
114 anEndShapeContext = aContextRes->shape();
116 if(!anEndShapeContext.get()) {
117 static const std::string aContextError = "Error: The end shape selection context is bad.";
118 setError(aContextError);
122 // Verify planarity of faces and linearity of edges
123 std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
124 for (int i = 0; i < 2; i++) {
125 if (!isShapeValid(aShapes[i])) {
130 // Flags of the Placement
131 bool isReverse = boolean(REVERSE_ID())->value();
132 bool isCentering = boolean(CENTERING_ID())->value();
134 // Getting transformation.
135 GeomAlgoAPI_Placement aPlacementAlgo(
136 aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
137 if(!aPlacementAlgo.isDone()) {
138 static const std::string aFeatureError = "Error: Placement algorithm failed.";
139 setError(aFeatureError);
142 std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
144 // Applying transformation to each object.
145 int aResultIndex = 0;
146 std::list<ResultPtr>::iterator aContext = aContextes.begin();
147 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
148 anObjectsIt++, aContext++) {
150 // for part results just set transformation
151 if (aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group()) {
152 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
153 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
154 aResultPart->setTrsf(aContextRes, aTrsf);
155 setResult(aResultPart, aResultIndex);
157 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
158 GeomAlgoAPI_Transform aTransformAlgo(aBaseShape, aTrsf);
160 // Checking that the algorithm worked properly.
161 if(!aTransformAlgo.isDone()) {
162 static const std::string aFeatureError = "Error: Transform algorithm failed.";
163 setError(aFeatureError);
166 if(aTransformAlgo.shape()->isNull()) {
167 static const std::string aShapeError = "Error: Resulting shape is Null.";
168 setError(aShapeError);
171 if(!aTransformAlgo.isValid()) {
172 std::string aFeatureError = "Error: Resulting shape is not valid.";
173 setError(aFeatureError);
178 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
179 document()->createBody(data(), aResultIndex);
180 loadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
181 setResult(aResultBody, aResultIndex);
186 // Remove the rest results if there were produced in the previous pass.
187 removeResults(aResultIndex);
190 //==================================================================================================
191 bool FeaturesPlugin_Placement::isShapeValid(GeomShapePtr theShape)
193 if (theShape->isCompound()) {
194 GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
195 for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
196 GeomShapePtr aCurrentShape = anIt.current();
197 if (aShapeType == GeomAPI_Shape::SHAPE) {
198 aShapeType = aCurrentShape->shapeType();
200 else if (aShapeType != aCurrentShape->shapeType()) {
201 static const std::string aLinearityError =
202 "Error: Selected compound contains shapes with different types.";
203 setError(aLinearityError);
207 if (!isShapeValid(aCurrentShape)) {
212 else if (theShape->isFace()) {
213 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(theShape));
214 if (!aFace->isPlanar()) {
215 static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
216 setError(aPlanarityError);
220 else if (theShape->isEdge()) {
221 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
222 if (!anEdge->isLine()) {
223 static const std::string aLinearityError = "Error: One of selected edges is not linear.";
224 setError(aLinearityError);
232 //============================================================================
233 void FeaturesPlugin_Placement::loadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
234 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
235 std::shared_ptr<GeomAPI_Shape> theBaseShape)
238 theResultBody->storeModified(theBaseShape, theTransformAlgo.shape());
240 std::string aPlacedName = "Placed";
241 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfSubShapes();
243 FeaturesPlugin_Tools::storeModifiedShapes(theTransformAlgo, theResultBody,
244 theBaseShape, 1, 2, 3, aPlacedName,