Salome HOME
a540e527f0599b13fa3e22939c9fd5a274556c34
[modules/hydro.git] / src / HYDROData / HYDROData_ShapesGroup.cxx
1
2 #include "HYDROData_ShapesGroup.h"
3
4 #include "HYDROData_ShapesTool.h"
5
6 #include <TDF_ChildIDIterator.hxx>
7
8 #include <TopoDS.hxx>
9 #include <TopoDS_Edge.hxx>
10
11 #include <TopTools_ListOfShape.hxx>
12 #include <TopTools_ListIteratorOfListOfShape.hxx>
13
14 #include <TNaming_Builder.hxx>
15 #include <TNaming_NamedShape.hxx>
16
17 #include <BRepBuilderAPI_MakeShape.hxx>
18
19 #include <ShapeUpgrade_UnifySameDomain.hxx>
20
21 IMPLEMENT_STANDARD_HANDLE(HYDROData_ShapesGroup,HYDROData_Entity)
22 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_ShapesGroup,HYDROData_Entity)
23
24 void HYDROData_ShapesGroup::GroupDefinition::Update( SeqOfGroupsDefs*          theGroupsDefs,
25                                                      BRepBuilderAPI_MakeShape* theAlgo )
26 {
27   if ( !theGroupsDefs || !theAlgo )
28     return;
29
30   SeqOfGroupsDefs::Iterator anIter( *theGroupsDefs );
31   for ( ; anIter.More(); anIter.Next() )
32   {
33     GroupDefinition& aGroupDef = anIter.ChangeValue();
34     if ( aGroupDef.Shapes.IsEmpty() )
35       continue;
36
37     TopTools_ListOfShape aShapesToAdd;
38     for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
39     {
40       TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
41       /*
42       if ( theAlgo->IsDeleted( aShape ) )
43       {
44         aGroupDef.Shapes.Remove( i );
45         --i;
46       }
47       else 
48       {*/
49         const TopTools_ListOfShape& aModifiedByAlgo = theAlgo->Modified( aShape );
50         if ( !aModifiedByAlgo.IsEmpty() )
51         {
52           HYDROData_ShapesTool::AddShapes( aShapesToAdd, aModifiedByAlgo );
53           aGroupDef.Shapes.Remove( i );
54           --i;
55         }
56         else
57         {
58           const TopTools_ListOfShape& aGeneratedByAlgo = theAlgo->Generated( aShape );
59           if ( !aGeneratedByAlgo.IsEmpty() )
60           {
61             HYDROData_ShapesTool::AddShapes( aShapesToAdd, aGeneratedByAlgo );
62             aGroupDef.Shapes.Remove( i );
63             --i;
64           }
65         }
66       //}
67     }
68
69     HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
70   }
71 }
72
73 void HYDROData_ShapesGroup::GroupDefinition::Update( SeqOfGroupsDefs*              theGroupsDefs,
74                                                      ShapeUpgrade_UnifySameDomain* theAlgo )
75 {
76   if ( !theGroupsDefs || !theAlgo )
77     return;
78
79   SeqOfGroupsDefs::Iterator anIter( *theGroupsDefs );
80   for ( ; anIter.More(); anIter.Next() )
81   {
82     GroupDefinition& aGroupDef = anIter.ChangeValue();
83     if ( aGroupDef.Shapes.IsEmpty() )
84       continue;
85
86     TopTools_ListOfShape aShapesToAdd;
87     for ( int i = 1; i <= aGroupDef.Shapes.Length(); ++i )
88     {
89       TopoDS_Shape aShape = aGroupDef.Shapes.Value( i );
90
91       TopoDS_Shape aGeneratedByAlgo = theAlgo->Generated( aShape );
92       if ( aGeneratedByAlgo.IsNull() || aShape.IsEqual( aGeneratedByAlgo ) )
93         continue;
94
95       aShapesToAdd.Append( aGeneratedByAlgo );
96       aGroupDef.Shapes.Remove( i );
97       --i;
98     }
99
100     HYDROData_ShapesTool::AddShapes( aGroupDef.Shapes, aShapesToAdd );
101   }
102 }
103
104 HYDROData_ShapesGroup::HYDROData_ShapesGroup()
105 : HYDROData_Entity()
106 {
107 }
108
109 HYDROData_ShapesGroup::~HYDROData_ShapesGroup()
110 {
111 }
112
113 bool HYDROData_ShapesGroup::CanBeUpdated() const
114 {
115   return false;
116 }
117
118 bool HYDROData_ShapesGroup::CanRemove()
119 {
120   return false;
121 }
122
123 void HYDROData_ShapesGroup::AddShape( const TopoDS_Shape& theShape )
124 {
125   if ( theShape.IsNull() )
126     return;
127
128   TDF_Label aNewLab = myLab.FindChild( DataTag_Shape ).NewChild();
129
130   TNaming_Builder aBuilder( aNewLab );
131   aBuilder.Generated( theShape );
132 }
133
134 void HYDROData_ShapesGroup::SetShapes( const TopTools_SequenceOfShape& theShapes )
135 {
136   RemoveShapes();
137
138   for ( int i = 1, n = theShapes.Length(); i <= n; ++i )
139   {
140     const TopoDS_Shape& aShape = theShapes.Value( i );
141     AddShape( aShape );
142   }
143 }
144
145 void HYDROData_ShapesGroup::SetShapes( const TopTools_ListOfShape& theShapes )
146 {
147   RemoveShapes();
148
149   TopTools_ListIteratorOfListOfShape anIter( theShapes );
150   for ( ; anIter.More(); anIter.Next() )
151   {
152     const TopoDS_Shape& aShape = anIter.Value();
153     AddShape( aShape );
154   }
155 }
156
157 void HYDROData_ShapesGroup::GetShapes( TopTools_SequenceOfShape& theShapes ) const
158 {
159   theShapes.Clear();
160
161   TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
162   if ( aLabel.IsNull() )
163     return;
164
165   TDF_ChildIDIterator aChildIt( aLabel, TNaming_NamedShape::GetID() );
166   for ( ; aChildIt.More(); aChildIt.Next() )
167   {
168     Handle(TNaming_NamedShape) aNamedShape = 
169       Handle(TNaming_NamedShape)::DownCast( aChildIt.Value() );
170     if ( aNamedShape.IsNull() )
171       continue;
172
173     TopoDS_Shape aStoredShape = aNamedShape->Get();
174     if ( aStoredShape.IsNull() )
175       continue;
176
177     theShapes.Append( aStoredShape );
178   }
179 }
180
181 void HYDROData_ShapesGroup::RemoveShapes()
182 {
183   TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
184   if ( !aLabel.IsNull() )
185     aLabel.ForgetAllAttributes();
186 }
187