1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROData_CalculationCase.h"
25 #include "HYDROData_ArtificialObject.h"
26 #include "HYDROData_IAltitudeObject.h"
27 #include "HYDROData_Document.h"
28 #include "HYDROData_ShapesGroup.h"
29 #include "HYDROData_Iterator.h"
30 #include "HYDROData_NaturalObject.h"
31 #include "HYDROData_PolylineXY.h"
32 #include "HYDROData_SplittedShapesGroup.h"
33 #include "HYDROData_Region.h"
34 #include "HYDROData_Tool.h"
35 #include "HYDROData_GeomTool.h"
42 #include <TopoDS_Shell.hxx>
43 #include <TopoDS_Edge.hxx>
45 #include <BRep_Builder.hxx>
46 #include <BRepBuilderAPI_Sewing.hxx>
47 #include <BRepTopAdaptor_FClass2d.hxx>
49 #include <BRepTools.hxx>
52 #include <TopExp_Explorer.hxx>
54 #include <TopTools_ListOfShape.hxx>
55 #include <TopTools_ListIteratorOfListOfShape.hxx>
56 #include <TDataStd_Integer.hxx>
58 //#define DEB_CALCULATION 1
59 #ifdef DEB_CALCULATION
60 #include <BRepTools.hxx>
61 #include <TopLoc_Location.hxx>
63 #define CALCULATION_REGIONS_PREF GetName() + "_Reg"
64 #define CALCULATION_ZONES_PREF GetName() + "_Zone"
65 #define CALCULATION_GROUPS_PREF GetName() + "_"
66 //#define DEB_CLASS2D 1
68 #include <BRepBuilderAPI_MakeVertex.hxx>
71 #define EXPORT_NAME "HYDRO_" + GetName()
73 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
74 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
76 HYDROData_CalculationCase::HYDROData_CalculationCase()
81 HYDROData_CalculationCase::~HYDROData_CalculationCase()
85 void HYDROData_CalculationCase::SetName( const QString& theName )
87 QString anOldCaseName = GetName();
88 if ( anOldCaseName != theName )
90 HYDROData_SequenceOfObjects aRegions = GetRegions();
92 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
93 for ( ; anIter.More(); anIter.Next() )
95 Handle(HYDROData_Region) aRegion =
96 Handle(HYDROData_Region)::DownCast( anIter.Value() );
97 if ( aRegion.IsNull() )
100 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegion );
102 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
103 HYDROData_SequenceOfObjects::Iterator anIter( aZones );
104 for ( ; anIter.More(); anIter.Next() )
106 Handle(HYDROData_Zone) aRegZone =
107 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
108 if ( aRegZone.IsNull() )
111 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegZone );
115 HYDROData_SequenceOfObjects aGroups = GetSplittedGroups();
117 anIter.Init( aGroups );
118 for ( ; anIter.More(); anIter.Next() )
120 Handle(HYDROData_SplittedShapesGroup) aGroup =
121 Handle(HYDROData_SplittedShapesGroup)::DownCast( anIter.Value() );
122 if ( aGroup.IsNull() )
125 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aGroup );
129 HYDROData_Entity::SetName( theName );
132 QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
134 QStringList aResList = dumpObjectCreation( theTreatedObjects );
135 aResList.prepend( "# Calculation case" );
137 QString aCalculName = GetObjPyName();
139 AssignmentMode aMode = GetAssignmentMode();
140 QString aModeStr = aMode==MANUAL ? "HYDROData_CalculationCase.MANUAL" : "HYDROData_CalculationCase.AUTOMATIC";
141 aResList << QString( "%0.SetAssignmentMode( %1 )" ).arg( aCalculName ).arg( aModeStr );
143 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
144 HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
145 for ( ; anIter.More(); anIter.Next() )
147 Handle(HYDROData_Object) aRefGeomObj =
148 Handle(HYDROData_Object)::DownCast( anIter.Value() );
149 setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
151 aResList << QString( "" );
153 QString aGroupName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "case_geom_group" );
155 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
156 anIter.Init( aGeomGroups );
157 for ( ; anIter.More(); anIter.Next() )
159 Handle(HYDROData_ShapesGroup) aGeomGroup =
160 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
161 if ( aGeomGroup.IsNull() )
164 Handle(HYDROData_Object) aFatherGeom =
165 Handle(HYDROData_Object)::DownCast( aGeomGroup->GetFatherObject() );
166 if ( aFatherGeom.IsNull() )
169 int aGroupId = aFatherGeom->GetGroupId( aGeomGroup );
170 aResList << QString( "%1 = %2.GetGroup( %3 );" )
171 .arg( aGroupName ).arg( aFatherGeom->GetObjPyName() ).arg( aGroupId );
173 aResList << QString( "%1.AddGeometryGroup( %2 );" ).arg( aCalculName ).arg( aGroupName );
176 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
177 setPythonReferenceObject( theTreatedObjects, aResList, aBoundaryPolyline, "SetBoundaryPolyline" );
179 if( aMode==AUTOMATIC )
180 DumpRulesToPython( aCalculName, aResList );
182 aResList << QString( "" );
183 aResList << "# Start the algorithm of the partition and assignment";
184 aResList << QString( "%1.Update();" ).arg( aCalculName );
188 // Now we restore the regions and zones order
189 HYDROData_SequenceOfObjects aRegions = GetRegions();
190 anIter.Init( aRegions );
191 for ( ; anIter.More(); anIter.Next() )
193 Handle(HYDROData_Region) aRegion =
194 Handle(HYDROData_Region)::DownCast( anIter.Value() );
195 if ( aRegion.IsNull() )
198 theTreatedObjects.insert( aRegion->GetName(), aRegion );
199 QStringList aRegDump = aRegion->DumpToPython( theTreatedObjects );
200 aResList << aRegDump;
204 // Export calculation case
205 aResList << QString( "" );
206 aResList << "# Export of the calculation case";
207 QString aStudyName = "theStudy";
208 QString anEntryVar = aCalculName + "_entry";
209 aResList << QString( "%1 = %2.Export( %3._get_StudyId() )" ).arg( anEntryVar ).arg( aCalculName ).arg( aStudyName );
211 // Get geometry object and print debug information
213 aResList << "# Get geometry shape and print debug information";
214 aResList << "import GEOM";
215 aResList << QString( "print \"Entry:\", %1" ).arg( anEntryVar );
216 QString aGeomShapeName = aCalculName + "_geom";
217 aResList << QString( "%1 = salome.IDToObject( str( %2 ) )" ).arg( aGeomShapeName ).arg( anEntryVar );
218 aResList << QString( "print \"Geom shape:\", %1" ).arg( aGeomShapeName );
219 aResList << QString( "print \"Geom shape name:\", %1.GetName()" ).arg( aGeomShapeName );
221 DumpSampleMeshing( aResList, aStudyName, aGeomShapeName, aCalculName+"_mesh" );
223 aResList << QString( "" );
227 void HYDROData_CalculationCase::DumpSampleMeshing( QStringList& theResList,
228 const QString& theStudyName,
229 const QString& theGeomShapeName,
230 const QString& theMeshName ) const
233 theResList << "# Meshing";
234 theResList << "import SMESH, SALOMEDS";
235 theResList << "from salome.smesh import smeshBuilder";
236 theResList << "from salome.geom import geomBuilder";
238 theResList << QString( "smesh = smeshBuilder.New( %1 )" ).arg( theStudyName );
239 theResList << QString( "%1 = smesh.Mesh( %2 )" ).arg( theMeshName ).arg( theGeomShapeName );
240 theResList << QString( "MEFISTO_2D = %1.Triangle( algo=smeshBuilder.MEFISTO )" ).arg( theMeshName );
241 theResList << "Max_Element_Area_1 = MEFISTO_2D.MaxElementArea( 10 )";
242 theResList << QString( "Regular_1D = %1.Segment()" ).arg( theMeshName );
243 theResList << "Max_Size_1 = Regular_1D.MaxSize(10)";
244 theResList << QString( "isDone = %1.Compute()" ).arg( theMeshName );
247 theResList << "# Set names of Mesh objects";
248 theResList << "smesh.SetName( MEFISTO_2D.GetAlgorithm(), 'MEFISTO_2D' )";
249 theResList << "smesh.SetName( Regular_1D.GetAlgorithm(), 'Regular_1D' )";
250 theResList << "smesh.SetName( Max_Size_1, 'Max Size_1' )";
251 theResList << "smesh.SetName( Max_Element_Area_1, 'Max. Element Area_1' )";
252 theResList << QString( "smesh.SetName( %1.GetMesh(), '%1' )" ).arg( theMeshName );
255 theResList << "# Greate SMESH groups";
256 theResList << QString( "geompy = geomBuilder.New( %1 )" ).arg( theStudyName );
257 theResList << QString( "geom_groups = geompy.GetGroups( %1 )" ).arg( theGeomShapeName );
258 theResList << QString( "for group in geom_groups:" );
259 theResList << QString( " smesh_group = %1.GroupOnGeom(group, group.GetName(), SMESH.EDGE)" )
261 theResList << QString( " smesh.SetName(smesh_group, group.GetName())" );
262 theResList << QString( " print \"SMESH group '%s': %s\" % (smesh_group.GetName(), smesh_group)" );
265 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
267 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
269 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
270 if ( !aBoundaryPolyline.IsNull() )
271 aResSeq.Append( aBoundaryPolyline );
273 HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
274 aResSeq.Append( aSeqOfRegions );
279 void HYDROData_CalculationCase::Update()
281 HYDROData_Entity::Update();
284 // At first we remove previously created objects
286 RemoveSplittedGroups();
288 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
289 if ( aDocument.IsNull() )
292 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
293 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
294 if ( aGeomObjects.IsEmpty() )
297 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
299 HYDROData_SplitToZonesTool::SplitDataList aSplitObjects =
300 HYDROData_SplitToZonesTool::Split( aGeomObjects, aGeomGroups, aBoundaryPolyline );
301 if ( aSplitObjects.isEmpty() )
304 HYDROData_SplitToZonesTool::SplitDataList aZonesList, anEdgesList;
306 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitObjects );
307 while( anIter.hasNext() )
309 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
310 if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Zone )
311 aZonesList.append( aSplitData );
312 else if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Edge )
313 anEdgesList.append( aSplitData );
316 switch( GetAssignmentMode() )
319 CreateRegionsDef( aDocument, aZonesList );
322 CreateRegionsAuto( aDocument, aZonesList );
325 CreateEdgeGroupsDef( aDocument, anEdgesList );
328 void HYDROData_CalculationCase::CreateRegionsDef( const Handle(HYDROData_Document)& theDoc,
329 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
331 // Create result regions for case, by default one zone for one region
332 QString aRegsPref = CALCULATION_REGIONS_PREF;
333 QString aZonesPref = CALCULATION_ZONES_PREF;
335 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
336 while( anIter.hasNext() )
338 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
340 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegsPref );
342 // Add the zone for region
343 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
347 void HYDROData_CalculationCase::CreateRegionsAuto( const Handle(HYDROData_Document)& theDoc,
348 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
350 QMap<QString, Handle(HYDROData_Region)> aRegionsMap; //object name to region
351 QMap<QString, QString> aRegionNameToObjNameMap;
352 QString aZonesPref = CALCULATION_ZONES_PREF;
353 HYDROData_PriorityQueue aPr( this );
355 // 1. First we create a default region for each object included into the calculation case
356 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
357 for( int i=aGeomObjects.Lower(), n=aGeomObjects.Upper(); i<=n; i++ )
359 Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( aGeomObjects.Value( i ) );
362 QString anObjName = anObj->GetName();
363 QString aRegName = anObjName + "_reg";
364 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegName, false );
365 aRegionsMap.insert( anObjName, aRegion );
366 aRegionNameToObjNameMap.insert( aRegName, anObjName );
369 // 2. Now for each zone it is necessary to determine the most priority object
370 // and assign to zone to corresponding region
371 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
372 while( anIter.hasNext() )
374 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
375 HYDROData_Zone::MergeAltitudesType aMergeType;
376 Handle(HYDROData_Object) aRegObj = aPr.GetMostPriorityObject( aSplitData.ObjectNames, aMergeType );
377 if( aRegObj.IsNull() )
379 Handle(HYDROData_Region) aRegion = aRegionsMap[aRegObj->GetName()];
380 if( aRegion.IsNull() )
382 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
384 if( aSplitData.ObjectNames.count() > 1 && aMergeType==HYDROData_Zone::Merge_UNKNOWN )
386 qDebug( "Error in algorithm: unresolved conflicts" );
391 case HYDROData_Zone::Merge_ZMIN:
392 case HYDROData_Zone::Merge_ZMAX:
393 aRegionZone->SetMergeType( aMergeType );
395 case HYDROData_Zone::Merge_Object:
396 aRegionZone->SetMergeType( aMergeType );
397 aRegionZone->RemoveMergeAltitude();
398 aRegionZone->SetMergeAltitude( aRegObj->GetAltitudeObject() );
403 QStringList anObjectsWithEmptyRegions;
404 QMap<QString, Handle(HYDROData_Region)>::const_iterator
405 anIt = aRegionsMap.begin(), aLast = aRegionsMap.end();
406 for( ; anIt!=aLast; anIt++ )
408 Handle(HYDROData_Region) aRegion = anIt.value();
409 if( aRegion->GetZones().IsEmpty() )
411 QString aRegName = aRegion->GetName();
412 QString anObjName = aRegionNameToObjNameMap[aRegName];
413 anObjectsWithEmptyRegions.append( anObjName );
417 if( !anObjectsWithEmptyRegions.empty() )
419 QString aData = anObjectsWithEmptyRegions.join( ", " );
420 SetWarning( WARN_EMPTY_REGIONS, aData );
424 void HYDROData_CalculationCase::CreateEdgeGroupsDef( const Handle(HYDROData_Document)& theDoc,
425 const HYDROData_SplitToZonesTool::SplitDataList& theEdges )
427 QMap<QString,Handle(HYDROData_SplittedShapesGroup)> aSplittedEdgesGroupsMap;
429 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theEdges );
430 while( anIter.hasNext() )
432 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
433 // Create new edges group
434 if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
437 QString anObjName = aSplitData.ObjectNames.first();
438 if ( anObjName.isEmpty() )
440 #ifdef DEB_CALCULATION
441 QString aStr = aSplitData.ObjectNames.join(" ");
442 cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl;
444 Handle(HYDROData_SplittedShapesGroup) aSplittedGroup;
445 if ( !aSplittedEdgesGroupsMap.contains( anObjName ) )
447 aSplittedGroup = addNewSplittedGroup( CALCULATION_GROUPS_PREF + anObjName );
448 aSplittedEdgesGroupsMap.insert( anObjName, aSplittedGroup );
452 aSplittedGroup = aSplittedEdgesGroupsMap[ anObjName ];
454 if ( aSplittedGroup.IsNull() )
457 aSplittedGroup->AddShape( aSplitData.Shape );
461 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
463 if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
464 return false; // Wrong type of object
466 if ( HasReference( theObject, DataTag_GeometryObject ) )
467 return false; // Object is already in reference list
469 AddReferenceObject( theObject, DataTag_GeometryObject );
471 // Indicate model of the need to update splitting
477 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
479 return GetReferenceObjects( DataTag_GeometryObject );
482 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
484 if ( theObject.IsNull() )
487 RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
489 // Indicate model of the need to update splitting
493 void HYDROData_CalculationCase::RemoveGeometryObjects()
495 ClearReferenceObjects( DataTag_GeometryObject );
497 // Indicate model of the need to update splitting
501 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
503 if ( theGroup.IsNull() )
506 if ( HasReference( theGroup, DataTag_GeometryGroup ) )
507 return false; // Object is already in reference list
509 AddReferenceObject( theGroup, DataTag_GeometryGroup );
511 // Indicate model of the need to update splitting
517 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
519 return GetReferenceObjects( DataTag_GeometryGroup );
522 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
524 if ( theGroup.IsNull() )
527 RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
529 // Indicate model of the need to update splitting
533 void HYDROData_CalculationCase::RemoveGeometryGroups()
535 ClearReferenceObjects( DataTag_GeometryGroup );
537 // Indicate model of the need to update splitting
541 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
543 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
545 SetReferenceObject( thePolyline, DataTag_Polyline );
547 // Indicate model of the need to update zones splitting
548 SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
551 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
553 return Handle(HYDROData_PolylineXY)::DownCast(
554 GetReferenceObject( DataTag_Polyline ) );
557 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
559 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
561 ClearReferenceObjects( DataTag_Polyline );
563 // Indicate model of the need to update zones splitting
564 SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
567 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
569 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
570 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
571 if ( aNewRegion.IsNull() )
574 aNewRegion->AddZone( theZone );
579 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
581 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
583 if ( theRegion.IsNull() )
586 if ( HasReference( theRegion, DataTag_Region ) )
587 return false; // Object is already in reference list
589 // Move the region from other calculation
590 Handle(HYDROData_CalculationCase) aFatherCalc =
591 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
592 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
594 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
595 theRegion->CopyTo( aNewRegion );
597 aFatherCalc->RemoveRegion( theRegion );
599 theRegion->SetLabel( aNewRegion->Label() );
603 AddReferenceObject( theRegion, DataTag_Region );
609 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
611 return GetReferenceObjects( DataTag_Region );
614 void HYDROData_CalculationCase::UpdateRegionsOrder()
616 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
617 if ( aDocument.IsNull() )
620 HYDROData_SequenceOfObjects aRegions = GetRegions();
622 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
623 for ( ; anIter.More(); anIter.Next() )
625 Handle(HYDROData_Region) aRegion =
626 Handle(HYDROData_Region)::DownCast( anIter.Value() );
627 if ( aRegion.IsNull() )
630 aRegion->SetName( "" );
633 QString aRegsPref = CALCULATION_REGIONS_PREF;
635 anIter.Init( aRegions );
636 for ( ; anIter.More(); anIter.Next() )
638 Handle(HYDROData_Region) aRegion =
639 Handle(HYDROData_Region)::DownCast( anIter.Value() );
640 if ( aRegion.IsNull() )
643 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
644 aRegion->SetName( aRegionName );
648 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
650 if ( theRegion.IsNull() )
653 RemoveReferenceObject( theRegion->Label(), DataTag_Region );
655 // Remove region from data model
656 Handle(HYDROData_CalculationCase) aFatherCalc =
657 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
658 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
662 void HYDROData_CalculationCase::RemoveRegions()
664 myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
667 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
669 return GetReferenceObjects( DataTag_SplittedGroups );
672 void HYDROData_CalculationCase::RemoveSplittedGroups()
674 myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
677 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
679 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
680 return GetAltitudeForPoint( thePoint, aZone );
683 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
684 const Handle(HYDROData_Region)& theRegion ) const
686 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
688 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
689 if ( !aZone.IsNull() )
691 Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
692 if ( IsEqual( aRefRegion, theRegion ) )
693 aResAltitude = GetAltitudeForPoint( thePoint, aZone );
699 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
700 const Handle(HYDROData_Zone)& theZone ) const
702 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
703 if ( theZone.IsNull() )
706 HYDROData_Zone::MergeAltitudesType aZoneMergeType = theZone->GetMergeType();
707 if ( !theZone->IsMergingNeed() )
709 aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
711 else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
716 HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
717 if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
719 Handle(HYDROData_IAltitudeObject) aMergeAltitude = theZone->GetMergeAltitude();
720 if ( !aMergeAltitude.IsNull() )
722 if ( aZoneInterpolator != NULL )
724 aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
725 aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
728 aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
733 HYDROData_SequenceOfObjects aZoneObjects = theZone->GetGeometryObjects();
734 HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
735 for ( ; anIter.More(); anIter.Next() )
737 Handle(HYDROData_Object) aZoneObj =
738 Handle(HYDROData_Object)::DownCast( anIter.Value() );
739 if ( aZoneObj.IsNull() )
742 Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
743 if ( anObjAltitude.IsNull() )
746 double aPointAltitude = 0.0;
747 if ( aZoneInterpolator != NULL )
749 aZoneInterpolator->SetAltitudeObject( anObjAltitude );
750 aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
753 aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
755 if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
758 if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
760 aResAltitude = aPointAltitude;
763 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
765 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
766 aResAltitude > aPointAltitude )
768 aResAltitude = aPointAltitude;
771 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
773 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
774 aResAltitude < aPointAltitude )
776 aResAltitude = aPointAltitude;
785 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
786 const NCollection_Sequence<gp_XY>& thePoints,
787 const Handle(HYDROData_Region)& theRegion ) const
789 NCollection_Sequence<double> aResSeq;
791 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
793 const gp_XY& thePnt = thePoints.Value( i );
795 double anAltitude = GetAltitudeForPoint( thePnt, theRegion );
796 aResSeq.Append( anAltitude );
802 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
803 const NCollection_Sequence<gp_XY>& thePoints,
804 const Handle(HYDROData_Zone)& theZone ) const
806 NCollection_Sequence<double> aResSeq;
808 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
810 const gp_XY& thePnt = thePoints.Value( i );
812 double anAltitude = GetAltitudeForPoint( thePnt, theZone );
813 aResSeq.Append( anAltitude );
819 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
821 Handle(HYDROData_Region) aResRegion;
823 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
824 if ( !aZone.IsNull() )
825 aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
830 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
832 Handle(HYDROData_Zone) aResZone;
834 HYDROData_SequenceOfObjects aRegions = GetRegions();
836 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
837 for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
839 Handle(HYDROData_Region) aRegion =
840 Handle(HYDROData_Region)::DownCast( anIter.Value() );
841 if ( aRegion.IsNull() )
844 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
845 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
846 for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
848 Handle(HYDROData_Zone) aRegZone =
849 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
850 if ( aRegZone.IsNull() )
853 PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
854 if ( aPointRelation != POINT_OUT )
855 aResZone = aRegZone; // We found the desired zone
862 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
863 const gp_XY& thePoint,
864 const Handle(HYDROData_Zone)& theZone ) const
866 PointClassification aRes = POINT_OUT;
867 if ( theZone.IsNull() )
870 TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
871 if ( aZoneFace.IsNull() )
874 TopoDS_Compound aCmp;
876 aBB.MakeCompound(aCmp);
877 aBB.Add(aCmp, aZoneFace);
878 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
879 BRepBuilderAPI_MakeVertex aMk(aPnt);
880 aBB.Add(aCmp, aMk.Vertex());
881 BRepTools::Write(aCmp, "FCL2d.brep");
883 TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
884 if (State == TopAbs_OUT)
886 else if(State == TopAbs_IN)
888 else if(State == TopAbs_ON)
893 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
894 const QString& thePrefixOrName,
897 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
898 int aTag = aNewLab.Tag();
900 Handle(HYDROData_Region) aNewRegion =
901 Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
902 AddRegion( aNewRegion );
904 QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
905 aNewRegion->SetName( aRegionName );
910 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup( const QString& theName )
912 TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
914 Handle(HYDROData_SplittedShapesGroup) aNewGroup =
915 Handle(HYDROData_SplittedShapesGroup)::DownCast(
916 HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
917 AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
919 aNewGroup->SetName( theName );
924 QString HYDROData_CalculationCase::Export( int theStudyId ) const
926 GEOM::GEOM_Gen_var aGEOMEngine = HYDROData_GeomTool::GetGeomGen();
927 SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( theStudyId );
929 QString aGeomObjEntry;
930 bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry );
931 return isOK ? aGeomObjEntry : QString();
934 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
935 SALOMEDS::Study_ptr theStudy,
936 QString& theGeomObjEntry ) const
938 HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
940 // Get groups definitions
941 HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
943 HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
944 for ( ; anIter.More(); anIter.Next() )
947 Handle(HYDROData_ShapesGroup) aGroup =
948 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
949 if ( aGroup.IsNull() )
952 HYDROData_ShapesGroup::GroupDefinition aGroupDef;
954 aGroupDef.Name = aGroup->GetName().toLatin1().constData();
955 aGroup->GetShapes( aGroupDef.Shapes );
957 aSeqOfGroupsDefs.Append( aGroupDef );
961 TopTools_ListOfShape aFaces;
962 HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
963 HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
964 for ( ; aRegionIter.More(); aRegionIter.Next() )
966 Handle(HYDROData_Region) aRegion =
967 Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
968 if( aRegion.IsNull() )
971 TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
972 aFaces.Append( aRegionShape );
975 return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs, theGeomObjEntry );
978 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
979 SALOMEDS::Study_ptr theStudy,
980 const TopTools_ListOfShape& theFaces,
981 const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs,
982 QString& theGeomObjEntry ) const
985 BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
986 aSewing.SetNonManifoldMode( Standard_False );
987 #ifdef DEB_CALCULATION
988 TCollection_AsciiString aNam("Sh_");
991 TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
992 for ( ; aFaceIter.More(); aFaceIter.Next() )
994 TopoDS_Shape aShape = aFaceIter.Value();
995 if ( aShape.IsNull() )
998 if ( aShape.ShapeType() == TopAbs_FACE )
1000 aSewing.Add( aShape );
1001 #ifdef DEB_CALCULATION
1002 TCollection_AsciiString aName = aNam + ++i + ".brep";
1003 BRepTools::Write(aShape ,aName.ToCString());
1008 #ifdef DEB_CALCULATION
1011 TopExp_Explorer anExp( aShape, TopAbs_FACE );
1012 for (; anExp.More(); anExp.Next() ) {
1013 aSewing.Add( anExp.Current() );
1014 #ifdef DEB_CALCULATION
1016 TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
1017 BRepTools::Write(anExp.Current() ,aName.ToCString());
1024 TopoDS_Shape aSewedShape = aSewing.SewedShape();
1026 // If the sewed shape is empty - return false
1027 if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
1030 #ifdef DEB_CALCULATION
1031 BRepTools::Write(aSewedShape ,"Sew.brep");
1033 // Publish the sewed shape
1034 QString aName = EXPORT_NAME;
1035 GEOM::GEOM_Object_ptr aMainShape =
1036 publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName, theGeomObjEntry );
1038 if ( aMainShape->_is_nil() )
1041 if ( theGroupsDefs.IsEmpty() )
1045 TopTools_IndexedMapOfShape aMapOfSubShapes;
1046 TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
1048 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
1050 for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
1052 const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
1054 NCollection_Sequence<int> aGroupIndexes;
1055 for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
1057 const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
1058 #ifdef DEB_CALCULATION
1059 cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
1062 TopoDS_Shape aModifiedShape = aShape;
1063 if ( aSewing.IsModified( aShape ) )
1064 aModifiedShape = aSewing.Modified( aShape );
1065 else if ( aSewing.IsModifiedSubShape( aShape ) )
1066 aModifiedShape = aSewing.ModifiedSubShape( aShape );
1068 #ifdef DEB_CALCULATION
1069 const TopLoc_Location& aL1 = aShape.Location();
1070 const TopLoc_Location& aL2 = aModifiedShape.Location();
1071 cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
1074 int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
1075 if ( anIndex > 0 ) {
1076 aGroupIndexes.Append( anIndex );
1078 #ifdef DEB_CALCULATION
1079 TCollection_AsciiString aNam("Lost_");
1080 if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1081 for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1083 const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1084 if ( aModifiedShape.IsPartner( aS ) )
1086 cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1087 TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1088 BRepTools::Write(aS ,aName.ToCString());
1096 if ( !aGroupIndexes.IsEmpty() )
1097 aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1100 if ( !aGroupsData.IsEmpty() )
1102 GEOM::GEOM_IGroupOperations_var aGroupOp =
1103 theGeomEngine->GetIGroupOperations( theStudy->StudyId() );
1105 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1106 for ( ; aMapIt.More(); aMapIt.Next() )
1108 const TCollection_AsciiString& aGroupName = aMapIt.Key();
1109 const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1111 GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1112 if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1115 GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1116 aGeomIndexes->length( aGroupIndexes.Length() );
1118 for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1119 aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1121 aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1122 if ( aGroupOp->IsDone() )
1124 SALOMEDS::SObject_var aGroupSO =
1125 theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1133 GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM(
1134 GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
1135 const TopoDS_Shape& theShape, const QString& theName,
1136 QString& theGeomObjEntry ) const
1138 theGeomObjEntry = "";
1139 GEOM::GEOM_Object_var aGeomObj;
1141 if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
1142 theShape.IsNull() ) {
1143 return aGeomObj._retn();
1146 std::ostringstream aStreamShape;
1147 // Write TopoDS_Shape in ASCII format to the stream
1148 BRepTools::Write( theShape, aStreamShape );
1149 // Returns the number of bytes that have been stored in the stream's buffer.
1150 int aSize = aStreamShape.str().size();
1151 // Allocate octect buffer of required size
1152 CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
1153 // Copy ostrstream content to the octect buffer
1154 memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
1156 SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
1158 // Restore shape from the stream and get the GEOM object
1159 GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
1160 aGeomObj = anInsOp->RestoreShape( aSeqFile );
1162 // Puplish the GEOM object
1163 if ( !aGeomObj->_is_nil() ) {
1164 QString aName = HYDROData_GeomTool::GetFreeName( theStudy, theName );
1166 SALOMEDS::SObject_var aResultSO =
1167 theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(),
1168 aGeomObj, qPrintable( aName ) );
1169 if ( aResultSO->_is_nil() ) {
1170 aGeomObj = GEOM::GEOM_Object::_nil();
1173 theGeomObjEntry = aResultSO->GetID();
1176 return aGeomObj._retn();
1179 void HYDROData_CalculationCase::ClearRules( const bool theIsSetToUpdate )
1181 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1182 HYDROData_PriorityQueue::ClearRules( aRulesLab );
1184 // Indicate model of the need to update splitting
1185 if ( theIsSetToUpdate ) {
1186 SetToUpdate( true );
1190 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Object)& theObject1,
1191 HYDROData_PriorityType thePriority,
1192 const Handle(HYDROData_Object)& theObject2,
1193 HYDROData_Zone::MergeAltitudesType theMergeType )
1195 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1196 HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1198 // Indicate model of the need to update splitting
1199 SetToUpdate( true );
1202 QString HYDROData_CalculationCase::DumpRules() const
1204 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1205 return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1208 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1210 TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1211 TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1213 // Indicate model of the need to update splitting
1214 SetToUpdate( true );
1217 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1219 Handle(TDataStd_Integer) aModeAttr;
1220 bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1222 return ( AssignmentMode ) aModeAttr->Get();
1227 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1228 QStringList& theScript ) const
1230 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1231 HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1234 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1236 return myLastWarning;
1239 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1241 myLastWarning.Type = theType;
1242 myLastWarning.Data = theData;
1245 bool HYDROData_CalculationCase::GetRule( int theIndex,
1246 Handle(HYDROData_Object)& theObject1,
1247 HYDROData_PriorityType& thePriority,
1248 Handle(HYDROData_Object)& theObject2,
1249 HYDROData_Zone::MergeAltitudesType& theMergeType ) const
1251 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1252 return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1253 theObject1, thePriority, theObject2, theMergeType );