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