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