Salome HOME
Merge remote-tracking branch 'remotes/origin/Operations_on_Groups'
[modules/shaper.git] / src / Model / Model_ResultGroup.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <Model_ResultGroup.h>
21 #include <Model_Data.h>
22 #include <ModelAPI_AttributeSelectionList.h>
23
24 #include <GeomAlgoAPI_CompoundBuilder.h>
25
26 #include <Config_PropManager.h>
27
28 #include <TDF_Label.hxx>
29 #include <TDF_Reference.hxx>
30 #include <TNaming_Builder.hxx>
31 #include <TNaming_NamedShape.hxx>
32 #include <TopoDS_Shape.hxx>
33
34 Model_ResultGroup::Model_ResultGroup(std::shared_ptr<ModelAPI_Data> theOwnerData)
35 {
36   myOwnerData = theOwnerData;
37 }
38
39 void Model_ResultGroup::colorConfigInfo(std::string& theSection, std::string& theName,
40                                        std::string& theDefault)
41 {
42   theSection = "Visualization";
43   theName = "result_group_color";
44   theDefault = DEFAULT_COLOR();
45 }
46
47 std::shared_ptr<GeomAPI_Shape> Model_ResultGroup::shape()
48 {
49   std::shared_ptr<GeomAPI_Shape> aResult;
50   // obtain stored shape
51   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
52   if (aData && aData->isValid()) {
53     TDF_Label aShapeLab = aData->shapeLab();
54     Handle(TDF_Reference) aRef;
55     if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
56       aShapeLab = aRef->Get();
57     }
58     Handle(TNaming_NamedShape) aName;
59     if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
60       TopoDS_Shape aShape = aName->Get();
61       if (!aShape.IsNull()) {
62         aResult.reset(new GeomAPI_Shape);
63         aResult->setImpl(new TopoDS_Shape(aShape));
64       }
65     }
66   }
67   // collect shapes selected in group
68   if (!aResult && myOwnerData) {
69     AttributeSelectionListPtr aList = myOwnerData->selectionList("group_list");
70     if (aList) {
71       std::list<std::shared_ptr<GeomAPI_Shape> > aSubs;
72       for(int a = aList->size() - 1; a >= 0; a--) {
73         std::shared_ptr<GeomAPI_Shape> aSelection = aList->value(a)->value();
74         if (aSelection && !aSelection->isNull()) {
75           aSubs.push_back(aSelection);
76         }
77       }
78       if (!aSubs.empty()) {
79         aResult = GeomAlgoAPI_CompoundBuilder::compound(aSubs);
80       }
81     }
82   }
83   return aResult;
84 }
85
86 void Model_ResultGroup::store(const GeomShapePtr& theShape)
87 {
88   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
89   if (aData) {
90     TDF_Label aShapeLab = aData->shapeLab();
91     aShapeLab.ForgetAttribute(TDF_Reference::GetID());
92     aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
93
94     if (!theShape)
95       return;  // bad shape
96     TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
97     if (aShape.IsNull())
98       return;  // null shape inside
99
100     // store the new shape as primitive
101     TNaming_Builder aBuilder(aShapeLab);
102     aBuilder.Generated(aShape);
103   }
104 }