Salome HOME
072b0d2290baa22a03bf24b3bb9c701281c0d07c
[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 <TopoDS_Shape.hxx>
10
11 #include <TopTools_SequenceOfShape.hxx>
12
13 #include <TopExp_Explorer.hxx>
14
15 #include <QFile>
16 #include <QStringList>
17 #include <QTextStream>
18
19 #include <limits>
20
21 static int aMaxNameId = std::numeric_limits<int>::max();
22
23 void HYDROData_Tool::WriteStringsToFile( QFile&             theFile,
24                                          const QStringList& theStrings,
25                                          const QString&     theSep )
26 {
27   if ( !theFile.isOpen() || theStrings.isEmpty() )
28     return;
29   
30   QString aWriteStr = theStrings.join( theSep );
31   if ( aWriteStr.isEmpty() )
32     return;
33
34   QTextStream anOutStream( &theFile );
35   anOutStream << aWriteStr << theSep << theSep;
36 }
37
38 void HYDROData_Tool::SetMustBeUpdatedObjects(
39   const Handle(HYDROData_Document)& theDoc  )
40 {
41   bool anIsChanged = true;
42
43   // iterate until there is no changes because objects on all level of dependency must be updated
44   while ( anIsChanged )
45   {
46     anIsChanged = false;
47
48     HYDROData_Iterator anIter( theDoc );
49     for ( ; anIter.More(); anIter.Next() )
50     {
51       Handle(HYDROData_Entity) anObject = anIter.Current();
52       if ( anObject.IsNull() || anObject->IsMustBeUpdated() )
53         continue;
54
55       HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
56       for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
57       {
58         Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
59         if ( aRefObject.IsNull() || !aRefObject->IsMustBeUpdated() )
60           continue;
61
62         anObject->SetToUpdate( true );
63         anIsChanged = true;
64         break;
65       }
66     }
67   }
68 }
69
70 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
71                                             const QString&                    thePrefix,
72                                             const QStringList&                theUsedNames,
73                                             const bool                        theIsTryToUsePurePrefix )
74 {
75   QStringList aNamesList( theUsedNames );
76
77   // Collect all used names in the document
78   HYDROData_Iterator anIter( theDoc );
79   for( ; anIter.More(); anIter.Next() )
80   {
81     Handle(HYDROData_Entity) anObject = anIter.Current();
82     if( anObject.IsNull() )
83       continue;
84
85     QString anObjName = anObject->GetName();
86     if ( anObjName.isEmpty() )
87       continue;
88
89     aNamesList.append( anObjName );
90   }
91
92   QString aName;
93
94   if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
95     aName = thePrefix;
96   } else {
97     int anId = 1;
98     while( anId < aMaxNameId )
99     {
100       aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
101
102       // check that there are no other objects with the same name in the document
103       if ( !aNamesList.contains( aName ) )
104         break;
105     }
106   }
107
108   return aName;
109 }
110
111 Handle(HYDROData_Entity) HYDROData_Tool::FindObjectByName( const Handle(HYDROData_Document)& theDoc,
112                                                            const QString&                    theName,
113                                                            const ObjectKind                  theObjectKind )
114 {
115   Handle(HYDROData_Entity) anObject;
116   if ( theName.isEmpty() || theDoc.IsNull() )
117     return anObject;
118
119   QStringList aNamesList;
120   aNamesList << theName;
121
122   HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( theDoc, aNamesList, theObjectKind );
123   if( aSeqOfObjs.IsEmpty() )
124     return anObject;
125   
126   anObject = aSeqOfObjs.First();
127   return anObject;
128 }
129
130 HYDROData_SequenceOfObjects HYDROData_Tool::FindObjectsByNames( const Handle(HYDROData_Document)& theDoc,
131                                                                 const QStringList&                theNames,
132                                                                 const ObjectKind                  theObjectKind )
133 {
134   HYDROData_SequenceOfObjects aResSeq;
135   if( theDoc.IsNull() )
136     return aResSeq;
137
138   QStringList aNamesList = theNames;
139
140   HYDROData_Iterator anIter( theDoc, theObjectKind );
141   for( ; anIter.More(); anIter.Next() )
142   {
143     Handle(HYDROData_Entity) anObject = anIter.Current();
144     if( anObject.IsNull() )
145       continue;
146
147     QString anObjName = anObject->GetName();
148     if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
149       continue;
150
151     aResSeq.Append( anObject );
152
153     aNamesList.removeAll( anObjName );
154     if ( aNamesList.isEmpty() )
155       break;
156   }
157
158   return aResSeq;
159 }
160
161 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
162 {
163   if ( theObject.IsNull() )
164     return false;
165   
166   return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
167          theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
168 }
169
170 void HYDROData_Tool::UpdateChildObjectName( const QString&                  theOldStr,
171                                             const QString&                  theNewStr,
172                                             const Handle(HYDROData_Entity)& theObject )
173 {
174   if ( theObject.IsNull() )
175     return;
176
177   QString anObjName = theObject->GetName();
178   if ( theOldStr.isEmpty() )
179   {
180     while ( anObjName.startsWith( '_' ) )
181       anObjName.remove( 0, 1 );
182
183     anObjName.prepend( theNewStr + "_" );
184   }
185   else if ( anObjName.startsWith( theOldStr ) )
186   {
187     anObjName.replace( 0, theOldStr.length(), theNewStr );
188   }
189   else
190     return;
191
192   theObject->SetName( anObjName );
193 }
194
195 QString HYDROData_Tool::GenerateNameForPython( const MapOfTreatedObjects& theTreatedObjects,
196                                                const QString&             thePrefix )
197 {
198   QString aName = thePrefix;
199   if ( !theTreatedObjects.contains( aName ) )
200     return aName;
201
202   int anId = 1;
203   while( anId < aMaxNameId )
204   {
205     aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
206
207     // check that there are no other objects with the same name
208     if ( !theTreatedObjects.contains( aName ) )
209       break;
210   }
211
212   return aName;
213 }