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_SplitToZonesTool.h"
12 #include "HYDROData_SplittedShapesGroup.h"
13 #include "HYDROData_Region.h"
14 #include "HYDROData_Tool.h"
15 #include "HYDROData_Zone.h"
22 #include <TopoDS_Shell.hxx>
23 #include <TopoDS_Edge.hxx>
25 #include <BRep_Builder.hxx>
26 #include <BRepBuilderAPI_Sewing.hxx>
27 #include <BRepTopAdaptor_FClass2d.hxx>
29 #include <BRepTools.hxx>
32 #include <TopExp_Explorer.hxx>
34 #include <TopTools_ListOfShape.hxx>
35 #include <TopTools_ListIteratorOfListOfShape.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() + "_"
46 #define EXPORT_NAME "HYDRO_" + GetName()
48 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
49 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
51 HYDROData_CalculationCase::HYDROData_CalculationCase()
56 HYDROData_CalculationCase::~HYDROData_CalculationCase()
60 void HYDROData_CalculationCase::SetName( const QString& theName )
62 QString anOldCaseName = GetName();
63 if ( anOldCaseName != theName )
65 HYDROData_SequenceOfObjects aRegions = GetRegions();
67 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
68 for ( ; anIter.More(); anIter.Next() )
70 Handle(HYDROData_Region) aRegion =
71 Handle(HYDROData_Region)::DownCast( anIter.Value() );
72 if ( aRegion.IsNull() )
75 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegion );
77 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
78 HYDROData_SequenceOfObjects::Iterator anIter( aZones );
79 for ( ; anIter.More(); anIter.Next() )
81 Handle(HYDROData_Zone) aRegZone =
82 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
83 if ( aRegZone.IsNull() )
86 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegZone );
90 HYDROData_SequenceOfObjects aGroups = GetGeometryGroups();
92 anIter.Init( aGroups );
93 for ( ; anIter.More(); anIter.Next() )
95 Handle(HYDROData_SplittedShapesGroup) aGroup =
96 Handle(HYDROData_SplittedShapesGroup)::DownCast( anIter.Value() );
97 if ( aGroup.IsNull() )
100 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aGroup );
104 HYDROData_Entity::SetName( theName );
107 QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
109 QStringList aResList = dumpObjectCreation( theTreatedObjects );
111 QString aCalculName = GetObjPyName();
113 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
114 HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
115 for ( ; anIter.More(); anIter.Next() )
117 Handle(HYDROData_Object) aRefGeomObj =
118 Handle(HYDROData_Object)::DownCast( anIter.Value() );
119 setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
121 aResList << QString( "" );
123 QString aGroupName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "case_geom_group" );
125 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
126 anIter.Init( aGeomGroups );
127 for ( ; anIter.More(); anIter.Next() )
129 Handle(HYDROData_ShapesGroup) aGeomGroup =
130 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
131 if ( aGeomGroup.IsNull() )
134 Handle(HYDROData_Object) aFatherGeom =
135 Handle(HYDROData_Object)::DownCast( aGeomGroup->GetFatherObject() );
136 if ( aFatherGeom.IsNull() )
139 int aGroupId = aFatherGeom->GetGroupId( aGeomGroup );
140 aResList << QString( "%1 = %2.GetGroup( %3 );" )
141 .arg( aGroupName ).arg( aFatherGeom->GetObjPyName() ).arg( aGroupId );
143 aResList << QString( "%1.AddGeometryGroup( %2 );" ).arg( aCalculName ).arg( aGroupName );
145 aResList << QString( "" );
147 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
148 setPythonReferenceObject( theTreatedObjects, aResList, aBoundaryPolyline, "SetBoundaryPolyline" );
149 aResList << QString( "" );
151 aResList << QString( "%1.Update();" ).arg( aCalculName );
152 aResList << QString( "" );
154 // Now we restore the regions and zones order
155 HYDROData_SequenceOfObjects aRegions = GetRegions();
156 anIter.Init( aRegions );
157 for ( ; anIter.More(); anIter.Next() )
159 Handle(HYDROData_Region) aRegion =
160 Handle(HYDROData_Region)::DownCast( anIter.Value() );
161 if ( aRegion.IsNull() )
164 QString aRegionName = aRegion->GetName();
166 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
167 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
168 for ( ; aZonesIter.More(); aZonesIter.Next() )
170 Handle(HYDROData_Zone) aRegZone =
171 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
172 if ( aRegZone.IsNull() )
178 aResList << QString( "" );
183 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
185 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
187 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
188 if ( !aBoundaryPolyline.IsNull() )
189 aResSeq.Append( aBoundaryPolyline );
191 HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
192 aResSeq.Append( aSeqOfRegions );
197 void HYDROData_CalculationCase::Update()
199 HYDROData_Entity::Update();
201 // At first we remove previously created objects
203 RemoveSplittedGroups();
205 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
206 if ( aDocument.IsNull() )
209 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
210 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
211 if ( aGeomObjects.IsEmpty() )
214 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
216 HYDROData_SplitToZonesTool::SplitDataList aSplitedObjects =
217 HYDROData_SplitToZonesTool::Split( aGeomObjects, aGeomGroups, aBoundaryPolyline );
218 if ( aSplitedObjects.isEmpty() )
221 QString aRegsPref = CALCULATION_REGIONS_PREF;
222 QString aZonesPref = CALCULATION_ZONES_PREF;
224 QMap<QString,Handle(HYDROData_SplittedShapesGroup)> aSplittedEdgesGroupsMap;
226 // Create result regions for case, by default one zone for one region
227 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitedObjects );
228 while( anIter.hasNext() )
230 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
232 if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Zone )
235 Handle(HYDROData_Region) aRegion = addNewRegion();
237 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
238 aRegion->SetName( aRegionName );
240 // Add the zone for region
241 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone();
243 QString aZoneName = HYDROData_Tool::GenerateObjectName( aDocument, aZonesPref );
244 aRegionZone->SetName( aZoneName );
246 aRegionZone->SetShape( aSplitData.Face() );
248 // Add the reference object for zone
249 for ( int i = 0, n = aSplitData.ObjectNames.length(); i < n; ++i )
251 const QString& anObjName = aSplitData.ObjectNames.at( i );
253 Handle(HYDROData_Object) aRefObject = Handle(HYDROData_Object)::DownCast(
254 HYDROData_Tool::FindObjectByName( aDocument, anObjName ) );
255 if ( aRefObject.IsNull() )
258 aRegionZone->AddGeometryObject( aRefObject );
261 else if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Edge )
263 // Create new edges group
264 if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
267 QString anObjName = aSplitData.ObjectNames.first();
268 if ( anObjName.isEmpty() )
271 Handle(HYDROData_SplittedShapesGroup) aSplittedGroup;
272 if ( !aSplittedEdgesGroupsMap.contains( anObjName ) )
274 aSplittedGroup = addNewSplittedGroup();
276 QString aCalcGroupName = CALCULATION_GROUPS_PREF + anObjName;
277 aSplittedGroup->SetName( aCalcGroupName );
279 aSplittedEdgesGroupsMap.insert( anObjName, aSplittedGroup );
283 aSplittedGroup = aSplittedEdgesGroupsMap[ anObjName ];
286 if ( aSplittedGroup.IsNull() )
289 aSplittedGroup->AddShape( aSplitData.Shape );
294 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
296 if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
297 return false; // Wrong type of object
299 if ( HasReference( theObject, DataTag_GeometryObject ) )
300 return false; // Object is already in reference list
302 AddReferenceObject( theObject, DataTag_GeometryObject );
304 // Indicate model of the need to update splitting
310 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
312 return GetReferenceObjects( DataTag_GeometryObject );
315 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
317 if ( theObject.IsNull() )
320 RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
322 // Indicate model of the need to update splitting
326 void HYDROData_CalculationCase::RemoveGeometryObjects()
328 ClearReferenceObjects( DataTag_GeometryObject );
330 // Indicate model of the need to update splitting
334 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
336 if ( theGroup.IsNull() )
339 if ( HasReference( theGroup, DataTag_GeometryGroup ) )
340 return false; // Object is already in reference list
342 AddReferenceObject( theGroup, DataTag_GeometryGroup );
344 // Indicate model of the need to update splitting
350 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
352 return GetReferenceObjects( DataTag_GeometryGroup );
355 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
357 if ( theGroup.IsNull() )
360 RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
362 // Indicate model of the need to update splitting
366 void HYDROData_CalculationCase::RemoveGeometryGroups()
368 ClearReferenceObjects( DataTag_GeometryGroup );
370 // Indicate model of the need to update splitting
374 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
376 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
378 SetReferenceObject( thePolyline, DataTag_Polyline );
380 // Indicate model of the need to update zones splitting
381 SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
384 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
386 return Handle(HYDROData_PolylineXY)::DownCast(
387 GetReferenceObject( DataTag_Polyline ) );
390 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
392 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
394 ClearReferenceObjects( DataTag_Polyline );
396 // Indicate model of the need to update zones splitting
397 SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
400 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
402 Handle(HYDROData_Region) aNewRegion = addNewRegion();
403 if ( aNewRegion.IsNull() )
406 // Generate new name for new region
407 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
408 if ( !aDocument.IsNull() )
410 QString aRegsPref = CALCULATION_REGIONS_PREF;
412 QString aNewRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
413 aNewRegion->SetName( aNewRegionName );
416 aNewRegion->AddZone( theZone );
421 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
423 if ( theRegion.IsNull() )
426 if ( HasReference( theRegion, DataTag_Region ) )
427 return false; // Object is already in reference list
429 // Move the region from other calculation
430 Handle(HYDROData_CalculationCase) aFatherCalc =
431 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
432 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
434 Handle(HYDROData_Region) aNewRegion = addNewRegion();
435 theRegion->CopyTo( aNewRegion );
437 aFatherCalc->RemoveRegion( theRegion );
439 theRegion->SetLabel( aNewRegion->Label() );
443 AddReferenceObject( theRegion, DataTag_Region );
449 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
451 return GetReferenceObjects( DataTag_Region );
454 void HYDROData_CalculationCase::UpdateRegionsOrder()
456 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
457 if ( aDocument.IsNull() )
460 HYDROData_SequenceOfObjects aRegions = GetRegions();
462 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
463 for ( ; anIter.More(); anIter.Next() )
465 Handle(HYDROData_Region) aRegion =
466 Handle(HYDROData_Region)::DownCast( anIter.Value() );
467 if ( aRegion.IsNull() )
470 aRegion->SetName( "" );
473 QString aRegsPref = CALCULATION_REGIONS_PREF;
475 anIter.Init( aRegions );
476 for ( ; anIter.More(); anIter.Next() )
478 Handle(HYDROData_Region) aRegion =
479 Handle(HYDROData_Region)::DownCast( anIter.Value() );
480 if ( aRegion.IsNull() )
483 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
484 aRegion->SetName( aRegionName );
488 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
490 if ( theRegion.IsNull() )
493 RemoveReferenceObject( theRegion->Label(), DataTag_Region );
495 // Remove region from data model
496 Handle(HYDROData_CalculationCase) aFatherCalc =
497 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
498 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
502 void HYDROData_CalculationCase::RemoveRegions()
504 myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
507 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
509 return GetReferenceObjects( DataTag_SplittedGroups );
512 void HYDROData_CalculationCase::RemoveSplittedGroups()
514 myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
517 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
519 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
521 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
522 if ( aZone.IsNull() )
525 HYDROData_Zone::MergeAltitudesType aZoneMergeType = aZone->GetMergeType();
526 if ( !aZone->IsMergingNeed() )
528 aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
530 else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
535 if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
537 Handle(HYDROData_IAltitudeObject) aMergeAltitude = aZone->GetMergeAltitude();
538 if ( !aMergeAltitude.IsNull() )
539 aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
543 HYDROData_SequenceOfObjects aZoneObjects = aZone->GetGeometryObjects();
544 HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
545 for ( ; anIter.More(); anIter.Next() )
547 Handle(HYDROData_Object) aZoneObj =
548 Handle(HYDROData_Object)::DownCast( anIter.Value() );
549 if ( aZoneObj.IsNull() )
552 Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
553 if ( anObjAltitude.IsNull() )
556 double aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
557 if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
560 if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
562 aResAltitude = aPointAltitude;
565 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
567 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
568 aResAltitude > aPointAltitude )
570 aResAltitude = aPointAltitude;
573 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
575 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
576 aResAltitude < aPointAltitude )
578 aResAltitude = aPointAltitude;
587 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
589 Handle(HYDROData_Zone) aResZone;
591 HYDROData_SequenceOfObjects aRegions = GetRegions();
593 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
594 for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
596 Handle(HYDROData_Region) aRegion =
597 Handle(HYDROData_Region)::DownCast( anIter.Value() );
598 if ( aRegion.IsNull() )
601 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
602 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
603 for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
605 Handle(HYDROData_Zone) aRegZone =
606 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
607 if ( aRegZone.IsNull() )
610 PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
611 if ( aPointRelation != POINT_OUT )
612 aResZone = aRegZone; // We found the desired zone
619 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
620 const gp_XY& thePoint,
621 const Handle(HYDROData_Zone)& theZone ) const
623 PointClassification aRes = POINT_OUT;
624 if ( theZone.IsNull() )
627 TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
628 if ( aZoneFace.IsNull() )
631 BRepTopAdaptor_FClass2d aClassifier( aZoneFace, Precision::Confusion() );
632 TopAbs_State State = aClassifier.Perform( gp_Pnt2d(thePoint), Standard_False );
633 if (State == TopAbs_OUT)
635 else if(State == TopAbs_IN)
637 else if(State == TopAbs_ON)
642 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion()
644 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
646 Handle(HYDROData_Region) aNewRegion =
647 Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
648 AddRegion( aNewRegion );
653 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup()
655 TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
657 Handle(HYDROData_SplittedShapesGroup) aNewGroup =
658 Handle(HYDROData_SplittedShapesGroup)::DownCast(
659 HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
660 AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
665 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
666 SALOMEDS::Study_ptr theStudy ) const
668 HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
670 // Get groups definitions
671 HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
673 HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
674 for ( ; anIter.More(); anIter.Next() )
677 Handle(HYDROData_ShapesGroup) aGroup =
678 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
679 if ( aGroup.IsNull() )
682 HYDROData_ShapesGroup::GroupDefinition aGroupDef;
684 aGroupDef.Name = aGroup->GetName().toLatin1().constData();
685 aGroup->GetShapes( aGroupDef.Shapes );
687 aSeqOfGroupsDefs.Append( aGroupDef );
691 TopTools_ListOfShape aFaces;
692 HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
693 HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
694 for ( ; aRegionIter.More(); aRegionIter.Next() )
696 Handle(HYDROData_Region) aRegion =
697 Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
698 if( aRegion.IsNull() )
701 TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
702 aFaces.Append( aRegionShape );
705 return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs );
708 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
709 SALOMEDS::Study_ptr theStudy,
710 const TopTools_ListOfShape& theFaces,
711 const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs ) const
714 BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
715 aSewing.SetNonManifoldMode( Standard_False );
716 #ifdef DEB_CALCULATION
717 TCollection_AsciiString aNam("Sh_");
720 TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
721 for ( ; aFaceIter.More(); aFaceIter.Next() )
723 TopoDS_Shape aShape = aFaceIter.Value();
724 if ( aShape.IsNull() )
727 if ( aShape.ShapeType() == TopAbs_FACE )
729 aSewing.Add( aShape );
730 #ifdef DEB_CALCULATION
731 TCollection_AsciiString aName = aNam + ++i + ".brep";
732 BRepTools::Write(aShape ,aName.ToCString());
737 #ifdef DEB_CALCULATION
740 TopExp_Explorer anExp( aShape, TopAbs_FACE );
741 for (; anExp.More(); anExp.Next() ) {
742 aSewing.Add( anExp.Current() );
743 #ifdef DEB_CALCULATION
745 TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
746 BRepTools::Write(anExp.Current() ,aName.ToCString());
753 TopoDS_Shape aSewedShape = aSewing.SewedShape();
755 // If the sewed shape is empty - return false
756 if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
759 #ifdef DEB_CALCULATION
760 BRepTools::Write(aSewedShape ,"Sew.brep");
762 // Publish the sewed shape
763 QString aName = EXPORT_NAME;
764 GEOM::GEOM_Object_ptr aMainShape =
765 publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName );
767 if ( aMainShape->_is_nil() )
770 if ( theGroupsDefs.IsEmpty() )
774 TopTools_IndexedMapOfShape aMapOfSubShapes;
775 TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
777 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
779 for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
781 const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
783 NCollection_Sequence<int> aGroupIndexes;
784 for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
786 const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
787 #ifdef DEB_CALCULATION
788 cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
790 const TopoDS_Shape& aModifiedShape = aSewing.Modified( aShape );
791 if ( aModifiedShape.IsNull() )
794 #ifdef DEB_CALCULATION
795 const TopLoc_Location& aL1 = aShape.Location();
796 const TopLoc_Location& aL2 = aModifiedShape.Location();
797 cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
800 int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
802 aGroupIndexes.Append( anIndex );
804 #ifdef DEB_CALCULATION
805 TCollection_AsciiString aNam("Lost_");
806 if(!aMapOfSubShapes.Contains(aModifiedShape)) {
807 for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
809 const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
810 if ( aModifiedShape.IsPartner( aS ) )
812 cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
813 TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
814 BRepTools::Write(aS ,aName.ToCString());
822 if ( !aGroupIndexes.IsEmpty() )
823 aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
826 if ( !aGroupsData.IsEmpty() )
828 GEOM::GEOM_IGroupOperations_var aGroupOp =
829 theGeomEngine->GetIGroupOperations( theStudy->StudyId() );
831 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
832 for ( ; aMapIt.More(); aMapIt.Next() )
834 const TCollection_AsciiString& aGroupName = aMapIt.Key();
835 const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
837 GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
838 if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
841 GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
842 aGeomIndexes->length( aGroupIndexes.Length() );
844 for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
845 aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
847 aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
848 if ( aGroupOp->IsDone() )
850 SALOMEDS::SObject_var aGroupSO =
851 theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
859 GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM(
860 GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
861 const TopoDS_Shape& theShape, const QString& theName ) const
863 GEOM::GEOM_Object_var aGeomObj;
865 if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
866 theShape.IsNull() ) {
867 return aGeomObj._retn();
870 std::ostringstream aStreamShape;
871 // Write TopoDS_Shape in ASCII format to the stream
872 BRepTools::Write( theShape, aStreamShape );
873 // Returns the number of bytes that have been stored in the stream's buffer.
874 int aSize = aStreamShape.str().size();
875 // Allocate octect buffer of required size
876 CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
877 // Copy ostrstream content to the octect buffer
878 memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
880 SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
882 // Restore shape from the stream and get the GEOM object
883 GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
884 aGeomObj = anInsOp->RestoreShape( aSeqFile );
886 // Puplish the GEOM object
887 if ( !aGeomObj->_is_nil() ) {
888 QString aName = GEOMBase::GetDefaultName( theName );
890 SALOMEDS::SObject_var aResultSO =
891 theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(),
892 aGeomObj, qPrintable( aName ) );
893 if ( aResultSO->_is_nil() ) {
894 aGeomObj = GEOM::GEOM_Object::_nil();
898 return aGeomObj._retn();