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