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