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 <Precision.hxx>
31 #include <TopTools_ListOfShape.hxx>
32 #include <TopTools_ListIteratorOfListOfShape.hxx>
33 #include <ShapeUpgrade_UnifySameDomain.hxx>
34 #include <BRepTools_ReShape.hxx>
37 #include "HYDRO_trace.hxx"
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 if (theAlgo->History().IsNull())
92 for ( ; anIter.More(); anIter.Next() )
94 GroupDefinition& aGroupDef = anIter.ChangeValue();
95 if ( aGroupDef.Shapes.IsEmpty() )
98 TopTools_ListOfShape aShapesToAdd;
99 for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
101 TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
103 TopoDS_Shape aModifSh;
104 if ( !aShape.IsNull())
106 const TopTools_ListOfShape& listModified = theAlgo->History()->Modified(aShape);
107 if (listModified.Extent() > 0)
109 aModifSh = listModified.First();
110 // Edge has been modified
111 aShapesToAdd.Append( aModifSh );
112 aGroupDef.Shapes.Remove( i );
115 else if ( theAlgo->History()->IsRemoved(aShape) )
117 // Edge has been removed
118 aGroupDef.Shapes.Remove( i );
125 HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
129 void HYDROData_ShapesGroup::GroupDefinition::Update( SeqOfGroupsDefs* theGroupsDefs,
130 BRepTools_ReShape* theAlgo )
132 if ( !theGroupsDefs || !theAlgo )
135 SeqOfGroupsDefs::Iterator anIter( *theGroupsDefs );
136 for ( ; anIter.More(); anIter.Next() )
138 GroupDefinition& aGroupDef = anIter.ChangeValue();
139 if ( aGroupDef.Shapes.IsEmpty() )
142 TopTools_ListOfShape aShapesToAdd;
143 for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
145 TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
147 TopoDS_Shape aGeneratedByAlgo = theAlgo->Value( aShape );
148 if ( aGeneratedByAlgo.IsNull() )
150 // Edge has been removed
151 aGroupDef.Shapes.Remove( i );
154 else if ( !aShape.IsEqual( aGeneratedByAlgo ) )
156 // Edge has been modified
157 aShapesToAdd.Append( aGeneratedByAlgo );
158 aGroupDef.Shapes.Remove( i );
163 HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
167 void HYDROData_ShapesGroup::GroupDefinition::Dump( std::ostream& theStream ) const
169 theStream << "Name: " << Name.ToCString() << "\n";
170 HYDROData_ShapesTool::DumpSequenceOfShapes( theStream, Shapes );
173 void HYDROData_ShapesGroup::GroupDefinition::Dump( std::ostream& theStream,
174 const SeqOfGroupsDefs& theGroups )
176 SeqOfGroupsDefs::Iterator anIter( theGroups );
177 for ( ; anIter.More(); anIter.Next() )
179 const GroupDefinition& aGroupDef = anIter.Value();
180 aGroupDef.Dump( theStream );
186 HYDROData_ShapesGroup::HYDROData_ShapesGroup()
187 : HYDROData_Entity( Geom_Groups )
191 HYDROData_ShapesGroup::~HYDROData_ShapesGroup()
195 bool HYDROData_ShapesGroup::CanBeUpdated() const
200 bool HYDROData_ShapesGroup::CanRemove()
205 void HYDROData_ShapesGroup::AddShape( const TopoDS_Shape& theShape )
207 if ( theShape.IsNull() )
210 TDF_Label aNewLab = myLab.FindChild( DataTag_Shape ).NewChild();
212 TNaming_Builder aBuilder( aNewLab );
213 aBuilder.Generated( theShape );
216 void HYDROData_ShapesGroup::SetShapes( const TopTools_SequenceOfShape& theShapes )
220 for ( int i = 1, n = theShapes.Length(); i <= n; ++i )
222 const TopoDS_Shape& aShape = theShapes.Value( i );
227 void HYDROData_ShapesGroup::SetShapes( const TopTools_ListOfShape& theShapes )
231 TopTools_ListIteratorOfListOfShape anIter( theShapes );
232 for ( ; anIter.More(); anIter.Next() )
234 const TopoDS_Shape& aShape = anIter.Value();
239 void HYDROData_ShapesGroup::GetShapes( TopTools_SequenceOfShape& theShapes ) const
243 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
244 if ( aLabel.IsNull() )
247 TDF_ChildIDIterator aChildIt( aLabel, TNaming_NamedShape::GetID() );
248 for ( ; aChildIt.More(); aChildIt.Next() )
250 Handle(TNaming_NamedShape) aNamedShape =
251 Handle(TNaming_NamedShape)::DownCast( aChildIt.Value() );
252 if ( aNamedShape.IsNull() )
255 TopoDS_Shape aStoredShape = aNamedShape->Get();
256 if ( aStoredShape.IsNull() )
259 theShapes.Append( aStoredShape );
263 void HYDROData_ShapesGroup::RemoveShapes()
265 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
266 if ( !aLabel.IsNull() )
267 aLabel.ForgetAllAttributes();