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 <TopTools_IndexedMapOfShape.hxx>
17 #include <BRep_Builder.hxx>
18 #include <BRepAlgoAPI_Fuse.hxx>
19 #include <ShapeUpgrade_UnifySameDomain.hxx>
21 #include <QStringList>
23 #define PYTHON_REGION_ID "KIND_REGION"
25 IMPLEMENT_STANDARD_HANDLE(HYDROData_Region, HYDROData_Entity)
26 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Region, HYDROData_Entity)
29 HYDROData_Region::HYDROData_Region()
34 HYDROData_Region::~HYDROData_Region()
38 QStringList HYDROData_Region::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
42 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
43 if ( aDocument.IsNull() )
46 QString aDocName = aDocument->GetDocPyName();
47 QString aRegionName = GetName();
49 aResList << QString( "%1 = %2.CreateObject( %3 );" )
50 .arg( aRegionName ).arg( aDocName ).arg( PYTHON_REGION_ID );
51 aResList << QString( "%1.SetName( \"%2\" );" )
52 .arg( aRegionName ).arg( aRegionName );
53 aResList << QString( "" );
55 HYDROData_SequenceOfObjects aZones = GetZones();
56 HYDROData_SequenceOfObjects::Iterator anIter( aZones );
57 for ( ; anIter.More(); anIter.Next() )
59 Handle(HYDROData_Zone) aRefZone =
60 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
61 if ( !aRefZone.IsNull() )
62 setPythonReferenceObject( theTreatedObjects, aResList, aRefZone, "AddZone" );
64 aResList << QString( "" );
69 void HYDROData_Region::Remove()
71 Handle(HYDROData_CalculationCase) aFatherCalc =
72 Handle(HYDROData_CalculationCase)::DownCast( GetFatherObject() );
74 HYDROData_Entity::Remove();
76 if ( !aFatherCalc.IsNull() )
77 aFatherCalc->UpdateRegionsOrder();
80 bool HYDROData_Region::CanRemove()
85 HYDROData_SequenceOfObjects HYDROData_Region::GetAllReferenceObjects() const
87 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
88 aResSeq.Append( GetZones() );
92 bool HYDROData_Region::AddZone( const Handle(HYDROData_Zone)& theZone )
94 if ( theZone.IsNull() )
97 if ( HasReference( theZone, DataTag_Zone ) )
98 return false; // Object is already in reference list
100 // Move the zone from other region
101 Handle(HYDROData_Region) aFatherRegion =
102 Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
103 if ( !aFatherRegion.IsNull() && aFatherRegion->Label() != myLab )
105 Handle(HYDROData_Zone) aNewZone = addNewZone();
106 theZone->CopyTo( aNewZone );
108 aFatherRegion->RemoveZone( theZone );
110 theZone->SetLabel( aNewZone->Label() );
114 AddReferenceObject( theZone, DataTag_Zone );
120 HYDROData_SequenceOfObjects HYDROData_Region::GetZones() const
122 return GetReferenceObjects( DataTag_Zone );
125 void HYDROData_Region::RemoveZone( const Handle(HYDROData_Zone)& theZone )
127 if ( theZone.IsNull() )
130 RemoveReferenceObject( theZone->Label(), DataTag_Zone );
132 // Remove zone from data model
133 Handle(HYDROData_Region) aFatherRegion =
134 Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
135 if ( !aFatherRegion.IsNull() && aFatherRegion->Label() == myLab )
138 // If the last zone has been removed from region we remove this region
139 HYDROData_SequenceOfObjects aRefZones = GetZones();
140 if ( aRefZones.IsEmpty() )
144 void HYDROData_Region::RemoveZones()
146 ClearReferenceObjects( DataTag_Zone );
147 myLab.FindChild( DataTag_ChildZone ).ForgetAllAttributes( true );
150 Handle(HYDROData_Zone) HYDROData_Region::addNewZone()
152 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildZone ).NewChild();
154 Handle(HYDROData_Zone) aNewZone =
155 Handle(HYDROData_Zone)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_ZONE ) );
161 TopoDS_Shape HYDROData_Region::GetShape() const
163 TopoDS_Shape aResShape;
165 // Unite the region zones (each zone is a face) into one face (united face)
166 // If the zones can't be united into the single face - unite them into shell
168 // Collect the list of region faces
169 TopTools_ListOfShape aRegionFacesList;
171 HYDROData_SequenceOfObjects aZones = GetZones();
172 HYDROData_SequenceOfObjects::Iterator aZoneIter( aZones );
173 for ( ; aZoneIter.More(); aZoneIter.Next() ) {
174 Handle(HYDROData_Zone) aZone =
175 Handle(HYDROData_Zone)::DownCast( aZoneIter.Value() );
177 if ( aZone.IsNull() ) {
181 TopoDS_Face aFace = TopoDS::Face( aZone->GetShape() );
182 if ( !aFace.IsNull() ) {
183 aRegionFacesList.Append( aFace );
187 // Check number of faces
188 int aNbFaces = aRegionFacesList.Extent();
189 if ( aNbFaces > 0 ) {
190 // The unite region face
191 TopoDS_Face aRegionFace;
193 if ( aNbFaces == 1 ) {
194 aRegionFace = TopoDS::Face( aRegionFacesList.First() );
196 // Fuse faces into one
197 TopoDS_Shape aFuseShape;
198 TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
199 for ( ; aFaceIter.More(); aFaceIter.Next() ) {
200 if ( aFuseShape.IsNull() ) {
201 aFuseShape = aFaceIter.Value();
203 BRepAlgoAPI_Fuse aFuse(aFuseShape, aFaceIter.Value());
204 if ( !aFuse.IsDone() ) {
205 aFuseShape.Nullify();
208 aFuseShape = aFuse.Shape();
212 // Check the result of fuse operation
213 if ( !aFuseShape.IsNull() ) {
214 ShapeUpgrade_UnifySameDomain anUnifier( aFuseShape );
216 TopoDS_Shape anUnitedShape = anUnifier.Shape();
218 TopTools_IndexedMapOfShape aMapOfFaces;
219 TopExp::MapShapes( anUnitedShape, TopAbs_FACE, aMapOfFaces );
220 if ( aMapOfFaces.Extent() == 1 ) {
221 aRegionFace = TopoDS::Face( aMapOfFaces(1) );
226 if ( !aRegionFace.IsNull() ) { // result shape is a face
227 aResShape = aRegionFace;
228 } else { // result shape is a shell
230 BRep_Builder aBuilder;
231 aBuilder.MakeShell( aShell );
233 TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
234 for ( ; aFaceIter.More(); aFaceIter.Next() ) {
235 aBuilder.Add( aShell, aFaceIter.Value() );