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>
19 void HYDROData_Tool::WriteStringsToFile( QFile& theFile,
20 const QStringList& theStrings,
21 const QString& theSep )
23 if ( !theFile.isOpen() || theStrings.isEmpty() )
26 QString aWriteStr = theStrings.join( theSep );
27 if ( aWriteStr.isEmpty() )
30 QTextStream anOutStream( &theFile );
31 anOutStream << aWriteStr << theSep << theSep;
34 void HYDROData_Tool::SetMustBeUpdatedObjects(
35 const Handle(HYDROData_Document)& theDoc )
37 bool anIsChanged = true;
39 // iterate until there is no changes because objects on all level of dependency must be updated
44 HYDROData_Iterator anIter( theDoc );
45 for ( ; anIter.More(); anIter.Next() )
47 Handle(HYDROData_Entity) anObject = anIter.Current();
48 if ( anObject.IsNull() || anObject->IsMustBeUpdated() )
51 HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
52 for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
54 Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
55 if ( aRefObject.IsNull() || !aRefObject->IsMustBeUpdated() )
58 anObject->SetToUpdate( true );
66 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
67 const QString& thePrefix,
68 const QStringList& theUsedNames,
69 const bool theIsTryToUsePurePrefix )
71 QStringList aNamesList( theUsedNames );
73 // Collect all used names in the document
74 HYDROData_Iterator anIter( theDoc );
75 for( ; anIter.More(); anIter.Next() )
77 Handle(HYDROData_Entity) anObject = anIter.Current();
78 if( anObject.IsNull() )
81 QString anObjName = anObject->GetName();
82 if ( anObjName.isEmpty() )
85 aNamesList.append( anObjName );
90 if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
94 int aMaxId = std::numeric_limits<int>::max();
95 while( anId < aMaxId )
97 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
99 // check that there are no other objects with the same name in the document
100 if ( !aNamesList.contains( aName ) )
108 Handle(HYDROData_Entity) HYDROData_Tool::FindObjectByName( const Handle(HYDROData_Document)& theDoc,
109 const QString& theName,
110 const ObjectKind theObjectKind )
112 Handle(HYDROData_Entity) anObject;
113 if ( theName.isEmpty() || theDoc.IsNull() )
116 QStringList aNamesList;
117 aNamesList << theName;
119 HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( theDoc, aNamesList, theObjectKind );
120 if( aSeqOfObjs.IsEmpty() )
123 anObject = aSeqOfObjs.First();
127 HYDROData_SequenceOfObjects HYDROData_Tool::FindObjectsByNames( const Handle(HYDROData_Document)& theDoc,
128 const QStringList& theNames,
129 const ObjectKind theObjectKind )
131 HYDROData_SequenceOfObjects aResSeq;
132 if( theDoc.IsNull() )
135 QStringList aNamesList = theNames;
137 HYDROData_Iterator anIter( theDoc, theObjectKind );
138 for( ; anIter.More(); anIter.Next() )
140 Handle(HYDROData_Entity) anObject = anIter.Current();
141 if( anObject.IsNull() )
144 QString anObjName = anObject->GetName();
145 if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
148 aResSeq.Append( anObject );
150 aNamesList.removeAll( anObjName );
151 if ( aNamesList.isEmpty() )
158 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
160 if ( theObject.IsNull() )
163 return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
164 theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
167 void HYDROData_Tool::UpdateChildObjectName( const QString& theOldStr,
168 const QString& theNewStr,
169 const Handle(HYDROData_Entity)& theObject )
171 if ( theObject.IsNull() )
174 QString anObjName = theObject->GetName();
175 if ( theOldStr.isEmpty() )
177 while ( anObjName.startsWith( '_' ) )
178 anObjName.remove( 0, 1 );
180 anObjName.prepend( theNewStr + "_" );
182 else if ( anObjName.startsWith( theOldStr ) )
184 anObjName.replace( 0, theOldStr.length(), theNewStr );
189 theObject->SetName( anObjName );