Salome HOME
Merge branch 'master' of ssh://git.salome-platform.org/modules/hydro
[modules/hydro.git] / src / HYDROData / HYDROData_Tool.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROData_Tool.h"
24
25 #include "HYDROData_ArtificialObject.h"
26 #include "HYDROData_Image.h"
27 #include "HYDROData_Iterator.h"
28 #include "HYDROData_NaturalObject.h"
29
30 #include <QFile>
31 #include <QStringList>
32 #include <QTextStream>
33
34 #include <limits>
35 #include <gp_Pnt.hxx>
36 #include <gp_Pln.hxx>
37 #include <ElSLib.hxx>
38 #include <TopAbs_State.hxx>
39 #include <BRepAdaptor_Surface.hxx>
40 #include <BRepTopAdaptor_FClass2d.hxx>
41 #include <BRep_Tool.hxx>
42
43 static int aMaxNameId = std::numeric_limits<int>::max();
44
45 void HYDROData_Tool::WriteStringsToFile( QFile&             theFile,
46                                          const QStringList& theStrings,
47                                          const QString&     theSep )
48 {
49   if ( !theFile.isOpen() || theStrings.isEmpty() )
50     return;
51   
52   QString aWriteStr = theStrings.join( theSep );
53   if ( aWriteStr.isEmpty() )
54     return;
55
56   QTextStream anOutStream( &theFile );
57   anOutStream << aWriteStr << theSep << theSep;
58 }
59
60 void HYDROData_Tool::SetMustBeUpdatedObjects(
61   const Handle(HYDROData_Document)& theDoc  )
62 {
63   bool anIsChanged = true;
64
65   // iterate until there is no changes because objects on all level of dependency must be updated
66   while ( anIsChanged )
67   {
68     anIsChanged = false;
69
70     HYDROData_Iterator anIter( theDoc );
71     for ( ; anIter.More(); anIter.Next() )
72     {
73       Handle(HYDROData_Entity) anObject = anIter.Current();
74       if ( anObject.IsNull() || anObject->IsMustBeUpdated() )
75         continue;
76
77       HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
78       for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
79       {
80         Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
81         if ( aRefObject.IsNull() || !aRefObject->IsMustBeUpdated() )
82           continue;
83
84         anObject->SetToUpdate( true );
85         anIsChanged = true;
86         break;
87       }
88     }
89   }
90 }
91
92 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
93                                             const QString&                    thePrefix,
94                                             const QStringList&                theUsedNames,
95                                             const bool                        theIsTryToUsePurePrefix )
96 {
97   QStringList aNamesList( theUsedNames );
98
99   // Collect all used names in the document
100   HYDROData_Iterator anIter( theDoc );
101   for( ; anIter.More(); anIter.Next() )
102   {
103     Handle(HYDROData_Entity) anObject = anIter.Current();
104     if( anObject.IsNull() )
105       continue;
106
107     QString anObjName = anObject->GetName();
108     if ( anObjName.isEmpty() )
109       continue;
110
111     aNamesList.append( anObjName );
112   }
113
114   QString aName;
115
116   if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
117     aName = thePrefix;
118   } else {
119     int anId = 1;
120     while( anId < aMaxNameId )
121     {
122       aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
123
124       // check that there are no other objects with the same name in the document
125       if ( !aNamesList.contains( aName ) )
126         break;
127     }
128   }
129
130   return aName;
131 }
132
133 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
134 {
135   if ( theObject.IsNull() )
136     return false;
137   
138   return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
139          theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
140 }
141
142 void HYDROData_Tool::UpdateChildObjectName( const QString&                  theOldStr,
143                                             const QString&                  theNewStr,
144                                             const Handle(HYDROData_Entity)& theObject )
145 {
146   if ( theObject.IsNull() )
147     return;
148
149   QString anObjName = theObject->GetName();
150   if ( theOldStr.isEmpty() )
151   {
152     while ( anObjName.startsWith( '_' ) )
153       anObjName.remove( 0, 1 );
154
155     anObjName.prepend( theNewStr + "_" );
156   }
157   else if ( anObjName.startsWith( theOldStr ) )
158   {
159     anObjName.replace( 0, theOldStr.length(), theNewStr );
160   }
161   else
162     return;
163
164   theObject->SetName( anObjName );
165 }
166
167 QString HYDROData_Tool::GenerateNameForPython( const MapOfTreatedObjects& theTreatedObjects,
168                                                const QString&             thePrefix )
169 {
170   QString aName = thePrefix;
171   if ( !theTreatedObjects.contains( aName ) )
172     return aName;
173
174   int anId = 1;
175   while( anId < aMaxNameId )
176   {
177     aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
178
179     // check that there are no other objects with the same name
180     if ( !theTreatedObjects.contains( aName ) )
181       break;
182   }
183
184   return aName;
185 }
186 //======================================================================================================
187 TopAbs_State HYDROData_Tool::ComputePointState( const gp_XY& theXY, const TopoDS_Face& theFace )
188 {
189   TopAbs_State aState(TopAbs_UNKNOWN);
190   if(theFace.IsNull()) return aState;  
191   Standard_Real aTol = BRep_Tool::Tolerance(theFace);
192   BRepAdaptor_Surface Ads ( theFace, Standard_False );
193   Standard_Real toluv = Min ( Ads.UResolution(aTol), Ads.VResolution(aTol) ); 
194   const gp_Pln& aPlane = Ads.Surface().Plane();
195   gp_Pnt aPnt(theXY.X(), theXY.Y(), 0.);
196   Standard_Real aU1, aV1;
197   ElSLib::Parameters(aPlane,aPnt, aU1, aV1);
198   BRepTopAdaptor_FClass2d aClassifier( theFace, toluv ); 
199   aState = aClassifier.Perform( gp_Pnt2d(aU1, aV1), Standard_False );
200   return aState;
201 }