Salome HOME
b058b8f31a0d59fbbb774232662c0c4061fafdbd
[modules/hydro.git] / src / HYDROData / HYDROData_Tool.cxx
1
2 #include "HYDROData_Tool.h"
3
4 #include "HYDROData_ArtificialObject.h"
5 #include "HYDROData_Image.h"
6 #include "HYDROData_Iterator.h"
7 #include "HYDROData_NaturalObject.h"
8
9 #include <TopTools_SequenceOfShape.hxx>
10 #include <TopExp_Explorer.hxx>
11
12 #include <QFile>
13 #include <QStringList>
14 #include <QTextStream>
15
16 #include <limits>
17 #include <gp_Pnt.hxx>
18 #include <gp_Vec.hxx>
19 #include <gp_Pln.hxx>
20 #include <TopExp.hxx>
21 #include <Geom_Plane.hxx>
22 #include <BRep_Tool.hxx>
23 #include <BRepBuilderAPI_FindPlane.hxx>
24 #include <TopoDS.hxx>
25 #include <TopoDS_Vertex.hxx>
26 #include <TopoDS_Wire.hxx>
27 #include <TopoDS_Face.hxx>
28 #include <TopoDS_Iterator.hxx>
29 #include <TopTools_MapOfShape.hxx>
30 #include <TColgp_SequenceOfVec.hxx>
31 #include <TopTools_ShapeMapHasher.hxx>
32 #include <ElSLib.hxx>
33 #include <BRepAdaptor_Surface.hxx>
34 #include <BRepTopAdaptor_FClass2d.hxx>
35 #include <BRep_Tool.hxx>
36 #include <TopAbs_State.hxx>
37 static int aMaxNameId = std::numeric_limits<int>::max();
38
39 void HYDROData_Tool::WriteStringsToFile( QFile&             theFile,
40                                          const QStringList& theStrings,
41                                          const QString&     theSep )
42 {
43   if ( !theFile.isOpen() || theStrings.isEmpty() )
44     return;
45   
46   QString aWriteStr = theStrings.join( theSep );
47   if ( aWriteStr.isEmpty() )
48     return;
49
50   QTextStream anOutStream( &theFile );
51   anOutStream << aWriteStr << theSep << theSep;
52 }
53
54 void HYDROData_Tool::SetMustBeUpdatedObjects(
55   const Handle(HYDROData_Document)& theDoc  )
56 {
57   bool anIsChanged = true;
58
59   // iterate until there is no changes because objects on all level of dependency must be updated
60   while ( anIsChanged )
61   {
62     anIsChanged = false;
63
64     HYDROData_Iterator anIter( theDoc );
65     for ( ; anIter.More(); anIter.Next() )
66     {
67       Handle(HYDROData_Entity) anObject = anIter.Current();
68       if ( anObject.IsNull() || anObject->IsMustBeUpdated() )
69         continue;
70
71       HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
72       for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
73       {
74         Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
75         if ( aRefObject.IsNull() || !aRefObject->IsMustBeUpdated() )
76           continue;
77
78         anObject->SetToUpdate( true );
79         anIsChanged = true;
80         break;
81       }
82     }
83   }
84 }
85
86 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
87                                             const QString&                    thePrefix,
88                                             const QStringList&                theUsedNames,
89                                             const bool                        theIsTryToUsePurePrefix )
90 {
91   QStringList aNamesList( theUsedNames );
92
93   // Collect all used names in the document
94   HYDROData_Iterator anIter( theDoc );
95   for( ; anIter.More(); anIter.Next() )
96   {
97     Handle(HYDROData_Entity) anObject = anIter.Current();
98     if( anObject.IsNull() )
99       continue;
100
101     QString anObjName = anObject->GetName();
102     if ( anObjName.isEmpty() )
103       continue;
104
105     aNamesList.append( anObjName );
106   }
107
108   QString aName;
109
110   if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
111     aName = thePrefix;
112   } else {
113     int anId = 1;
114     while( anId < aMaxNameId )
115     {
116       aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
117
118       // check that there are no other objects with the same name in the document
119       if ( !aNamesList.contains( aName ) )
120         break;
121     }
122   }
123
124   return aName;
125 }
126
127 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
128 {
129   if ( theObject.IsNull() )
130     return false;
131   
132   return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
133          theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
134 }
135
136 void HYDROData_Tool::UpdateChildObjectName( const QString&                  theOldStr,
137                                             const QString&                  theNewStr,
138                                             const Handle(HYDROData_Entity)& theObject )
139 {
140   if ( theObject.IsNull() )
141     return;
142
143   QString anObjName = theObject->GetName();
144   if ( theOldStr.isEmpty() )
145   {
146     while ( anObjName.startsWith( '_' ) )
147       anObjName.remove( 0, 1 );
148
149     anObjName.prepend( theNewStr + "_" );
150   }
151   else if ( anObjName.startsWith( theOldStr ) )
152   {
153     anObjName.replace( 0, theOldStr.length(), theNewStr );
154   }
155   else
156     return;
157
158   theObject->SetName( anObjName );
159 }
160
161 QString HYDROData_Tool::GenerateNameForPython( const MapOfTreatedObjects& theTreatedObjects,
162                                                const QString&             thePrefix )
163 {
164   QString aName = thePrefix;
165   if ( !theTreatedObjects.contains( aName ) )
166     return aName;
167
168   int anId = 1;
169   while( anId < aMaxNameId )
170   {
171     aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
172
173     // check that there are no other objects with the same name
174     if ( !theTreatedObjects.contains( aName ) )
175       break;
176   }
177
178   return aName;
179 }
180 //======================================================================================================
181 TopAbs_State HYDROData_Tool::ComputePointState( const gp_XY& theXY, const TopoDS_Face& theFace )
182 {
183   TopAbs_State aState(TopAbs_UNKNOWN);
184   if(theFace.IsNull()) return aState;  
185   Standard_Real aTol = BRep_Tool::Tolerance(theFace);
186   BRepAdaptor_Surface Ads ( theFace, Standard_False );
187   Standard_Real toluv = Min ( Ads.UResolution(aTol), Ads.VResolution(aTol) ); 
188   const gp_Pln& aPlane = Ads.Surface().Plane();
189   gp_Pnt aPnt(theXY.X(), theXY.Y(), 0.);
190   Standard_Real aU1, aV1;
191   ElSLib::Parameters(aPlane,aPnt, aU1, aV1);
192   BRepTopAdaptor_FClass2d aClassifier( theFace, toluv ); 
193   aState = aClassifier.Perform( gp_Pnt2d(aU1, aV1), Standard_False );
194   return aState;
195 }