Salome HOME
69c8551bbbc6ee15bb9625feb7433c0858631ea6
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Compound.cpp
1 // Copyright (C) 2017-20xx  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "BuildPlugin_Compound.h"
22
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultBody.h>
25
26 #include <GeomAlgoAPI_Copy.h>
27 #include <GeomAlgoAPI_CompoundBuilder.h>
28
29
30 //=================================================================================================
31 BuildPlugin_Compound::BuildPlugin_Compound()
32 {
33 }
34
35 //=================================================================================================
36 void BuildPlugin_Compound::initAttributes()
37 {
38   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
39 }
40
41 //=================================================================================================
42 void BuildPlugin_Compound::execute()
43 {
44   // Get base objects list.
45   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
46   if(!aSelectionList.get()) {
47     setError("Error: Could not get selection list.");
48     return;
49   }
50   if(aSelectionList->size() == 0) {
51     setError("Error: Empty selection list.");
52     return;
53   }
54
55   // Collect base shapes.
56   ListOfShape anOriginalShapes;
57   for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
58     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
59     GeomShapePtr aShape = aSelection->value();
60     if (!aShape.get()) {
61       if (!aSelection->context().get()) {
62         setError("Invalid selection");
63         return;
64       }
65       aShape = aSelection->context()->shape();
66     }
67     anOriginalShapes.push_back(aShape);
68   }
69
70   // Build compound.
71   GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(anOriginalShapes);
72   // Copy shape.
73   GeomAlgoAPI_Copy aCopyAlgo(aCompound);
74   GeomShapePtr aCopyCompound = aCopyAlgo.shape();
75
76   int anIndexToRemove = 0;
77   if (aCopyCompound) {
78     std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfShapes = aCopyAlgo.mapOfSubShapes();
79
80     ResultBodyPtr aResultBody = document()->createBody(data(), anIndexToRemove++);
81     aResultBody->store(aCopyCompound);
82     aResultBody->loadAndOrientModifiedShapes(&aCopyAlgo, aCompound, GeomAPI_Shape::VERTEX,
83                                               1, "Modified_Vertex", *aMapOfShapes.get(),
84                                               true, false, true);
85     aResultBody->loadAndOrientModifiedShapes(&aCopyAlgo, aCompound, GeomAPI_Shape::EDGE,
86                                               100000, "Modified_Edge", *aMapOfShapes.get(),
87                                               true, false, true);
88     aResultBody->loadAndOrientModifiedShapes(&aCopyAlgo, aCompound, GeomAPI_Shape::FACE,
89                                               200000, "Modified_Face", *aMapOfShapes.get(),
90                                               true, false, true);
91     setResult(aResultBody);
92   }
93   removeResults(anIndexToRemove);
94 }