Salome HOME
copyrights in HYDRO files are updated
[modules/hydro.git] / src / HYDROData / HYDROData_ShapesGroup.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROData_ShapesGroup.h"
24
25 #include "HYDROData_ShapesTool.h"
26
27 #include <TDF_ChildIDIterator.hxx>
28
29 #include <TopoDS.hxx>
30 #include <TopoDS_Edge.hxx>
31
32 #include <TopTools_ListOfShape.hxx>
33 #include <TopTools_ListIteratorOfListOfShape.hxx>
34
35 #include <TNaming_Builder.hxx>
36 #include <TNaming_NamedShape.hxx>
37
38 #include <BRepBuilderAPI_MakeShape.hxx>
39
40 #include <ShapeUpgrade_UnifySameDomain.hxx>
41
42 IMPLEMENT_STANDARD_HANDLE(HYDROData_ShapesGroup,HYDROData_Entity)
43 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_ShapesGroup,HYDROData_Entity)
44
45 void HYDROData_ShapesGroup::GroupDefinition::Update( SeqOfGroupsDefs*          theGroupsDefs,
46                                                      BRepBuilderAPI_MakeShape* theAlgo )
47 {
48   if ( !theGroupsDefs || !theAlgo )
49     return;
50
51   SeqOfGroupsDefs::Iterator anIter( *theGroupsDefs );
52   for ( ; anIter.More(); anIter.Next() )
53   {
54     GroupDefinition& aGroupDef = anIter.ChangeValue();
55     if ( aGroupDef.Shapes.IsEmpty() )
56       continue;
57
58     TopTools_ListOfShape aShapesToAdd;
59     for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
60     {
61       TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
62
63       const TopTools_ListOfShape& aModifiedByAlgo = theAlgo->Modified( aShape );
64       if ( !aModifiedByAlgo.IsEmpty() )
65       {
66         HYDROData_ShapesTool::AddShapes( aShapesToAdd, aModifiedByAlgo );
67         aGroupDef.Shapes.Remove( i );
68         --i;
69       }
70       else
71       {
72         const TopTools_ListOfShape& aGeneratedByAlgo = theAlgo->Generated( aShape );
73         if ( !aGeneratedByAlgo.IsEmpty() )
74         {
75           HYDROData_ShapesTool::AddShapes( aShapesToAdd, aGeneratedByAlgo );
76           aGroupDef.Shapes.Remove( i );
77           --i;
78         }
79       }
80     }
81
82     HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
83   }
84 }
85
86 void HYDROData_ShapesGroup::GroupDefinition::Update( SeqOfGroupsDefs*              theGroupsDefs,
87                                                      ShapeUpgrade_UnifySameDomain* theAlgo )
88 {
89   if ( !theGroupsDefs || !theAlgo )
90     return;
91
92   SeqOfGroupsDefs::Iterator anIter( *theGroupsDefs );
93   for ( ; anIter.More(); anIter.Next() )
94   {
95     GroupDefinition& aGroupDef = anIter.ChangeValue();
96     if ( aGroupDef.Shapes.IsEmpty() )
97       continue;
98
99     TopTools_ListOfShape aShapesToAdd;
100     for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
101     {
102       TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
103
104       TopoDS_Shape aGeneratedByAlgo = theAlgo->Generated( aShape );
105       if ( aGeneratedByAlgo.IsNull() )
106       {
107         // Edge has been removed
108         aGroupDef.Shapes.Remove( i );
109         --i;
110       }
111       else if ( !aShape.IsEqual( aGeneratedByAlgo ) )
112       {
113         // Edge has been modified
114         aShapesToAdd.Append( aGeneratedByAlgo );
115         aGroupDef.Shapes.Remove( i );
116         --i;
117       }      
118     }
119
120     HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
121   }
122 }
123
124 void HYDROData_ShapesGroup::GroupDefinition::Dump( std::ostream& theStream ) const
125 {
126   theStream << "Name: " << Name.ToCString() << "\n";
127   HYDROData_ShapesTool::DumpSequenceOfShapes( theStream, Shapes );
128 }
129
130 void HYDROData_ShapesGroup::GroupDefinition::Dump( std::ostream&           theStream,
131                                                    const SeqOfGroupsDefs& theGroups  )
132 {
133   SeqOfGroupsDefs::Iterator anIter( theGroups );
134   for ( ; anIter.More(); anIter.Next() )
135   {
136     const GroupDefinition& aGroupDef = anIter.Value();
137     aGroupDef.Dump( theStream );
138   }
139 }
140
141
142
143 HYDROData_ShapesGroup::HYDROData_ShapesGroup()
144 : HYDROData_Entity()
145 {
146 }
147
148 HYDROData_ShapesGroup::~HYDROData_ShapesGroup()
149 {
150 }
151
152 bool HYDROData_ShapesGroup::CanBeUpdated() const
153 {
154   return false;
155 }
156
157 bool HYDROData_ShapesGroup::CanRemove()
158 {
159   return false;
160 }
161
162 void HYDROData_ShapesGroup::AddShape( const TopoDS_Shape& theShape )
163 {
164   if ( theShape.IsNull() )
165     return;
166
167   TDF_Label aNewLab = myLab.FindChild( DataTag_Shape ).NewChild();
168
169   TNaming_Builder aBuilder( aNewLab );
170   aBuilder.Generated( theShape );
171 }
172
173 void HYDROData_ShapesGroup::SetShapes( const TopTools_SequenceOfShape& theShapes )
174 {
175   RemoveShapes();
176
177   for ( int i = 1, n = theShapes.Length(); i <= n; ++i )
178   {
179     const TopoDS_Shape& aShape = theShapes.Value( i );
180     AddShape( aShape );
181   }
182 }
183
184 void HYDROData_ShapesGroup::SetShapes( const TopTools_ListOfShape& theShapes )
185 {
186   RemoveShapes();
187
188   TopTools_ListIteratorOfListOfShape anIter( theShapes );
189   for ( ; anIter.More(); anIter.Next() )
190   {
191     const TopoDS_Shape& aShape = anIter.Value();
192     AddShape( aShape );
193   }
194 }
195
196 void HYDROData_ShapesGroup::GetShapes( TopTools_SequenceOfShape& theShapes ) const
197 {
198   theShapes.Clear();
199
200   TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
201   if ( aLabel.IsNull() )
202     return;
203
204   TDF_ChildIDIterator aChildIt( aLabel, TNaming_NamedShape::GetID() );
205   for ( ; aChildIt.More(); aChildIt.Next() )
206   {
207     Handle(TNaming_NamedShape) aNamedShape = 
208       Handle(TNaming_NamedShape)::DownCast( aChildIt.Value() );
209     if ( aNamedShape.IsNull() )
210       continue;
211
212     TopoDS_Shape aStoredShape = aNamedShape->Get();
213     if ( aStoredShape.IsNull() )
214       continue;
215
216     theShapes.Append( aStoredShape );
217   }
218 }
219
220 void HYDROData_ShapesGroup::RemoveShapes()
221 {
222   TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
223   if ( !aLabel.IsNull() )
224     aLabel.ForgetAllAttributes();
225 }
226