1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "FeaturesPlugin_Placement.h"
22 #include <ModelAPI_ResultConstruction.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_ResultPart.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeBoolean.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_BodyBuilder.h>
29 #include <ModelAPI_Tools.h>
31 #include <GeomAPI_Edge.h>
32 #include <GeomAPI_Face.h>
33 #include <GeomAPI_Pln.h>
34 #include <GeomAPI_ShapeHierarchy.h>
35 #include <GeomAPI_ShapeIterator.h>
37 #include <GeomAlgoAPI_MakeShapeList.h>
38 #include <GeomAlgoAPI_Placement.h>
39 #include <GeomAlgoAPI_Transform.h>
40 #include <GeomAlgoAPI_Tools.h>
42 #include <FeaturesPlugin_Tools.h>
44 static const std::string PLACEMENT_VERSION_1("v9.5");
46 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
50 void FeaturesPlugin_Placement::initAttributes()
52 AttributeSelectionListPtr aSelection =
53 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
54 OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
56 data()->addAttribute(START_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
57 data()->addAttribute(END_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
58 data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
59 data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
61 if (!aSelection->isInitialized()) {
62 // new feature, not read from file
63 data()->setVersion(PLACEMENT_VERSION_1);
67 void FeaturesPlugin_Placement::execute()
69 bool isKeepSubShapes = data()->version() == PLACEMENT_VERSION_1;
72 GeomAPI_ShapeHierarchy anObjects;
73 std::list<ResultPtr> aParts;
74 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
75 if (!FeaturesPlugin_Tools::shapesFromSelectionList(
76 anObjectsSelList, isKeepSubShapes, anObjects, aParts))
79 // Verify the start shape
80 AttributeSelectionPtr anObjRef = selection(START_SHAPE_ID());
84 std::shared_ptr<GeomAPI_Shape> aStartShape = anObjRef->value();
86 static const std::string aSelectionError = "Error: The start shape selection is bad.";
87 setError(aSelectionError);
92 std::shared_ptr<GeomAPI_Shape> aStartShapeContext;
93 ResultPtr aContextRes = anObjRef->context();
94 if (aContextRes.get()) {
95 aStartShapeContext = aContextRes->shape();
97 if(!aStartShapeContext.get()) {
98 static const std::string aContextError = "Error: The start shape selection context is bad.";
99 setError(aContextError);
103 // Verify the end shape
104 anObjRef = selection(END_SHAPE_ID());
105 std::shared_ptr<GeomAPI_Shape> anEndShape = anObjRef->value();
107 static const std::string aSelectionError = "Error: The end shape selection is bad.";
108 setError(aSelectionError);
112 std::shared_ptr<GeomAPI_Shape> anEndShapeContext;
113 aContextRes = anObjRef->context();
114 if(aContextRes.get()) {
115 anEndShapeContext = aContextRes->shape();
117 if(!anEndShapeContext.get()) {
118 static const std::string aContextError = "Error: The end shape selection context is bad.";
119 setError(aContextError);
123 // Verify planarity of faces and linearity of edges
124 std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
125 for (int i = 0; i < 2; i++) {
126 if (!isShapeValid(aShapes[i])) {
131 // Flags of the Placement
132 bool isReverse = boolean(REVERSE_ID())->value();
133 bool isCentering = boolean(CENTERING_ID())->value();
135 // Getting transformation.
136 GeomAlgoAPI_Placement aPlacementAlgo(
137 aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
138 if(!aPlacementAlgo.isDone()) {
139 static const std::string aFeatureError = "Error: Placement algorithm failed.";
140 setError(aFeatureError);
143 std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
145 int aResultIndex = 0;
147 for (std::list<ResultPtr>::iterator aPRes = aParts.begin(); aPRes != aParts.end(); ++aPRes) {
148 // Applying transformation to each part.
149 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aPRes);
150 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
151 aResultPart->setTrsf(aContextRes, aTrsf);
152 setResult(aResultPart, aResultIndex++);
155 // Collect transformations for each object.
156 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
158 for(GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
159 anObjectsIt != anObjects.end(); anObjectsIt++) {
160 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
161 std::shared_ptr<GeomAlgoAPI_Transform> aTransformAlgo(
162 new GeomAlgoAPI_Transform(aBaseShape, aTrsf));
164 // Checking that the algorithm worked properly.
165 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTransformAlgo, getKind(), anError)) {
170 anObjects.markModified(aBaseShape, aTransformAlgo->shape());
171 aMakeShapeList->appendAlgo(aTransformAlgo);
174 // Build results of the operation.
175 const ListOfShape& anOriginalShapes = anObjects.objects();
176 ListOfShape aTopLevel;
177 anObjects.topLevelObjects(aTopLevel);
178 for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
180 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
181 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, anOriginalShapes, ListOfShape(),
182 aMakeShapeList, *anIt, "Placed");
183 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);