2 #include "HYDROData_Region.h"
4 #include "HYDROData_CalculationCase.h"
5 #include "HYDROData_Document.h"
6 #include "HYDROData_Iterator.h"
7 #include "HYDROData_ShapesTool.h"
8 #include "HYDROData_Zone.h"
11 #include <TopoDS_Shape.hxx>
12 #include <TopoDS_Shell.hxx>
13 #include <TopoDS_Face.hxx>
17 #include <TopTools_ListOfShape.hxx>
18 #include <TopTools_SequenceOfShape.hxx>
19 #include <TopTools_ListIteratorOfListOfShape.hxx>
20 #include <TopTools_IndexedMapOfShape.hxx>
22 #include <BRep_Builder.hxx>
23 #include <BRepAlgoAPI_Fuse.hxx>
25 #include <ShapeUpgrade_UnifySameDomain.hxx>
27 #include <QStringList>
29 //#define DEB_GET_REGION_SHAPE
31 IMPLEMENT_STANDARD_HANDLE(HYDROData_Region, HYDROData_Entity)
32 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Region, HYDROData_Entity)
35 HYDROData_Region::HYDROData_Region()
40 HYDROData_Region::~HYDROData_Region()
44 bool HYDROData_Region::CanBeUpdated() const
49 void HYDROData_Region::Remove()
51 Handle(HYDROData_CalculationCase) aFatherCalc =
52 Handle(HYDROData_CalculationCase)::DownCast( GetFatherObject() );
54 HYDROData_Entity::Remove();
56 if ( !aFatherCalc.IsNull() )
57 aFatherCalc->UpdateRegionsOrder();
60 bool HYDROData_Region::CanRemove()
65 HYDROData_SequenceOfObjects HYDROData_Region::GetAllReferenceObjects() const
67 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
69 HYDROData_SequenceOfObjects aSeqOfZones = GetZones();
70 aResSeq.Append( aSeqOfZones );
75 bool HYDROData_Region::AddZone( const Handle(HYDROData_Zone)& theZone )
77 if ( theZone.IsNull() )
80 if ( HasReference( theZone, DataTag_Zone ) )
81 return false; // Object is already in reference list
83 // Move the zone from other region
84 Handle(HYDROData_Region) aFatherRegion =
85 Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
86 if ( !aFatherRegion.IsNull() && aFatherRegion->Label() != myLab )
88 Handle(HYDROData_Zone) aNewZone = addNewZone();
89 theZone->CopyTo( aNewZone );
91 // To prevent changing of stored shape
92 aNewZone->SetShape( theZone->GetShape() );
94 aFatherRegion->RemoveZone( theZone );
96 theZone->SetLabel( aNewZone->Label() );
100 AddReferenceObject( theZone, DataTag_Zone );
106 HYDROData_SequenceOfObjects HYDROData_Region::GetZones() const
108 return GetReferenceObjects( DataTag_Zone );
111 void HYDROData_Region::RemoveZone( const Handle(HYDROData_Zone)& theZone )
113 if ( theZone.IsNull() )
116 RemoveReferenceObject( theZone->Label(), DataTag_Zone );
118 // Remove zone from data model
119 Handle(HYDROData_Region) aFatherRegion =
120 Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
121 if ( !aFatherRegion.IsNull() && aFatherRegion->Label() == myLab )
124 // If the last zone has been removed from region we remove this region
125 HYDROData_SequenceOfObjects aRefZones = GetZones();
126 if ( aRefZones.IsEmpty() )
130 void HYDROData_Region::RemoveZones()
132 ClearReferenceObjects( DataTag_Zone );
133 myLab.FindChild( DataTag_ChildZone ).ForgetAllAttributes( true );
136 Handle(HYDROData_Zone) HYDROData_Region::addNewZone()
138 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildZone ).NewChild();
140 Handle(HYDROData_Zone) aNewZone =
141 Handle(HYDROData_Zone)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_ZONE ) );
147 void getUsedGroups( const TopoDS_Shape& theShape,
148 HYDROData_ShapesGroup::SeqOfGroupsDefs& theOriGroups,
149 HYDROData_ShapesGroup::SeqOfGroupsDefs& theUsedGroups )
151 #ifdef DEB_GET_REGION_SHAPE
152 HYDROData_ShapesTool::DumpShapeSubShapes( std::cout, "Zone face edges:", theShape, TopAbs_EDGE );
155 TopTools_IndexedMapOfShape aMapOfSubShapes;
156 TopExp::MapShapes( theShape, TopAbs_EDGE, aMapOfSubShapes );
158 HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator anIter( theOriGroups );
159 for ( ; anIter.More(); anIter.Next() )
161 HYDROData_ShapesGroup::GroupDefinition& anOriGroupDef = anIter.ChangeValue();
162 if ( anOriGroupDef.Shapes.IsEmpty() )
165 for ( int i = 1; i <= anOriGroupDef.Shapes.Length(); ++i )
167 TopoDS_Shape aGroupEdge = anOriGroupDef.Shapes.Value( i );
169 int aShapeIndex = aMapOfSubShapes.FindIndex( aGroupEdge );
170 if ( aShapeIndex <= 0 )
173 anOriGroupDef.Shapes.Remove( i );
176 bool anIsAdded = false;
178 HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator aUsedIter( theUsedGroups );
179 for ( ; aUsedIter.More(); aUsedIter.Next() )
181 HYDROData_ShapesGroup::GroupDefinition& aUsedGroupDef = aUsedIter.ChangeValue();
182 if ( aUsedGroupDef.Name != anOriGroupDef.Name )
185 aUsedGroupDef.Shapes.Append( aGroupEdge );
192 HYDROData_ShapesGroup::GroupDefinition aUsedGroupDef;
193 aUsedGroupDef.Name = anOriGroupDef.Name;
194 aUsedGroupDef.Shapes.Append( aGroupEdge );
195 theUsedGroups.Append( aUsedGroupDef );
201 TopoDS_Shape HYDROData_Region::GetShape( HYDROData_ShapesGroup::SeqOfGroupsDefs* theSeqOfGroups ) const
203 HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroups;
204 HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfUsedGroups;
205 if ( theSeqOfGroups )
206 aSeqOfGroups = *theSeqOfGroups;
208 #ifdef DEB_GET_REGION_SHAPE
209 HYDROData_ShapesGroup::GroupDefinition::Dump( std::cout, aSeqOfGroups );
212 TopoDS_Shape aResShape;
214 // Unite the region zones (each zone is a face) into one face (united face)
215 // If the zones can't be united into the single face - unite them into shell
217 // Collect the list of region faces
218 TopTools_ListOfShape aRegionFacesList;
220 HYDROData_SequenceOfObjects aZones = GetZones();
221 HYDROData_SequenceOfObjects::Iterator aZoneIter( aZones );
222 for ( ; aZoneIter.More(); aZoneIter.Next() )
224 Handle(HYDROData_Zone) aZone =
225 Handle(HYDROData_Zone)::DownCast( aZoneIter.Value() );
226 if ( aZone.IsNull() )
229 TopoDS_Shape aZoneShape = aZone->GetShape();
230 if ( aZoneShape.IsNull() || aZoneShape.ShapeType() != TopAbs_FACE )
233 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
234 aRegionFacesList.Append( aZoneFace );
236 getUsedGroups( aZoneFace, aSeqOfGroups, aSeqOfUsedGroups );
239 if ( aRegionFacesList.IsEmpty() )
242 // The unite region face
243 TopoDS_Face aRegionFace;
245 if ( aRegionFacesList.Extent() == 1 )
247 aRegionFace = TopoDS::Face( aRegionFacesList.First() );
251 #ifdef DEB_GET_REGION_SHAPE
252 HYDROData_ShapesGroup::GroupDefinition::Dump( std::cout, aSeqOfUsedGroups );
255 // Try to fuse all region faces into one common face
256 TopoDS_Shape aFuseShape;
257 TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
258 for ( ; aFaceIter.More(); aFaceIter.Next() )
260 if ( aFuseShape.IsNull() )
262 aFuseShape = aFaceIter.Value();
266 BRepAlgoAPI_Fuse aFuse( aFuseShape, aFaceIter.Value() );
267 if ( !aFuse.IsDone() )
269 aFuseShape.Nullify();
273 aFuseShape = aFuse.Shape();
274 HYDROData_ShapesGroup::GroupDefinition::Update( &aSeqOfUsedGroups, &aFuse );
277 #ifdef DEB_GET_REGION_SHAPE
278 HYDROData_ShapesTool::DumpShapeSubShapes( std::cout, "Fused face edges:", aFuseShape, TopAbs_EDGE );
281 // Check the result of fuse operation
282 if ( !aFuseShape.IsNull() )
284 ShapeUpgrade_UnifySameDomain anUnifier( aFuseShape );
287 const TopoDS_Shape& anUnitedShape = anUnifier.Shape();
289 TopTools_SequenceOfShape aShapeFaces;
290 HYDROData_ShapesTool::ExploreShapeToShapes( anUnitedShape, TopAbs_FACE, aShapeFaces );
291 if ( aShapeFaces.Length() == 1 )
293 aRegionFace = TopoDS::Face( aShapeFaces.Value( 1 ) );
295 #ifdef DEB_GET_REGION_SHAPE
296 HYDROData_ShapesTool::DumpShapeSubShapes( std::cout, "Result face edges:", aRegionFace, TopAbs_EDGE );
299 HYDROData_ShapesGroup::GroupDefinition::Update( &aSeqOfUsedGroups, &anUnifier );
301 // Update the sequence of groups
302 if ( theSeqOfGroups )
304 HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator aUsedIter( aSeqOfUsedGroups );
305 for ( ; aUsedIter.More(); aUsedIter.Next() )
307 const HYDROData_ShapesGroup::GroupDefinition& aUsedGroupDef = aUsedIter.Value();
308 if ( aUsedGroupDef.Shapes.IsEmpty() )
311 HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator anOriIter( aSeqOfGroups );
312 for ( ; anOriIter.More(); anOriIter.Next() )
314 HYDROData_ShapesGroup::GroupDefinition& anOriGroupDef = anOriIter.ChangeValue();
315 if ( anOriGroupDef.Name != aUsedGroupDef.Name )
318 HYDROData_ShapesTool::AddShapes( anOriGroupDef.Shapes, aUsedGroupDef.Shapes );
323 *theSeqOfGroups = aSeqOfGroups;
329 if ( !aRegionFace.IsNull() )
331 // result shape is a face
332 aResShape = aRegionFace;
336 // result shape is a shell
338 BRep_Builder aBuilder;
339 aBuilder.MakeShell( aShell );
341 TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
342 for ( ; aFaceIter.More(); aFaceIter.Next() )
343 aBuilder.Add( aShell, aFaceIter.Value() );