1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_ShapesGroup.h"
21 #include "HYDROData_ShapesTool.h"
23 #include <TDF_ChildIDIterator.hxx>
26 #include <TopoDS_Edge.hxx>
28 #include <TopTools_ListOfShape.hxx>
29 #include <TopTools_ListIteratorOfListOfShape.hxx>
31 #include <TNaming_Builder.hxx>
32 #include <TNaming_NamedShape.hxx>
34 #include <BRepBuilderAPI_MakeShape.hxx>
36 #include <ShapeUpgrade_UnifySameDomain.hxx>
38 IMPLEMENT_STANDARD_HANDLE(HYDROData_ShapesGroup,HYDROData_Entity)
39 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_ShapesGroup,HYDROData_Entity)
41 void HYDROData_ShapesGroup::GroupDefinition::Update( SeqOfGroupsDefs* theGroupsDefs,
42 BRepBuilderAPI_MakeShape* theAlgo )
44 if ( !theGroupsDefs || !theAlgo )
47 SeqOfGroupsDefs::Iterator anIter( *theGroupsDefs );
48 for ( ; anIter.More(); anIter.Next() )
50 GroupDefinition& aGroupDef = anIter.ChangeValue();
51 if ( aGroupDef.Shapes.IsEmpty() )
54 TopTools_ListOfShape aShapesToAdd;
55 for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
57 TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
59 const TopTools_ListOfShape& aModifiedByAlgo = theAlgo->Modified( aShape );
60 if ( !aModifiedByAlgo.IsEmpty() )
62 HYDROData_ShapesTool::AddShapes( aShapesToAdd, aModifiedByAlgo );
63 aGroupDef.Shapes.Remove( i );
68 const TopTools_ListOfShape& aGeneratedByAlgo = theAlgo->Generated( aShape );
69 if ( !aGeneratedByAlgo.IsEmpty() )
71 HYDROData_ShapesTool::AddShapes( aShapesToAdd, aGeneratedByAlgo );
72 aGroupDef.Shapes.Remove( i );
78 HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
82 void HYDROData_ShapesGroup::GroupDefinition::Update( SeqOfGroupsDefs* theGroupsDefs,
83 ShapeUpgrade_UnifySameDomain* theAlgo )
85 if ( !theGroupsDefs || !theAlgo )
88 SeqOfGroupsDefs::Iterator anIter( *theGroupsDefs );
89 for ( ; anIter.More(); anIter.Next() )
91 GroupDefinition& aGroupDef = anIter.ChangeValue();
92 if ( aGroupDef.Shapes.IsEmpty() )
95 TopTools_ListOfShape aShapesToAdd;
96 for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
98 TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
100 TopoDS_Shape aGeneratedByAlgo = theAlgo->Generated( aShape );
101 if ( aGeneratedByAlgo.IsNull() )
103 // Edge has been removed
104 aGroupDef.Shapes.Remove( i );
107 else if ( !aShape.IsEqual( aGeneratedByAlgo ) )
109 // Edge has been modified
110 aShapesToAdd.Append( aGeneratedByAlgo );
111 aGroupDef.Shapes.Remove( i );
116 HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
120 void HYDROData_ShapesGroup::GroupDefinition::Dump( std::ostream& theStream ) const
122 theStream << "Name: " << Name.ToCString() << "\n";
123 HYDROData_ShapesTool::DumpSequenceOfShapes( theStream, Shapes );
126 void HYDROData_ShapesGroup::GroupDefinition::Dump( std::ostream& theStream,
127 const SeqOfGroupsDefs& theGroups )
129 SeqOfGroupsDefs::Iterator anIter( theGroups );
130 for ( ; anIter.More(); anIter.Next() )
132 const GroupDefinition& aGroupDef = anIter.Value();
133 aGroupDef.Dump( theStream );
139 HYDROData_ShapesGroup::HYDROData_ShapesGroup()
140 : HYDROData_Entity( Geom_Groups )
144 HYDROData_ShapesGroup::~HYDROData_ShapesGroup()
148 bool HYDROData_ShapesGroup::CanBeUpdated() const
153 bool HYDROData_ShapesGroup::CanRemove()
158 void HYDROData_ShapesGroup::AddShape( const TopoDS_Shape& theShape )
160 if ( theShape.IsNull() )
163 TDF_Label aNewLab = myLab.FindChild( DataTag_Shape ).NewChild();
165 TNaming_Builder aBuilder( aNewLab );
166 aBuilder.Generated( theShape );
169 void HYDROData_ShapesGroup::SetShapes( const TopTools_SequenceOfShape& theShapes )
173 for ( int i = 1, n = theShapes.Length(); i <= n; ++i )
175 const TopoDS_Shape& aShape = theShapes.Value( i );
180 void HYDROData_ShapesGroup::SetShapes( const TopTools_ListOfShape& theShapes )
184 TopTools_ListIteratorOfListOfShape anIter( theShapes );
185 for ( ; anIter.More(); anIter.Next() )
187 const TopoDS_Shape& aShape = anIter.Value();
192 void HYDROData_ShapesGroup::GetShapes( TopTools_SequenceOfShape& theShapes ) const
196 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
197 if ( aLabel.IsNull() )
200 TDF_ChildIDIterator aChildIt( aLabel, TNaming_NamedShape::GetID() );
201 for ( ; aChildIt.More(); aChildIt.Next() )
203 Handle(TNaming_NamedShape) aNamedShape =
204 Handle(TNaming_NamedShape)::DownCast( aChildIt.Value() );
205 if ( aNamedShape.IsNull() )
208 TopoDS_Shape aStoredShape = aNamedShape->Get();
209 if ( aStoredShape.IsNull() )
212 theShapes.Append( aStoredShape );
216 void HYDROData_ShapesGroup::RemoveShapes()
218 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
219 if ( !aLabel.IsNull() )
220 aLabel.ForgetAllAttributes();