1 // Copyright (C) 2014-2020 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, std::string & theTextureFile)
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 aResultBody->setTextureFile(theTextureFile);
213 setResult(aResultBody, theResultIndex++);
216 //=================================================================================================
217 void FeaturesPlugin_Symmetry::buildResult(ResultPartPtr theOriginal,
218 std::shared_ptr<GeomAPI_Trsf> theTrsf,
221 if (boolean(KEEP_ORIGINAL_RESULT())->value()) {
222 std::shared_ptr<GeomAPI_Trsf> anIdentity(new GeomAPI_Trsf());
223 ResultPartPtr aCopy = document()->copyPart(theOriginal, data(), theResultIndex);
224 aCopy->setTrsf(theOriginal, anIdentity);
225 setResult(aCopy, theResultIndex++);
228 ResultPartPtr aResultPart = document()->copyPart(theOriginal, data(), theResultIndex);
229 aResultPart->setTrsf(theOriginal, theTrsf);
230 setResult(aResultPart, theResultIndex++);
233 //=================================================================================================
234 static bool performShapeSymmetry(std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theAlgoList,
235 GeomAPI_ShapeHierarchy& theHierarchy,
236 GeomShapePtr theBaseShape,
238 bool isKeepOriginalResult,
239 const std::string& theFeatureKind,
240 std::string& theError)
242 std::shared_ptr<GeomAlgoAPI_Transform> aSymmetryAlgo(
243 new GeomAlgoAPI_Transform(theBaseShape, theTrsf));
245 // Checking that the algorithm worked properly.
246 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aSymmetryAlgo, theFeatureKind, theError))
249 theAlgoList->appendAlgo(aSymmetryAlgo);
251 // Compose source shape and the result of symmetry.
252 GeomShapePtr aCompound;
253 if (isKeepOriginalResult) {
255 // add a copy of a base shape otherwise selection of this base shape is bad (2592)
256 std::shared_ptr<GeomAlgoAPI_Copy> aCopyAlgo(new GeomAlgoAPI_Copy(theBaseShape));
257 aShapes.push_back(aCopyAlgo->shape());
258 theAlgoList->appendAlgo(aCopyAlgo);
260 aShapes.push_back(aSymmetryAlgo->shape());
261 aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
264 aCompound = aSymmetryAlgo->shape();
266 theHierarchy.markModified(theBaseShape, aCompound);
270 void FeaturesPlugin_Symmetry::performSymmetry(GeomTrsfPtr theTrsf)
273 setError("Invalid transformation.");
277 bool isKeepOriginal = boolean(KEEP_ORIGINAL_RESULT())->value();
278 bool isKeepSubShapes = data()->version() == SYMMETRY_VERSION_1;
281 GeomAPI_ShapeHierarchy anObjects;
282 std::list<ResultPtr> aParts;
283 std::string theTextureFile;
284 AttributeSelectionListPtr anObjSelList = selectionList(OBJECTS_LIST_ID());
285 if (!FeaturesPlugin_Tools::shapesFromSelectionList(
286 anObjSelList, isKeepSubShapes, anObjects, aParts, theTextureFile))
290 int aResultIndex = 0;
291 // Symmetrying parts.
292 for (std::list<ResultPtr>::iterator aPRes = aParts.begin(); aPRes != aParts.end(); ++aPRes) {
293 ResultPartPtr anOriginal = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aPRes);
294 buildResult(anOriginal, theTrsf, aResultIndex);
297 // Collect transformations for each object in a part.
298 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
299 for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
300 anObjectsIt != anObjects.end(); ++anObjectsIt) {
301 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
302 if (!performShapeSymmetry(aMakeShapeList, anObjects, aBaseShape, theTrsf,
303 isKeepOriginal, getKind(), anError)) {
309 // Build results of the rotation.
310 const ListOfShape& anOriginalShapes = anObjects.objects();
311 ListOfShape aTopLevel;
312 anObjects.topLevelObjects(aTopLevel);
313 for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt)
314 buildResult(aMakeShapeList, anOriginalShapes, *anIt, aResultIndex, theTextureFile);
316 // Remove the rest results if there were produced in the previous pass.
317 removeResults(aResultIndex);