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