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"
25 #include "HYDROData_ShapesGroup.h"
28 #include <QStringList>
29 #include <QTextStream>
35 #include <TopAbs_State.hxx>
36 #include <BRepAdaptor_Surface.hxx>
37 #include <BRepTopAdaptor_FClass2d.hxx>
38 #include <BRep_Tool.hxx>
39 #include <Geom_Curve.hxx>
41 #include <TopoDS_Wire.hxx>
42 #include <TopExp_Explorer.hxx>
44 static int aMaxNameId = std::numeric_limits<int>::max();
46 void HYDROData_Tool::WriteStringsToFile( QFile& theFile,
47 const QStringList& theStrings,
48 const QString& theSep )
50 if ( !theFile.isOpen() || theStrings.isEmpty() )
53 QString aWriteStr = theStrings.join( theSep );
54 if ( aWriteStr.isEmpty() )
57 QTextStream anOutStream( &theFile );
58 anOutStream << aWriteStr << theSep << theSep;
61 void HYDROData_Tool::SetMustBeUpdatedObjects(
62 const Handle(HYDROData_Document)& theDoc )
64 bool anIsChanged = true;
66 // iterate until there is no changes because objects on all level of dependency must be updated
71 HYDROData_Iterator anIter( theDoc );
72 for ( ; anIter.More(); anIter.Next() )
74 Handle(HYDROData_Entity) anObject = anIter.Current();
75 if ( anObject.IsNull() || anObject->IsMustBeUpdated() )
78 HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
79 for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
81 Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
82 if ( aRefObject.IsNull() || !aRefObject->IsMustBeUpdated() )
85 anObject->SetToUpdate( true );
93 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
94 const QString& thePrefix,
95 const QStringList& theUsedNames,
96 const bool theIsTryToUsePurePrefix )
98 QStringList aNamesList( theUsedNames );
100 // Collect all used names in the document
101 HYDROData_Iterator anIter( theDoc );
102 for( ; anIter.More(); anIter.Next() )
104 Handle(HYDROData_Entity) anObject = anIter.Current();
105 if( anObject.IsNull() )
108 QString anObjName = anObject->GetName();
109 if ( anObjName.isEmpty() )
112 aNamesList.append( anObjName );
117 if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
121 while( anId < aMaxNameId )
123 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
125 // check that there are no other objects with the same name in the document
126 if ( !aNamesList.contains( aName ) )
134 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
136 if ( theObject.IsNull() )
139 return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
140 theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
143 void HYDROData_Tool::UpdateChildObjectName( const QString& theOldStr,
144 const QString& theNewStr,
145 const Handle(HYDROData_Entity)& theObject )
147 if ( theObject.IsNull() )
150 QString anObjName = theObject->GetName();
151 if ( theOldStr.isEmpty() )
153 while ( anObjName.startsWith( '_' ) )
154 anObjName.remove( 0, 1 );
156 anObjName.prepend( theNewStr + "_" );
158 else if ( anObjName.startsWith( theOldStr ) )
160 anObjName.replace( 0, theOldStr.length(), theNewStr );
165 theObject->SetName( anObjName );
168 QString HYDROData_Tool::GenerateNameForPython( const MapOfTreatedObjects& theTreatedObjects,
169 const QString& thePrefix )
171 QString aName = thePrefix;
172 if ( !theTreatedObjects.contains( aName ) )
176 while( anId < aMaxNameId )
178 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
180 // check that there are no other objects with the same name
181 if ( !theTreatedObjects.contains( aName ) )
187 //======================================================================================================
188 TopAbs_State HYDROData_Tool::ComputePointState( const gp_XY& theXY, const TopoDS_Face& theFace )
190 TopAbs_State aState(TopAbs_UNKNOWN);
191 if(theFace.IsNull()) return aState;
192 Standard_Real aTol = BRep_Tool::Tolerance(theFace);
193 BRepAdaptor_Surface Ads ( theFace, Standard_False );
194 Standard_Real toluv = Min ( Ads.UResolution(aTol), Ads.VResolution(aTol) );
195 const gp_Pln& aPlane = Ads.Surface().Plane();
196 gp_Pnt aPnt(theXY.X(), theXY.Y(), 0.);
197 Standard_Real aU1, aV1;
198 ElSLib::Parameters(aPlane,aPnt, aU1, aV1);
199 BRepTopAdaptor_FClass2d aClassifier( theFace, toluv );
200 aState = aClassifier.Perform( gp_Pnt2d(aU1, aV1), Standard_False );
204 double HYDROData_Tool::GetAltitudeForEdge( const TopoDS_Edge& theEdge,
205 const gp_XY& thePoint,
206 double theParameterTolerance,
207 double theSquareDistanceTolerance,
208 double theInvalidAltitude )
210 double aFirst, aLast;
211 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
212 if( aCurve.IsNull() )
213 return theInvalidAltitude;
215 gp_Pnt aFirstPnt, aLastPnt;
217 aCurve->D0( aFirst, aFirstPnt );
218 aCurve->D0( aLast, aLastPnt );
220 gp_Pnt2d aFirstPnt2d( aFirstPnt.X(), aFirstPnt.Y() );
221 gp_Pnt2d aLastPnt2d( aLastPnt.X(), aLastPnt.Y() );
223 double aFirstDist = 0;
224 double aLastDist = aFirstPnt2d.SquareDistance( aLastPnt2d );
225 double aNecDist = aFirstPnt2d.SquareDistance( thePoint );
227 while( fabs( aLast - aFirst ) > theParameterTolerance )
229 double aMid = ( aFirst + aLast ) / 2;
231 aCurve->D0( aMid, aMidPnt );
232 double aDist = aFirstPnt2d.SquareDistance( gp_Pnt2d( aMidPnt.X(), aMidPnt.Y() ) );
234 if( aDist < aNecDist )
240 double aMid = ( aFirst + aLast ) / 2;
242 aCurve->D0( aMid, aMidPnt );
244 gp_Pnt2d aMidPnt2d( aMidPnt.X(), aMidPnt.Y() );
245 if( aMidPnt2d.SquareDistance( thePoint ) < theSquareDistanceTolerance )
248 return theInvalidAltitude;
251 double HYDROData_Tool::GetAltitudeForWire( const TopoDS_Wire& theWire,
252 const gp_XY& thePoint,
253 double theParameterTolerance,
254 double theSquareDistanceTolerance,
255 double theInvalidAltitude )
257 TopExp_Explorer anExp( theWire, TopAbs_EDGE );
258 for( ; anExp.More(); anExp.Next() )
260 double anAltitude = GetAltitudeForEdge( TopoDS::Edge( anExp.Current() ), thePoint,
261 theParameterTolerance, theSquareDistanceTolerance, theInvalidAltitude );
262 if( anAltitude != theInvalidAltitude )
265 return theInvalidAltitude;
268 TopoDS_Shape HYDROData_Tool::getFirstShapeFromGroup( const HYDROData_SequenceOfObjects& theGroups,
269 const int theGroupId )
271 TopoDS_Shape aResShape;
272 if ( theGroupId < 1 || theGroupId > theGroups.Length() )
275 Handle(HYDROData_ShapesGroup) aGroup =
276 Handle(HYDROData_ShapesGroup)::DownCast( theGroups.Value( theGroupId ) );
277 if ( aGroup.IsNull() )
280 TopTools_SequenceOfShape aGroupShapes;
281 aGroup->GetShapes( aGroupShapes );
283 if ( !aGroupShapes.IsEmpty() )
284 aResShape = aGroupShapes.First();
289 TCollection_ExtendedString HYDROData_Tool::toExtString( const QString& theStr )
291 TCollection_ExtendedString aRes;
292 if( !theStr.isEmpty() )
294 Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
295 memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
296 ((short*)extStr)[theStr.length()] = '\0';
297 aRes = TCollection_ExtendedString( extStr );
303 QString HYDROData_Tool::toQString( const TCollection_ExtendedString& theStr )
305 return QString( (QChar*)theStr.ToExtString(), theStr.Length() );