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 <GeomAlgoAPI_Placement.h>
35 #include <GeomAlgoAPI_Transform.h>
37 #include <FeaturesPlugin_Tools.h>
39 FeaturesPlugin_Placement::FeaturesPlugin_Placement()
43 void FeaturesPlugin_Placement::initAttributes()
46 AttributeSelectionListPtr aSelection =
47 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
48 OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
50 data()->addAttribute(START_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
51 data()->addAttribute(END_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
52 data()->addAttribute(REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
53 data()->addAttribute(CENTERING_ID(), ModelAPI_AttributeBoolean::typeId());
56 void FeaturesPlugin_Placement::execute()
59 ListOfShape anObjects;
60 std::list<ResultPtr> aContextes;
61 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID());
62 if(anObjectsSelList->size() == 0) {
65 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
66 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
67 anObjectsSelList->value(anObjectsIndex);
68 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
69 if(!anObject.get()) { // may be for not-activated parts
73 anObjects.push_back(anObject);
74 aContextes.push_back(anObjectAttr->context());
77 // Verify the start shape
78 AttributeSelectionPtr anObjRef = selection(START_SHAPE_ID());
82 std::shared_ptr<GeomAPI_Shape> aStartShape = anObjRef->value();
84 static const std::string aSelectionError = "Error: The start shape selection is bad.";
85 setError(aSelectionError);
90 std::shared_ptr<GeomAPI_Shape> aStartShapeContext;
91 ResultPtr aContextRes = anObjRef->context();
92 if (aContextRes.get()) {
93 aStartShapeContext = aContextRes->shape();
95 if(!aStartShapeContext.get()) {
96 static const std::string aContextError = "Error: The start shape selection context is bad.";
97 setError(aContextError);
101 // Verify the end shape
102 anObjRef = selection(END_SHAPE_ID());
103 std::shared_ptr<GeomAPI_Shape> anEndShape = anObjRef->value();
105 static const std::string aSelectionError = "Error: The end shape selection is bad.";
106 setError(aSelectionError);
110 std::shared_ptr<GeomAPI_Shape> anEndShapeContext;
111 aContextRes = anObjRef->context();
112 if(aContextRes.get()) {
113 anEndShapeContext = aContextRes->shape();
115 if(!anEndShapeContext.get()) {
116 static const std::string aContextError = "Error: The end shape selection context is bad.";
117 setError(aContextError);
121 // Verify planarity of faces and linearity of edges
122 std::shared_ptr<GeomAPI_Shape> aShapes[2] = {aStartShape, anEndShape};
123 for (int i = 0; i < 2; i++) {
124 if (aShapes[i]->isFace()) {
125 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
126 if (!aFace->isPlanar()) {
127 static const std::string aPlanarityError = "Error: One of selected faces is not planar.";
128 setError(aPlanarityError);
132 else if (aShapes[i]->isEdge()) {
133 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
134 if (!anEdge->isLine()) {
135 static const std::string aLinearityError = "Error: One of selected endges is not linear.";
136 setError(aLinearityError);
142 // Flags of the Placement
143 bool isReverse = boolean(REVERSE_ID())->value();
144 bool isCentering = boolean(CENTERING_ID())->value();
146 // Getting transformation.
147 GeomAlgoAPI_Placement aPlacementAlgo(
148 aStartShapeContext, anEndShapeContext, aStartShape, anEndShape, isReverse, isCentering, true);
149 if(!aPlacementAlgo.isDone()) {
150 static const std::string aFeatureError = "Error: Placement algorithm failed.";
151 setError(aFeatureError);
154 std::shared_ptr<GeomAPI_Trsf> aTrsf = aPlacementAlgo.transformation();
156 // Applying transformation to each object.
157 int aResultIndex = 0;
158 std::list<ResultPtr>::iterator aContext = aContextes.begin();
159 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
160 anObjectsIt++, aContext++) {
162 // for part results just set transformation
163 if (aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group()) {
164 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
165 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
166 aResultPart->setTrsf(aContextRes, aTrsf);
167 setResult(aResultPart, aResultIndex);
169 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
170 GeomAlgoAPI_Transform aTransformAlgo(aBaseShape, aTrsf);
172 // Checking that the algorithm worked properly.
173 if(!aTransformAlgo.isDone()) {
174 static const std::string aFeatureError = "Error: Transform algorithm failed.";
175 setError(aFeatureError);
178 if(aTransformAlgo.shape()->isNull()) {
179 static const std::string aShapeError = "Error: Resulting shape is Null.";
180 setError(aShapeError);
183 if(!aTransformAlgo.isValid()) {
184 std::string aFeatureError = "Error: Resulting shape is not valid.";
185 setError(aFeatureError);
190 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
191 document()->createBody(data(), aResultIndex);
192 loadNamingDS(aTransformAlgo, aResultBody, aBaseShape);
193 setResult(aResultBody, aResultIndex);
198 // Remove the rest results if there were produced in the previous pass.
199 removeResults(aResultIndex);
202 //============================================================================
203 void FeaturesPlugin_Placement::loadNamingDS(GeomAlgoAPI_Transform& theTransformAlgo,
204 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
205 std::shared_ptr<GeomAPI_Shape> theBaseShape)
208 theResultBody->storeModified(theBaseShape, theTransformAlgo.shape());
210 std::string aPlacedName = "Placed";
211 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTransformAlgo.mapOfSubShapes();
213 FeaturesPlugin_Tools::storeModifiedShapes(theTransformAlgo, theResultBody,
214 theBaseShape, 1, 2, 3, aPlacedName,