Salome HOME
Merge branch 'BR_HYDRO_IMPS_2016' of ssh://gitolite3@git.salome-platform.org/modules...
[modules/hydro.git] / src / HYDROData / HYDROData_ShapesGroup.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROData_ShapesGroup.h"
20 #include "HYDROData_ShapesTool.h"
21
22 #include <BRepBuilderAPI_MakeShape.hxx>
23 #include <TDF_ChildIDIterator.hxx>
24 #include <TNaming_Builder.hxx>
25 #include <TNaming_NamedShape.hxx>
26
27 #include <TopoDS.hxx>
28 #include <TopoDS_Edge.hxx>
29
30 #include <TopTools_ListOfShape.hxx>
31 #include <TopTools_ListIteratorOfListOfShape.hxx>
32 #include <ShapeUpgrade_UnifySameDomain.hxx>
33 #include <BRepTools_ReShape.hxx>
34
35 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_ShapesGroup,HYDROData_Entity)
36
37 void HYDROData_ShapesGroup::GroupDefinition::Update( SeqOfGroupsDefs*          theGroupsDefs,
38                                                      BRepBuilderAPI_MakeShape* theAlgo )
39 {
40   if ( !theGroupsDefs || !theAlgo )
41     return;
42
43   SeqOfGroupsDefs::Iterator anIter( *theGroupsDefs );
44   for ( ; anIter.More(); anIter.Next() )
45   {
46     GroupDefinition& aGroupDef = anIter.ChangeValue();
47     if ( aGroupDef.Shapes.IsEmpty() )
48       continue;
49
50     TopTools_ListOfShape aShapesToAdd;
51     for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
52     {
53       TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
54
55       const TopTools_ListOfShape& aModifiedByAlgo = theAlgo->Modified( aShape );
56       if ( !aModifiedByAlgo.IsEmpty() )
57       {
58         HYDROData_ShapesTool::AddShapes( aShapesToAdd, aModifiedByAlgo );
59         aGroupDef.Shapes.Remove( i );
60         --i;
61       }
62       else
63       {
64         const TopTools_ListOfShape& aGeneratedByAlgo = theAlgo->Generated( aShape );
65         if ( !aGeneratedByAlgo.IsEmpty() )
66         {
67           HYDROData_ShapesTool::AddShapes( aShapesToAdd, aGeneratedByAlgo );
68           aGroupDef.Shapes.Remove( i );
69           --i;
70         }
71       }
72     }
73
74     HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
75   }
76 }
77
78 void HYDROData_ShapesGroup::GroupDefinition::Update( SeqOfGroupsDefs*              theGroupsDefs,
79                                                      ShapeUpgrade_UnifySameDomain* theAlgo )
80 {
81   if ( !theGroupsDefs || !theAlgo )
82     return;
83
84   SeqOfGroupsDefs::Iterator anIter( *theGroupsDefs );
85   for ( ; anIter.More(); anIter.Next() )
86   {
87     GroupDefinition& aGroupDef = anIter.ChangeValue();
88     if ( aGroupDef.Shapes.IsEmpty() )
89       continue;
90
91     TopTools_ListOfShape aShapesToAdd;
92     for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
93     {
94       TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
95
96       TopoDS_Shape aGeneratedByAlgo = theAlgo->Generated( aShape );
97       if ( aGeneratedByAlgo.IsNull() )
98       {
99         // Edge has been removed
100         aGroupDef.Shapes.Remove( i );
101         --i;
102       }
103       else if ( !aShape.IsEqual( aGeneratedByAlgo ) )
104       {
105         // Edge has been modified
106         aShapesToAdd.Append( aGeneratedByAlgo );
107         aGroupDef.Shapes.Remove( i );
108         --i;
109       }      
110     }
111
112     HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
113   }
114 }
115
116 void HYDROData_ShapesGroup::GroupDefinition::Update( SeqOfGroupsDefs*              theGroupsDefs,
117                                                      BRepTools_ReShape*            theAlgo )
118 {
119   if ( !theGroupsDefs || !theAlgo )
120     return;
121
122   SeqOfGroupsDefs::Iterator anIter( *theGroupsDefs );
123   for ( ; anIter.More(); anIter.Next() )
124   {
125     GroupDefinition& aGroupDef = anIter.ChangeValue();
126     if ( aGroupDef.Shapes.IsEmpty() )
127       continue;
128
129     TopTools_ListOfShape aShapesToAdd;
130     for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
131     {
132       TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
133
134       TopoDS_Shape aGeneratedByAlgo = theAlgo->Value( aShape );
135       if ( aGeneratedByAlgo.IsNull() )
136       {
137         // Edge has been removed
138         aGroupDef.Shapes.Remove( i );
139         --i;
140       }
141       else if ( !aShape.IsEqual( aGeneratedByAlgo ) )
142       {
143         // Edge has been modified
144         aShapesToAdd.Append( aGeneratedByAlgo );
145         aGroupDef.Shapes.Remove( i );
146         --i;
147       }      
148     }
149
150     HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
151   }
152 }
153
154 void HYDROData_ShapesGroup::GroupDefinition::Dump( std::ostream& theStream ) const
155 {
156   theStream << "Name: " << Name.ToCString() << "\n";
157   HYDROData_ShapesTool::DumpSequenceOfShapes( theStream, Shapes );
158 }
159
160 void HYDROData_ShapesGroup::GroupDefinition::Dump( std::ostream&           theStream,
161                                                    const SeqOfGroupsDefs& theGroups  )
162 {
163   SeqOfGroupsDefs::Iterator anIter( theGroups );
164   for ( ; anIter.More(); anIter.Next() )
165   {
166     const GroupDefinition& aGroupDef = anIter.Value();
167     aGroupDef.Dump( theStream );
168   }
169 }
170
171
172
173 HYDROData_ShapesGroup::HYDROData_ShapesGroup()
174 : HYDROData_Entity( Geom_Groups )
175 {
176 }
177
178 HYDROData_ShapesGroup::~HYDROData_ShapesGroup()
179 {
180 }
181
182 bool HYDROData_ShapesGroup::CanBeUpdated() const
183 {
184   return false;
185 }
186
187 bool HYDROData_ShapesGroup::CanRemove()
188 {
189   return false;
190 }
191
192 void HYDROData_ShapesGroup::AddShape( const TopoDS_Shape& theShape )
193 {
194   if ( theShape.IsNull() )
195     return;
196
197   TDF_Label aNewLab = myLab.FindChild( DataTag_Shape ).NewChild();
198
199   TNaming_Builder aBuilder( aNewLab );
200   aBuilder.Generated( theShape );
201 }
202
203 void HYDROData_ShapesGroup::SetShapes( const TopTools_SequenceOfShape& theShapes )
204 {
205   RemoveShapes();
206
207   for ( int i = 1, n = theShapes.Length(); i <= n; ++i )
208   {
209     const TopoDS_Shape& aShape = theShapes.Value( i );
210     AddShape( aShape );
211   }
212 }
213
214 void HYDROData_ShapesGroup::SetShapes( const TopTools_ListOfShape& theShapes )
215 {
216   RemoveShapes();
217
218   TopTools_ListIteratorOfListOfShape anIter( theShapes );
219   for ( ; anIter.More(); anIter.Next() )
220   {
221     const TopoDS_Shape& aShape = anIter.Value();
222     AddShape( aShape );
223   }
224 }
225
226 void HYDROData_ShapesGroup::GetShapes( TopTools_SequenceOfShape& theShapes ) const
227 {
228   theShapes.Clear();
229
230   TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
231   if ( aLabel.IsNull() )
232     return;
233
234   TDF_ChildIDIterator aChildIt( aLabel, TNaming_NamedShape::GetID() );
235   for ( ; aChildIt.More(); aChildIt.Next() )
236   {
237     Handle(TNaming_NamedShape) aNamedShape = 
238       Handle(TNaming_NamedShape)::DownCast( aChildIt.Value() );
239     if ( aNamedShape.IsNull() )
240       continue;
241
242     TopoDS_Shape aStoredShape = aNamedShape->Get();
243     if ( aStoredShape.IsNull() )
244       continue;
245
246     theShapes.Append( aStoredShape );
247   }
248 }
249
250 void HYDROData_ShapesGroup::RemoveShapes()
251 {
252   TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
253   if ( !aLabel.IsNull() )
254     aLabel.ForgetAllAttributes();
255 }
256