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_Symmetry.h>
23 #include <GeomAlgoAPI_PointBuilder.h>
25 #include <GeomAPI_Edge.h>
26 #include <GeomAPI_Face.h>
27 #include <GeomAPI_Lin.h>
28 #include <GeomAPI_Pln.h>
29 #include <GeomAPI_Trsf.h>
31 #include <ModelAPI_AttributeSelectionList.h>
32 #include <ModelAPI_AttributeString.h>
33 #include <ModelAPI_ResultBody.h>
34 #include <ModelAPI_ResultPart.h>
37 #include <GeomAlgoAPI_FaceBuilder.h>
39 //=================================================================================================
40 FeaturesPlugin_Symmetry::FeaturesPlugin_Symmetry()
44 //=================================================================================================
45 void FeaturesPlugin_Symmetry::initAttributes()
47 data()->addAttribute(FeaturesPlugin_Symmetry::CREATION_METHOD(),
48 ModelAPI_AttributeString::typeId());
50 AttributeSelectionListPtr aSelection =
51 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
52 FeaturesPlugin_Symmetry::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
54 data()->addAttribute(FeaturesPlugin_Symmetry::POINT_OBJECT_ID(),
55 ModelAPI_AttributeSelection::typeId());
57 data()->addAttribute(FeaturesPlugin_Symmetry::AXIS_OBJECT_ID(),
58 ModelAPI_AttributeSelection::typeId());
60 data()->addAttribute(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID(),
61 ModelAPI_AttributeSelection::typeId());
64 //=================================================================================================
65 void FeaturesPlugin_Symmetry::execute()
67 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Symmetry::CREATION_METHOD());
68 std::string aMethodType = aMethodTypeAttr->value();
70 if (aMethodType == CREATION_METHOD_BY_POINT()) {
71 performSymmetryByPoint();
74 if (aMethodType == CREATION_METHOD_BY_AXIS()) {
75 performSymmetryByAxis();
78 if (aMethodType == CREATION_METHOD_BY_PLANE()) {
79 performSymmetryByPlane();
83 //=================================================================================================
84 void FeaturesPlugin_Symmetry::performSymmetryByPoint()
87 ListOfShape anObjects;
88 std::list<ResultPtr> aContextes;
89 AttributeSelectionListPtr anObjectsSelList =
90 selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
91 if (anObjectsSelList->size() == 0) {
94 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
95 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
96 anObjectsSelList->value(anObjectsIndex);
97 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
98 if(!anObject.get()) { // may be for not-activated parts
102 anObjects.push_back(anObject);
103 aContextes.push_back(anObjectAttr->context());
107 std::shared_ptr<GeomAPI_Pnt> aPoint;
108 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
109 selection(FeaturesPlugin_Symmetry::POINT_OBJECT_ID());
110 if (anObjRef.get() != NULL) {
111 GeomShapePtr aShape1 = anObjRef->value();
112 if (!aShape1.get()) {
113 aShape1 = anObjRef->context()->shape();
116 aPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
120 // Moving each object.
121 int aResultIndex = 0;
122 std::list<ResultPtr>::iterator aContext = aContextes.begin();
123 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
124 anObjectsIt++, aContext++) {
125 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
126 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
130 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
131 aTrsf->setSymmetry(aPoint);
132 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
133 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
134 aResultPart->setTrsf(*aContext, aTrsf);
135 setResult(aResultPart, aResultIndex);
137 GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, aPoint);
139 if (!aSymmetryAlgo.check()) {
140 setError(aSymmetryAlgo.getError());
144 aSymmetryAlgo.build();
146 // Checking that the algorithm worked properly.
147 if(!aSymmetryAlgo.isDone()) {
148 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
149 setError(aFeatureError);
152 if(aSymmetryAlgo.shape()->isNull()) {
153 static const std::string aShapeError = "Error: Resulting shape is Null.";
154 setError(aShapeError);
157 if(!aSymmetryAlgo.isValid()) {
158 std::string aFeatureError = "Error: Resulting shape is not valid.";
159 setError(aFeatureError);
163 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
164 loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape);
165 setResult(aResultBody, aResultIndex);
170 // Remove the rest results if there were produced in the previous pass.
171 removeResults(aResultIndex);
174 //=================================================================================================
175 void FeaturesPlugin_Symmetry::performSymmetryByAxis()
178 ListOfShape anObjects;
179 std::list<ResultPtr> aContextes;
180 AttributeSelectionListPtr anObjectsSelList =
181 selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
182 if (anObjectsSelList->size() == 0) {
185 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
186 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
187 anObjectsSelList->value(anObjectsIndex);
188 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
189 if(!anObject.get()) { // may be for not-activated parts
193 anObjects.push_back(anObject);
194 aContextes.push_back(anObjectAttr->context());
198 std::shared_ptr<GeomAPI_Ax1> anAxis;
199 std::shared_ptr<GeomAPI_Edge> anEdge;
200 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
201 selection(FeaturesPlugin_Symmetry::AXIS_OBJECT_ID());
202 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
203 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
204 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
205 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
206 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
209 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
210 anEdge->line()->direction()));
213 // Moving each object.
214 int aResultIndex = 0;
215 std::list<ResultPtr>::iterator aContext = aContextes.begin();
216 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
217 anObjectsIt++, aContext++) {
218 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
219 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
223 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
224 aTrsf->setSymmetry(anAxis);
225 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
226 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
227 aResultPart->setTrsf(*aContext, aTrsf);
228 setResult(aResultPart, aResultIndex);
230 GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, anAxis);
232 if (!aSymmetryAlgo.check()) {
233 setError(aSymmetryAlgo.getError());
237 aSymmetryAlgo.build();
239 // Checking that the algorithm worked properly.
240 if(!aSymmetryAlgo.isDone()) {
241 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
242 setError(aFeatureError);
245 if(aSymmetryAlgo.shape()->isNull()) {
246 static const std::string aShapeError = "Error: Resulting shape is Null.";
247 setError(aShapeError);
250 if(!aSymmetryAlgo.isValid()) {
251 std::string aFeatureError = "Error: Resulting shape is not valid.";
252 setError(aFeatureError);
256 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
257 loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape);
258 setResult(aResultBody, aResultIndex);
263 // Remove the rest results if there were produced in the previous pass.
264 removeResults(aResultIndex);
267 //=================================================================================================
268 void FeaturesPlugin_Symmetry::performSymmetryByPlane()
271 ListOfShape anObjects;
272 std::list<ResultPtr> aContextes;
273 AttributeSelectionListPtr anObjectsSelList =
274 selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID());
275 if (anObjectsSelList->size() == 0) {
278 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
279 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
280 anObjectsSelList->value(anObjectsIndex);
281 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
282 if(!anObject.get()) { // may be for not-activated parts
286 anObjects.push_back(anObject);
287 aContextes.push_back(anObjectAttr->context());
291 std::shared_ptr<GeomAPI_Ax2> aPlane;
292 std::shared_ptr<GeomAPI_Pln> aPln;
293 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
294 selection(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID());
295 if (anObjRef && anObjRef->value() && anObjRef->value()->isFace()) {
296 aPln = std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(anObjRef->value()))->getPlane();
298 else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
299 anObjRef->context()->shape() && anObjRef->context()->shape()->isFace()) {
301 std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(anObjRef->context()->shape()))->getPlane();
304 aPlane = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aPln->location(),
308 // Moving each object.
309 int aResultIndex = 0;
310 std::list<ResultPtr>::iterator aContext = aContextes.begin();
311 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
312 anObjectsIt++, aContext++) {
313 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
314 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
318 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
319 aTrsf->setSymmetry(aPlane);
320 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
321 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
322 aResultPart->setTrsf(*aContext, aTrsf);
323 setResult(aResultPart, aResultIndex);
325 GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, aPlane);
327 if (!aSymmetryAlgo.check()) {
328 setError(aSymmetryAlgo.getError());
332 aSymmetryAlgo.build();
334 // Checking that the algorithm worked properly.
335 if(!aSymmetryAlgo.isDone()) {
336 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
337 setError(aFeatureError);
340 if(aSymmetryAlgo.shape()->isNull()) {
341 static const std::string aShapeError = "Error: Resulting shape is Null.";
342 setError(aShapeError);
345 if(!aSymmetryAlgo.isValid()) {
346 std::string aFeatureError = "Error: Resulting shape is not valid.";
347 setError(aFeatureError);
351 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
352 loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape);
353 setResult(aResultBody, aResultIndex);
358 // Remove the rest results if there were produced in the previous pass.
359 removeResults(aResultIndex);
362 //=================================================================================================
363 void FeaturesPlugin_Symmetry::loadNamingDS(GeomAlgoAPI_Symmetry& theSymmetryAlgo,
364 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
365 std::shared_ptr<GeomAPI_Shape> theBaseShape)
367 // Store and name the result.
368 theResultBody->storeModified(theBaseShape, theSymmetryAlgo.shape());
371 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theSymmetryAlgo.mapOfSubShapes();
372 int aReflectedTag = 1;
373 std::string aReflectedName = "Symmetried";
374 theResultBody->loadAndOrientModifiedShapes(&theSymmetryAlgo,
375 theBaseShape, GeomAPI_Shape::FACE,
376 aReflectedTag, aReflectedName, *aSubShapes.get());