1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_Region.h"
21 #include "HYDROData_CalculationCase.h"
22 #include "HYDROData_Document.h"
23 #include "HYDROData_Iterator.h"
24 #include "HYDROData_Object.h"
25 #include "HYDROData_ShapesTool.h"
26 #include "HYDROData_Zone.h"
27 #include "HYDROData_Tool.h"
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Shell.hxx>
32 #include <TopoDS_Face.hxx>
36 #include <TopTools_ListOfShape.hxx>
37 #include <TopTools_SequenceOfShape.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
42 #include <BRep_Builder.hxx>
43 #include <BRepAlgoAPI_Fuse.hxx>
45 #include <ShapeUpgrade_UnifySameDomain.hxx>
47 #include <QStringList>
50 #include "Geom_Plane.hxx"
52 #include "BRepTools_ReShape.hxx"
55 #include "HYDRO_trace.hxx"
57 //#define DEB_GET_REGION_SHAPE
59 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Region, HYDROData_Entity)
62 HYDROData_Region::HYDROData_Region()
63 : HYDROData_Entity( Geom_2d )
67 HYDROData_Region::~HYDROData_Region()
71 bool HYDROData_Region::CanBeUpdated() const
76 void HYDROData_Region::Remove()
78 Handle(HYDROData_CalculationCase) aFatherCalc =
79 Handle(HYDROData_CalculationCase)::DownCast( GetFatherObject() );
81 HYDROData_Entity::Remove();
83 if ( !aFatherCalc.IsNull() )
84 aFatherCalc->UpdateRegionsOrder();
87 bool HYDROData_Region::CanRemove()
92 HYDROData_SequenceOfObjects HYDROData_Region::GetAllReferenceObjects() const
94 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
96 HYDROData_SequenceOfObjects aSeqOfZones = GetZones();
97 aResSeq.Append( aSeqOfZones );
102 bool HYDROData_Region::AddZone( const Handle(HYDROData_Zone)& theZone )
104 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
106 if ( theZone.IsNull() )
109 if ( HasReference( theZone, DataTag_Zone ) )
110 return false; // Object is already in reference list
112 // Move the zone from other region
113 Handle(HYDROData_Region) aFatherRegion =
114 Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
115 if ( !aFatherRegion.IsNull() && aFatherRegion->Label() != myLab )
117 Handle(HYDROData_Zone) aNewZone = addNewZone( aDocument, "", TopoDS_Face(), QStringList() );
118 theZone->CopyTo( aNewZone, false );
120 // To prevent changing of stored shape
121 aNewZone->SetShape( theZone->GetShape() );
123 aFatherRegion->RemoveZone( theZone );
125 theZone->SetLabel( aNewZone->Label() );
129 AddReferenceObject( theZone, DataTag_Zone );
135 HYDROData_SequenceOfObjects HYDROData_Region::GetZones() const
137 return GetReferenceObjects( DataTag_Zone );
140 void HYDROData_Region::RemoveZone( const Handle(HYDROData_Zone)& theZone )
142 if ( theZone.IsNull() )
145 RemoveReferenceObject( theZone->Label(), DataTag_Zone );
147 // Remove zone from data model
148 Handle(HYDROData_Region) aFatherRegion =
149 Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
150 if ( !aFatherRegion.IsNull() && aFatherRegion->Label() == myLab )
153 // If the last zone has been removed from region we remove this region
154 HYDROData_SequenceOfObjects aRefZones = GetZones();
155 if ( aRefZones.IsEmpty() )
159 void HYDROData_Region::RemoveZones()
161 ClearReferenceObjects( DataTag_Zone );
162 myLab.FindChild( DataTag_ChildZone ).ForgetAllAttributes( true );
165 Handle(HYDROData_Zone) HYDROData_Region::addNewZone( const Handle(HYDROData_Document)& theDoc,
166 const QString& thePrefix,
167 const TopoDS_Face& theFace,
168 const QStringList& theRefObjects )
170 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildZone ).NewChild();
172 Handle(HYDROData_Zone) aNewZone =
173 Handle(HYDROData_Zone)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_ZONE ) );
176 QString aZoneName = HYDROData_Tool::GenerateObjectName( theDoc, thePrefix );
177 aNewZone->SetName( aZoneName );
179 aNewZone->SetShape( theFace );
181 // Add the reference object for zone
182 for ( int i = 0, n = theRefObjects.length(); i < n; ++i )
184 const QString& anObjName = theRefObjects.at( i );
185 Handle(HYDROData_Entity) aRefObject = theDoc->FindObjectByName( anObjName );
186 if ( aRefObject.IsNull() )
189 aNewZone->AddObject( aRefObject );
195 void getUsedGroups( const TopoDS_Shape& theShape,
196 HYDROData_ShapesGroup::SeqOfGroupsDefs& theOriGroups,
197 HYDROData_ShapesGroup::SeqOfGroupsDefs& theUsedGroups )
199 #ifdef DEB_GET_REGION_SHAPE
200 HYDROData_ShapesTool::DumpShapeSubShapes( std::cout, "Zone face edges:", theShape, TopAbs_EDGE );
203 TopTools_IndexedMapOfShape aMapOfSubShapes;
204 TopExp::MapShapes( theShape, TopAbs_EDGE, aMapOfSubShapes );
206 HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator anIter( theOriGroups );
207 for ( ; anIter.More(); anIter.Next() )
209 HYDROData_ShapesGroup::GroupDefinition& anOriGroupDef = anIter.ChangeValue();
210 if ( anOriGroupDef.Shapes.IsEmpty() )
213 for ( int i = 1; i <= anOriGroupDef.Shapes.Length(); ++i )
215 TopoDS_Shape aGroupEdge = anOriGroupDef.Shapes.Value( i );
217 int aShapeIndex = aMapOfSubShapes.FindIndex( aGroupEdge );
218 if ( aShapeIndex <= 0 )
221 anOriGroupDef.Shapes.Remove( i );
224 bool anIsAdded = false;
226 HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator aUsedIter( theUsedGroups );
227 for ( ; aUsedIter.More(); aUsedIter.Next() )
229 HYDROData_ShapesGroup::GroupDefinition& aUsedGroupDef = aUsedIter.ChangeValue();
230 if ( aUsedGroupDef.Name != anOriGroupDef.Name )
233 aUsedGroupDef.Shapes.Append( aGroupEdge );
240 HYDROData_ShapesGroup::GroupDefinition aUsedGroupDef;
241 aUsedGroupDef.Name = anOriGroupDef.Name;
242 aUsedGroupDef.Shapes.Append( aGroupEdge );
243 theUsedGroups.Append( aUsedGroupDef );
250 TopoDS_Shape HYDROData_Region::GetShape( HYDROData_ShapesGroup::SeqOfGroupsDefs* theSeqOfGroups, const TopTools_SequenceOfShape* IntSh ) const
252 DEBTRACE("GetShape");
253 HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroups;
254 HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfUsedGroups;
255 if ( theSeqOfGroups )
256 aSeqOfGroups = *theSeqOfGroups;
258 #ifdef DEB_GET_REGION_SHAPE
259 HYDROData_ShapesGroup::GroupDefinition::Dump( std::cout, aSeqOfGroups );
262 TopoDS_Shape aResShape;
264 // Unite the region zones (each zone is a face) into one face (united face)
265 // If the zones can't be united into the single face - unite them into shell
267 // Collect the list of region faces
268 TopTools_ListOfShape aRegionFacesList;
270 HYDROData_SequenceOfObjects aZones = GetZones();
271 HYDROData_SequenceOfObjects::Iterator aZoneIter( aZones );
272 TopTools_IndexedMapOfShape AllE;
273 TopTools_IndexedMapOfShape IE; //int edges
275 for ( ; aZoneIter.More(); aZoneIter.Next() )
277 Handle(HYDROData_Zone) aZone =
278 Handle(HYDROData_Zone)::DownCast( aZoneIter.Value() );
279 if ( aZone.IsNull() )
282 TopoDS_Shape aZoneShape = aZone->GetShape();
283 if ( aZoneShape.IsNull() || aZoneShape.ShapeType() != TopAbs_FACE )
286 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
287 aRegionFacesList.Append( aZoneFace );
288 TopExp::MapShapes(aZoneFace, TopAbs_EDGE, AllE); // collect all edges
289 getUsedGroups( aZoneFace, aSeqOfGroups, aSeqOfUsedGroups );
292 DEBTRACE("--- IntSh->Length():" << IntSh->Length());
293 for (int i = 1; i <= IntSh->Length(); i++)
295 const TopoDS_Shape& CS = (*IntSh)(i);
296 if (AllE.Contains(CS))
300 if ( aRegionFacesList.IsEmpty() )
303 TopoDS_Face aRegionFace;
305 if ( aRegionFacesList.Extent() == 1 )
307 DEBTRACE("--- aRegionFacesList.Extent() == 1 ");
308 aResShape = TopoDS::Face( aRegionFacesList.First() );
312 #ifdef DEB_GET_REGION_SHAPE
313 HYDROData_ShapesGroup::GroupDefinition::Dump( std::cout, aSeqOfUsedGroups );
316 // Try to fuse all region faces into one common face
317 TopoDS_Shape aFuseShape;
318 TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
319 for ( ; aFaceIter.More(); aFaceIter.Next() )
321 if ( aFuseShape.IsNull() )
323 aFuseShape = aFaceIter.Value();
327 BRepAlgoAPI_Fuse aFuse( aFuseShape, aFaceIter.Value() );
328 if ( !aFuse.IsDone() )
330 aFuseShape.Nullify();
334 aFuseShape = aFuse.Shape();
336 //update history of internal edges
337 TopTools_IndexedMapOfShape DIE;
338 TopTools_ListOfShape newSh1, newSh2;
339 DEBTRACE("IE.Extent():" << IE.Extent());
340 for (int i = 1; i <= IE.Extent(); i++)
342 const TopoDS_Shape& CSH = IE(i);
345 newSh1 = aFuse.Modified(CSH);
346 if (newSh1.IsEmpty())
348 newSh2 = aFuse.Generated(CSH);
349 if (newSh2.IsEmpty())
352 for (TopTools_ListIteratorOfListOfShape lt(newSh2); lt.More(); lt.Next())
353 if (!lt.Value().IsNull())
358 for (TopTools_ListIteratorOfListOfShape lt(newSh1); lt.More(); lt.Next())
359 if (!lt.Value().IsNull())
365 HYDROData_ShapesGroup::GroupDefinition::Update( &aSeqOfUsedGroups, &aFuse );
368 #ifdef DEB_GET_REGION_SHAPE
369 HYDROData_ShapesTool::DumpShapeSubShapes( std::cout, "Fused face edges:", aFuseShape, TopAbs_EDGE );
372 aFuseShape = HYDROData_Tool::RebuildCmp(aFuseShape);
374 ShapeUpgrade_UnifySameDomain unif( aFuseShape, Standard_False, Standard_True, Standard_False );
377 TopTools_MapOfShape IEM;
378 for (int i = 1; i <= IE.Extent(); i++)
380 unif.KeepShapes(IEM);
384 TopoDS_Shape anUnitedShape;
385 anUnitedShape = unif.Shape();
387 HYDROData_ShapesGroup::GroupDefinition::Update( &aSeqOfUsedGroups, &unif );
389 //cout << "inif: fuseshape: " << aFuseShape.TShape().get() << "unifS " << anUnitedShape.TShape().get() << endl;
390 #ifdef DEB_GET_REGION_SHAPE
391 HYDROData_ShapesGroup::GroupDefinition::Dump( std::cout, aSeqOfUsedGroups );
394 TopTools_SequenceOfShape aShapeFaces;
395 HYDROData_ShapesTool::ExploreShapeToShapes( anUnitedShape, TopAbs_FACE, aShapeFaces );
396 if ( aShapeFaces.Length() == 1 ) //it should be either face or compound of faces (?)
398 const TopoDS_Face& CF = TopoDS::Face( aShapeFaces.Value( 1 ));
403 TopTools_SequenceOfShape aShapeShells;
404 HYDROData_ShapesTool::ExploreShapeToShapes( anUnitedShape, TopAbs_SHELL, aShapeShells );
405 if (aShapeShells.Length() == 1)
406 aResShape = TopoDS::Shell(aShapeShells(1));
408 aResShape = anUnitedShape;
411 // Update the sequence of groups
412 if ( theSeqOfGroups )
414 HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator aUsedIter( aSeqOfUsedGroups );
415 for ( ; aUsedIter.More(); aUsedIter.Next() )
417 const HYDROData_ShapesGroup::GroupDefinition& aUsedGroupDef = aUsedIter.Value();
418 if ( aUsedGroupDef.Shapes.IsEmpty() )
421 HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator anOriIter( aSeqOfGroups );
422 for ( ; anOriIter.More(); anOriIter.Next() )
424 HYDROData_ShapesGroup::GroupDefinition& anOriGroupDef = anOriIter.ChangeValue();
425 if ( anOriGroupDef.Name != aUsedGroupDef.Name )
428 HYDROData_ShapesTool::AddShapes( anOriGroupDef.Shapes, aUsedGroupDef.Shapes );
433 *theSeqOfGroups = aSeqOfGroups;
440 QStringList HYDROData_Region::DumpToPython( const QString& thePyScriptPath,
441 MapOfTreatedObjects& theTreatedObjects,
442 QString defRegName ) const
444 QStringList aResList;
447 findPythonReferenceObject( aResList, defRegName );
450 HYDROData_SequenceOfObjects aZones = GetZones();
451 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
452 for ( ; aZonesIter.More(); aZonesIter.Next() ) {
453 Handle(HYDROData_Zone) aZone =
454 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
455 if ( aZone.IsNull() ) {
460 aZone->findPythonReferenceObject( aResList );
461 theTreatedObjects.insert( aZone->GetName(), aZone );
463 // set zone merge type
464 QString aMergeTypeStr;
465 HYDROData_Zone::MergeType aMergeType = aZone->GetMergeType();
466 if ( aMergeType == HYDROData_Zone::Merge_ZMIN ) {
467 aMergeTypeStr = "HYDROData_Zone.Merge_ZMIN";
468 } else if ( aMergeType == HYDROData_Zone::Merge_ZMAX ) {
469 aMergeTypeStr = "HYDROData_Zone.Merge_ZMAX";
470 } else if ( aMergeType == HYDROData_Zone::Merge_Object ) {
471 aMergeTypeStr = "HYDROData_Zone.Merge_Object";
474 if ( !aMergeTypeStr.isEmpty() ) {
475 aResList << QString( "%1.SetMergeType( %2 )" ).arg( aZone->GetObjPyName() ).arg( aMergeTypeStr );
477 if ( aMergeType == HYDROData_Zone::Merge_Object ) {
478 Handle(HYDROData_Entity) aMergeObject = aZone->GetMergeObject();
479 if ( !aMergeObject.IsNull() ) {
480 aMergeObject->findPythonReferenceObject( aResList );
481 aResList << QString( "%1.SetMergeObject( %2 )" ).arg( aZone->GetObjPyName() )
482 .arg( aMergeObject->GetObjPyName() );
486 QColor zoneColor = aZone->GetColor(Qt::darkBlue);
487 aResList << QString( "%1.SetColor( QColor( %2, %3, %4 ))" )
488 .arg( aZone->GetObjPyName() ).arg( zoneColor.red() ).arg( zoneColor.green() ).arg( zoneColor.blue() );
490 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aZone, "AddZone" );
497 bool HYDROData_Region::IsSubmersible() const
499 HYDROData_SequenceOfObjects aZones = GetZones();
500 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
501 for ( ; aZonesIter.More(); aZonesIter.Next() )
503 Handle(HYDROData_Zone) aZone =
504 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
505 if ( !aZone->IsSubmersible() )
506 return false; //if one of zones is not submersible the region is considered as not submersible