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