Salome HOME
Import of profiles corrected.
[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 <QFile>
10 #include <QStringList>
11 #include <QTextStream>
12
13 void HYDROData_Tool::WriteStringsToFile( QFile&             theFile,
14                                          const QStringList& theStrings,
15                                          const QString&     theSep )
16 {
17   if ( !theFile.isOpen() || theStrings.isEmpty() )
18     return;
19   
20   QString aWriteStr = theStrings.join( theSep );
21   if ( aWriteStr.isEmpty() )
22     return;
23
24   QTextStream anOutStream( &theFile );
25   anOutStream << aWriteStr << theSep << theSep;
26 }
27
28 void HYDROData_Tool::SetMustBeUpdatedImages(
29   const Handle(HYDROData_Document)& theDoc  )
30 {
31   bool anIsChanged = true;
32
33   // iterate until there is no changes because images on all level of dependency must be updated
34   while ( anIsChanged )
35   {
36     anIsChanged = false;
37
38     HYDROData_Iterator anIter( theDoc, KIND_IMAGE );
39     for ( ; anIter.More(); anIter.Next() )
40     {
41       Handle(HYDROData_Image) anImage = 
42         Handle(HYDROData_Image)::DownCast( anIter.Current() );
43       if ( anImage.IsNull() || anImage->IsMustBeUpdated() )
44         continue;
45
46       Handle(HYDROData_Image) aTrsfRefImage = anImage->GetTrsfReferenceImage();
47       if ( !aTrsfRefImage.IsNull() && aTrsfRefImage->IsMustBeUpdated() )
48       {
49         anImage->SetToUpdate( true );
50         anIsChanged = true;
51         continue;
52       }
53
54       for ( int i = 0, aNBRefs = anImage->NbReferences(); i < aNBRefs; ++i )
55       {
56         Handle(HYDROData_Image) aRefImage =
57           Handle(HYDROData_Image)::DownCast( anImage->Reference( i ) );
58         if ( !aRefImage.IsNull() && aRefImage->IsMustBeUpdated() )
59         {
60            // image references to updated => also must be updated
61            anImage->SetToUpdate( true );
62            anIsChanged = true;
63         }
64       }
65     }
66   }
67 }
68
69 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
70                                             const QString&                    thePrefix,
71                                             const QStringList&                theUsedNames,
72                                             const bool                        theIsTryToUsePurePrefix )
73 {
74   QStringList aNamesList( theUsedNames );
75
76   // Collect all used names in the document
77   HYDROData_Iterator anIter( theDoc );
78   for( ; anIter.More(); anIter.Next() )
79   {
80     Handle(HYDROData_Entity) anObject = anIter.Current();
81     if( anObject.IsNull() )
82       continue;
83
84     QString anObjName = anObject->GetName();
85     if ( anObjName.isEmpty() )
86       continue;
87
88     aNamesList.append( anObjName );
89   }
90
91   QString aName;
92
93   if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
94     aName = thePrefix;
95   } else {
96     int anId = 1;
97     while( anId < 1000 )
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 }