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 <Model_ResultGroup.h>
21 #include <Model_Data.h>
22 #include <ModelAPI_AttributeSelectionList.h>
24 #include <GeomAlgoAPI_CompoundBuilder.h>
25 #include <GeomAPI_ShapeExplorer.h>
27 #include <Config_PropManager.h>
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>
35 Model_ResultGroup::Model_ResultGroup(std::shared_ptr<ModelAPI_Data> theOwnerData)
37 myOwnerData = theOwnerData;
40 void Model_ResultGroup::colorConfigInfo(std::string& theSection, std::string& theName,
41 std::string& theDefault)
43 theSection = "Visualization";
44 theName = "result_group_color";
45 theDefault = DEFAULT_COLOR();
48 std::shared_ptr<GeomAPI_Shape> Model_ResultGroup::shape()
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();
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));
68 // collect shapes selected in group
69 if (!aResult && myOwnerData) {
70 AttributeSelectionListPtr aList = myOwnerData->selectionList("group_list");
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();
80 aSelection = aContext->shape();
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);
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());
94 } else { // take selection as it is
95 if (aSelection && !aSelection->isNull()) {
96 if (aShapesMap.bind(aSelection, aSelection))
97 aSubs.push_back(aSelection);
101 if (!aSubs.empty()) {
102 aResult = GeomAlgoAPI_CompoundBuilder::compound(aSubs);
109 void Model_ResultGroup::store(const GeomShapePtr& theShape)
111 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
113 TDF_Label aShapeLab = aData->shapeLab();
114 aShapeLab.ForgetAttribute(TDF_Reference::GetID());
115 aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
119 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
121 return; // null shape inside
123 // store the new shape as primitive
124 TNaming_Builder aBuilder(aShapeLab);
125 aBuilder.Generated(aShape);