1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_Tool.h"
21 #include "HYDROData_ArtificialObject.h"
22 #include "HYDROData_Image.h"
23 #include "HYDROData_Iterator.h"
24 #include "HYDROData_NaturalObject.h"
27 #include <QStringList>
28 #include <QTextStream>
34 #include <TopAbs_State.hxx>
35 #include <BRepAdaptor_Surface.hxx>
36 #include <BRepTopAdaptor_FClass2d.hxx>
37 #include <BRep_Tool.hxx>
39 static int aMaxNameId = std::numeric_limits<int>::max();
41 void HYDROData_Tool::WriteStringsToFile( QFile& theFile,
42 const QStringList& theStrings,
43 const QString& theSep )
45 if ( !theFile.isOpen() || theStrings.isEmpty() )
48 QString aWriteStr = theStrings.join( theSep );
49 if ( aWriteStr.isEmpty() )
52 QTextStream anOutStream( &theFile );
53 anOutStream << aWriteStr << theSep << theSep;
56 void HYDROData_Tool::SetMustBeUpdatedObjects(
57 const Handle(HYDROData_Document)& theDoc )
59 bool anIsChanged = true;
61 // iterate until there is no changes because objects on all level of dependency must be updated
66 HYDROData_Iterator anIter( theDoc );
67 for ( ; anIter.More(); anIter.Next() )
69 Handle(HYDROData_Entity) anObject = anIter.Current();
70 if ( anObject.IsNull() || anObject->IsMustBeUpdated() )
73 HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
74 for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
76 Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
77 if ( aRefObject.IsNull() || !aRefObject->IsMustBeUpdated() )
80 anObject->SetToUpdate( true );
88 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
89 const QString& thePrefix,
90 const QStringList& theUsedNames,
91 const bool theIsTryToUsePurePrefix )
93 QStringList aNamesList( theUsedNames );
95 // Collect all used names in the document
96 HYDROData_Iterator anIter( theDoc );
97 for( ; anIter.More(); anIter.Next() )
99 Handle(HYDROData_Entity) anObject = anIter.Current();
100 if( anObject.IsNull() )
103 QString anObjName = anObject->GetName();
104 if ( anObjName.isEmpty() )
107 aNamesList.append( anObjName );
112 if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
116 while( anId < aMaxNameId )
118 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
120 // check that there are no other objects with the same name in the document
121 if ( !aNamesList.contains( aName ) )
129 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
131 if ( theObject.IsNull() )
134 return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
135 theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
138 void HYDROData_Tool::UpdateChildObjectName( const QString& theOldStr,
139 const QString& theNewStr,
140 const Handle(HYDROData_Entity)& theObject )
142 if ( theObject.IsNull() )
145 QString anObjName = theObject->GetName();
146 if ( theOldStr.isEmpty() )
148 while ( anObjName.startsWith( '_' ) )
149 anObjName.remove( 0, 1 );
151 anObjName.prepend( theNewStr + "_" );
153 else if ( anObjName.startsWith( theOldStr ) )
155 anObjName.replace( 0, theOldStr.length(), theNewStr );
160 theObject->SetName( anObjName );
163 QString HYDROData_Tool::GenerateNameForPython( const MapOfTreatedObjects& theTreatedObjects,
164 const QString& thePrefix )
166 QString aName = thePrefix;
167 if ( !theTreatedObjects.contains( aName ) )
171 while( anId < aMaxNameId )
173 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
175 // check that there are no other objects with the same name
176 if ( !theTreatedObjects.contains( aName ) )
182 //======================================================================================================
183 TopAbs_State HYDROData_Tool::ComputePointState( const gp_XY& theXY, const TopoDS_Face& theFace )
185 TopAbs_State aState(TopAbs_UNKNOWN);
186 if(theFace.IsNull()) return aState;
187 Standard_Real aTol = BRep_Tool::Tolerance(theFace);
188 BRepAdaptor_Surface Ads ( theFace, Standard_False );
189 Standard_Real toluv = Min ( Ads.UResolution(aTol), Ads.VResolution(aTol) );
190 const gp_Pln& aPlane = Ads.Surface().Plane();
191 gp_Pnt aPnt(theXY.X(), theXY.Y(), 0.);
192 Standard_Real aU1, aV1;
193 ElSLib::Parameters(aPlane,aPnt, aU1, aV1);
194 BRepTopAdaptor_FClass2d aClassifier( theFace, toluv );
195 aState = aClassifier.Perform( gp_Pnt2d(aU1, aV1), Standard_False );