2 #include "HYDROData_Tool.h"
4 #include "HYDROData_ArtificialObject.h"
5 #include "HYDROData_Image.h"
6 #include "HYDROData_Iterator.h"
7 #include "HYDROData_NaturalObject.h"
9 #include <TopoDS_Shape.hxx>
11 #include <TopTools_SequenceOfShape.hxx>
13 #include <TopExp_Explorer.hxx>
16 #include <QStringList>
17 #include <QTextStream>
21 static int aMaxNameId = std::numeric_limits<int>::max();
23 void HYDROData_Tool::WriteStringsToFile( QFile& theFile,
24 const QStringList& theStrings,
25 const QString& theSep )
27 if ( !theFile.isOpen() || theStrings.isEmpty() )
30 QString aWriteStr = theStrings.join( theSep );
31 if ( aWriteStr.isEmpty() )
34 QTextStream anOutStream( &theFile );
35 anOutStream << aWriteStr << theSep << theSep;
38 void HYDROData_Tool::SetMustBeUpdatedObjects(
39 const Handle(HYDROData_Document)& theDoc )
41 bool anIsChanged = true;
43 // iterate until there is no changes because objects on all level of dependency must be updated
48 HYDROData_Iterator anIter( theDoc );
49 for ( ; anIter.More(); anIter.Next() )
51 Handle(HYDROData_Entity) anObject = anIter.Current();
52 if ( anObject.IsNull() || anObject->IsMustBeUpdated() )
55 HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
56 for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
58 Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
59 if ( aRefObject.IsNull() || !aRefObject->IsMustBeUpdated() )
62 anObject->SetToUpdate( true );
70 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
71 const QString& thePrefix,
72 const QStringList& theUsedNames,
73 const bool theIsTryToUsePurePrefix )
75 QStringList aNamesList( theUsedNames );
77 // Collect all used names in the document
78 HYDROData_Iterator anIter( theDoc );
79 for( ; anIter.More(); anIter.Next() )
81 Handle(HYDROData_Entity) anObject = anIter.Current();
82 if( anObject.IsNull() )
85 QString anObjName = anObject->GetName();
86 if ( anObjName.isEmpty() )
89 aNamesList.append( anObjName );
94 if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
98 while( anId < aMaxNameId )
100 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
102 // check that there are no other objects with the same name in the document
103 if ( !aNamesList.contains( aName ) )
111 Handle(HYDROData_Entity) HYDROData_Tool::FindObjectByName( const Handle(HYDROData_Document)& theDoc,
112 const QString& theName,
113 const ObjectKind theObjectKind )
115 Handle(HYDROData_Entity) anObject;
116 if ( theName.isEmpty() || theDoc.IsNull() )
119 QStringList aNamesList;
120 aNamesList << theName;
122 HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( theDoc, aNamesList, theObjectKind );
123 if( aSeqOfObjs.IsEmpty() )
126 anObject = aSeqOfObjs.First();
130 HYDROData_SequenceOfObjects HYDROData_Tool::FindObjectsByNames( const Handle(HYDROData_Document)& theDoc,
131 const QStringList& theNames,
132 const ObjectKind theObjectKind )
134 HYDROData_SequenceOfObjects aResSeq;
135 if( theDoc.IsNull() )
138 QStringList aNamesList = theNames;
140 HYDROData_Iterator anIter( theDoc, theObjectKind );
141 for( ; anIter.More(); anIter.Next() )
143 Handle(HYDROData_Entity) anObject = anIter.Current();
144 if( anObject.IsNull() )
147 QString anObjName = anObject->GetName();
148 if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
151 aResSeq.Append( anObject );
153 aNamesList.removeAll( anObjName );
154 if ( aNamesList.isEmpty() )
161 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
163 if ( theObject.IsNull() )
166 return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
167 theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
170 void HYDROData_Tool::UpdateChildObjectName( const QString& theOldStr,
171 const QString& theNewStr,
172 const Handle(HYDROData_Entity)& theObject )
174 if ( theObject.IsNull() )
177 QString anObjName = theObject->GetName();
178 if ( theOldStr.isEmpty() )
180 while ( anObjName.startsWith( '_' ) )
181 anObjName.remove( 0, 1 );
183 anObjName.prepend( theNewStr + "_" );
185 else if ( anObjName.startsWith( theOldStr ) )
187 anObjName.replace( 0, theOldStr.length(), theNewStr );
192 theObject->SetName( anObjName );
195 QString HYDROData_Tool::GenerateNameForPython( const MapOfTreatedObjects& theTreatedObjects,
196 const QString& thePrefix )
198 QString aName = thePrefix;
199 if ( !theTreatedObjects.contains( aName ) )
203 while( anId < aMaxNameId )
205 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
207 // check that there are no other objects with the same name
208 if ( !theTreatedObjects.contains( aName ) )