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>
21 #include <BRep_Builder.hxx>
22 #include <BRepAlgoAPI_Fuse.hxx>
24 #include <ShapeUpgrade_UnifySameDomain.hxx>
26 #include <QStringList>
28 IMPLEMENT_STANDARD_HANDLE(HYDROData_Region, HYDROData_Entity)
29 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Region, HYDROData_Entity)
32 HYDROData_Region::HYDROData_Region()
37 HYDROData_Region::~HYDROData_Region()
41 QStringList HYDROData_Region::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
43 QStringList aResList = HYDROData_Entity::DumpToPython( theTreatedObjects );
44 QString aRegionName = GetName();
46 HYDROData_SequenceOfObjects aZones = GetZones();
47 HYDROData_SequenceOfObjects::Iterator anIter( aZones );
48 for ( ; anIter.More(); anIter.Next() )
50 Handle(HYDROData_Zone) aRefZone =
51 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
52 if ( !aRefZone.IsNull() )
53 setPythonReferenceObject( theTreatedObjects, aResList, aRefZone, "AddZone" );
55 aResList << QString( "" );
60 bool HYDROData_Region::CanBeUpdated() const
65 void HYDROData_Region::Remove()
67 Handle(HYDROData_CalculationCase) aFatherCalc =
68 Handle(HYDROData_CalculationCase)::DownCast( GetFatherObject() );
70 HYDROData_Entity::Remove();
72 if ( !aFatherCalc.IsNull() )
73 aFatherCalc->UpdateRegionsOrder();
76 bool HYDROData_Region::CanRemove()
81 HYDROData_SequenceOfObjects HYDROData_Region::GetAllReferenceObjects() const
83 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
85 HYDROData_SequenceOfObjects aSeqOfZones = GetZones();
86 aResSeq.Append( aSeqOfZones );
91 bool HYDROData_Region::AddZone( const Handle(HYDROData_Zone)& theZone )
93 if ( theZone.IsNull() )
96 if ( HasReference( theZone, DataTag_Zone ) )
97 return false; // Object is already in reference list
99 // Move the zone from other region
100 Handle(HYDROData_Region) aFatherRegion =
101 Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
102 if ( !aFatherRegion.IsNull() && aFatherRegion->Label() != myLab )
104 Handle(HYDROData_Zone) aNewZone = addNewZone();
105 theZone->CopyTo( aNewZone );
107 aFatherRegion->RemoveZone( theZone );
109 theZone->SetLabel( aNewZone->Label() );
113 AddReferenceObject( theZone, DataTag_Zone );
119 HYDROData_SequenceOfObjects HYDROData_Region::GetZones() const
121 return GetReferenceObjects( DataTag_Zone );
124 void HYDROData_Region::RemoveZone( const Handle(HYDROData_Zone)& theZone )
126 if ( theZone.IsNull() )
129 RemoveReferenceObject( theZone->Label(), DataTag_Zone );
131 // Remove zone from data model
132 Handle(HYDROData_Region) aFatherRegion =
133 Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
134 if ( !aFatherRegion.IsNull() && aFatherRegion->Label() == myLab )
137 // If the last zone has been removed from region we remove this region
138 HYDROData_SequenceOfObjects aRefZones = GetZones();
139 if ( aRefZones.IsEmpty() )
143 void HYDROData_Region::RemoveZones()
145 ClearReferenceObjects( DataTag_Zone );
146 myLab.FindChild( DataTag_ChildZone ).ForgetAllAttributes( true );
149 Handle(HYDROData_Zone) HYDROData_Region::addNewZone()
151 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildZone ).NewChild();
153 Handle(HYDROData_Zone) aNewZone =
154 Handle(HYDROData_Zone)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_ZONE ) );
160 TopoDS_Shape HYDROData_Region::GetShape( HYDROData_ShapesGroup::SeqOfGroupsDefs* theSeqOfGroups ) const
162 HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroups;
163 if ( theSeqOfGroups )
164 aSeqOfGroups = *theSeqOfGroups;
166 TopoDS_Shape aResShape;
168 // Unite the region zones (each zone is a face) into one face (united face)
169 // If the zones can't be united into the single face - unite them into shell
171 // Collect the list of region faces
172 TopTools_ListOfShape aRegionFacesList;
174 HYDROData_SequenceOfObjects aZones = GetZones();
175 HYDROData_SequenceOfObjects::Iterator aZoneIter( aZones );
176 for ( ; aZoneIter.More(); aZoneIter.Next() )
178 Handle(HYDROData_Zone) aZone =
179 Handle(HYDROData_Zone)::DownCast( aZoneIter.Value() );
180 if ( aZone.IsNull() )
183 TopoDS_Shape aZoneShape = aZone->GetShape();
184 if ( aZoneShape.IsNull() || aZoneShape.ShapeType() != TopAbs_FACE )
187 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
188 aRegionFacesList.Append( aZoneFace );
191 if ( aRegionFacesList.IsEmpty() )
194 // The unite region face
195 TopoDS_Face aRegionFace;
197 if ( aRegionFacesList.Extent() == 1 )
199 aRegionFace = TopoDS::Face( aRegionFacesList.First() );
203 // Try to fuse all region faces into one common face
204 TopoDS_Shape aFuseShape;
205 TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
206 for ( ; aFaceIter.More(); aFaceIter.Next() )
208 if ( aFuseShape.IsNull() )
210 aFuseShape = aFaceIter.Value();
214 BRepAlgoAPI_Fuse aFuse( aFuseShape, aFaceIter.Value() );
215 if ( !aFuse.IsDone() )
217 aFuseShape.Nullify();
221 aFuseShape = aFuse.Shape();
222 HYDROData_ShapesGroup::GroupDefinition::Update( &aSeqOfGroups, &aFuse );
225 // Check the result of fuse operation
226 if ( !aFuseShape.IsNull() )
228 ShapeUpgrade_UnifySameDomain anUnifier( aFuseShape );
231 const TopoDS_Shape& anUnitedShape = anUnifier.Shape();
233 TopTools_SequenceOfShape aShapeFaces;
234 HYDROData_ShapesTool::ExploreShapeToShapes( anUnitedShape, TopAbs_FACE, aShapeFaces );
235 if ( aShapeFaces.Length() == 1 )
237 aRegionFace = TopoDS::Face( aShapeFaces.Value( 1 ) );
238 HYDROData_ShapesGroup::GroupDefinition::Update( &aSeqOfGroups, &anUnifier );
240 // temporary commented because of needs of testing
241 //if ( theSeqOfGroups )
242 //*theSeqOfGroups = aSeqOfGroups;
247 if ( !aRegionFace.IsNull() )
249 // result shape is a face
250 aResShape = aRegionFace;
254 // result shape is a shell
256 BRep_Builder aBuilder;
257 aBuilder.MakeShell( aShell );
259 TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
260 for ( ; aFaceIter.More(); aFaceIter.Next() )
261 aBuilder.Add( aShell, aFaceIter.Value() );