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