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() + "_"
45 //#define DEB_CLASS2D 1
47 #include <BRepBuilderAPI_MakeVertex.hxx>
50 #define EXPORT_NAME "HYDRO_" + GetName()
52 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
53 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
55 HYDROData_CalculationCase::HYDROData_CalculationCase()
60 HYDROData_CalculationCase::~HYDROData_CalculationCase()
64 void HYDROData_CalculationCase::SetName( const QString& theName )
66 QString anOldCaseName = GetName();
67 if ( anOldCaseName != theName )
69 HYDROData_SequenceOfObjects aRegions = GetRegions();
71 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
72 for ( ; anIter.More(); anIter.Next() )
74 Handle(HYDROData_Region) aRegion =
75 Handle(HYDROData_Region)::DownCast( anIter.Value() );
76 if ( aRegion.IsNull() )
79 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegion );
81 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
82 HYDROData_SequenceOfObjects::Iterator anIter( aZones );
83 for ( ; anIter.More(); anIter.Next() )
85 Handle(HYDROData_Zone) aRegZone =
86 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
87 if ( aRegZone.IsNull() )
90 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegZone );
94 HYDROData_SequenceOfObjects aGroups = GetSplittedGroups();
96 anIter.Init( aGroups );
97 for ( ; anIter.More(); anIter.Next() )
99 Handle(HYDROData_SplittedShapesGroup) aGroup =
100 Handle(HYDROData_SplittedShapesGroup)::DownCast( anIter.Value() );
101 if ( aGroup.IsNull() )
104 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aGroup );
108 HYDROData_Entity::SetName( theName );
111 QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
113 QStringList aResList = dumpObjectCreation( theTreatedObjects );
115 QString aCalculName = GetObjPyName();
117 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
118 HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
119 for ( ; anIter.More(); anIter.Next() )
121 Handle(HYDROData_Object) aRefGeomObj =
122 Handle(HYDROData_Object)::DownCast( anIter.Value() );
123 setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
125 aResList << QString( "" );
127 QString aGroupName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "case_geom_group" );
129 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
130 anIter.Init( aGeomGroups );
131 for ( ; anIter.More(); anIter.Next() )
133 Handle(HYDROData_ShapesGroup) aGeomGroup =
134 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
135 if ( aGeomGroup.IsNull() )
138 Handle(HYDROData_Object) aFatherGeom =
139 Handle(HYDROData_Object)::DownCast( aGeomGroup->GetFatherObject() );
140 if ( aFatherGeom.IsNull() )
143 int aGroupId = aFatherGeom->GetGroupId( aGeomGroup );
144 aResList << QString( "%1 = %2.GetGroup( %3 );" )
145 .arg( aGroupName ).arg( aFatherGeom->GetObjPyName() ).arg( aGroupId );
147 aResList << QString( "%1.AddGeometryGroup( %2 );" ).arg( aCalculName ).arg( aGroupName );
149 aResList << QString( "" );
151 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
152 setPythonReferenceObject( theTreatedObjects, aResList, aBoundaryPolyline, "SetBoundaryPolyline" );
153 aResList << QString( "" );
155 aResList << QString( "%1.Update();" ).arg( aCalculName );
156 aResList << QString( "" );
158 // Now we restore the regions and zones order
159 HYDROData_SequenceOfObjects aRegions = GetRegions();
160 anIter.Init( aRegions );
161 for ( ; anIter.More(); anIter.Next() )
163 Handle(HYDROData_Region) aRegion =
164 Handle(HYDROData_Region)::DownCast( anIter.Value() );
165 if ( aRegion.IsNull() )
168 QString aRegionName = aRegion->GetName();
170 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
171 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
172 for ( ; aZonesIter.More(); aZonesIter.Next() )
174 Handle(HYDROData_Zone) aRegZone =
175 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
176 if ( aRegZone.IsNull() )
182 aResList << QString( "" );
187 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
189 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
191 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
192 if ( !aBoundaryPolyline.IsNull() )
193 aResSeq.Append( aBoundaryPolyline );
195 HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
196 aResSeq.Append( aSeqOfRegions );
201 void HYDROData_CalculationCase::Update()
203 HYDROData_Entity::Update();
205 // At first we remove previously created objects
207 RemoveSplittedGroups();
209 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
210 if ( aDocument.IsNull() )
213 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
214 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
215 if ( aGeomObjects.IsEmpty() )
218 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
220 HYDROData_SplitToZonesTool::SplitDataList aSplitedObjects =
221 HYDROData_SplitToZonesTool::Split( aGeomObjects, aGeomGroups, aBoundaryPolyline );
222 if ( aSplitedObjects.isEmpty() )
225 QString aRegsPref = CALCULATION_REGIONS_PREF;
226 QString aZonesPref = CALCULATION_ZONES_PREF;
228 QMap<QString,Handle(HYDROData_SplittedShapesGroup)> aSplittedEdgesGroupsMap;
230 // Create result regions for case, by default one zone for one region
231 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitedObjects );
232 while( anIter.hasNext() )
234 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
236 if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Zone )
239 Handle(HYDROData_Region) aRegion = addNewRegion();
241 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
242 aRegion->SetName( aRegionName );
244 // Add the zone for region
245 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone();
247 QString aZoneName = HYDROData_Tool::GenerateObjectName( aDocument, aZonesPref );
248 aRegionZone->SetName( aZoneName );
250 aRegionZone->SetShape( aSplitData.Face() );
252 // Add the reference object for zone
253 for ( int i = 0, n = aSplitData.ObjectNames.length(); i < n; ++i )
255 const QString& anObjName = aSplitData.ObjectNames.at( i );
257 Handle(HYDROData_Object) aRefObject =
258 Handle(HYDROData_Object)::DownCast( aDocument->FindObjectByName( anObjName ) );
259 if ( aRefObject.IsNull() )
262 aRegionZone->AddGeometryObject( aRefObject );
265 else if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Edge )
267 // Create new edges group
268 if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
271 QString anObjName = aSplitData.ObjectNames.first();
272 if ( anObjName.isEmpty() )
274 #ifdef DEB_CALCULATION
275 QString aStr = aSplitData.ObjectNames.join(" ");
276 cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl;
278 Handle(HYDROData_SplittedShapesGroup) aSplittedGroup;
279 if ( !aSplittedEdgesGroupsMap.contains( anObjName ) )
281 aSplittedGroup = addNewSplittedGroup();
283 QString aCalcGroupName = CALCULATION_GROUPS_PREF + anObjName;
284 aSplittedGroup->SetName( aCalcGroupName );
286 aSplittedEdgesGroupsMap.insert( anObjName, aSplittedGroup );
290 aSplittedGroup = aSplittedEdgesGroupsMap[ anObjName ];
293 if ( aSplittedGroup.IsNull() )
296 aSplittedGroup->AddShape( aSplitData.Shape );
301 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
303 if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
304 return false; // Wrong type of object
306 if ( HasReference( theObject, DataTag_GeometryObject ) )
307 return false; // Object is already in reference list
309 AddReferenceObject( theObject, DataTag_GeometryObject );
311 // Indicate model of the need to update splitting
317 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
319 return GetReferenceObjects( DataTag_GeometryObject );
322 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
324 if ( theObject.IsNull() )
327 RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
329 // Indicate model of the need to update splitting
333 void HYDROData_CalculationCase::RemoveGeometryObjects()
335 ClearReferenceObjects( DataTag_GeometryObject );
337 // Indicate model of the need to update splitting
341 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
343 if ( theGroup.IsNull() )
346 if ( HasReference( theGroup, DataTag_GeometryGroup ) )
347 return false; // Object is already in reference list
349 AddReferenceObject( theGroup, DataTag_GeometryGroup );
351 // Indicate model of the need to update splitting
357 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
359 return GetReferenceObjects( DataTag_GeometryGroup );
362 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
364 if ( theGroup.IsNull() )
367 RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
369 // Indicate model of the need to update splitting
373 void HYDROData_CalculationCase::RemoveGeometryGroups()
375 ClearReferenceObjects( DataTag_GeometryGroup );
377 // Indicate model of the need to update splitting
381 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
383 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
385 SetReferenceObject( thePolyline, DataTag_Polyline );
387 // Indicate model of the need to update zones splitting
388 SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
391 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
393 return Handle(HYDROData_PolylineXY)::DownCast(
394 GetReferenceObject( DataTag_Polyline ) );
397 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
399 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
401 ClearReferenceObjects( DataTag_Polyline );
403 // Indicate model of the need to update zones splitting
404 SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
407 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
409 Handle(HYDROData_Region) aNewRegion = addNewRegion();
410 if ( aNewRegion.IsNull() )
413 // Generate new name for new region
414 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
415 if ( !aDocument.IsNull() )
417 QString aRegsPref = CALCULATION_REGIONS_PREF;
419 QString aNewRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
420 aNewRegion->SetName( aNewRegionName );
423 aNewRegion->AddZone( theZone );
428 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
430 if ( theRegion.IsNull() )
433 if ( HasReference( theRegion, DataTag_Region ) )
434 return false; // Object is already in reference list
436 // Move the region from other calculation
437 Handle(HYDROData_CalculationCase) aFatherCalc =
438 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
439 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
441 Handle(HYDROData_Region) aNewRegion = addNewRegion();
442 theRegion->CopyTo( aNewRegion );
444 aFatherCalc->RemoveRegion( theRegion );
446 theRegion->SetLabel( aNewRegion->Label() );
450 AddReferenceObject( theRegion, DataTag_Region );
456 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
458 return GetReferenceObjects( DataTag_Region );
461 void HYDROData_CalculationCase::UpdateRegionsOrder()
463 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
464 if ( aDocument.IsNull() )
467 HYDROData_SequenceOfObjects aRegions = GetRegions();
469 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
470 for ( ; anIter.More(); anIter.Next() )
472 Handle(HYDROData_Region) aRegion =
473 Handle(HYDROData_Region)::DownCast( anIter.Value() );
474 if ( aRegion.IsNull() )
477 aRegion->SetName( "" );
480 QString aRegsPref = CALCULATION_REGIONS_PREF;
482 anIter.Init( aRegions );
483 for ( ; anIter.More(); anIter.Next() )
485 Handle(HYDROData_Region) aRegion =
486 Handle(HYDROData_Region)::DownCast( anIter.Value() );
487 if ( aRegion.IsNull() )
490 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
491 aRegion->SetName( aRegionName );
495 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
497 if ( theRegion.IsNull() )
500 RemoveReferenceObject( theRegion->Label(), DataTag_Region );
502 // Remove region from data model
503 Handle(HYDROData_CalculationCase) aFatherCalc =
504 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
505 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
509 void HYDROData_CalculationCase::RemoveRegions()
511 myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
514 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
516 return GetReferenceObjects( DataTag_SplittedGroups );
519 void HYDROData_CalculationCase::RemoveSplittedGroups()
521 myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
524 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
526 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
527 return GetAltitudeForPoint( thePoint, aZone );
530 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
531 const Handle(HYDROData_Zone)& theZone ) const
533 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
534 if ( theZone.IsNull() )
537 HYDROData_Zone::MergeAltitudesType aZoneMergeType = theZone->GetMergeType();
538 if ( !theZone->IsMergingNeed() )
540 aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
542 else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
547 HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
548 if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
550 Handle(HYDROData_IAltitudeObject) aMergeAltitude = theZone->GetMergeAltitude();
551 if ( !aMergeAltitude.IsNull() )
553 if ( aZoneInterpolator != NULL )
555 aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
556 aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
559 aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
564 HYDROData_SequenceOfObjects aZoneObjects = theZone->GetGeometryObjects();
565 HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
566 for ( ; anIter.More(); anIter.Next() )
568 Handle(HYDROData_Object) aZoneObj =
569 Handle(HYDROData_Object)::DownCast( anIter.Value() );
570 if ( aZoneObj.IsNull() )
573 Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
574 if ( anObjAltitude.IsNull() )
577 double aPointAltitude = 0.0;
578 if ( aZoneInterpolator != NULL )
580 aZoneInterpolator->SetAltitudeObject( anObjAltitude );
581 aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
584 aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
586 if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
589 if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
591 aResAltitude = aPointAltitude;
594 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
596 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
597 aResAltitude > aPointAltitude )
599 aResAltitude = aPointAltitude;
602 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
604 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
605 aResAltitude < aPointAltitude )
607 aResAltitude = aPointAltitude;
616 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
617 const NCollection_Sequence<gp_XY>& thePoints,
618 const Handle(HYDROData_Zone)& theZone ) const
620 NCollection_Sequence<double> aResSeq;
622 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
624 const gp_XY& thePnt = thePoints.Value( i );
626 double anAltitude = GetAltitudeForPoint( thePnt, theZone );
627 aResSeq.Append( anAltitude );
633 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
635 Handle(HYDROData_Zone) aResZone;
637 HYDROData_SequenceOfObjects aRegions = GetRegions();
639 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
640 for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
642 Handle(HYDROData_Region) aRegion =
643 Handle(HYDROData_Region)::DownCast( anIter.Value() );
644 if ( aRegion.IsNull() )
647 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
648 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
649 for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
651 Handle(HYDROData_Zone) aRegZone =
652 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
653 if ( aRegZone.IsNull() )
656 PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
657 if ( aPointRelation != POINT_OUT )
658 aResZone = aRegZone; // We found the desired zone
665 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
666 const gp_XY& thePoint,
667 const Handle(HYDROData_Zone)& theZone ) const
669 PointClassification aRes = POINT_OUT;
670 if ( theZone.IsNull() )
673 TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
674 if ( aZoneFace.IsNull() )
677 TopoDS_Compound aCmp;
679 aBB.MakeCompound(aCmp);
680 aBB.Add(aCmp, aZoneFace);
681 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
682 BRepBuilderAPI_MakeVertex aMk(aPnt);
683 aBB.Add(aCmp, aMk.Vertex());
684 BRepTools::Write(aCmp, "FCL2d.brep");
686 TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
687 if (State == TopAbs_OUT)
689 else if(State == TopAbs_IN)
691 else if(State == TopAbs_ON)
696 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion()
698 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
700 Handle(HYDROData_Region) aNewRegion =
701 Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
702 AddRegion( aNewRegion );
707 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup()
709 TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
711 Handle(HYDROData_SplittedShapesGroup) aNewGroup =
712 Handle(HYDROData_SplittedShapesGroup)::DownCast(
713 HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
714 AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
719 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
720 SALOMEDS::Study_ptr theStudy ) const
722 HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
724 // Get groups definitions
725 HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
727 HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
728 for ( ; anIter.More(); anIter.Next() )
731 Handle(HYDROData_ShapesGroup) aGroup =
732 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
733 if ( aGroup.IsNull() )
736 HYDROData_ShapesGroup::GroupDefinition aGroupDef;
738 aGroupDef.Name = aGroup->GetName().toLatin1().constData();
739 aGroup->GetShapes( aGroupDef.Shapes );
741 aSeqOfGroupsDefs.Append( aGroupDef );
745 TopTools_ListOfShape aFaces;
746 HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
747 HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
748 for ( ; aRegionIter.More(); aRegionIter.Next() )
750 Handle(HYDROData_Region) aRegion =
751 Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
752 if( aRegion.IsNull() )
755 TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
756 aFaces.Append( aRegionShape );
759 return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs );
762 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
763 SALOMEDS::Study_ptr theStudy,
764 const TopTools_ListOfShape& theFaces,
765 const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs ) const
768 BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
769 aSewing.SetNonManifoldMode( Standard_False );
770 #ifdef DEB_CALCULATION
771 TCollection_AsciiString aNam("Sh_");
774 TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
775 for ( ; aFaceIter.More(); aFaceIter.Next() )
777 TopoDS_Shape aShape = aFaceIter.Value();
778 if ( aShape.IsNull() )
781 if ( aShape.ShapeType() == TopAbs_FACE )
783 aSewing.Add( aShape );
784 #ifdef DEB_CALCULATION
785 TCollection_AsciiString aName = aNam + ++i + ".brep";
786 BRepTools::Write(aShape ,aName.ToCString());
791 #ifdef DEB_CALCULATION
794 TopExp_Explorer anExp( aShape, TopAbs_FACE );
795 for (; anExp.More(); anExp.Next() ) {
796 aSewing.Add( anExp.Current() );
797 #ifdef DEB_CALCULATION
799 TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
800 BRepTools::Write(anExp.Current() ,aName.ToCString());
807 TopoDS_Shape aSewedShape = aSewing.SewedShape();
809 // If the sewed shape is empty - return false
810 if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
813 #ifdef DEB_CALCULATION
814 BRepTools::Write(aSewedShape ,"Sew.brep");
816 // Publish the sewed shape
817 QString aName = EXPORT_NAME;
818 GEOM::GEOM_Object_ptr aMainShape =
819 publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName );
821 if ( aMainShape->_is_nil() )
824 if ( theGroupsDefs.IsEmpty() )
828 TopTools_IndexedMapOfShape aMapOfSubShapes;
829 TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
831 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
833 for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
835 const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
837 NCollection_Sequence<int> aGroupIndexes;
838 for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
840 const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
841 #ifdef DEB_CALCULATION
842 cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
845 TopoDS_Shape aModifiedShape = aShape;
846 if ( aSewing.IsModified( aShape ) )
847 aModifiedShape = aSewing.Modified( aShape );
848 else if ( aSewing.IsModifiedSubShape( aShape ) )
849 aModifiedShape = aSewing.ModifiedSubShape( aShape );
851 #ifdef DEB_CALCULATION
852 const TopLoc_Location& aL1 = aShape.Location();
853 const TopLoc_Location& aL2 = aModifiedShape.Location();
854 cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
857 int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
859 aGroupIndexes.Append( anIndex );
861 #ifdef DEB_CALCULATION
862 TCollection_AsciiString aNam("Lost_");
863 if(!aMapOfSubShapes.Contains(aModifiedShape)) {
864 for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
866 const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
867 if ( aModifiedShape.IsPartner( aS ) )
869 cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
870 TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
871 BRepTools::Write(aS ,aName.ToCString());
879 if ( !aGroupIndexes.IsEmpty() )
880 aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
883 if ( !aGroupsData.IsEmpty() )
885 GEOM::GEOM_IGroupOperations_var aGroupOp =
886 theGeomEngine->GetIGroupOperations( theStudy->StudyId() );
888 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
889 for ( ; aMapIt.More(); aMapIt.Next() )
891 const TCollection_AsciiString& aGroupName = aMapIt.Key();
892 const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
894 GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
895 if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
898 GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
899 aGeomIndexes->length( aGroupIndexes.Length() );
901 for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
902 aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
904 aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
905 if ( aGroupOp->IsDone() )
907 SALOMEDS::SObject_var aGroupSO =
908 theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
916 GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM(
917 GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
918 const TopoDS_Shape& theShape, const QString& theName ) const
920 GEOM::GEOM_Object_var aGeomObj;
922 if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
923 theShape.IsNull() ) {
924 return aGeomObj._retn();
927 std::ostringstream aStreamShape;
928 // Write TopoDS_Shape in ASCII format to the stream
929 BRepTools::Write( theShape, aStreamShape );
930 // Returns the number of bytes that have been stored in the stream's buffer.
931 int aSize = aStreamShape.str().size();
932 // Allocate octect buffer of required size
933 CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
934 // Copy ostrstream content to the octect buffer
935 memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
937 SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
939 // Restore shape from the stream and get the GEOM object
940 GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
941 aGeomObj = anInsOp->RestoreShape( aSeqFile );
943 // Puplish the GEOM object
944 if ( !aGeomObj->_is_nil() ) {
945 QString aName = GEOMBase::GetDefaultName( theName );
947 SALOMEDS::SObject_var aResultSO =
948 theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(),
949 aGeomObj, qPrintable( aName ) );
950 if ( aResultSO->_is_nil() ) {
951 aGeomObj = GEOM::GEOM_Object::_nil();
955 return aGeomObj._retn();