1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_CalculationCase.h"
21 #include "HYDROData_ArtificialObject.h"
22 #include "HYDROData_IAltitudeObject.h"
23 #include "HYDROData_Document.h"
24 #include "HYDROData_ShapesGroup.h"
25 #include "HYDROData_Iterator.h"
26 #include "HYDROData_NaturalObject.h"
27 #include "HYDROData_PolylineXY.h"
28 #include "HYDROData_SplittedShapesGroup.h"
29 #include "HYDROData_Region.h"
30 #include "HYDROData_Tool.h"
31 #include "HYDROData_GeomTool.h"
38 #include <TopoDS_Shell.hxx>
39 #include <TopoDS_Edge.hxx>
41 #include <BRep_Builder.hxx>
42 #include <BRepBuilderAPI_Sewing.hxx>
43 #include <BRepTopAdaptor_FClass2d.hxx>
45 #include <BRepTools.hxx>
48 #include <TopExp_Explorer.hxx>
50 #include <TopTools_ListOfShape.hxx>
51 #include <TopTools_ListIteratorOfListOfShape.hxx>
52 #include <TDataStd_Integer.hxx>
54 //#define DEB_CALCULATION 1
55 #ifdef DEB_CALCULATION
56 #include <BRepTools.hxx>
57 #include <TopLoc_Location.hxx>
59 #define CALCULATION_REGIONS_PREF GetName() + "_Reg"
60 #define CALCULATION_ZONES_PREF GetName() + "_Zone"
61 #define CALCULATION_GROUPS_PREF GetName() + "_"
62 //#define DEB_CLASS2D 1
64 #include <BRepBuilderAPI_MakeVertex.hxx>
67 #define EXPORT_NAME "HYDRO_" + GetName()
69 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
70 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
72 HYDROData_CalculationCase::HYDROData_CalculationCase()
77 HYDROData_CalculationCase::~HYDROData_CalculationCase()
81 void HYDROData_CalculationCase::SetName( const QString& theName )
83 QString anOldCaseName = GetName();
84 if ( anOldCaseName != theName )
86 HYDROData_SequenceOfObjects aRegions = GetRegions();
88 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
89 for ( ; anIter.More(); anIter.Next() )
91 Handle(HYDROData_Region) aRegion =
92 Handle(HYDROData_Region)::DownCast( anIter.Value() );
93 if ( aRegion.IsNull() )
96 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegion );
98 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
99 HYDROData_SequenceOfObjects::Iterator anIter( aZones );
100 for ( ; anIter.More(); anIter.Next() )
102 Handle(HYDROData_Zone) aRegZone =
103 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
104 if ( aRegZone.IsNull() )
107 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegZone );
111 HYDROData_SequenceOfObjects aGroups = GetSplittedGroups();
113 anIter.Init( aGroups );
114 for ( ; anIter.More(); anIter.Next() )
116 Handle(HYDROData_SplittedShapesGroup) aGroup =
117 Handle(HYDROData_SplittedShapesGroup)::DownCast( anIter.Value() );
118 if ( aGroup.IsNull() )
121 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aGroup );
125 HYDROData_Entity::SetName( theName );
128 QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
130 QStringList aResList = dumpObjectCreation( theTreatedObjects );
131 aResList.prepend( "# Calculation case" );
133 QString aCalculName = GetObjPyName();
135 AssignmentMode aMode = GetAssignmentMode();
136 QString aModeStr = aMode==MANUAL ? "HYDROData_CalculationCase.MANUAL" : "HYDROData_CalculationCase.AUTOMATIC";
137 aResList << QString( "%0.SetAssignmentMode( %1 )" ).arg( aCalculName ).arg( aModeStr );
139 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
140 HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
141 for ( ; anIter.More(); anIter.Next() )
143 Handle(HYDROData_Object) aRefGeomObj =
144 Handle(HYDROData_Object)::DownCast( anIter.Value() );
145 setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
147 aResList << QString( "" );
149 QString aGroupName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "case_geom_group" );
151 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
152 anIter.Init( aGeomGroups );
153 for ( ; anIter.More(); anIter.Next() )
155 Handle(HYDROData_ShapesGroup) aGeomGroup =
156 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
157 if ( aGeomGroup.IsNull() )
160 Handle(HYDROData_Object) aFatherGeom =
161 Handle(HYDROData_Object)::DownCast( aGeomGroup->GetFatherObject() );
162 if ( aFatherGeom.IsNull() )
165 int aGroupId = aFatherGeom->GetGroupId( aGeomGroup );
166 aResList << QString( "%1 = %2.GetGroup( %3 );" )
167 .arg( aGroupName ).arg( aFatherGeom->GetObjPyName() ).arg( aGroupId );
169 aResList << QString( "%1.AddGeometryGroup( %2 );" ).arg( aCalculName ).arg( aGroupName );
172 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
173 setPythonReferenceObject( theTreatedObjects, aResList, aBoundaryPolyline, "SetBoundaryPolyline" );
175 if( aMode==AUTOMATIC )
176 DumpRulesToPython( aCalculName, aResList );
178 aResList << QString( "" );
179 aResList << "# Start the algorithm of the partition and assignment";
180 aResList << QString( "%1.Update();" ).arg( aCalculName );
184 // Now we restore the regions and zones order
185 HYDROData_SequenceOfObjects aRegions = GetRegions();
186 anIter.Init( aRegions );
187 for ( ; anIter.More(); anIter.Next() )
189 Handle(HYDROData_Region) aRegion =
190 Handle(HYDROData_Region)::DownCast( anIter.Value() );
191 if ( aRegion.IsNull() )
194 theTreatedObjects.insert( aRegion->GetName(), aRegion );
195 QStringList aRegDump = aRegion->DumpToPython( theTreatedObjects );
196 aResList << aRegDump;
200 // Export calculation case
201 aResList << QString( "" );
202 aResList << "# Export of the calculation case";
203 QString aStudyName = "theStudy";
204 QString anEntryVar = aCalculName + "_entry";
205 aResList << QString( "%1 = %2.Export( %3._get_StudyId() )" ).arg( anEntryVar ).arg( aCalculName ).arg( aStudyName );
207 // Get geometry object and print debug information
209 aResList << "# Get geometry shape and print debug information";
210 aResList << "import GEOM";
211 aResList << QString( "print \"Entry:\", %1" ).arg( anEntryVar );
212 QString aGeomShapeName = aCalculName + "_geom";
213 aResList << QString( "%1 = salome.IDToObject( str( %2 ) )" ).arg( aGeomShapeName ).arg( anEntryVar );
214 aResList << QString( "print \"Geom shape:\", %1" ).arg( aGeomShapeName );
215 aResList << QString( "print \"Geom shape name:\", %1.GetName()" ).arg( aGeomShapeName );
217 DumpSampleMeshing( aResList, aStudyName, aGeomShapeName, aCalculName+"_mesh" );
219 aResList << QString( "" );
223 void HYDROData_CalculationCase::DumpSampleMeshing( QStringList& theResList,
224 const QString& theStudyName,
225 const QString& theGeomShapeName,
226 const QString& theMeshName ) const
229 theResList << "# Meshing";
230 theResList << "import SMESH, SALOMEDS";
231 theResList << "from salome.smesh import smeshBuilder";
232 theResList << "from salome.geom import geomBuilder";
234 theResList << QString( "smesh = smeshBuilder.New( %1 )" ).arg( theStudyName );
235 theResList << QString( "%1 = smesh.Mesh( %2 )" ).arg( theMeshName ).arg( theGeomShapeName );
236 theResList << QString( "MEFISTO_2D = %1.Triangle( algo=smeshBuilder.MEFISTO )" ).arg( theMeshName );
237 theResList << "Max_Element_Area_1 = MEFISTO_2D.MaxElementArea( 10 )";
238 theResList << QString( "Regular_1D = %1.Segment()" ).arg( theMeshName );
239 theResList << "Max_Size_1 = Regular_1D.MaxSize(10)";
240 theResList << QString( "isDone = %1.Compute()" ).arg( theMeshName );
243 theResList << "# Set names of Mesh objects";
244 theResList << "smesh.SetName( MEFISTO_2D.GetAlgorithm(), 'MEFISTO_2D' )";
245 theResList << "smesh.SetName( Regular_1D.GetAlgorithm(), 'Regular_1D' )";
246 theResList << "smesh.SetName( Max_Size_1, 'Max Size_1' )";
247 theResList << "smesh.SetName( Max_Element_Area_1, 'Max. Element Area_1' )";
248 theResList << QString( "smesh.SetName( %1.GetMesh(), '%1' )" ).arg( theMeshName );
251 theResList << "# Greate SMESH groups";
252 theResList << QString( "geompy = geomBuilder.New( %1 )" ).arg( theStudyName );
253 theResList << QString( "geom_groups = geompy.GetGroups( %1 )" ).arg( theGeomShapeName );
254 theResList << QString( "for group in geom_groups:" );
255 theResList << QString( " smesh_group = %1.GroupOnGeom(group, group.GetName(), SMESH.EDGE)" )
257 theResList << QString( " smesh.SetName(smesh_group, group.GetName())" );
258 theResList << QString( " print \"SMESH group '%s': %s\" % (smesh_group.GetName(), smesh_group)" );
261 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
263 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
265 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
266 if ( !aBoundaryPolyline.IsNull() )
267 aResSeq.Append( aBoundaryPolyline );
269 HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
270 aResSeq.Append( aSeqOfRegions );
275 void HYDROData_CalculationCase::Update()
277 HYDROData_Entity::Update();
280 // At first we remove previously created objects
282 RemoveSplittedGroups();
284 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
285 if ( aDocument.IsNull() )
288 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
289 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
290 if ( aGeomObjects.IsEmpty() )
293 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
295 HYDROData_SplitToZonesTool::SplitDataList aSplitObjects =
296 HYDROData_SplitToZonesTool::Split( aGeomObjects, aGeomGroups, aBoundaryPolyline );
297 if ( aSplitObjects.isEmpty() )
300 HYDROData_SplitToZonesTool::SplitDataList aZonesList, anEdgesList;
302 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitObjects );
303 while( anIter.hasNext() )
305 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
306 if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Zone )
307 aZonesList.append( aSplitData );
308 else if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Edge )
309 anEdgesList.append( aSplitData );
312 switch( GetAssignmentMode() )
315 CreateRegionsDef( aDocument, aZonesList );
318 CreateRegionsAuto( aDocument, aZonesList );
321 CreateEdgeGroupsDef( aDocument, anEdgesList );
324 void HYDROData_CalculationCase::CreateRegionsDef( const Handle(HYDROData_Document)& theDoc,
325 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
327 // Create result regions for case, by default one zone for one region
328 QString aRegsPref = CALCULATION_REGIONS_PREF;
329 QString aZonesPref = CALCULATION_ZONES_PREF;
331 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
332 while( anIter.hasNext() )
334 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
336 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegsPref );
338 // Add the zone for region
339 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
343 void HYDROData_CalculationCase::CreateRegionsAuto( const Handle(HYDROData_Document)& theDoc,
344 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
346 QMap<QString, Handle(HYDROData_Region)> aRegionsMap; //object name to region
347 QMap<QString, QString> aRegionNameToObjNameMap;
348 QString aZonesPref = CALCULATION_ZONES_PREF;
349 HYDROData_PriorityQueue aPr( this );
351 // 1. First we create a default region for each object included into the calculation case
352 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
353 for( int i=aGeomObjects.Lower(), n=aGeomObjects.Upper(); i<=n; i++ )
355 Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( aGeomObjects.Value( i ) );
358 QString anObjName = anObj->GetName();
359 QString aRegName = anObjName + "_reg";
360 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegName, false );
361 aRegionsMap.insert( anObjName, aRegion );
362 aRegionNameToObjNameMap.insert( aRegName, anObjName );
365 // 2. Now for each zone it is necessary to determine the most priority object
366 // and assign to zone to corresponding region
367 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
368 while( anIter.hasNext() )
370 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
371 HYDROData_Zone::MergeAltitudesType aMergeType;
372 Handle(HYDROData_Object) aRegObj = aPr.GetMostPriorityObject( aSplitData.ObjectNames, aMergeType );
373 if( aRegObj.IsNull() )
375 Handle(HYDROData_Region) aRegion = aRegionsMap[aRegObj->GetName()];
376 if( aRegion.IsNull() )
378 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
380 if( aSplitData.ObjectNames.count() > 1 && aMergeType==HYDROData_Zone::Merge_UNKNOWN )
382 qDebug( "Error in algorithm: unresolved conflicts" );
387 case HYDROData_Zone::Merge_ZMIN:
388 case HYDROData_Zone::Merge_ZMAX:
389 aRegionZone->SetMergeType( aMergeType );
391 case HYDROData_Zone::Merge_Object:
392 aRegionZone->SetMergeType( aMergeType );
393 aRegionZone->RemoveMergeAltitude();
394 aRegionZone->SetMergeAltitude( aRegObj->GetAltitudeObject() );
399 QStringList anObjectsWithEmptyRegions;
400 QMap<QString, Handle(HYDROData_Region)>::const_iterator
401 anIt = aRegionsMap.begin(), aLast = aRegionsMap.end();
402 for( ; anIt!=aLast; anIt++ )
404 Handle(HYDROData_Region) aRegion = anIt.value();
405 if( aRegion->GetZones().IsEmpty() )
407 QString aRegName = aRegion->GetName();
408 QString anObjName = aRegionNameToObjNameMap[aRegName];
409 anObjectsWithEmptyRegions.append( anObjName );
413 if( !anObjectsWithEmptyRegions.empty() )
415 QString aData = anObjectsWithEmptyRegions.join( ", " );
416 SetWarning( WARN_EMPTY_REGIONS, aData );
420 void HYDROData_CalculationCase::CreateEdgeGroupsDef( const Handle(HYDROData_Document)& theDoc,
421 const HYDROData_SplitToZonesTool::SplitDataList& theEdges )
423 QMap<QString,Handle(HYDROData_SplittedShapesGroup)> aSplittedEdgesGroupsMap;
425 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theEdges );
426 while( anIter.hasNext() )
428 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
429 // Create new edges group
430 if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
433 QString anObjName = aSplitData.ObjectNames.first();
434 if ( anObjName.isEmpty() )
436 #ifdef DEB_CALCULATION
437 QString aStr = aSplitData.ObjectNames.join(" ");
438 cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl;
440 Handle(HYDROData_SplittedShapesGroup) aSplittedGroup;
441 if ( !aSplittedEdgesGroupsMap.contains( anObjName ) )
443 aSplittedGroup = addNewSplittedGroup( CALCULATION_GROUPS_PREF + anObjName );
444 aSplittedEdgesGroupsMap.insert( anObjName, aSplittedGroup );
448 aSplittedGroup = aSplittedEdgesGroupsMap[ anObjName ];
450 if ( aSplittedGroup.IsNull() )
453 aSplittedGroup->AddShape( aSplitData.Shape );
457 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
459 if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
460 return false; // Wrong type of object
462 if ( HasReference( theObject, DataTag_GeometryObject ) )
463 return false; // Object is already in reference list
465 AddReferenceObject( theObject, DataTag_GeometryObject );
467 // Indicate model of the need to update splitting
473 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
475 return GetReferenceObjects( DataTag_GeometryObject );
478 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
480 if ( theObject.IsNull() )
483 RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
485 // Indicate model of the need to update splitting
489 void HYDROData_CalculationCase::RemoveGeometryObjects()
491 ClearReferenceObjects( DataTag_GeometryObject );
493 // Indicate model of the need to update splitting
497 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
499 if ( theGroup.IsNull() )
502 if ( HasReference( theGroup, DataTag_GeometryGroup ) )
503 return false; // Object is already in reference list
505 AddReferenceObject( theGroup, DataTag_GeometryGroup );
507 // Indicate model of the need to update splitting
513 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
515 return GetReferenceObjects( DataTag_GeometryGroup );
518 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
520 if ( theGroup.IsNull() )
523 RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
525 // Indicate model of the need to update splitting
529 void HYDROData_CalculationCase::RemoveGeometryGroups()
531 ClearReferenceObjects( DataTag_GeometryGroup );
533 // Indicate model of the need to update splitting
537 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
539 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
541 SetReferenceObject( thePolyline, DataTag_Polyline );
543 // Indicate model of the need to update zones splitting
544 SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
547 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
549 return Handle(HYDROData_PolylineXY)::DownCast(
550 GetReferenceObject( DataTag_Polyline ) );
553 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
555 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
557 ClearReferenceObjects( DataTag_Polyline );
559 // Indicate model of the need to update zones splitting
560 SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
563 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
565 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
566 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
567 if ( aNewRegion.IsNull() )
570 aNewRegion->AddZone( theZone );
575 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
577 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
579 if ( theRegion.IsNull() )
582 if ( HasReference( theRegion, DataTag_Region ) )
583 return false; // Object is already in reference list
585 // Move the region from other calculation
586 Handle(HYDROData_CalculationCase) aFatherCalc =
587 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
588 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
590 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
591 theRegion->CopyTo( aNewRegion );
593 aFatherCalc->RemoveRegion( theRegion );
595 theRegion->SetLabel( aNewRegion->Label() );
599 AddReferenceObject( theRegion, DataTag_Region );
605 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
607 return GetReferenceObjects( DataTag_Region );
610 void HYDROData_CalculationCase::UpdateRegionsOrder()
612 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
613 if ( aDocument.IsNull() )
616 HYDROData_SequenceOfObjects aRegions = GetRegions();
618 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
619 for ( ; anIter.More(); anIter.Next() )
621 Handle(HYDROData_Region) aRegion =
622 Handle(HYDROData_Region)::DownCast( anIter.Value() );
623 if ( aRegion.IsNull() )
626 aRegion->SetName( "" );
629 QString aRegsPref = CALCULATION_REGIONS_PREF;
631 anIter.Init( aRegions );
632 for ( ; anIter.More(); anIter.Next() )
634 Handle(HYDROData_Region) aRegion =
635 Handle(HYDROData_Region)::DownCast( anIter.Value() );
636 if ( aRegion.IsNull() )
639 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
640 aRegion->SetName( aRegionName );
644 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
646 if ( theRegion.IsNull() )
649 RemoveReferenceObject( theRegion->Label(), DataTag_Region );
651 // Remove region from data model
652 Handle(HYDROData_CalculationCase) aFatherCalc =
653 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
654 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
658 void HYDROData_CalculationCase::RemoveRegions()
660 myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
663 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
665 return GetReferenceObjects( DataTag_SplittedGroups );
668 void HYDROData_CalculationCase::RemoveSplittedGroups()
670 myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
673 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
675 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
676 return GetAltitudeForPoint( thePoint, aZone );
679 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
680 const Handle(HYDROData_Region)& theRegion ) const
682 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
684 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
685 if ( !aZone.IsNull() )
687 Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
688 if ( IsEqual( aRefRegion, theRegion ) )
689 aResAltitude = GetAltitudeForPoint( thePoint, aZone );
695 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
696 const Handle(HYDROData_Zone)& theZone ) const
698 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
699 if ( theZone.IsNull() )
702 HYDROData_Zone::MergeAltitudesType aZoneMergeType = theZone->GetMergeType();
703 if ( !theZone->IsMergingNeed() )
705 aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
707 else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
712 HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
713 if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
715 Handle(HYDROData_IAltitudeObject) aMergeAltitude = theZone->GetMergeAltitude();
716 if ( !aMergeAltitude.IsNull() )
718 if ( aZoneInterpolator != NULL )
720 aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
721 aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
724 aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
729 HYDROData_SequenceOfObjects aZoneObjects = theZone->GetGeometryObjects();
730 HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
731 for ( ; anIter.More(); anIter.Next() )
733 Handle(HYDROData_Object) aZoneObj =
734 Handle(HYDROData_Object)::DownCast( anIter.Value() );
735 if ( aZoneObj.IsNull() )
738 Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
739 if ( anObjAltitude.IsNull() )
742 double aPointAltitude = 0.0;
743 if ( aZoneInterpolator != NULL )
745 aZoneInterpolator->SetAltitudeObject( anObjAltitude );
746 aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
749 aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
751 if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
754 if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
756 aResAltitude = aPointAltitude;
759 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
761 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
762 aResAltitude > aPointAltitude )
764 aResAltitude = aPointAltitude;
767 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
769 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
770 aResAltitude < aPointAltitude )
772 aResAltitude = aPointAltitude;
781 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
782 const NCollection_Sequence<gp_XY>& thePoints,
783 const Handle(HYDROData_Region)& theRegion ) const
785 NCollection_Sequence<double> aResSeq;
787 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
789 const gp_XY& thePnt = thePoints.Value( i );
791 double anAltitude = GetAltitudeForPoint( thePnt, theRegion );
792 aResSeq.Append( anAltitude );
798 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
799 const NCollection_Sequence<gp_XY>& thePoints,
800 const Handle(HYDROData_Zone)& theZone ) const
802 NCollection_Sequence<double> aResSeq;
804 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
806 const gp_XY& thePnt = thePoints.Value( i );
808 double anAltitude = GetAltitudeForPoint( thePnt, theZone );
809 aResSeq.Append( anAltitude );
815 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
817 Handle(HYDROData_Region) aResRegion;
819 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
820 if ( !aZone.IsNull() )
821 aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
826 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
828 Handle(HYDROData_Zone) aResZone;
830 HYDROData_SequenceOfObjects aRegions = GetRegions();
832 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
833 for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
835 Handle(HYDROData_Region) aRegion =
836 Handle(HYDROData_Region)::DownCast( anIter.Value() );
837 if ( aRegion.IsNull() )
840 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
841 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
842 for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
844 Handle(HYDROData_Zone) aRegZone =
845 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
846 if ( aRegZone.IsNull() )
849 PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
850 if ( aPointRelation != POINT_OUT )
851 aResZone = aRegZone; // We found the desired zone
858 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
859 const gp_XY& thePoint,
860 const Handle(HYDROData_Zone)& theZone ) const
862 PointClassification aRes = POINT_OUT;
863 if ( theZone.IsNull() )
866 TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
867 if ( aZoneFace.IsNull() )
870 TopoDS_Compound aCmp;
872 aBB.MakeCompound(aCmp);
873 aBB.Add(aCmp, aZoneFace);
874 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
875 BRepBuilderAPI_MakeVertex aMk(aPnt);
876 aBB.Add(aCmp, aMk.Vertex());
877 BRepTools::Write(aCmp, "FCL2d.brep");
879 TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
880 if (State == TopAbs_OUT)
882 else if(State == TopAbs_IN)
884 else if(State == TopAbs_ON)
889 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
890 const QString& thePrefixOrName,
893 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
894 int aTag = aNewLab.Tag();
896 Handle(HYDROData_Region) aNewRegion =
897 Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
898 AddRegion( aNewRegion );
900 QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
901 aNewRegion->SetName( aRegionName );
906 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup( const QString& theName )
908 TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
910 Handle(HYDROData_SplittedShapesGroup) aNewGroup =
911 Handle(HYDROData_SplittedShapesGroup)::DownCast(
912 HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
913 AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
915 aNewGroup->SetName( theName );
920 QString HYDROData_CalculationCase::Export( int theStudyId ) const
922 GEOM::GEOM_Gen_var aGEOMEngine = HYDROData_GeomTool::GetGeomGen();
923 SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( theStudyId );
925 QString aGeomObjEntry;
926 bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry );
927 return isOK ? aGeomObjEntry : QString();
930 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
931 SALOMEDS::Study_ptr theStudy,
932 QString& theGeomObjEntry ) const
934 HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
936 // Get groups definitions
937 HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
939 HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
940 for ( ; anIter.More(); anIter.Next() )
943 Handle(HYDROData_ShapesGroup) aGroup =
944 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
945 if ( aGroup.IsNull() )
948 HYDROData_ShapesGroup::GroupDefinition aGroupDef;
950 aGroupDef.Name = aGroup->GetName().toLatin1().constData();
951 aGroup->GetShapes( aGroupDef.Shapes );
953 aSeqOfGroupsDefs.Append( aGroupDef );
957 TopTools_ListOfShape aFaces;
958 HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
959 HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
960 for ( ; aRegionIter.More(); aRegionIter.Next() )
962 Handle(HYDROData_Region) aRegion =
963 Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
964 if( aRegion.IsNull() )
967 TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
968 aFaces.Append( aRegionShape );
971 return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs, theGeomObjEntry );
974 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
975 SALOMEDS::Study_ptr theStudy,
976 const TopTools_ListOfShape& theFaces,
977 const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs,
978 QString& theGeomObjEntry ) const
981 BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
982 aSewing.SetNonManifoldMode( Standard_False );
983 #ifdef DEB_CALCULATION
984 TCollection_AsciiString aNam("Sh_");
987 TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
988 for ( ; aFaceIter.More(); aFaceIter.Next() )
990 TopoDS_Shape aShape = aFaceIter.Value();
991 if ( aShape.IsNull() )
994 if ( aShape.ShapeType() == TopAbs_FACE )
996 aSewing.Add( aShape );
997 #ifdef DEB_CALCULATION
998 TCollection_AsciiString aName = aNam + ++i + ".brep";
999 BRepTools::Write(aShape ,aName.ToCString());
1004 #ifdef DEB_CALCULATION
1007 TopExp_Explorer anExp( aShape, TopAbs_FACE );
1008 for (; anExp.More(); anExp.Next() ) {
1009 aSewing.Add( anExp.Current() );
1010 #ifdef DEB_CALCULATION
1012 TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
1013 BRepTools::Write(anExp.Current() ,aName.ToCString());
1020 TopoDS_Shape aSewedShape = aSewing.SewedShape();
1022 // If the sewed shape is empty - return false
1023 if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
1026 #ifdef DEB_CALCULATION
1027 BRepTools::Write(aSewedShape ,"Sew.brep");
1029 // Publish the sewed shape
1030 QString aName = EXPORT_NAME;
1031 GEOM::GEOM_Object_ptr aMainShape =
1032 publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName, theGeomObjEntry );
1034 if ( aMainShape->_is_nil() )
1037 if ( theGroupsDefs.IsEmpty() )
1041 TopTools_IndexedMapOfShape aMapOfSubShapes;
1042 TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
1044 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
1046 for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
1048 const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
1050 NCollection_Sequence<int> aGroupIndexes;
1051 for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
1053 const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
1054 #ifdef DEB_CALCULATION
1055 cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
1058 TopoDS_Shape aModifiedShape = aShape;
1059 if ( aSewing.IsModified( aShape ) )
1060 aModifiedShape = aSewing.Modified( aShape );
1061 else if ( aSewing.IsModifiedSubShape( aShape ) )
1062 aModifiedShape = aSewing.ModifiedSubShape( aShape );
1064 #ifdef DEB_CALCULATION
1065 const TopLoc_Location& aL1 = aShape.Location();
1066 const TopLoc_Location& aL2 = aModifiedShape.Location();
1067 cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
1070 int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
1071 if ( anIndex > 0 ) {
1072 aGroupIndexes.Append( anIndex );
1074 #ifdef DEB_CALCULATION
1075 TCollection_AsciiString aNam("Lost_");
1076 if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1077 for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1079 const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1080 if ( aModifiedShape.IsPartner( aS ) )
1082 cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1083 TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1084 BRepTools::Write(aS ,aName.ToCString());
1092 if ( !aGroupIndexes.IsEmpty() )
1093 aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1096 if ( !aGroupsData.IsEmpty() )
1098 GEOM::GEOM_IGroupOperations_var aGroupOp =
1099 theGeomEngine->GetIGroupOperations( theStudy->StudyId() );
1101 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1102 for ( ; aMapIt.More(); aMapIt.Next() )
1104 const TCollection_AsciiString& aGroupName = aMapIt.Key();
1105 const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1107 GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1108 if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1111 GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1112 aGeomIndexes->length( aGroupIndexes.Length() );
1114 for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1115 aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1117 aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1118 if ( aGroupOp->IsDone() )
1120 SALOMEDS::SObject_var aGroupSO =
1121 theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1129 GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM(
1130 GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
1131 const TopoDS_Shape& theShape, const QString& theName,
1132 QString& theGeomObjEntry ) const
1134 theGeomObjEntry = "";
1135 GEOM::GEOM_Object_var aGeomObj;
1137 if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
1138 theShape.IsNull() ) {
1139 return aGeomObj._retn();
1142 std::ostringstream aStreamShape;
1143 // Write TopoDS_Shape in ASCII format to the stream
1144 BRepTools::Write( theShape, aStreamShape );
1145 // Returns the number of bytes that have been stored in the stream's buffer.
1146 int aSize = aStreamShape.str().size();
1147 // Allocate octect buffer of required size
1148 CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
1149 // Copy ostrstream content to the octect buffer
1150 memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
1152 SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
1154 // Restore shape from the stream and get the GEOM object
1155 GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
1156 aGeomObj = anInsOp->RestoreShape( aSeqFile );
1158 // Puplish the GEOM object
1159 if ( !aGeomObj->_is_nil() ) {
1160 QString aName = HYDROData_GeomTool::GetFreeName( theStudy, theName );
1162 SALOMEDS::SObject_var aResultSO =
1163 theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(),
1164 aGeomObj, qPrintable( aName ) );
1165 if ( aResultSO->_is_nil() ) {
1166 aGeomObj = GEOM::GEOM_Object::_nil();
1169 theGeomObjEntry = aResultSO->GetID();
1172 return aGeomObj._retn();
1175 void HYDROData_CalculationCase::ClearRules( const bool theIsSetToUpdate )
1177 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1178 HYDROData_PriorityQueue::ClearRules( aRulesLab );
1180 // Indicate model of the need to update splitting
1181 if ( theIsSetToUpdate ) {
1182 SetToUpdate( true );
1186 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Object)& theObject1,
1187 HYDROData_PriorityType thePriority,
1188 const Handle(HYDROData_Object)& theObject2,
1189 HYDROData_Zone::MergeAltitudesType theMergeType )
1191 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1192 HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1194 // Indicate model of the need to update splitting
1195 SetToUpdate( true );
1198 QString HYDROData_CalculationCase::DumpRules() const
1200 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1201 return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1204 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1206 TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1207 TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1209 // Indicate model of the need to update splitting
1210 SetToUpdate( true );
1213 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1215 Handle(TDataStd_Integer) aModeAttr;
1216 bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1218 return ( AssignmentMode ) aModeAttr->Get();
1223 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1224 QStringList& theScript ) const
1226 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1227 HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1230 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1232 return myLastWarning;
1235 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1237 myLastWarning.Type = theType;
1238 myLastWarning.Data = theData;
1241 bool HYDROData_CalculationCase::GetRule( int theIndex,
1242 Handle(HYDROData_Object)& theObject1,
1243 HYDROData_PriorityType& thePriority,
1244 Handle(HYDROData_Object)& theObject2,
1245 HYDROData_Zone::MergeAltitudesType& theMergeType ) const
1247 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1248 return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1249 theObject1, thePriority, theObject2, theMergeType );