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"
14 #include "HYDROData_GeomTool.h"
21 #include <TopoDS_Shell.hxx>
22 #include <TopoDS_Edge.hxx>
24 #include <BRep_Builder.hxx>
25 #include <BRepBuilderAPI_Sewing.hxx>
26 #include <BRepTopAdaptor_FClass2d.hxx>
28 #include <BRepTools.hxx>
31 #include <TopExp_Explorer.hxx>
33 #include <TopTools_ListOfShape.hxx>
34 #include <TopTools_ListIteratorOfListOfShape.hxx>
35 #include <TDataStd_Integer.hxx>
37 //#define DEB_CALCULATION 1
38 #ifdef DEB_CALCULATION
39 #include <BRepTools.hxx>
40 #include <TopLoc_Location.hxx>
42 #define CALCULATION_REGIONS_PREF GetName() + "_Reg"
43 #define CALCULATION_ZONES_PREF GetName() + "_Zone"
44 #define CALCULATION_GROUPS_PREF GetName() + "_"
45 //#define DEB_CLASS2D 1
47 #include <BRepBuilderAPI_MakeVertex.hxx>
50 #define EXPORT_NAME "HYDRO_" + GetName()
52 #include <SALOME_NamingService.hxx>
53 #include <SALOME_LifeCycleCORBA.hxx>
59 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
60 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
62 HYDROData_CalculationCase::HYDROData_CalculationCase()
67 HYDROData_CalculationCase::~HYDROData_CalculationCase()
71 void HYDROData_CalculationCase::SetName( const QString& theName )
73 QString anOldCaseName = GetName();
74 if ( anOldCaseName != theName )
76 HYDROData_SequenceOfObjects aRegions = GetRegions();
78 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
79 for ( ; anIter.More(); anIter.Next() )
81 Handle(HYDROData_Region) aRegion =
82 Handle(HYDROData_Region)::DownCast( anIter.Value() );
83 if ( aRegion.IsNull() )
86 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegion );
88 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
89 HYDROData_SequenceOfObjects::Iterator anIter( aZones );
90 for ( ; anIter.More(); anIter.Next() )
92 Handle(HYDROData_Zone) aRegZone =
93 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
94 if ( aRegZone.IsNull() )
97 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegZone );
101 HYDROData_SequenceOfObjects aGroups = GetSplittedGroups();
103 anIter.Init( aGroups );
104 for ( ; anIter.More(); anIter.Next() )
106 Handle(HYDROData_SplittedShapesGroup) aGroup =
107 Handle(HYDROData_SplittedShapesGroup)::DownCast( anIter.Value() );
108 if ( aGroup.IsNull() )
111 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aGroup );
115 HYDROData_Entity::SetName( theName );
118 QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
120 QStringList aResList = dumpObjectCreation( theTreatedObjects );
121 aResList.prepend( "# Calculation case" );
123 QString aCalculName = GetObjPyName();
125 AssignmentMode aMode = GetAssignmentMode();
126 QString aModeStr = aMode==MANUAL ? "HYDROData_CalculationCase.MANUAL" : "HYDROData_CalculationCase.AUTOMATIC";
127 aResList << QString( "%0.SetAssignmentMode( %1 )" ).arg( aCalculName ).arg( aModeStr );
129 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
130 HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
131 for ( ; anIter.More(); anIter.Next() )
133 Handle(HYDROData_Object) aRefGeomObj =
134 Handle(HYDROData_Object)::DownCast( anIter.Value() );
135 setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
137 aResList << QString( "" );
139 QString aGroupName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "case_geom_group" );
141 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
142 anIter.Init( aGeomGroups );
143 for ( ; anIter.More(); anIter.Next() )
145 Handle(HYDROData_ShapesGroup) aGeomGroup =
146 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
147 if ( aGeomGroup.IsNull() )
150 Handle(HYDROData_Object) aFatherGeom =
151 Handle(HYDROData_Object)::DownCast( aGeomGroup->GetFatherObject() );
152 if ( aFatherGeom.IsNull() )
155 int aGroupId = aFatherGeom->GetGroupId( aGeomGroup );
156 aResList << QString( "%1 = %2.GetGroup( %3 );" )
157 .arg( aGroupName ).arg( aFatherGeom->GetObjPyName() ).arg( aGroupId );
159 aResList << QString( "%1.AddGeometryGroup( %2 );" ).arg( aCalculName ).arg( aGroupName );
162 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
163 setPythonReferenceObject( theTreatedObjects, aResList, aBoundaryPolyline, "SetBoundaryPolyline" );
165 if( aMode==AUTOMATIC )
166 DumpRulesToPython( aCalculName, aResList );
168 aResList << QString( "" );
169 aResList << "# Start the algorithm of the partition and assignment";
170 aResList << QString( "%1.Update();" ).arg( aCalculName );
174 // Now we restore the regions and zones order
175 HYDROData_SequenceOfObjects aRegions = GetRegions();
176 anIter.Init( aRegions );
177 for ( ; anIter.More(); anIter.Next() )
179 Handle(HYDROData_Region) aRegion =
180 Handle(HYDROData_Region)::DownCast( anIter.Value() );
181 if ( aRegion.IsNull() )
184 theTreatedObjects.insert( aRegion->GetName(), aRegion );
185 QStringList aRegDump = aRegion->DumpToPython( theTreatedObjects );
186 aResList << aRegDump;
190 // Export calculation case
191 aResList << QString( "" );
192 aResList << "# Export of the calculation case";
193 QString aStudyName = "theStudy";
194 QString anEntryVar = aCalculName + "_entry";
195 aResList << QString( "%1 = %2.Export( %3._get_StudyId() )" ).arg( anEntryVar ).arg( aCalculName ).arg( aStudyName );
197 // Get geometry object and print debug information
199 aResList << "# Get geometry shape and print debug information";
200 aResList << "import GEOM";
201 aResList << QString( "print \"Entry:\", %1" ).arg( anEntryVar );
202 QString aGeomShapeName = aCalculName + "_geom";
203 aResList << QString( "%1 = salome.IDToObject( str( %2 ) )" ).arg( aGeomShapeName ).arg( anEntryVar );
204 aResList << QString( "print \"Geom shape:\", %1" ).arg( aGeomShapeName );
205 aResList << QString( "print \"Geom shape name:\", %1.GetName()" ).arg( aGeomShapeName );
207 DumpSampleMeshing( aResList, aStudyName, aGeomShapeName, aCalculName+"_mesh" );
209 aResList << QString( "" );
213 void HYDROData_CalculationCase::DumpSampleMeshing( QStringList& theResList,
214 const QString& theStudyName,
215 const QString& theGeomShapeName,
216 const QString& theMeshName ) const
219 theResList << "# Meshing";
220 theResList << "import SMESH, SALOMEDS";
221 theResList << "from salome.smesh import smeshBuilder";
222 theResList << "from salome.geom import geomBuilder";
224 theResList << QString( "smesh = smeshBuilder.New( %1 )" ).arg( theStudyName );
225 theResList << QString( "%1 = smesh.Mesh( %2 )" ).arg( theMeshName ).arg( theGeomShapeName );
226 theResList << QString( "MEFISTO_2D = %1.Triangle( algo=smeshBuilder.MEFISTO )" ).arg( theMeshName );
227 theResList << "Max_Element_Area_1 = MEFISTO_2D.MaxElementArea( 10 )";
228 theResList << QString( "Regular_1D = %1.Segment()" ).arg( theMeshName );
229 theResList << "Max_Size_1 = Regular_1D.MaxSize(10)";
230 theResList << QString( "isDone = %1.Compute()" ).arg( theMeshName );
233 theResList << "# Set names of Mesh objects";
234 theResList << "smesh.SetName( MEFISTO_2D.GetAlgorithm(), 'MEFISTO_2D' )";
235 theResList << "smesh.SetName( Regular_1D.GetAlgorithm(), 'Regular_1D' )";
236 theResList << "smesh.SetName( Max_Size_1, 'Max Size_1' )";
237 theResList << "smesh.SetName( Max_Element_Area_1, 'Max. Element Area_1' )";
238 theResList << QString( "smesh.SetName( %1.GetMesh(), '%1' )" ).arg( theMeshName );
241 theResList << "# Greate SMESH groups";
242 theResList << QString( "geompy = geomBuilder.New( %1 )" ).arg( theStudyName );
243 theResList << QString( "geom_groups = geompy.GetGroups( %1 )" ).arg( theGeomShapeName );
244 theResList << QString( "for group in geom_groups:" );
245 theResList << QString( " smesh_group = %1.GroupOnGeom(group, group.GetName(), SMESH.EDGE)" )
247 theResList << QString( " smesh.SetName(smesh_group, group.GetName())" );
248 theResList << QString( " print \"SMESH group '%s': %s\" % (smesh_group.GetName(), smesh_group)" );
251 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
253 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
255 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
256 if ( !aBoundaryPolyline.IsNull() )
257 aResSeq.Append( aBoundaryPolyline );
259 HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
260 aResSeq.Append( aSeqOfRegions );
265 void HYDROData_CalculationCase::Update()
267 HYDROData_Entity::Update();
270 // At first we remove previously created objects
272 RemoveSplittedGroups();
274 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
275 if ( aDocument.IsNull() )
278 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
279 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
280 if ( aGeomObjects.IsEmpty() )
283 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
285 HYDROData_SplitToZonesTool::SplitDataList aSplitObjects =
286 HYDROData_SplitToZonesTool::Split( aGeomObjects, aGeomGroups, aBoundaryPolyline );
287 if ( aSplitObjects.isEmpty() )
290 HYDROData_SplitToZonesTool::SplitDataList aZonesList, anEdgesList;
292 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitObjects );
293 while( anIter.hasNext() )
295 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
296 if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Zone )
297 aZonesList.append( aSplitData );
298 else if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Edge )
299 anEdgesList.append( aSplitData );
302 switch( GetAssignmentMode() )
305 CreateRegionsDef( aDocument, aZonesList );
308 CreateRegionsAuto( aDocument, aZonesList );
311 CreateEdgeGroupsDef( aDocument, anEdgesList );
314 void HYDROData_CalculationCase::CreateRegionsDef( const Handle(HYDROData_Document)& theDoc,
315 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
317 // Create result regions for case, by default one zone for one region
318 QString aRegsPref = CALCULATION_REGIONS_PREF;
319 QString aZonesPref = CALCULATION_ZONES_PREF;
321 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
322 while( anIter.hasNext() )
324 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
326 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegsPref );
328 // Add the zone for region
329 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
333 void HYDROData_CalculationCase::CreateRegionsAuto( const Handle(HYDROData_Document)& theDoc,
334 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
336 QMap<QString, Handle(HYDROData_Region)> aRegionsMap; //object name to region
337 QMap<QString, QString> aRegionNameToObjNameMap;
338 QString aZonesPref = CALCULATION_ZONES_PREF;
339 HYDROData_PriorityQueue aPr( this );
341 // 1. First we create a default region for each object included into the calculation case
342 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
343 for( int i=aGeomObjects.Lower(), n=aGeomObjects.Upper(); i<=n; i++ )
345 Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( aGeomObjects.Value( i ) );
348 QString anObjName = anObj->GetName();
349 QString aRegName = anObjName + "_reg";
350 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegName, false );
351 aRegionsMap.insert( anObjName, aRegion );
352 aRegionNameToObjNameMap.insert( aRegName, anObjName );
355 // 2. Now for each zone it is necessary to determine the most priority object
356 // and assign to zone to corresponding region
357 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
358 while( anIter.hasNext() )
360 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
361 HYDROData_Zone::MergeAltitudesType aMergeType;
362 Handle(HYDROData_Object) aRegObj = aPr.GetMostPriorityObject( aSplitData.ObjectNames, aMergeType );
363 if( aRegObj.IsNull() )
365 Handle(HYDROData_Region) aRegion = aRegionsMap[aRegObj->GetName()];
366 if( aRegion.IsNull() )
368 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
370 if( aSplitData.ObjectNames.count() > 1 && aMergeType==HYDROData_Zone::Merge_UNKNOWN )
372 qDebug( "Error in algorithm: unresolved conflicts" );
377 case HYDROData_Zone::Merge_ZMIN:
378 case HYDROData_Zone::Merge_ZMAX:
379 aRegionZone->SetMergeType( aMergeType );
381 case HYDROData_Zone::Merge_Object:
382 aRegionZone->SetMergeType( aMergeType );
383 aRegionZone->RemoveMergeAltitude();
384 aRegionZone->SetMergeAltitude( aRegObj->GetAltitudeObject() );
389 QStringList anObjectsWithEmptyRegions;
390 QMap<QString, Handle(HYDROData_Region)>::const_iterator
391 anIt = aRegionsMap.begin(), aLast = aRegionsMap.end();
392 for( ; anIt!=aLast; anIt++ )
394 Handle(HYDROData_Region) aRegion = anIt.value();
395 if( aRegion->GetZones().IsEmpty() )
397 QString aRegName = aRegion->GetName();
398 QString anObjName = aRegionNameToObjNameMap[aRegName];
399 anObjectsWithEmptyRegions.append( anObjName );
403 if( !anObjectsWithEmptyRegions.empty() )
405 QString aData = anObjectsWithEmptyRegions.join( ", " );
406 SetWarning( WARN_EMPTY_REGIONS, aData );
410 void HYDROData_CalculationCase::CreateEdgeGroupsDef( const Handle(HYDROData_Document)& theDoc,
411 const HYDROData_SplitToZonesTool::SplitDataList& theEdges )
413 QMap<QString,Handle(HYDROData_SplittedShapesGroup)> aSplittedEdgesGroupsMap;
415 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theEdges );
416 while( anIter.hasNext() )
418 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
419 // Create new edges group
420 if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
423 QString anObjName = aSplitData.ObjectNames.first();
424 if ( anObjName.isEmpty() )
426 #ifdef DEB_CALCULATION
427 QString aStr = aSplitData.ObjectNames.join(" ");
428 cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl;
430 Handle(HYDROData_SplittedShapesGroup) aSplittedGroup;
431 if ( !aSplittedEdgesGroupsMap.contains( anObjName ) )
433 aSplittedGroup = addNewSplittedGroup( CALCULATION_GROUPS_PREF + anObjName );
434 aSplittedEdgesGroupsMap.insert( anObjName, aSplittedGroup );
438 aSplittedGroup = aSplittedEdgesGroupsMap[ anObjName ];
440 if ( aSplittedGroup.IsNull() )
443 aSplittedGroup->AddShape( aSplitData.Shape );
447 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
449 if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
450 return false; // Wrong type of object
452 if ( HasReference( theObject, DataTag_GeometryObject ) )
453 return false; // Object is already in reference list
455 AddReferenceObject( theObject, DataTag_GeometryObject );
457 // Indicate model of the need to update splitting
463 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
465 return GetReferenceObjects( DataTag_GeometryObject );
468 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
470 if ( theObject.IsNull() )
473 RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
475 // Indicate model of the need to update splitting
479 void HYDROData_CalculationCase::RemoveGeometryObjects()
481 ClearReferenceObjects( DataTag_GeometryObject );
483 // Indicate model of the need to update splitting
487 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
489 if ( theGroup.IsNull() )
492 if ( HasReference( theGroup, DataTag_GeometryGroup ) )
493 return false; // Object is already in reference list
495 AddReferenceObject( theGroup, DataTag_GeometryGroup );
497 // Indicate model of the need to update splitting
503 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
505 return GetReferenceObjects( DataTag_GeometryGroup );
508 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
510 if ( theGroup.IsNull() )
513 RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
515 // Indicate model of the need to update splitting
519 void HYDROData_CalculationCase::RemoveGeometryGroups()
521 ClearReferenceObjects( DataTag_GeometryGroup );
523 // Indicate model of the need to update splitting
527 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
529 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
531 SetReferenceObject( thePolyline, DataTag_Polyline );
533 // Indicate model of the need to update zones splitting
534 SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
537 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
539 return Handle(HYDROData_PolylineXY)::DownCast(
540 GetReferenceObject( DataTag_Polyline ) );
543 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
545 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
547 ClearReferenceObjects( DataTag_Polyline );
549 // Indicate model of the need to update zones splitting
550 SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
553 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
555 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
556 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
557 if ( aNewRegion.IsNull() )
560 aNewRegion->AddZone( theZone );
565 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
567 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
569 if ( theRegion.IsNull() )
572 if ( HasReference( theRegion, DataTag_Region ) )
573 return false; // Object is already in reference list
575 // Move the region from other calculation
576 Handle(HYDROData_CalculationCase) aFatherCalc =
577 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
578 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
580 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
581 theRegion->CopyTo( aNewRegion );
583 aFatherCalc->RemoveRegion( theRegion );
585 theRegion->SetLabel( aNewRegion->Label() );
589 AddReferenceObject( theRegion, DataTag_Region );
595 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
597 return GetReferenceObjects( DataTag_Region );
600 void HYDROData_CalculationCase::UpdateRegionsOrder()
602 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
603 if ( aDocument.IsNull() )
606 HYDROData_SequenceOfObjects aRegions = GetRegions();
608 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
609 for ( ; anIter.More(); anIter.Next() )
611 Handle(HYDROData_Region) aRegion =
612 Handle(HYDROData_Region)::DownCast( anIter.Value() );
613 if ( aRegion.IsNull() )
616 aRegion->SetName( "" );
619 QString aRegsPref = CALCULATION_REGIONS_PREF;
621 anIter.Init( aRegions );
622 for ( ; anIter.More(); anIter.Next() )
624 Handle(HYDROData_Region) aRegion =
625 Handle(HYDROData_Region)::DownCast( anIter.Value() );
626 if ( aRegion.IsNull() )
629 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
630 aRegion->SetName( aRegionName );
634 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
636 if ( theRegion.IsNull() )
639 RemoveReferenceObject( theRegion->Label(), DataTag_Region );
641 // Remove region from data model
642 Handle(HYDROData_CalculationCase) aFatherCalc =
643 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
644 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
648 void HYDROData_CalculationCase::RemoveRegions()
650 myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
653 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
655 return GetReferenceObjects( DataTag_SplittedGroups );
658 void HYDROData_CalculationCase::RemoveSplittedGroups()
660 myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
663 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
665 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
666 return GetAltitudeForPoint( thePoint, aZone );
669 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
670 const Handle(HYDROData_Region)& theRegion ) const
672 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
674 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
675 if ( !aZone.IsNull() )
677 Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
678 if ( IsEqual( aRefRegion, theRegion ) )
679 aResAltitude = GetAltitudeForPoint( thePoint, aZone );
685 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
686 const Handle(HYDROData_Zone)& theZone ) const
688 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
689 if ( theZone.IsNull() )
692 HYDROData_Zone::MergeAltitudesType aZoneMergeType = theZone->GetMergeType();
693 if ( !theZone->IsMergingNeed() )
695 aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
697 else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
702 HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
703 if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
705 Handle(HYDROData_IAltitudeObject) aMergeAltitude = theZone->GetMergeAltitude();
706 if ( !aMergeAltitude.IsNull() )
708 if ( aZoneInterpolator != NULL )
710 aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
711 aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
714 aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
719 HYDROData_SequenceOfObjects aZoneObjects = theZone->GetGeometryObjects();
720 HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
721 for ( ; anIter.More(); anIter.Next() )
723 Handle(HYDROData_Object) aZoneObj =
724 Handle(HYDROData_Object)::DownCast( anIter.Value() );
725 if ( aZoneObj.IsNull() )
728 Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
729 if ( anObjAltitude.IsNull() )
732 double aPointAltitude = 0.0;
733 if ( aZoneInterpolator != NULL )
735 aZoneInterpolator->SetAltitudeObject( anObjAltitude );
736 aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
739 aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
741 if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
744 if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
746 aResAltitude = aPointAltitude;
749 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
751 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
752 aResAltitude > aPointAltitude )
754 aResAltitude = aPointAltitude;
757 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
759 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
760 aResAltitude < aPointAltitude )
762 aResAltitude = aPointAltitude;
771 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
772 const NCollection_Sequence<gp_XY>& thePoints,
773 const Handle(HYDROData_Region)& theRegion ) const
775 NCollection_Sequence<double> aResSeq;
777 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
779 const gp_XY& thePnt = thePoints.Value( i );
781 double anAltitude = GetAltitudeForPoint( thePnt, theRegion );
782 aResSeq.Append( anAltitude );
788 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
789 const NCollection_Sequence<gp_XY>& thePoints,
790 const Handle(HYDROData_Zone)& theZone ) const
792 NCollection_Sequence<double> aResSeq;
794 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
796 const gp_XY& thePnt = thePoints.Value( i );
798 double anAltitude = GetAltitudeForPoint( thePnt, theZone );
799 aResSeq.Append( anAltitude );
805 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
807 Handle(HYDROData_Region) aResRegion;
809 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
810 if ( !aZone.IsNull() )
811 aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
816 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
818 Handle(HYDROData_Zone) aResZone;
820 HYDROData_SequenceOfObjects aRegions = GetRegions();
822 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
823 for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
825 Handle(HYDROData_Region) aRegion =
826 Handle(HYDROData_Region)::DownCast( anIter.Value() );
827 if ( aRegion.IsNull() )
830 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
831 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
832 for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
834 Handle(HYDROData_Zone) aRegZone =
835 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
836 if ( aRegZone.IsNull() )
839 PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
840 if ( aPointRelation != POINT_OUT )
841 aResZone = aRegZone; // We found the desired zone
848 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
849 const gp_XY& thePoint,
850 const Handle(HYDROData_Zone)& theZone ) const
852 PointClassification aRes = POINT_OUT;
853 if ( theZone.IsNull() )
856 TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
857 if ( aZoneFace.IsNull() )
860 TopoDS_Compound aCmp;
862 aBB.MakeCompound(aCmp);
863 aBB.Add(aCmp, aZoneFace);
864 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
865 BRepBuilderAPI_MakeVertex aMk(aPnt);
866 aBB.Add(aCmp, aMk.Vertex());
867 BRepTools::Write(aCmp, "FCL2d.brep");
869 TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
870 if (State == TopAbs_OUT)
872 else if(State == TopAbs_IN)
874 else if(State == TopAbs_ON)
879 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
880 const QString& thePrefixOrName,
883 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
884 int aTag = aNewLab.Tag();
886 Handle(HYDROData_Region) aNewRegion =
887 Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
888 AddRegion( aNewRegion );
890 QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
891 aNewRegion->SetName( aRegionName );
896 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup( const QString& theName )
898 TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
900 Handle(HYDROData_SplittedShapesGroup) aNewGroup =
901 Handle(HYDROData_SplittedShapesGroup)::DownCast(
902 HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
903 AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
905 aNewGroup->SetName( theName );
910 QString HYDROData_CalculationCase::Export( int theStudyId ) const
912 GEOM::GEOM_Gen_var aGEOMEngine = HYDROData_GeomTool::GetGeomGen();
913 SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( theStudyId );
915 QString aGeomObjEntry;
916 bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry );
917 return isOK ? aGeomObjEntry : QString();
920 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
921 SALOMEDS::Study_ptr theStudy,
922 QString& theGeomObjEntry ) const
924 HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
926 // Get groups definitions
927 HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
929 HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
930 for ( ; anIter.More(); anIter.Next() )
933 Handle(HYDROData_ShapesGroup) aGroup =
934 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
935 if ( aGroup.IsNull() )
938 HYDROData_ShapesGroup::GroupDefinition aGroupDef;
940 aGroupDef.Name = aGroup->GetName().toLatin1().constData();
941 aGroup->GetShapes( aGroupDef.Shapes );
943 aSeqOfGroupsDefs.Append( aGroupDef );
947 TopTools_ListOfShape aFaces;
948 HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
949 HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
950 for ( ; aRegionIter.More(); aRegionIter.Next() )
952 Handle(HYDROData_Region) aRegion =
953 Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
954 if( aRegion.IsNull() )
957 TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
958 aFaces.Append( aRegionShape );
961 return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs, theGeomObjEntry );
964 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
965 SALOMEDS::Study_ptr theStudy,
966 const TopTools_ListOfShape& theFaces,
967 const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs,
968 QString& theGeomObjEntry ) const
971 BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
972 aSewing.SetNonManifoldMode( Standard_False );
973 #ifdef DEB_CALCULATION
974 TCollection_AsciiString aNam("Sh_");
977 TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
978 for ( ; aFaceIter.More(); aFaceIter.Next() )
980 TopoDS_Shape aShape = aFaceIter.Value();
981 if ( aShape.IsNull() )
984 if ( aShape.ShapeType() == TopAbs_FACE )
986 aSewing.Add( aShape );
987 #ifdef DEB_CALCULATION
988 TCollection_AsciiString aName = aNam + ++i + ".brep";
989 BRepTools::Write(aShape ,aName.ToCString());
994 #ifdef DEB_CALCULATION
997 TopExp_Explorer anExp( aShape, TopAbs_FACE );
998 for (; anExp.More(); anExp.Next() ) {
999 aSewing.Add( anExp.Current() );
1000 #ifdef DEB_CALCULATION
1002 TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
1003 BRepTools::Write(anExp.Current() ,aName.ToCString());
1010 TopoDS_Shape aSewedShape = aSewing.SewedShape();
1012 // If the sewed shape is empty - return false
1013 if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
1016 #ifdef DEB_CALCULATION
1017 BRepTools::Write(aSewedShape ,"Sew.brep");
1019 // Publish the sewed shape
1020 QString aName = EXPORT_NAME;
1021 GEOM::GEOM_Object_ptr aMainShape =
1022 publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName, theGeomObjEntry );
1024 if ( aMainShape->_is_nil() )
1027 if ( theGroupsDefs.IsEmpty() )
1031 TopTools_IndexedMapOfShape aMapOfSubShapes;
1032 TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
1034 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
1036 for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
1038 const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
1040 NCollection_Sequence<int> aGroupIndexes;
1041 for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
1043 const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
1044 #ifdef DEB_CALCULATION
1045 cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
1048 TopoDS_Shape aModifiedShape = aShape;
1049 if ( aSewing.IsModified( aShape ) )
1050 aModifiedShape = aSewing.Modified( aShape );
1051 else if ( aSewing.IsModifiedSubShape( aShape ) )
1052 aModifiedShape = aSewing.ModifiedSubShape( aShape );
1054 #ifdef DEB_CALCULATION
1055 const TopLoc_Location& aL1 = aShape.Location();
1056 const TopLoc_Location& aL2 = aModifiedShape.Location();
1057 cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
1060 int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
1061 if ( anIndex > 0 ) {
1062 aGroupIndexes.Append( anIndex );
1064 #ifdef DEB_CALCULATION
1065 TCollection_AsciiString aNam("Lost_");
1066 if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1067 for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1069 const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1070 if ( aModifiedShape.IsPartner( aS ) )
1072 cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1073 TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1074 BRepTools::Write(aS ,aName.ToCString());
1082 if ( !aGroupIndexes.IsEmpty() )
1083 aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1086 if ( !aGroupsData.IsEmpty() )
1088 GEOM::GEOM_IGroupOperations_var aGroupOp =
1089 theGeomEngine->GetIGroupOperations( theStudy->StudyId() );
1091 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1092 for ( ; aMapIt.More(); aMapIt.Next() )
1094 const TCollection_AsciiString& aGroupName = aMapIt.Key();
1095 const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1097 GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1098 if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1101 GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1102 aGeomIndexes->length( aGroupIndexes.Length() );
1104 for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1105 aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1107 aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1108 if ( aGroupOp->IsDone() )
1110 SALOMEDS::SObject_var aGroupSO =
1111 theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1119 GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM(
1120 GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
1121 const TopoDS_Shape& theShape, const QString& theName,
1122 QString& theGeomObjEntry ) const
1124 theGeomObjEntry = "";
1125 GEOM::GEOM_Object_var aGeomObj;
1127 if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
1128 theShape.IsNull() ) {
1129 return aGeomObj._retn();
1132 std::ostringstream aStreamShape;
1133 // Write TopoDS_Shape in ASCII format to the stream
1134 BRepTools::Write( theShape, aStreamShape );
1135 // Returns the number of bytes that have been stored in the stream's buffer.
1136 int aSize = aStreamShape.str().size();
1137 // Allocate octect buffer of required size
1138 CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
1139 // Copy ostrstream content to the octect buffer
1140 memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
1142 SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
1144 // Restore shape from the stream and get the GEOM object
1145 GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
1146 aGeomObj = anInsOp->RestoreShape( aSeqFile );
1148 // Puplish the GEOM object
1149 if ( !aGeomObj->_is_nil() ) {
1150 QString aName = HYDROData_GeomTool::GetFreeName( theStudy, theName );
1152 SALOMEDS::SObject_var aResultSO =
1153 theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(),
1154 aGeomObj, qPrintable( aName ) );
1155 if ( aResultSO->_is_nil() ) {
1156 aGeomObj = GEOM::GEOM_Object::_nil();
1159 theGeomObjEntry = aResultSO->GetID();
1162 return aGeomObj._retn();
1165 void HYDROData_CalculationCase::ClearRules( const bool theIsSetToUpdate )
1167 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1168 HYDROData_PriorityQueue::ClearRules( aRulesLab );
1170 // Indicate model of the need to update splitting
1171 if ( theIsSetToUpdate ) {
1172 SetToUpdate( true );
1176 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Object)& theObject1,
1177 HYDROData_PriorityType thePriority,
1178 const Handle(HYDROData_Object)& theObject2,
1179 HYDROData_Zone::MergeAltitudesType theMergeType )
1181 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1182 HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1184 // Indicate model of the need to update splitting
1185 SetToUpdate( true );
1188 QString HYDROData_CalculationCase::DumpRules() const
1190 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1191 return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1194 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1196 TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1197 TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1199 // Indicate model of the need to update splitting
1200 SetToUpdate( true );
1203 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1205 Handle(TDataStd_Integer) aModeAttr;
1206 bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1208 return ( AssignmentMode ) aModeAttr->Get();
1213 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1214 QStringList& theScript ) const
1216 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1217 HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1220 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1222 return myLastWarning;
1225 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1227 myLastWarning.Type = theType;
1228 myLastWarning.Data = theData;
1231 bool HYDROData_CalculationCase::GetRule( int theIndex,
1232 Handle(HYDROData_Object)& theObject1,
1233 HYDROData_PriorityType& thePriority,
1234 Handle(HYDROData_Object)& theObject2,
1235 HYDROData_Zone::MergeAltitudesType& theMergeType ) const
1237 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1238 return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1239 theObject1, thePriority, theObject2, theMergeType );