Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[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
34 IMPLEMENT_STANDARD_HANDLE(HYDROData_ShapesGroup,HYDROData_Entity)
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::Dump( std::ostream& theStream ) const
117 {
118   theStream << "Name: " << Name.ToCString() << "\n";
119   HYDROData_ShapesTool::DumpSequenceOfShapes( theStream, Shapes );
120 }
121
122 void HYDROData_ShapesGroup::GroupDefinition::Dump( std::ostream&           theStream,
123                                                    const SeqOfGroupsDefs& theGroups  )
124 {
125   SeqOfGroupsDefs::Iterator anIter( theGroups );
126   for ( ; anIter.More(); anIter.Next() )
127   {
128     const GroupDefinition& aGroupDef = anIter.Value();
129     aGroupDef.Dump( theStream );
130   }
131 }
132
133
134
135 HYDROData_ShapesGroup::HYDROData_ShapesGroup()
136 : HYDROData_Entity( Geom_Groups )
137 {
138 }
139
140 HYDROData_ShapesGroup::~HYDROData_ShapesGroup()
141 {
142 }
143
144 bool HYDROData_ShapesGroup::CanBeUpdated() const
145 {
146   return false;
147 }
148
149 bool HYDROData_ShapesGroup::CanRemove()
150 {
151   return false;
152 }
153
154 void HYDROData_ShapesGroup::AddShape( const TopoDS_Shape& theShape )
155 {
156   if ( theShape.IsNull() )
157     return;
158
159   TDF_Label aNewLab = myLab.FindChild( DataTag_Shape ).NewChild();
160
161   TNaming_Builder aBuilder( aNewLab );
162   aBuilder.Generated( theShape );
163 }
164
165 void HYDROData_ShapesGroup::SetShapes( const TopTools_SequenceOfShape& theShapes )
166 {
167   RemoveShapes();
168
169   for ( int i = 1, n = theShapes.Length(); i <= n; ++i )
170   {
171     const TopoDS_Shape& aShape = theShapes.Value( i );
172     AddShape( aShape );
173   }
174 }
175
176 void HYDROData_ShapesGroup::SetShapes( const TopTools_ListOfShape& theShapes )
177 {
178   RemoveShapes();
179
180   TopTools_ListIteratorOfListOfShape anIter( theShapes );
181   for ( ; anIter.More(); anIter.Next() )
182   {
183     const TopoDS_Shape& aShape = anIter.Value();
184     AddShape( aShape );
185   }
186 }
187
188 void HYDROData_ShapesGroup::GetShapes( TopTools_SequenceOfShape& theShapes ) const
189 {
190   theShapes.Clear();
191
192   TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
193   if ( aLabel.IsNull() )
194     return;
195
196   TDF_ChildIDIterator aChildIt( aLabel, TNaming_NamedShape::GetID() );
197   for ( ; aChildIt.More(); aChildIt.Next() )
198   {
199     Handle(TNaming_NamedShape) aNamedShape = 
200       Handle(TNaming_NamedShape)::DownCast( aChildIt.Value() );
201     if ( aNamedShape.IsNull() )
202       continue;
203
204     TopoDS_Shape aStoredShape = aNamedShape->Get();
205     if ( aStoredShape.IsNull() )
206       continue;
207
208     theShapes.Append( aStoredShape );
209   }
210 }
211
212 void HYDROData_ShapesGroup::RemoveShapes()
213 {
214   TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
215   if ( !aLabel.IsNull() )
216     aLabel.ForgetAllAttributes();
217 }
218