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