2 #include "HYDROData_Region.h"
4 #include "HYDROData_CalculationCase.h"
5 #include "HYDROData_Document.h"
6 #include "HYDROData_Iterator.h"
7 #include "HYDROData_Zone.h"
10 #include <TopoDS_Shape.hxx>
11 #include <TopoDS_Shell.hxx>
12 #include <TopoDS_Face.hxx>
13 #include <TopTools_ListOfShape.hxx>
14 #include <TopTools_ListIteratorOfListOfShape.hxx>
15 #include <TopExp_Explorer.hxx>
16 #include <ShapeUpgrade_UnifySameDomain.hxx>
17 #include <BRep_Builder.hxx>
18 #include <BRepAlgoAPI_Fuse.hxx>
20 #include <QStringList>
22 #define PYTHON_REGION_ID "KIND_REGION"
24 IMPLEMENT_STANDARD_HANDLE(HYDROData_Region, HYDROData_Entity)
25 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Region, HYDROData_Entity)
28 HYDROData_Region::HYDROData_Region()
33 HYDROData_Region::~HYDROData_Region()
37 QStringList HYDROData_Region::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
41 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
42 if ( aDocument.IsNull() )
45 QString aDocName = aDocument->GetDocPyName();
46 QString aRegionName = GetName();
48 aResList << QString( "%1 = %2.CreateObject( %3 );" )
49 .arg( aRegionName ).arg( aDocName ).arg( PYTHON_REGION_ID );
50 aResList << QString( "%1.SetName( \"%2\" );" )
51 .arg( aRegionName ).arg( aRegionName );
52 aResList << QString( "" );
54 HYDROData_SequenceOfObjects aZones = GetZones();
55 HYDROData_SequenceOfObjects::Iterator anIter( aZones );
56 for ( ; anIter.More(); anIter.Next() )
58 Handle(HYDROData_Zone) aRefZone =
59 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
60 if ( !aRefZone.IsNull() )
61 setPythonReferenceObject( theTreatedObjects, aResList, aRefZone, "AddZone" );
63 aResList << QString( "" );
68 void HYDROData_Region::Remove()
70 Handle(HYDROData_CalculationCase) aFatherCalc =
71 Handle(HYDROData_CalculationCase)::DownCast( GetFatherObject() );
73 HYDROData_Entity::Remove();
75 if ( !aFatherCalc.IsNull() )
76 aFatherCalc->UpdateRegionsOrder();
79 bool HYDROData_Region::AddZone( const Handle(HYDROData_Zone)& theZone )
81 if ( theZone.IsNull() )
84 if ( HasReference( theZone, DataTag_Zone ) )
85 return false; // Object is already in reference list
87 // Move the zone from other region
88 Handle(HYDROData_Region) aFatherRegion =
89 Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
90 if ( !aFatherRegion.IsNull() && aFatherRegion->Label() != myLab )
92 Handle(HYDROData_Zone) aNewZone = addNewZone();
93 theZone->CopyTo( aNewZone );
95 aFatherRegion->RemoveZone( theZone );
97 theZone->SetLabel( aNewZone->Label() );
101 AddReferenceObject( theZone, DataTag_Zone );
107 HYDROData_SequenceOfObjects HYDROData_Region::GetZones() const
109 return GetReferenceObjects( DataTag_Zone );
112 void HYDROData_Region::RemoveZone( const Handle(HYDROData_Zone)& theZone )
114 if ( theZone.IsNull() )
117 RemoveReferenceObject( theZone->Label(), DataTag_Zone );
119 // Remove zone from data model
120 Handle(HYDROData_Region) aFatherRegion =
121 Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
122 if ( !aFatherRegion.IsNull() && aFatherRegion->Label() == myLab )
125 // If the last zone has been removed from region we remove this region
126 HYDROData_SequenceOfObjects aRefZones = GetZones();
127 if ( aRefZones.IsEmpty() )
131 void HYDROData_Region::RemoveZones()
133 ClearReferenceObjects( DataTag_Zone );
134 myLab.FindChild( DataTag_ChildZone ).ForgetAllAttributes( true );
137 Handle(HYDROData_Zone) HYDROData_Region::addNewZone()
139 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildZone ).NewChild();
141 Handle(HYDROData_Zone) aNewZone =
142 Handle(HYDROData_Zone)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_ZONE ) );
148 TopoDS_Shape HYDROData_Region::GetShape() const
150 TopoDS_Shape aResShape;
152 // Unite the region zones (each zone is a face) into one face (united face)
153 // If the zones can't be united into the single face - unite them into shell
155 // Collect the list of region faces
156 TopTools_ListOfShape aRegionFacesList;
158 HYDROData_SequenceOfObjects aZones = GetZones();
159 HYDROData_SequenceOfObjects::Iterator aZoneIter( aZones );
160 for ( ; aZoneIter.More(); aZoneIter.Next() ) {
161 Handle(HYDROData_Zone) aZone =
162 Handle(HYDROData_Zone)::DownCast( aZoneIter.Value() );
164 if ( aZone.IsNull() ) {
168 TopoDS_Face aFace = TopoDS::Face( aZone->GetShape() );
169 if ( !aFace.IsNull() ) {
170 aRegionFacesList.Append( aFace );
174 // Check number of faces
175 int aNbFaces = aRegionFacesList.Extent();
176 if ( aNbFaces > 0 ) {
177 // The unite region face
178 TopoDS_Face aRegionFace;
180 if ( aNbFaces == 1 ) {
181 aRegionFace = TopoDS::Face( aRegionFacesList.First() );
183 // Fuse faces into one
184 TopoDS_Shape aFuseShape;
185 TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
186 for ( ; aFaceIter.More(); aFaceIter.Next() ) {
187 if ( aFuseShape.IsNull() ) {
188 aFuseShape = aFaceIter.Value();
190 BRepAlgoAPI_Fuse aFuse(aFuseShape, aFaceIter.Value());
191 if ( !aFuse.IsDone() ) {
192 aFuseShape.Nullify();
195 aFuseShape = aFuse.Shape();
199 if ( !aFuseShape.IsNull() ) {
200 ShapeUpgrade_UnifySameDomain aUnify;
201 aUnify.Initialize( aFuseShape );
202 aUnify.UnifyFacesAndEdges();
204 TopoDS_Shape aUnitedShape = aUnify.Shape();
206 TopExp_Explorer anExp( aUnitedShape, TopAbs_FACE );
207 if ( anExp.More() ) {
208 aRegionFace = TopoDS::Face( anExp.Current() );
210 if ( anExp.More() ) {
211 aRegionFace.Nullify();
217 if ( !aRegionFace.IsNull() ) { // result shape is a face
218 aResShape = aRegionFace;
219 } else { // result shape is a shell
221 BRep_Builder aBuilder;
222 aBuilder.MakeShell( aShell );
224 TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
225 for ( ; aFaceIter.More(); aFaceIter.Next() ) {
226 aBuilder.Add( aShell, aFaceIter.Value() );