Salome HOME
31888e45a782c4a94cecbb93e7c5b17dedc6da0c
[modules/shaper.git] / src / Model / Model_ResultGroup.cpp
1 // Copyright (C) 2014-2022  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 #include <GeomAPI_ShapeExplorer.h>
26
27 #include <Config_PropManager.h>
28
29 #include <TDF_Label.hxx>
30 #include <TDF_Reference.hxx>
31 #include <TNaming_Builder.hxx>
32 #include <TNaming_NamedShape.hxx>
33 #include <TopoDS_Shape.hxx>
34
35 Model_ResultGroup::Model_ResultGroup(std::shared_ptr<ModelAPI_Data> theOwnerData)
36 {
37   myOwnerData = theOwnerData;
38 }
39
40 void Model_ResultGroup::colorConfigInfo(std::string& theSection, std::string& theName,
41                                        std::string& theDefault)
42 {
43   theSection = "Visualization";
44   theName = "result_group_color";
45   theDefault = DEFAULT_COLOR();
46 }
47
48 std::shared_ptr<GeomAPI_Shape> Model_ResultGroup::shape()
49 {
50   std::shared_ptr<GeomAPI_Shape> aResult;
51   // obtain stored shape
52   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
53   if (aData && aData->isValid()) {
54     TDF_Label aShapeLab = aData->shapeLab();
55     Handle(TDF_Reference) aRef;
56     if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
57       aShapeLab = aRef->Get();
58     }
59     Handle(TNaming_NamedShape) aName;
60     if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
61       TopoDS_Shape aShape = aName->Get();
62       if (!aShape.IsNull()) {
63         aResult.reset(new GeomAPI_Shape);
64         aResult->setImpl(new TopoDS_Shape(aShape));
65       }
66     }
67   }
68   // collect shapes selected in group
69   if (!aResult && myOwnerData) {
70     AttributeSelectionListPtr aList = myOwnerData->selectionList("group_list");
71     if (aList) {
72       GeomAPI_DataMapOfShapeShape aShapesMap; // to avoid shapes duplication
73       std::list<std::shared_ptr<GeomAPI_Shape> > aSubs;
74       for(int a = aList->size() - 1; a >= 0; a--) {
75         std::shared_ptr<GeomAPI_Shape> aSelection = aList->value(a)->value();
76         if (aList->isWholeResultAllowed()) { // whole result selection, explode to sub-shapes
77           if (!aSelection.get() || aSelection->isNull()) {
78             ResultPtr aContext = aList->value(a)->context();
79             if (aContext)
80               aSelection = aContext->shape();
81           }
82           if (aSelection && !aSelection->isNull()) {
83             GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(aList->selectionType());
84             if (aType == aSelection->shapeType()) {
85               if (aShapesMap.bind(aSelection, aSelection))
86                 aSubs.push_back(aSelection);
87             } else {
88               for(GeomAPI_ShapeExplorer anExp(aSelection, aType); anExp.more(); anExp.next()) {
89                 if (aShapesMap.bind(anExp.current(), anExp.current()))
90                   aSubs.push_back(anExp.current());
91               }
92             }
93           }
94         } else { // take selection as it is
95           if (aSelection && !aSelection->isNull()) {
96             if (aShapesMap.bind(aSelection, aSelection))
97               aSubs.push_back(aSelection);
98           }
99         }
100       }
101       if (!aSubs.empty()) {
102         aResult = GeomAlgoAPI_CompoundBuilder::compound(aSubs);
103       }
104     }
105   }
106   return aResult;
107 }
108
109 void Model_ResultGroup::store(const GeomShapePtr& theShape)
110 {
111   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
112   if (aData) {
113     TDF_Label aShapeLab = aData->shapeLab();
114     aShapeLab.ForgetAttribute(TDF_Reference::GetID());
115     aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
116
117     if (!theShape)
118       return;  // bad shape
119     TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
120     if (aShape.IsNull())
121       return;  // null shape inside
122
123     // store the new shape as primitive
124     TNaming_Builder aBuilder(aShapeLab);
125     aBuilder.Generated(aShape);
126   }
127 }