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 email : webmaster.salome@opencascade.com<mailto: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>
30 #include <GeomAPI_Edge.h>
31 #include <GeomAPI_Face.h>
32 #include <GeomAPI_Pln.h>
33 #include <GeomAlgoAPI_Placement.h>
34 #include <GeomAlgoAPI_Transform.h>
36 #include <FeaturesPlugin_Tools.h>
38 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
42 void FeaturesPlugin_Placement::initAttributes()
45 AttributeSelectionListPtr aSelection =
46 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
47 OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
49 data()->addAttribute(START_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
50 data()->addAttribute(END_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
51 data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
52 data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
55 void FeaturesPlugin_Placement::execute()
58 ListOfShape anObjects;
59 std::list<ResultPtr> aContextes;
60 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
61 if(anObjectsSelList->size() == 0) {
64 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
65 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
66 anObjectsSelList->value(anObjectsIndex);
67 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
68 if(!anObject.get()) { // may be for not-activated parts
72 anObjects.push_back(anObject);
73 aContextes.push_back(anObjectAttr->context());
76 // Verify the start shape
77 AttributeSelectionPtr anObjRef = selection(START_SHAPE_ID());
81 std::shared_ptr<GeomAPI_Shape> aStartShape = anObjRef->value();
83 static const std::string aSelectionError = "Error: The start shape selection is bad.";
84 setError(aSelectionError);
89 std::shared_ptr<GeomAPI_Shape> aStartShapeContext;
90 ResultPtr aContextRes = anObjRef->context();
91 if (aContextRes.get()) {
92 aStartShapeContext = aContextRes->shape();
94 if(!aStartShapeContext.get()) {
95 static const std::string aContextError = "Error: The start shape selection context is bad.";
96 setError(aContextError);
100 // Verify the end shape
101 anObjRef = selection(END_SHAPE_ID());
102 std::shared_ptr<GeomAPI_Shape> anEndShape = anObjRef->value();
104 static const std::string aSelectionError = "Error: The end shape selection is bad.";
105 setError(aSelectionError);
109 std::shared_ptr<GeomAPI_Shape> anEndShapeContext;
110 aContextRes = anObjRef->context();
111 if(aContextRes.get()) {
112 anEndShapeContext = aContextRes->shape();
114 if(!anEndShapeContext.get()) {
115 static const std::string aContextError = "Error: The end shape selection context is bad.";
116 setError(aContextError);
120 // Verify planarity of faces and linearity of edges
121 std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
122 for (int i = 0; i < 2; i++) {
123 if (aShapes[i]->isFace()) {
124 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
125 if (!aFace->isPlanar()) {
126 static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
127 setError(aPlanarityError);
131 else if (aShapes[i]->isEdge()) {
132 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
133 if (!anEdge->isLine()) {
134 static const std::string aLinearityError = "Error: One of selected endges is not linear.";
135 setError(aLinearityError);
141 // Flags of the Placement
142 bool isReverse = boolean(REVERSE_ID())->value();
143 bool isCentering = boolean(CENTERING_ID())->value();
145 // Getting transformation.
146 GeomAlgoAPI_Placement aPlacementAlgo(
147 aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
148 if(!aPlacementAlgo.isDone()) {
149 static const std::string aFeatureError = "Error: Placement algorithm failed.";
150 setError(aFeatureError);
153 std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
155 // Applying transformation to each object.
156 int aResultIndex = 0;
157 std::list<ResultPtr>::iterator aContext = aContextes.begin();
158 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
159 anObjectsIt++, aContext++) {
161 // for part results just set transformation
162 if ((*aContext)->groupName() == ModelAPI_ResultPart::group()) {
163 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
164 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
165 aResultPart->setTrsf(aContextRes, aTrsf);
166 setResult(aResultPart, aResultIndex);
168 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
169 GeomAlgoAPI_Transform aTransformAlgo(aBaseShape, aTrsf);
171 // Checking that the algorithm worked properly.
172 if(!aTransformAlgo.isDone()) {
173 static const std::string aFeatureError = "Error: Transform algorithm failed.";
174 setError(aFeatureError);
177 if(aTransformAlgo.shape()->isNull()) {
178 static const std::string aShapeError = "Error: Resulting shape is Null.";
179 setError(aShapeError);
182 if(!aTransformAlgo.isValid()) {
183 std::string aFeatureError = "Error: Resulting shape is not valid.";
184 setError(aFeatureError);
189 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
190 document()->createBody(data(), aResultIndex);
191 loadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
192 setResult(aResultBody, aResultIndex);
197 // Remove the rest results if there were produced in the previous pass.
198 removeResults(aResultIndex);
201 //============================================================================
202 void FeaturesPlugin_Placement::loadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
203 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
204 std::shared_ptr<GeomAPI_Shape> theBaseShape)
207 theResultBody->storeModified(theBaseShape, theTransformAlgo.shape());
209 std::string aPlacedName = "Placed";
210 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfSubShapes();
212 FeaturesPlugin_Tools::storeModifiedShapes(theTransformAlgo, theResultBody,
213 theBaseShape, 1, 2, 3, aPlacedName,