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 void HYDROData_Tool::WriteStringsToFile( QFile& theFile,
22 const QStringList& theStrings,
23 const QString& theSep )
25 if ( !theFile.isOpen() || theStrings.isEmpty() )
28 QString aWriteStr = theStrings.join( theSep );
29 if ( aWriteStr.isEmpty() )
32 QTextStream anOutStream( &theFile );
33 anOutStream << aWriteStr << theSep << theSep;
36 void HYDROData_Tool::SetMustBeUpdatedObjects(
37 const Handle(HYDROData_Document)& theDoc )
39 bool anIsChanged = true;
41 // iterate until there is no changes because objects on all level of dependency must be updated
46 HYDROData_Iterator anIter( theDoc );
47 for ( ; anIter.More(); anIter.Next() )
49 Handle(HYDROData_Entity) anObject = anIter.Current();
50 if ( anObject.IsNull() || anObject->IsMustBeUpdated() )
53 HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
54 for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
56 Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
57 if ( aRefObject.IsNull() || !aRefObject->IsMustBeUpdated() )
60 anObject->SetToUpdate( true );
68 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
69 const QString& thePrefix,
70 const QStringList& theUsedNames,
71 const bool theIsTryToUsePurePrefix )
73 QStringList aNamesList( theUsedNames );
75 // Collect all used names in the document
76 HYDROData_Iterator anIter( theDoc );
77 for( ; anIter.More(); anIter.Next() )
79 Handle(HYDROData_Entity) anObject = anIter.Current();
80 if( anObject.IsNull() )
83 QString anObjName = anObject->GetName();
84 if ( anObjName.isEmpty() )
87 aNamesList.append( anObjName );
92 if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
96 int aMaxId = std::numeric_limits<int>::max();
97 while( anId < aMaxId )
99 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
101 // check that there are no other objects with the same name in the document
102 if ( !aNamesList.contains( aName ) )
110 Handle(HYDROData_Entity) HYDROData_Tool::FindObjectByName( const Handle(HYDROData_Document)& theDoc,
111 const QString& theName,
112 const ObjectKind theObjectKind )
114 Handle(HYDROData_Entity) anObject;
115 if ( theName.isEmpty() || theDoc.IsNull() )
118 QStringList aNamesList;
119 aNamesList << theName;
121 HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( theDoc, aNamesList, theObjectKind );
122 if( aSeqOfObjs.IsEmpty() )
125 anObject = aSeqOfObjs.First();
129 HYDROData_SequenceOfObjects HYDROData_Tool::FindObjectsByNames( const Handle(HYDROData_Document)& theDoc,
130 const QStringList& theNames,
131 const ObjectKind theObjectKind )
133 HYDROData_SequenceOfObjects aResSeq;
134 if( theDoc.IsNull() )
137 QStringList aNamesList = theNames;
139 HYDROData_Iterator anIter( theDoc, theObjectKind );
140 for( ; anIter.More(); anIter.Next() )
142 Handle(HYDROData_Entity) anObject = anIter.Current();
143 if( anObject.IsNull() )
146 QString anObjName = anObject->GetName();
147 if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
150 aResSeq.Append( anObject );
152 aNamesList.removeAll( anObjName );
153 if ( aNamesList.isEmpty() )
160 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
162 if ( theObject.IsNull() )
165 return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
166 theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
169 void HYDROData_Tool::UpdateChildObjectName( const QString& theOldStr,
170 const QString& theNewStr,
171 const Handle(HYDROData_Entity)& theObject )
173 if ( theObject.IsNull() )
176 QString anObjName = theObject->GetName();
177 if ( theOldStr.isEmpty() )
179 while ( anObjName.startsWith( '_' ) )
180 anObjName.remove( 0, 1 );
182 anObjName.prepend( theNewStr + "_" );
184 else if ( anObjName.startsWith( theOldStr ) )
186 anObjName.replace( 0, theOldStr.length(), theNewStr );
191 theObject->SetName( anObjName );