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 if (theAlgo->History().IsNull())
91 for ( ; anIter.More(); anIter.Next() )
93 GroupDefinition& aGroupDef = anIter.ChangeValue();
94 if ( aGroupDef.Shapes.IsEmpty() )
97 TopTools_ListOfShape aShapesToAdd;
98 for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
100 TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
102 TopoDS_Shape aModifSh;
103 if ( !aShape.IsNull())
105 const TopTools_ListOfShape& listModified = theAlgo->History()->Modified(aShape);
106 if (listModified.Extent() > 0)
108 aModifSh = listModified.First();
109 // Edge has been modified
110 aShapesToAdd.Append( aModifSh );
111 aGroupDef.Shapes.Remove( i );
114 else if ( theAlgo->History()->IsRemoved(aShape) )
116 // Edge has been removed
117 aGroupDef.Shapes.Remove( i );
124 HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
128 void HYDROData_ShapesGroup::GroupDefinition::Update( SeqOfGroupsDefs* theGroupsDefs,
129 BRepTools_ReShape* theAlgo )
131 if ( !theGroupsDefs || !theAlgo )
134 SeqOfGroupsDefs::Iterator anIter( *theGroupsDefs );
135 for ( ; anIter.More(); anIter.Next() )
137 GroupDefinition& aGroupDef = anIter.ChangeValue();
138 if ( aGroupDef.Shapes.IsEmpty() )
141 TopTools_ListOfShape aShapesToAdd;
142 for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
144 TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
146 TopoDS_Shape aGeneratedByAlgo = theAlgo->Value( aShape );
147 if ( aGeneratedByAlgo.IsNull() )
149 // Edge has been removed
150 aGroupDef.Shapes.Remove( i );
153 else if ( !aShape.IsEqual( aGeneratedByAlgo ) )
155 // Edge has been modified
156 aShapesToAdd.Append( aGeneratedByAlgo );
157 aGroupDef.Shapes.Remove( i );
162 HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
166 void HYDROData_ShapesGroup::GroupDefinition::Dump( std::ostream& theStream ) const
168 theStream << "Name: " << Name.ToCString() << "\n";
169 HYDROData_ShapesTool::DumpSequenceOfShapes( theStream, Shapes );
172 void HYDROData_ShapesGroup::GroupDefinition::Dump( std::ostream& theStream,
173 const SeqOfGroupsDefs& theGroups )
175 SeqOfGroupsDefs::Iterator anIter( theGroups );
176 for ( ; anIter.More(); anIter.Next() )
178 const GroupDefinition& aGroupDef = anIter.Value();
179 aGroupDef.Dump( theStream );
185 HYDROData_ShapesGroup::HYDROData_ShapesGroup()
186 : HYDROData_Entity( Geom_Groups )
190 HYDROData_ShapesGroup::~HYDROData_ShapesGroup()
194 bool HYDROData_ShapesGroup::CanBeUpdated() const
199 bool HYDROData_ShapesGroup::CanRemove()
204 void HYDROData_ShapesGroup::AddShape( const TopoDS_Shape& theShape )
206 if ( theShape.IsNull() )
209 TDF_Label aNewLab = myLab.FindChild( DataTag_Shape ).NewChild();
211 TNaming_Builder aBuilder( aNewLab );
212 aBuilder.Generated( theShape );
215 void HYDROData_ShapesGroup::SetShapes( const TopTools_SequenceOfShape& theShapes )
219 for ( int i = 1, n = theShapes.Length(); i <= n; ++i )
221 const TopoDS_Shape& aShape = theShapes.Value( i );
226 void HYDROData_ShapesGroup::SetShapes( const TopTools_ListOfShape& theShapes )
230 TopTools_ListIteratorOfListOfShape anIter( theShapes );
231 for ( ; anIter.More(); anIter.Next() )
233 const TopoDS_Shape& aShape = anIter.Value();
238 void HYDROData_ShapesGroup::GetShapes( TopTools_SequenceOfShape& theShapes ) const
242 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
243 if ( aLabel.IsNull() )
246 TDF_ChildIDIterator aChildIt( aLabel, TNaming_NamedShape::GetID() );
247 for ( ; aChildIt.More(); aChildIt.Next() )
249 Handle(TNaming_NamedShape) aNamedShape =
250 Handle(TNaming_NamedShape)::DownCast( aChildIt.Value() );
251 if ( aNamedShape.IsNull() )
254 TopoDS_Shape aStoredShape = aNamedShape->Get();
255 if ( aStoredShape.IsNull() )
258 theShapes.Append( aStoredShape );
262 void HYDROData_ShapesGroup::RemoveShapes()
264 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
265 if ( !aLabel.IsNull() )
266 aLabel.ForgetAllAttributes();