Salome HOME
Merge branch 'BR_v14_rc' of ssh://git.salome-platform.org/modules/hydro into HEAD
[modules/hydro.git] / src / HYDROData / HYDROData_Tool.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROData_Tool.h"
20
21 #include "HYDROData_ArtificialObject.h"
22 #include "HYDROData_Image.h"
23 #include "HYDROData_Iterator.h"
24 #include "HYDROData_NaturalObject.h"
25
26 #include <QFile>
27 #include <QStringList>
28 #include <QTextStream>
29
30 #include <limits>
31 #include <gp_Pnt.hxx>
32 #include <gp_Pln.hxx>
33 #include <ElSLib.hxx>
34 #include <TopAbs_State.hxx>
35 #include <BRepAdaptor_Surface.hxx>
36 #include <BRepTopAdaptor_FClass2d.hxx>
37 #include <BRep_Tool.hxx>
38
39 static int aMaxNameId = std::numeric_limits<int>::max();
40
41 void HYDROData_Tool::WriteStringsToFile( QFile&             theFile,
42                                          const QStringList& theStrings,
43                                          const QString&     theSep )
44 {
45   if ( !theFile.isOpen() || theStrings.isEmpty() )
46     return;
47   
48   QString aWriteStr = theStrings.join( theSep );
49   if ( aWriteStr.isEmpty() )
50     return;
51
52   QTextStream anOutStream( &theFile );
53   anOutStream << aWriteStr << theSep << theSep;
54 }
55
56 void HYDROData_Tool::SetMustBeUpdatedObjects(
57   const Handle(HYDROData_Document)& theDoc  )
58 {
59   bool anIsChanged = true;
60
61   // iterate until there is no changes because objects on all level of dependency must be updated
62   while ( anIsChanged )
63   {
64     anIsChanged = false;
65
66     HYDROData_Iterator anIter( theDoc );
67     for ( ; anIter.More(); anIter.Next() )
68     {
69       Handle(HYDROData_Entity) anObject = anIter.Current();
70       if ( anObject.IsNull() || anObject->IsMustBeUpdated() )
71         continue;
72
73       HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
74       for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
75       {
76         Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
77         if ( aRefObject.IsNull() || !aRefObject->IsMustBeUpdated() )
78           continue;
79
80         anObject->SetToUpdate( true );
81         anIsChanged = true;
82         break;
83       }
84     }
85   }
86 }
87
88 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
89                                             const QString&                    thePrefix,
90                                             const QStringList&                theUsedNames,
91                                             const bool                        theIsTryToUsePurePrefix )
92 {
93   QStringList aNamesList( theUsedNames );
94
95   // Collect all used names in the document
96   HYDROData_Iterator anIter( theDoc );
97   for( ; anIter.More(); anIter.Next() )
98   {
99     Handle(HYDROData_Entity) anObject = anIter.Current();
100     if( anObject.IsNull() )
101       continue;
102
103     QString anObjName = anObject->GetName();
104     if ( anObjName.isEmpty() )
105       continue;
106
107     aNamesList.append( anObjName );
108   }
109
110   QString aName;
111
112   if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
113     aName = thePrefix;
114   } else {
115     int anId = 1;
116     while( anId < aMaxNameId )
117     {
118       aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
119
120       // check that there are no other objects with the same name in the document
121       if ( !aNamesList.contains( aName ) )
122         break;
123     }
124   }
125
126   return aName;
127 }
128
129 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
130 {
131   if ( theObject.IsNull() )
132     return false;
133   
134   return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
135          theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
136 }
137
138 void HYDROData_Tool::UpdateChildObjectName( const QString&                  theOldStr,
139                                             const QString&                  theNewStr,
140                                             const Handle(HYDROData_Entity)& theObject )
141 {
142   if ( theObject.IsNull() )
143     return;
144
145   QString anObjName = theObject->GetName();
146   if ( theOldStr.isEmpty() )
147   {
148     while ( anObjName.startsWith( '_' ) )
149       anObjName.remove( 0, 1 );
150
151     anObjName.prepend( theNewStr + "_" );
152   }
153   else if ( anObjName.startsWith( theOldStr ) )
154   {
155     anObjName.replace( 0, theOldStr.length(), theNewStr );
156   }
157   else
158     return;
159
160   theObject->SetName( anObjName );
161 }
162
163 QString HYDROData_Tool::GenerateNameForPython( const MapOfTreatedObjects& theTreatedObjects,
164                                                const QString&             thePrefix )
165 {
166   QString aName = thePrefix;
167   if ( !theTreatedObjects.contains( aName ) )
168     return aName;
169
170   int anId = 1;
171   while( anId < aMaxNameId )
172   {
173     aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
174
175     // check that there are no other objects with the same name
176     if ( !theTreatedObjects.contains( aName ) )
177       break;
178   }
179
180   return aName;
181 }
182 //======================================================================================================
183 TopAbs_State HYDROData_Tool::ComputePointState( const gp_XY& theXY, const TopoDS_Face& theFace )
184 {
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 );
196   return aState;
197 }