]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_ShapesGroup.cxx
Salome HOME
lots 3,8
[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   for ( ; anIter.More(); anIter.Next() )
89   {
90     GroupDefinition& aGroupDef = anIter.ChangeValue();
91     if ( aGroupDef.Shapes.IsEmpty() )
92       continue;
93
94     TopTools_ListOfShape aShapesToAdd;
95     for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
96     {
97       TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
98
99       TopoDS_Shape aGeneratedByAlgo = TopoDS_Shape();
100       if ( !theAlgo->History().IsNull() && ! aShape.IsNull())
101         {
102           const TopTools_ListOfShape& listModified = theAlgo->History()->Modified(aShape);
103           if (listModified.Extent() > 0)
104             aGeneratedByAlgo = listModified.First();
105         }
106       if ( aGeneratedByAlgo.IsNull() )
107       {
108         // Edge has been removed
109         aGroupDef.Shapes.Remove( i );
110         --i;
111       }
112       else if ( !aShape.IsEqual( aGeneratedByAlgo ) )
113       {
114         // Edge has been modified
115         aShapesToAdd.Append( aGeneratedByAlgo );
116         aGroupDef.Shapes.Remove( i );
117         --i;
118       }      
119     }
120
121     HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
122   }
123 }
124
125 void HYDROData_ShapesGroup::GroupDefinition::Update( SeqOfGroupsDefs*              theGroupsDefs,
126                                                      BRepTools_ReShape*            theAlgo )
127 {
128   if ( !theGroupsDefs || !theAlgo )
129     return;
130
131   SeqOfGroupsDefs::Iterator anIter( *theGroupsDefs );
132   for ( ; anIter.More(); anIter.Next() )
133   {
134     GroupDefinition& aGroupDef = anIter.ChangeValue();
135     if ( aGroupDef.Shapes.IsEmpty() )
136       continue;
137
138     TopTools_ListOfShape aShapesToAdd;
139     for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
140     {
141       TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
142
143       TopoDS_Shape aGeneratedByAlgo = theAlgo->Value( aShape );
144       if ( aGeneratedByAlgo.IsNull() )
145       {
146         // Edge has been removed
147         aGroupDef.Shapes.Remove( i );
148         --i;
149       }
150       else if ( !aShape.IsEqual( aGeneratedByAlgo ) )
151       {
152         // Edge has been modified
153         aShapesToAdd.Append( aGeneratedByAlgo );
154         aGroupDef.Shapes.Remove( i );
155         --i;
156       }      
157     }
158
159     HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
160   }
161 }
162
163 void HYDROData_ShapesGroup::GroupDefinition::Dump( std::ostream& theStream ) const
164 {
165   theStream << "Name: " << Name.ToCString() << "\n";
166   HYDROData_ShapesTool::DumpSequenceOfShapes( theStream, Shapes );
167 }
168
169 void HYDROData_ShapesGroup::GroupDefinition::Dump( std::ostream&           theStream,
170                                                    const SeqOfGroupsDefs& theGroups  )
171 {
172   SeqOfGroupsDefs::Iterator anIter( theGroups );
173   for ( ; anIter.More(); anIter.Next() )
174   {
175     const GroupDefinition& aGroupDef = anIter.Value();
176     aGroupDef.Dump( theStream );
177   }
178 }
179
180
181
182 HYDROData_ShapesGroup::HYDROData_ShapesGroup()
183 : HYDROData_Entity( Geom_Groups )
184 {
185 }
186
187 HYDROData_ShapesGroup::~HYDROData_ShapesGroup()
188 {
189 }
190
191 bool HYDROData_ShapesGroup::CanBeUpdated() const
192 {
193   return false;
194 }
195
196 bool HYDROData_ShapesGroup::CanRemove()
197 {
198   return false;
199 }
200
201 void HYDROData_ShapesGroup::AddShape( const TopoDS_Shape& theShape )
202 {
203   if ( theShape.IsNull() )
204     return;
205
206   TDF_Label aNewLab = myLab.FindChild( DataTag_Shape ).NewChild();
207
208   TNaming_Builder aBuilder( aNewLab );
209   aBuilder.Generated( theShape );
210 }
211
212 void HYDROData_ShapesGroup::SetShapes( const TopTools_SequenceOfShape& theShapes )
213 {
214   RemoveShapes();
215
216   for ( int i = 1, n = theShapes.Length(); i <= n; ++i )
217   {
218     const TopoDS_Shape& aShape = theShapes.Value( i );
219     AddShape( aShape );
220   }
221 }
222
223 void HYDROData_ShapesGroup::SetShapes( const TopTools_ListOfShape& theShapes )
224 {
225   RemoveShapes();
226
227   TopTools_ListIteratorOfListOfShape anIter( theShapes );
228   for ( ; anIter.More(); anIter.Next() )
229   {
230     const TopoDS_Shape& aShape = anIter.Value();
231     AddShape( aShape );
232   }
233 }
234
235 void HYDROData_ShapesGroup::GetShapes( TopTools_SequenceOfShape& theShapes ) const
236 {
237   theShapes.Clear();
238
239   TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
240   if ( aLabel.IsNull() )
241     return;
242
243   TDF_ChildIDIterator aChildIt( aLabel, TNaming_NamedShape::GetID() );
244   for ( ; aChildIt.More(); aChildIt.Next() )
245   {
246     Handle(TNaming_NamedShape) aNamedShape = 
247       Handle(TNaming_NamedShape)::DownCast( aChildIt.Value() );
248     if ( aNamedShape.IsNull() )
249       continue;
250
251     TopoDS_Shape aStoredShape = aNamedShape->Get();
252     if ( aStoredShape.IsNull() )
253       continue;
254
255     theShapes.Append( aStoredShape );
256   }
257 }
258
259 void HYDROData_ShapesGroup::RemoveShapes()
260 {
261   TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
262   if ( !aLabel.IsNull() )
263     aLabel.ForgetAllAttributes();
264 }
265