2 #include "HYDROData_CalculationCase.h"
4 #include "HYDROData_ArtificialObject.h"
5 #include "HYDROData_IAltitudeObject.h"
6 #include "HYDROData_Document.h"
7 #include "HYDROData_ShapesGroup.h"
8 #include "HYDROData_Iterator.h"
9 #include "HYDROData_NaturalObject.h"
10 #include "HYDROData_PolylineXY.h"
11 #include "HYDROData_SplittedShapesGroup.h"
12 #include "HYDROData_Region.h"
13 #include "HYDROData_Tool.h"
20 #include <TopoDS_Shell.hxx>
21 #include <TopoDS_Edge.hxx>
23 #include <BRep_Builder.hxx>
24 #include <BRepBuilderAPI_Sewing.hxx>
25 #include <BRepTopAdaptor_FClass2d.hxx>
27 #include <BRepTools.hxx>
30 #include <TopExp_Explorer.hxx>
32 #include <TopTools_ListOfShape.hxx>
33 #include <TopTools_ListIteratorOfListOfShape.hxx>
34 #include <TDataStd_Integer.hxx>
36 //#define DEB_CALCULATION 1
37 #ifdef DEB_CALCULATION
38 #include <BRepTools.hxx>
39 #include <TopLoc_Location.hxx>
41 #define CALCULATION_REGIONS_PREF GetName() + "_Reg"
42 #define CALCULATION_ZONES_PREF GetName() + "_Zone"
43 #define CALCULATION_GROUPS_PREF GetName() + "_"
44 //#define DEB_CLASS2D 1
46 #include <BRepBuilderAPI_MakeVertex.hxx>
49 #define EXPORT_NAME "HYDRO_" + GetName()
51 #include <SALOME_NamingService.hxx>
52 #include <SALOME_LifeCycleCORBA.hxx>
58 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
59 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
61 HYDROData_CalculationCase::HYDROData_CalculationCase()
66 HYDROData_CalculationCase::~HYDROData_CalculationCase()
70 void HYDROData_CalculationCase::SetName( const QString& theName )
72 QString anOldCaseName = GetName();
73 if ( anOldCaseName != theName )
75 HYDROData_SequenceOfObjects aRegions = GetRegions();
77 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
78 for ( ; anIter.More(); anIter.Next() )
80 Handle(HYDROData_Region) aRegion =
81 Handle(HYDROData_Region)::DownCast( anIter.Value() );
82 if ( aRegion.IsNull() )
85 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegion );
87 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
88 HYDROData_SequenceOfObjects::Iterator anIter( aZones );
89 for ( ; anIter.More(); anIter.Next() )
91 Handle(HYDROData_Zone) aRegZone =
92 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
93 if ( aRegZone.IsNull() )
96 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegZone );
100 HYDROData_SequenceOfObjects aGroups = GetSplittedGroups();
102 anIter.Init( aGroups );
103 for ( ; anIter.More(); anIter.Next() )
105 Handle(HYDROData_SplittedShapesGroup) aGroup =
106 Handle(HYDROData_SplittedShapesGroup)::DownCast( anIter.Value() );
107 if ( aGroup.IsNull() )
110 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aGroup );
114 HYDROData_Entity::SetName( theName );
117 QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
119 QStringList aResList = dumpObjectCreation( theTreatedObjects );
120 aResList.prepend( "# Calculation case" );
122 QString aCalculName = GetObjPyName();
124 AssignmentMode aMode = GetAssignmentMode();
125 QString aModeStr = aMode==MANUAL ? "HYDROData_CalculationCase.MANUAL" : "HYDROData_CalculationCase.AUTOMATIC";
126 aResList << QString( "%0.SetAssignmentMode( %1 )" ).arg( aCalculName ).arg( aModeStr );
128 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
129 HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
130 for ( ; anIter.More(); anIter.Next() )
132 Handle(HYDROData_Object) aRefGeomObj =
133 Handle(HYDROData_Object)::DownCast( anIter.Value() );
134 setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
136 aResList << QString( "" );
138 QString aGroupName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "case_geom_group" );
140 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
141 anIter.Init( aGeomGroups );
142 for ( ; anIter.More(); anIter.Next() )
144 Handle(HYDROData_ShapesGroup) aGeomGroup =
145 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
146 if ( aGeomGroup.IsNull() )
149 Handle(HYDROData_Object) aFatherGeom =
150 Handle(HYDROData_Object)::DownCast( aGeomGroup->GetFatherObject() );
151 if ( aFatherGeom.IsNull() )
154 int aGroupId = aFatherGeom->GetGroupId( aGeomGroup );
155 aResList << QString( "%1 = %2.GetGroup( %3 );" )
156 .arg( aGroupName ).arg( aFatherGeom->GetObjPyName() ).arg( aGroupId );
158 aResList << QString( "%1.AddGeometryGroup( %2 );" ).arg( aCalculName ).arg( aGroupName );
161 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
162 setPythonReferenceObject( theTreatedObjects, aResList, aBoundaryPolyline, "SetBoundaryPolyline" );
164 if( aMode==AUTOMATIC )
165 DumpRulesToPython( aCalculName, aResList );
167 aResList << QString( "" );
168 aResList << "# Start the algorithm of the partition and assignment";
169 aResList << QString( "%1.Update();" ).arg( aCalculName );
173 // Now we restore the regions and zones order
174 HYDROData_SequenceOfObjects aRegions = GetRegions();
175 anIter.Init( aRegions );
176 for ( ; anIter.More(); anIter.Next() )
178 Handle(HYDROData_Region) aRegion =
179 Handle(HYDROData_Region)::DownCast( anIter.Value() );
180 if ( aRegion.IsNull() )
183 theTreatedObjects.insert( aRegion->GetName(), aRegion );
184 QStringList aRegDump = aRegion->DumpToPython( theTreatedObjects );
185 aResList << aRegDump;
189 // Export calculation case
190 aResList << QString( "" );
191 aResList << "# Export of the calculation case";
192 QString aStudyName = "theStudy";
193 QString anEntryVar = aCalculName + "_entry";
194 aResList << QString( "%1 = %2.Export( %3._get_StudyId() )" ).arg( anEntryVar ).arg( aCalculName ).arg( aStudyName );
196 // Get geometry object and print debug information
198 aResList << "# Get geometry shape and print debug information";
199 aResList << "import GEOM";
200 aResList << QString( "print \"Entry:\", %1" ).arg( anEntryVar );
201 QString aGeomShapeName = aCalculName + "_geom";
202 aResList << QString( "%1 = salome.IDToObject( str( %2 ) )" ).arg( aGeomShapeName ).arg( anEntryVar );
203 aResList << QString( "print \"Geom shape:\", %1" ).arg( aGeomShapeName );
204 aResList << QString( "print \"Geom shape name:\", %1.GetName()" ).arg( aGeomShapeName );
206 DumpSampleMeshing( aResList, aStudyName, aGeomShapeName, aCalculName+"_mesh" );
208 aResList << QString( "" );
212 void HYDROData_CalculationCase::DumpSampleMeshing( QStringList& theResList,
213 const QString& theStudyName,
214 const QString& theGeomShapeName,
215 const QString& theMeshName ) const
218 theResList << "# Meshing";
219 theResList << "import SMESH, SALOMEDS";
220 theResList << "from salome.smesh import smeshBuilder";
221 theResList << "from salome.geom import geomBuilder";
223 theResList << QString( "smesh = smeshBuilder.New( %1 )" ).arg( theStudyName );
224 theResList << QString( "%1 = smesh.Mesh( %2 )" ).arg( theMeshName ).arg( theGeomShapeName );
225 theResList << QString( "MEFISTO_2D = %1.Triangle( algo=smeshBuilder.MEFISTO )" ).arg( theMeshName );
226 theResList << "Max_Element_Area_1 = MEFISTO_2D.MaxElementArea( 10 )";
227 theResList << QString( "Regular_1D = %1.Segment()" ).arg( theMeshName );
228 theResList << "Max_Size_1 = Regular_1D.MaxSize(10)";
229 theResList << QString( "isDone = %1.Compute()" ).arg( theMeshName );
232 theResList << "# Set names of Mesh objects";
233 theResList << "smesh.SetName( MEFISTO_2D.GetAlgorithm(), 'MEFISTO_2D' )";
234 theResList << "smesh.SetName( Regular_1D.GetAlgorithm(), 'Regular_1D' )";
235 theResList << "smesh.SetName( Max_Size_1, 'Max Size_1' )";
236 theResList << "smesh.SetName( Max_Element_Area_1, 'Max. Element Area_1' )";
237 theResList << QString( "smesh.SetName( %1.GetMesh(), '%1' )" ).arg( theMeshName );
240 theResList << "# Greate SMESH groups";
241 theResList << QString( "geompy = geomBuilder.New( %1 )" ).arg( theStudyName );
242 theResList << QString( "geom_groups = geompy.GetGroups( %1 )" ).arg( theGeomShapeName );
243 theResList << QString( "for group in geom_groups:" );
244 theResList << QString( " smesh_group = %1.GroupOnGeom(group, group.GetName(), SMESH.EDGE)" )
246 theResList << QString( " smesh.SetName(smesh_group, group.GetName())" );
247 theResList << QString( " print \"SMESH group '%s': %s\" % (smesh_group.GetName(), smesh_group)" );
250 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
252 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
254 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
255 if ( !aBoundaryPolyline.IsNull() )
256 aResSeq.Append( aBoundaryPolyline );
258 HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
259 aResSeq.Append( aSeqOfRegions );
264 void HYDROData_CalculationCase::Update()
266 HYDROData_Entity::Update();
269 // At first we remove previously created objects
271 RemoveSplittedGroups();
273 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
274 if ( aDocument.IsNull() )
277 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
278 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
279 if ( aGeomObjects.IsEmpty() )
282 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
284 HYDROData_SplitToZonesTool::SplitDataList aSplitObjects =
285 HYDROData_SplitToZonesTool::Split( aGeomObjects, aGeomGroups, aBoundaryPolyline );
286 if ( aSplitObjects.isEmpty() )
289 HYDROData_SplitToZonesTool::SplitDataList aZonesList, anEdgesList;
291 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitObjects );
292 while( anIter.hasNext() )
294 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
295 if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Zone )
296 aZonesList.append( aSplitData );
297 else if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Edge )
298 anEdgesList.append( aSplitData );
301 switch( GetAssignmentMode() )
304 CreateRegionsDef( aDocument, aZonesList );
307 CreateRegionsAuto( aDocument, aZonesList );
310 CreateEdgeGroupsDef( aDocument, anEdgesList );
313 void HYDROData_CalculationCase::CreateRegionsDef( const Handle(HYDROData_Document)& theDoc,
314 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
316 // Create result regions for case, by default one zone for one region
317 QString aRegsPref = CALCULATION_REGIONS_PREF;
318 QString aZonesPref = CALCULATION_ZONES_PREF;
320 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
321 while( anIter.hasNext() )
323 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
325 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegsPref );
327 // Add the zone for region
328 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
332 void HYDROData_CalculationCase::CreateRegionsAuto( const Handle(HYDROData_Document)& theDoc,
333 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
335 QMap<QString, Handle(HYDROData_Region)> aRegionsMap; //object name to region
336 QMap<QString, QString> aRegionNameToObjNameMap;
337 QString aZonesPref = CALCULATION_ZONES_PREF;
338 HYDROData_PriorityQueue aPr( this );
340 // 1. First we create a default region for each object included into the calculation case
341 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
342 for( int i=aGeomObjects.Lower(), n=aGeomObjects.Upper(); i<=n; i++ )
344 Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( aGeomObjects.Value( i ) );
347 QString anObjName = anObj->GetName();
348 QString aRegName = anObjName + "_reg";
349 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegName, false );
350 aRegionsMap.insert( anObjName, aRegion );
351 aRegionNameToObjNameMap.insert( aRegName, anObjName );
354 // 2. Now for each zone it is necessary to determine the most priority object
355 // and assign to zone to corresponding region
356 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
357 while( anIter.hasNext() )
359 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
360 HYDROData_Zone::MergeAltitudesType aMergeType;
361 Handle(HYDROData_Object) aRegObj = aPr.GetMostPriorityObject( aSplitData.ObjectNames, aMergeType );
362 if( aRegObj.IsNull() )
364 Handle(HYDROData_Region) aRegion = aRegionsMap[aRegObj->GetName()];
365 if( aRegion.IsNull() )
367 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
369 if( aSplitData.ObjectNames.count() > 1 && aMergeType==HYDROData_Zone::Merge_UNKNOWN )
371 qDebug( "Error in algorithm: unresolved conflicts" );
376 case HYDROData_Zone::Merge_ZMIN:
377 case HYDROData_Zone::Merge_ZMAX:
378 aRegionZone->SetMergeType( aMergeType );
380 case HYDROData_Zone::Merge_Object:
381 aRegionZone->SetMergeType( aMergeType );
382 aRegionZone->RemoveMergeAltitude();
383 aRegionZone->SetMergeAltitude( aRegObj->GetAltitudeObject() );
388 QStringList anObjectsWithEmptyRegions;
389 QMap<QString, Handle(HYDROData_Region)>::const_iterator
390 anIt = aRegionsMap.begin(), aLast = aRegionsMap.end();
391 for( ; anIt!=aLast; anIt++ )
393 Handle(HYDROData_Region) aRegion = anIt.value();
394 if( aRegion->GetZones().IsEmpty() )
396 QString aRegName = aRegion->GetName();
397 QString anObjName = aRegionNameToObjNameMap[aRegName];
398 anObjectsWithEmptyRegions.append( anObjName );
402 if( !anObjectsWithEmptyRegions.empty() )
404 QString aData = anObjectsWithEmptyRegions.join( ", " );
405 SetWarning( WARN_EMPTY_REGIONS, aData );
409 void HYDROData_CalculationCase::CreateEdgeGroupsDef( const Handle(HYDROData_Document)& theDoc,
410 const HYDROData_SplitToZonesTool::SplitDataList& theEdges )
412 QMap<QString,Handle(HYDROData_SplittedShapesGroup)> aSplittedEdgesGroupsMap;
414 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theEdges );
415 while( anIter.hasNext() )
417 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
418 // Create new edges group
419 if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
422 QString anObjName = aSplitData.ObjectNames.first();
423 if ( anObjName.isEmpty() )
425 #ifdef DEB_CALCULATION
426 QString aStr = aSplitData.ObjectNames.join(" ");
427 cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl;
429 Handle(HYDROData_SplittedShapesGroup) aSplittedGroup;
430 if ( !aSplittedEdgesGroupsMap.contains( anObjName ) )
432 aSplittedGroup = addNewSplittedGroup( CALCULATION_GROUPS_PREF + anObjName );
433 aSplittedEdgesGroupsMap.insert( anObjName, aSplittedGroup );
437 aSplittedGroup = aSplittedEdgesGroupsMap[ anObjName ];
439 if ( aSplittedGroup.IsNull() )
442 aSplittedGroup->AddShape( aSplitData.Shape );
446 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
448 if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
449 return false; // Wrong type of object
451 if ( HasReference( theObject, DataTag_GeometryObject ) )
452 return false; // Object is already in reference list
454 AddReferenceObject( theObject, DataTag_GeometryObject );
456 // Indicate model of the need to update splitting
462 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
464 return GetReferenceObjects( DataTag_GeometryObject );
467 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
469 if ( theObject.IsNull() )
472 RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
474 // Indicate model of the need to update splitting
478 void HYDROData_CalculationCase::RemoveGeometryObjects()
480 ClearReferenceObjects( DataTag_GeometryObject );
482 // Indicate model of the need to update splitting
486 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
488 if ( theGroup.IsNull() )
491 if ( HasReference( theGroup, DataTag_GeometryGroup ) )
492 return false; // Object is already in reference list
494 AddReferenceObject( theGroup, DataTag_GeometryGroup );
496 // Indicate model of the need to update splitting
502 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
504 return GetReferenceObjects( DataTag_GeometryGroup );
507 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
509 if ( theGroup.IsNull() )
512 RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
514 // Indicate model of the need to update splitting
518 void HYDROData_CalculationCase::RemoveGeometryGroups()
520 ClearReferenceObjects( DataTag_GeometryGroup );
522 // Indicate model of the need to update splitting
526 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
528 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
530 SetReferenceObject( thePolyline, DataTag_Polyline );
532 // Indicate model of the need to update zones splitting
533 SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
536 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
538 return Handle(HYDROData_PolylineXY)::DownCast(
539 GetReferenceObject( DataTag_Polyline ) );
542 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
544 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
546 ClearReferenceObjects( DataTag_Polyline );
548 // Indicate model of the need to update zones splitting
549 SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
552 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
554 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
555 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
556 if ( aNewRegion.IsNull() )
559 aNewRegion->AddZone( theZone );
564 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
566 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
568 if ( theRegion.IsNull() )
571 if ( HasReference( theRegion, DataTag_Region ) )
572 return false; // Object is already in reference list
574 // Move the region from other calculation
575 Handle(HYDROData_CalculationCase) aFatherCalc =
576 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
577 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
579 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
580 theRegion->CopyTo( aNewRegion );
582 aFatherCalc->RemoveRegion( theRegion );
584 theRegion->SetLabel( aNewRegion->Label() );
588 AddReferenceObject( theRegion, DataTag_Region );
594 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
596 return GetReferenceObjects( DataTag_Region );
599 void HYDROData_CalculationCase::UpdateRegionsOrder()
601 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
602 if ( aDocument.IsNull() )
605 HYDROData_SequenceOfObjects aRegions = GetRegions();
607 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
608 for ( ; anIter.More(); anIter.Next() )
610 Handle(HYDROData_Region) aRegion =
611 Handle(HYDROData_Region)::DownCast( anIter.Value() );
612 if ( aRegion.IsNull() )
615 aRegion->SetName( "" );
618 QString aRegsPref = CALCULATION_REGIONS_PREF;
620 anIter.Init( aRegions );
621 for ( ; anIter.More(); anIter.Next() )
623 Handle(HYDROData_Region) aRegion =
624 Handle(HYDROData_Region)::DownCast( anIter.Value() );
625 if ( aRegion.IsNull() )
628 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
629 aRegion->SetName( aRegionName );
633 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
635 if ( theRegion.IsNull() )
638 RemoveReferenceObject( theRegion->Label(), DataTag_Region );
640 // Remove region from data model
641 Handle(HYDROData_CalculationCase) aFatherCalc =
642 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
643 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
647 void HYDROData_CalculationCase::RemoveRegions()
649 myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
652 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
654 return GetReferenceObjects( DataTag_SplittedGroups );
657 void HYDROData_CalculationCase::RemoveSplittedGroups()
659 myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
662 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
664 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
665 return GetAltitudeForPoint( thePoint, aZone );
668 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
669 const Handle(HYDROData_Region)& theRegion ) const
671 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
673 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
674 if ( !aZone.IsNull() )
676 Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
677 if ( IsEqual( aRefRegion, theRegion ) )
678 aResAltitude = GetAltitudeForPoint( thePoint, aZone );
684 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
685 const Handle(HYDROData_Zone)& theZone ) const
687 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
688 if ( theZone.IsNull() )
691 HYDROData_Zone::MergeAltitudesType aZoneMergeType = theZone->GetMergeType();
692 if ( !theZone->IsMergingNeed() )
694 aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
696 else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
701 HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
702 if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
704 Handle(HYDROData_IAltitudeObject) aMergeAltitude = theZone->GetMergeAltitude();
705 if ( !aMergeAltitude.IsNull() )
707 if ( aZoneInterpolator != NULL )
709 aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
710 aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
713 aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
718 HYDROData_SequenceOfObjects aZoneObjects = theZone->GetGeometryObjects();
719 HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
720 for ( ; anIter.More(); anIter.Next() )
722 Handle(HYDROData_Object) aZoneObj =
723 Handle(HYDROData_Object)::DownCast( anIter.Value() );
724 if ( aZoneObj.IsNull() )
727 Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
728 if ( anObjAltitude.IsNull() )
731 double aPointAltitude = 0.0;
732 if ( aZoneInterpolator != NULL )
734 aZoneInterpolator->SetAltitudeObject( anObjAltitude );
735 aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
738 aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
740 if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
743 if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
745 aResAltitude = aPointAltitude;
748 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
750 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
751 aResAltitude > aPointAltitude )
753 aResAltitude = aPointAltitude;
756 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
758 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
759 aResAltitude < aPointAltitude )
761 aResAltitude = aPointAltitude;
770 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
771 const NCollection_Sequence<gp_XY>& thePoints,
772 const Handle(HYDROData_Region)& theRegion ) const
774 NCollection_Sequence<double> aResSeq;
776 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
778 const gp_XY& thePnt = thePoints.Value( i );
780 double anAltitude = GetAltitudeForPoint( thePnt, theRegion );
781 aResSeq.Append( anAltitude );
787 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
788 const NCollection_Sequence<gp_XY>& thePoints,
789 const Handle(HYDROData_Zone)& theZone ) const
791 NCollection_Sequence<double> aResSeq;
793 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
795 const gp_XY& thePnt = thePoints.Value( i );
797 double anAltitude = GetAltitudeForPoint( thePnt, theZone );
798 aResSeq.Append( anAltitude );
804 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
806 Handle(HYDROData_Region) aResRegion;
808 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
809 if ( !aZone.IsNull() )
810 aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
815 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
817 Handle(HYDROData_Zone) aResZone;
819 HYDROData_SequenceOfObjects aRegions = GetRegions();
821 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
822 for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
824 Handle(HYDROData_Region) aRegion =
825 Handle(HYDROData_Region)::DownCast( anIter.Value() );
826 if ( aRegion.IsNull() )
829 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
830 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
831 for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
833 Handle(HYDROData_Zone) aRegZone =
834 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
835 if ( aRegZone.IsNull() )
838 PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
839 if ( aPointRelation != POINT_OUT )
840 aResZone = aRegZone; // We found the desired zone
847 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
848 const gp_XY& thePoint,
849 const Handle(HYDROData_Zone)& theZone ) const
851 PointClassification aRes = POINT_OUT;
852 if ( theZone.IsNull() )
855 TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
856 if ( aZoneFace.IsNull() )
859 TopoDS_Compound aCmp;
861 aBB.MakeCompound(aCmp);
862 aBB.Add(aCmp, aZoneFace);
863 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
864 BRepBuilderAPI_MakeVertex aMk(aPnt);
865 aBB.Add(aCmp, aMk.Vertex());
866 BRepTools::Write(aCmp, "FCL2d.brep");
868 TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
869 if (State == TopAbs_OUT)
871 else if(State == TopAbs_IN)
873 else if(State == TopAbs_ON)
878 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
879 const QString& thePrefixOrName,
882 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
883 int aTag = aNewLab.Tag();
885 Handle(HYDROData_Region) aNewRegion =
886 Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
887 AddRegion( aNewRegion );
889 QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
890 aNewRegion->SetName( aRegionName );
895 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup( const QString& theName )
897 TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
899 Handle(HYDROData_SplittedShapesGroup) aNewGroup =
900 Handle(HYDROData_SplittedShapesGroup)::DownCast(
901 HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
902 AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
904 aNewGroup->SetName( theName );
909 QString HYDROData_CalculationCase::Export( int theStudyId ) const
913 CORBA::ORB_var anORB = CORBA::ORB_init( argc, argv, "omniORB4"/*CORBA::ORB_ID*/ );
914 SALOME_NamingService aNameService( anORB );
915 SALOME_LifeCycleCORBA aLCC( &aNameService );
916 Engines::EngineComponent_var aComponent = aLCC.FindOrLoad_Component( "FactoryServer", "GEOM" );
917 GEOM::GEOM_Gen_var aGEOMEngine = GEOM::GEOM_Gen::_narrow( aComponent );
919 CORBA::Object_var aSMObject = aNameService.Resolve("/myStudyManager");
920 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
921 SALOMEDS::Study_var aDSStudy = aStudyManager->GetStudyByID( theStudyId );
923 QString aGeomObjEntry;
924 bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry );
925 return isOK ? aGeomObjEntry : QString();
928 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
929 SALOMEDS::Study_ptr theStudy,
930 QString& theGeomObjEntry ) const
932 HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
934 // Get groups definitions
935 HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
937 HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
938 for ( ; anIter.More(); anIter.Next() )
941 Handle(HYDROData_ShapesGroup) aGroup =
942 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
943 if ( aGroup.IsNull() )
946 HYDROData_ShapesGroup::GroupDefinition aGroupDef;
948 aGroupDef.Name = aGroup->GetName().toLatin1().constData();
949 aGroup->GetShapes( aGroupDef.Shapes );
951 aSeqOfGroupsDefs.Append( aGroupDef );
955 TopTools_ListOfShape aFaces;
956 HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
957 HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
958 for ( ; aRegionIter.More(); aRegionIter.Next() )
960 Handle(HYDROData_Region) aRegion =
961 Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
962 if( aRegion.IsNull() )
965 TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
966 aFaces.Append( aRegionShape );
969 return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs, theGeomObjEntry );
972 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
973 SALOMEDS::Study_ptr theStudy,
974 const TopTools_ListOfShape& theFaces,
975 const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs,
976 QString& theGeomObjEntry ) const
979 BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
980 aSewing.SetNonManifoldMode( Standard_False );
981 #ifdef DEB_CALCULATION
982 TCollection_AsciiString aNam("Sh_");
985 TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
986 for ( ; aFaceIter.More(); aFaceIter.Next() )
988 TopoDS_Shape aShape = aFaceIter.Value();
989 if ( aShape.IsNull() )
992 if ( aShape.ShapeType() == TopAbs_FACE )
994 aSewing.Add( aShape );
995 #ifdef DEB_CALCULATION
996 TCollection_AsciiString aName = aNam + ++i + ".brep";
997 BRepTools::Write(aShape ,aName.ToCString());
1002 #ifdef DEB_CALCULATION
1005 TopExp_Explorer anExp( aShape, TopAbs_FACE );
1006 for (; anExp.More(); anExp.Next() ) {
1007 aSewing.Add( anExp.Current() );
1008 #ifdef DEB_CALCULATION
1010 TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
1011 BRepTools::Write(anExp.Current() ,aName.ToCString());
1018 TopoDS_Shape aSewedShape = aSewing.SewedShape();
1020 // If the sewed shape is empty - return false
1021 if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
1024 #ifdef DEB_CALCULATION
1025 BRepTools::Write(aSewedShape ,"Sew.brep");
1027 // Publish the sewed shape
1028 QString aName = EXPORT_NAME;
1029 GEOM::GEOM_Object_ptr aMainShape =
1030 publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName, theGeomObjEntry );
1032 if ( aMainShape->_is_nil() )
1035 if ( theGroupsDefs.IsEmpty() )
1039 TopTools_IndexedMapOfShape aMapOfSubShapes;
1040 TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
1042 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
1044 for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
1046 const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
1048 NCollection_Sequence<int> aGroupIndexes;
1049 for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
1051 const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
1052 #ifdef DEB_CALCULATION
1053 cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
1056 TopoDS_Shape aModifiedShape = aShape;
1057 if ( aSewing.IsModified( aShape ) )
1058 aModifiedShape = aSewing.Modified( aShape );
1059 else if ( aSewing.IsModifiedSubShape( aShape ) )
1060 aModifiedShape = aSewing.ModifiedSubShape( aShape );
1062 #ifdef DEB_CALCULATION
1063 const TopLoc_Location& aL1 = aShape.Location();
1064 const TopLoc_Location& aL2 = aModifiedShape.Location();
1065 cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
1068 int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
1069 if ( anIndex > 0 ) {
1070 aGroupIndexes.Append( anIndex );
1072 #ifdef DEB_CALCULATION
1073 TCollection_AsciiString aNam("Lost_");
1074 if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1075 for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1077 const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1078 if ( aModifiedShape.IsPartner( aS ) )
1080 cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1081 TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1082 BRepTools::Write(aS ,aName.ToCString());
1090 if ( !aGroupIndexes.IsEmpty() )
1091 aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1094 if ( !aGroupsData.IsEmpty() )
1096 GEOM::GEOM_IGroupOperations_var aGroupOp =
1097 theGeomEngine->GetIGroupOperations( theStudy->StudyId() );
1099 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1100 for ( ; aMapIt.More(); aMapIt.Next() )
1102 const TCollection_AsciiString& aGroupName = aMapIt.Key();
1103 const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1105 GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1106 if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1109 GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1110 aGeomIndexes->length( aGroupIndexes.Length() );
1112 for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1113 aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1115 aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1116 if ( aGroupOp->IsDone() )
1118 SALOMEDS::SObject_var aGroupSO =
1119 theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1127 GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM(
1128 GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
1129 const TopoDS_Shape& theShape, const QString& theName,
1130 QString& theGeomObjEntry ) const
1132 theGeomObjEntry = "";
1133 GEOM::GEOM_Object_var aGeomObj;
1135 if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
1136 theShape.IsNull() ) {
1137 return aGeomObj._retn();
1140 std::ostringstream aStreamShape;
1141 // Write TopoDS_Shape in ASCII format to the stream
1142 BRepTools::Write( theShape, aStreamShape );
1143 // Returns the number of bytes that have been stored in the stream's buffer.
1144 int aSize = aStreamShape.str().size();
1145 // Allocate octect buffer of required size
1146 CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
1147 // Copy ostrstream content to the octect buffer
1148 memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
1150 SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
1152 // Restore shape from the stream and get the GEOM object
1153 GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
1154 aGeomObj = anInsOp->RestoreShape( aSeqFile );
1156 // Puplish the GEOM object
1157 if ( !aGeomObj->_is_nil() ) {
1158 QString aName = GEOMBase::GetDefaultName( theName );
1160 SALOMEDS::SObject_var aResultSO =
1161 theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(),
1162 aGeomObj, qPrintable( aName ) );
1163 if ( aResultSO->_is_nil() ) {
1164 aGeomObj = GEOM::GEOM_Object::_nil();
1167 theGeomObjEntry = aResultSO->GetID();
1170 return aGeomObj._retn();
1173 void HYDROData_CalculationCase::ClearRules( const bool theIsSetToUpdate )
1175 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1176 HYDROData_PriorityQueue::ClearRules( aRulesLab );
1178 // Indicate model of the need to update splitting
1179 if ( theIsSetToUpdate ) {
1180 SetToUpdate( true );
1184 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Object)& theObject1,
1185 HYDROData_PriorityType thePriority,
1186 const Handle(HYDROData_Object)& theObject2,
1187 HYDROData_Zone::MergeAltitudesType theMergeType )
1189 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1190 HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1192 // Indicate model of the need to update splitting
1193 SetToUpdate( true );
1196 QString HYDROData_CalculationCase::DumpRules() const
1198 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1199 return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1202 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1204 TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1205 TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1207 // Indicate model of the need to update splitting
1208 SetToUpdate( true );
1211 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1213 Handle(TDataStd_Integer) aModeAttr;
1214 bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1216 return ( AssignmentMode ) aModeAttr->Get();
1221 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1222 QStringList& theScript ) const
1224 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1225 HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1228 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1230 return myLastWarning;
1233 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1235 myLastWarning.Type = theType;
1236 myLastWarning.Data = theData;
1239 bool HYDROData_CalculationCase::GetRule( int theIndex,
1240 Handle(HYDROData_Object)& theObject1,
1241 HYDROData_PriorityType& thePriority,
1242 Handle(HYDROData_Object)& theObject2,
1243 HYDROData_Zone::MergeAltitudesType& theMergeType ) const
1245 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1246 return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1247 theObject1, thePriority, theObject2, theMergeType );