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"
20 #include "HYDROData_ShapesTool.h"
22 #include <BRepBuilderAPI_MakeShape.hxx>
23 #include <TDF_ChildIDIterator.hxx>
24 #include <TNaming_Builder.hxx>
25 #include <TNaming_NamedShape.hxx>
28 #include <TopoDS_Edge.hxx>
30 #include <TopTools_ListOfShape.hxx>
31 #include <TopTools_ListIteratorOfListOfShape.hxx>
32 #include <ShapeUpgrade_UnifySameDomain.hxx>
33 #include <BRepTools_ReShape.hxx>
36 #include "HYDRO_trace.hxx"
38 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_ShapesGroup,HYDROData_Entity)
40 void HYDROData_ShapesGroup::GroupDefinition::Update( SeqOfGroupsDefs* theGroupsDefs,
41 BRepBuilderAPI_MakeShape* theAlgo )
43 if ( !theGroupsDefs || !theAlgo )
46 SeqOfGroupsDefs::Iterator anIter( *theGroupsDefs );
47 for ( ; anIter.More(); anIter.Next() )
49 GroupDefinition& aGroupDef = anIter.ChangeValue();
50 if ( aGroupDef.Shapes.IsEmpty() )
53 TopTools_ListOfShape aShapesToAdd;
54 for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
56 TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
58 const TopTools_ListOfShape& aModifiedByAlgo = theAlgo->Modified( aShape );
59 if ( !aModifiedByAlgo.IsEmpty() )
61 HYDROData_ShapesTool::AddShapes( aShapesToAdd, aModifiedByAlgo );
62 aGroupDef.Shapes.Remove( i );
67 const TopTools_ListOfShape& aGeneratedByAlgo = theAlgo->Generated( aShape );
68 if ( !aGeneratedByAlgo.IsEmpty() )
70 HYDROData_ShapesTool::AddShapes( aShapesToAdd, aGeneratedByAlgo );
71 aGroupDef.Shapes.Remove( i );
77 HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
81 void HYDROData_ShapesGroup::GroupDefinition::Update( SeqOfGroupsDefs* theGroupsDefs,
82 ShapeUpgrade_UnifySameDomain* theAlgo )
84 if ( !theGroupsDefs || !theAlgo )
87 SeqOfGroupsDefs::Iterator anIter( *theGroupsDefs );
88 for ( ; anIter.More(); anIter.Next() )
90 GroupDefinition& aGroupDef = anIter.ChangeValue();
91 if ( aGroupDef.Shapes.IsEmpty() )
94 TopTools_ListOfShape aShapesToAdd;
95 for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
97 TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
99 TopoDS_Shape aGeneratedByAlgo = TopoDS_Shape();
100 if ( !theAlgo->History().IsNull() && ! aShape.IsNull())
102 const TopTools_ListOfShape& listModified = theAlgo->History()->Modified(aShape);
103 if (listModified.Extent() > 0)
104 aGeneratedByAlgo = listModified.First();
106 if ( aGeneratedByAlgo.IsNull() )
108 // Edge has been removed
109 aGroupDef.Shapes.Remove( i );
112 else if ( !aShape.IsEqual( aGeneratedByAlgo ) )
114 // Edge has been modified
115 aShapesToAdd.Append( aGeneratedByAlgo );
116 aGroupDef.Shapes.Remove( i );
121 HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
125 void HYDROData_ShapesGroup::GroupDefinition::Update( SeqOfGroupsDefs* theGroupsDefs,
126 BRepTools_ReShape* theAlgo )
128 if ( !theGroupsDefs || !theAlgo )
131 SeqOfGroupsDefs::Iterator anIter( *theGroupsDefs );
132 for ( ; anIter.More(); anIter.Next() )
134 GroupDefinition& aGroupDef = anIter.ChangeValue();
135 if ( aGroupDef.Shapes.IsEmpty() )
138 TopTools_ListOfShape aShapesToAdd;
139 for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
141 TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
143 TopoDS_Shape aGeneratedByAlgo = theAlgo->Value( aShape );
144 if ( aGeneratedByAlgo.IsNull() )
146 // Edge has been removed
147 aGroupDef.Shapes.Remove( i );
150 else if ( !aShape.IsEqual( aGeneratedByAlgo ) )
152 // Edge has been modified
153 aShapesToAdd.Append( aGeneratedByAlgo );
154 aGroupDef.Shapes.Remove( i );
159 HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
163 void HYDROData_ShapesGroup::GroupDefinition::Dump( std::ostream& theStream ) const
165 theStream << "Name: " << Name.ToCString() << "\n";
166 HYDROData_ShapesTool::DumpSequenceOfShapes( theStream, Shapes );
169 void HYDROData_ShapesGroup::GroupDefinition::Dump( std::ostream& theStream,
170 const SeqOfGroupsDefs& theGroups )
172 SeqOfGroupsDefs::Iterator anIter( theGroups );
173 for ( ; anIter.More(); anIter.Next() )
175 const GroupDefinition& aGroupDef = anIter.Value();
176 aGroupDef.Dump( theStream );
182 HYDROData_ShapesGroup::HYDROData_ShapesGroup()
183 : HYDROData_Entity( Geom_Groups )
187 HYDROData_ShapesGroup::~HYDROData_ShapesGroup()
191 bool HYDROData_ShapesGroup::CanBeUpdated() const
196 bool HYDROData_ShapesGroup::CanRemove()
201 void HYDROData_ShapesGroup::AddShape( const TopoDS_Shape& theShape )
203 if ( theShape.IsNull() )
206 TDF_Label aNewLab = myLab.FindChild( DataTag_Shape ).NewChild();
208 TNaming_Builder aBuilder( aNewLab );
209 aBuilder.Generated( theShape );
212 void HYDROData_ShapesGroup::SetShapes( const TopTools_SequenceOfShape& theShapes )
216 for ( int i = 1, n = theShapes.Length(); i <= n; ++i )
218 const TopoDS_Shape& aShape = theShapes.Value( i );
223 void HYDROData_ShapesGroup::SetShapes( const TopTools_ListOfShape& theShapes )
227 TopTools_ListIteratorOfListOfShape anIter( theShapes );
228 for ( ; anIter.More(); anIter.Next() )
230 const TopoDS_Shape& aShape = anIter.Value();
235 void HYDROData_ShapesGroup::GetShapes( TopTools_SequenceOfShape& theShapes ) const
239 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
240 if ( aLabel.IsNull() )
243 TDF_ChildIDIterator aChildIt( aLabel, TNaming_NamedShape::GetID() );
244 for ( ; aChildIt.More(); aChildIt.Next() )
246 Handle(TNaming_NamedShape) aNamedShape =
247 Handle(TNaming_NamedShape)::DownCast( aChildIt.Value() );
248 if ( aNamedShape.IsNull() )
251 TopoDS_Shape aStoredShape = aNamedShape->Get();
252 if ( aStoredShape.IsNull() )
255 theShapes.Append( aStoredShape );
259 void HYDROData_ShapesGroup::RemoveShapes()
261 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
262 if ( !aLabel.IsNull() )
263 aLabel.ForgetAllAttributes();