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_Symmetry.h>
22 #include <GeomAlgoAPI_CompoundBuilder.h>
23 #include <GeomAlgoAPI_PointBuilder.h>
24 #include <GeomAlgoAPI_FaceBuilder.h>
25 #include <GeomAlgoAPI_Copy.h>
26 #include <GeomAlgoAPI_MakeShapeList.h>
27 #include <GeomAlgoAPI_Tools.h>
28 #include <GeomAlgoAPI_Transform.h>
30 #include <GeomAPI_Ax1.h>
31 #include <GeomAPI_Ax2.h>
32 #include <GeomAPI_Edge.h>
33 #include <GeomAPI_Face.h>
34 #include <GeomAPI_Lin.h>
35 #include <GeomAPI_Pln.h>
36 #include <GeomAPI_ShapeIterator.h>
37 #include <GeomAPI_Trsf.h>
39 #include <ModelAPI_AttributeBoolean.h>
40 #include <ModelAPI_AttributeSelectionList.h>
41 #include <ModelAPI_AttributeString.h>
42 #include <ModelAPI_ResultBody.h>
43 #include <ModelAPI_ResultPart.h>
45 #include <FeaturesPlugin_Tools.h>
47 static const std::string SYMMETRY_VERSION_1("v9.5");
49 //=================================================================================================
50 FeaturesPlugin_Symmetry::FeaturesPlugin_Symmetry()
54 //=================================================================================================
55 void FeaturesPlugin_Symmetry::initAttributes()
57 data()->addAttribute(FeaturesPlugin_Symmetry::CREATION_METHOD(),
58 ModelAPI_AttributeString::typeId());
60 AttributeSelectionListPtr aSelection =
61 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
62 FeaturesPlugin_Symmetry::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
64 data()->addAttribute(FeaturesPlugin_Symmetry::POINT_OBJECT_ID(),
65 ModelAPI_AttributeSelection::typeId());
67 data()->addAttribute(FeaturesPlugin_Symmetry::AXIS_OBJECT_ID(),
68 ModelAPI_AttributeSelection::typeId());
70 data()->addAttribute(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID(),
71 ModelAPI_AttributeSelection::typeId());
73 data()->addAttribute(FeaturesPlugin_Symmetry::KEEP_ORIGINAL_RESULT(),
74 ModelAPI_AttributeBoolean::typeId());
76 if (!aSelection->isInitialized()) {
77 // new feature, not read from file
78 data()->setVersion(SYMMETRY_VERSION_1);
82 //=================================================================================================
83 void FeaturesPlugin_Symmetry::execute()
85 AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Symmetry::CREATION_METHOD());
86 std::string aMethodType = aMethodTypeAttr->value();
89 if (aMethodType == CREATION_METHOD_BY_POINT())
90 aTrsf = symmetryByPoint();
91 else if (aMethodType == CREATION_METHOD_BY_AXIS())
92 aTrsf = symmetryByAxis();
93 else if (aMethodType == CREATION_METHOD_BY_PLANE())
94 aTrsf = symmetryByPlane();
96 performSymmetry(aTrsf);
99 //=================================================================================================
100 GeomTrsfPtr FeaturesPlugin_Symmetry::symmetryByPoint()
103 std::shared_ptr<GeomAPI_Pnt> aPoint;
104 AttributeSelectionPtr anObjRef = selection(FeaturesPlugin_Symmetry::POINT_OBJECT_ID());
105 if (anObjRef.get() != NULL) {
106 GeomShapePtr aShape1 = anObjRef->value();
107 if (!aShape1.get()) {
108 aShape1 = anObjRef->context()->shape();
111 aPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
115 GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
116 aTrsf->setSymmetry(aPoint);
120 //=================================================================================================
121 GeomTrsfPtr FeaturesPlugin_Symmetry::symmetryByAxis()
124 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
125 AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID());
126 GeomShapePtr aShape = anObjRef->value();
128 if (anObjRef->context().get()) {
129 aShape = anObjRef->context()->shape();
133 setError(aSelectionError);
134 return GeomTrsfPtr();
138 if (aShape->isEdge())
140 anEdge = aShape->edge();
142 else if (aShape->isCompound())
144 GeomAPI_ShapeIterator anIt(aShape);
145 anEdge = anIt.current()->edge();
150 setError(aSelectionError);
151 return GeomTrsfPtr();
154 std::shared_ptr<GeomAPI_Ax1> anAxis (new GeomAPI_Ax1(anEdge->line()->location(),
155 anEdge->line()->direction()));
156 GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
157 aTrsf->setSymmetry(anAxis);
161 //=================================================================================================
162 GeomTrsfPtr FeaturesPlugin_Symmetry::symmetryByPlane()
165 static const std::string aSelectionError = "Error: The plane shape selection is bad.";
166 AttributeSelectionPtr anObjRef = selection(PLANE_OBJECT_ID());
167 GeomShapePtr aShape = anObjRef->value();
169 if (anObjRef->context().get()) {
170 aShape = anObjRef->context()->shape();
174 setError(aSelectionError);
175 return GeomTrsfPtr();
179 if (aShape->isFace())
181 aFace = aShape->face();
183 else if (aShape->isCompound())
185 GeomAPI_ShapeIterator anIt(aShape);
186 aFace = anIt.current()->face();
191 setError(aSelectionError);
192 return GeomTrsfPtr();
195 std::shared_ptr<GeomAPI_Ax2> aPlane(new GeomAPI_Ax2(aFace->getPlane()->location(),
196 aFace->getPlane()->direction()));
197 GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
198 aTrsf->setSymmetry(aPlane);
202 //=================================================================================================
203 void FeaturesPlugin_Symmetry::buildResult(
204 const std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theAlgo,
205 const std::list<std::shared_ptr<GeomAPI_Shape> >& theOriginalShapes,
206 std::shared_ptr<GeomAPI_Shape> theTargetShape, int& theResultIndex)
208 // Store and name the result.
209 ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
210 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, theOriginalShapes, ListOfShape(),
211 theAlgo, theTargetShape, "Symmetried");
212 setResult(aResultBody, theResultIndex++);
215 //=================================================================================================
216 void FeaturesPlugin_Symmetry::buildResult(ResultPartPtr theOriginal,
217 std::shared_ptr<GeomAPI_Trsf> theTrsf,
220 if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
221 std::shared_ptr<GeomAPI_Trsf> anIdentity(new GeomAPI_Trsf());
222 ResultPartPtr aCopy = document()->copyPart(theOriginal, data(), theResultIndex);
223 aCopy->setTrsf(theOriginal, anIdentity);
224 setResult(aCopy, theResultIndex++);
227 ResultPartPtr aResultPart = document()->copyPart(theOriginal, data(), theResultIndex);
228 aResultPart->setTrsf(theOriginal, theTrsf);
229 setResult(aResultPart, theResultIndex++);
232 //=================================================================================================
233 static bool performShapeSymmetry(std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theAlgoList,
234 GeomAPI_ShapeHierarchy& theHierarchy,
235 GeomShapePtr theBaseShape,
237 bool isKeepOriginalResult,
238 const std::string& theFeatureKind,
239 std::string& theError)
241 std::shared_ptr<GeomAlgoAPI_Transform> aSymmetryAlgo(
242 new GeomAlgoAPI_Transform(theBaseShape, theTrsf));
244 // Checking that the algorithm worked properly.
245 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aSymmetryAlgo, theFeatureKind, theError))
248 theAlgoList->appendAlgo(aSymmetryAlgo);
250 // Compose source shape and the result of symmetry.
251 GeomShapePtr aCompound;
252 if (isKeepOriginalResult) {
254 // add a copy of a base shape otherwise selection of this base shape is bad (2592)
255 std::shared_ptr<GeomAlgoAPI_Copy> aCopyAlgo(new GeomAlgoAPI_Copy(theBaseShape));
256 aShapes.push_back(aCopyAlgo->shape());
257 theAlgoList->appendAlgo(aCopyAlgo);
259 aShapes.push_back(aSymmetryAlgo->shape());
260 aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
263 aCompound = aSymmetryAlgo->shape();
265 theHierarchy.markModified(theBaseShape, aCompound);
269 void FeaturesPlugin_Symmetry::performSymmetry(GeomTrsfPtr theTrsf)
272 setError("Invalid transformation.");
276 bool isKeepOriginal = boolean(KEEP_ORIGINAL_RESULT())->value();
277 bool isKeepSubShapes = data()->version() == SYMMETRY_VERSION_1;
280 GeomAPI_ShapeHierarchy anObjects;
281 std::list<ResultPtr> aParts;
282 AttributeSelectionListPtr anObjSelList = selectionList(OBJECTS_LIST_ID());
283 if (!FeaturesPlugin_Tools::shapesFromSelectionList(
284 anObjSelList, isKeepSubShapes, anObjects, aParts))
288 int aResultIndex = 0;
289 // Symmetrying parts.
290 for (std::list<ResultPtr>::iterator aPRes = aParts.begin(); aPRes != aParts.end(); ++aPRes) {
291 ResultPartPtr anOriginal = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aPRes);
292 buildResult(anOriginal, theTrsf, aResultIndex);
295 // Collect transformations for each object in a part.
296 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
297 for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
298 anObjectsIt != anObjects.end(); ++anObjectsIt) {
299 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
300 if (!performShapeSymmetry(aMakeShapeList, anObjects, aBaseShape, theTrsf,
301 isKeepOriginal, getKind(), anError)) {
307 // Build results of the rotation.
308 const ListOfShape& anOriginalShapes = anObjects.objects();
309 ListOfShape aTopLevel;
310 anObjects.topLevelObjects(aTopLevel);
311 for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt)
312 buildResult(aMakeShapeList, anOriginalShapes, *anIt, aResultIndex);
314 // Remove the rest results if there were produced in the previous pass.
315 removeResults(aResultIndex);