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>
20 #include <HYDROData_ArtificialObject.h>
21 #include <HYDROData_Document.h>
22 #include <HYDROData_Entity.h>
23 #include <HYDROData_Iterator.h>
24 #include <HYDROData_NaturalObject.h>
25 #include <HYDROData_ShapesGroup.h>
28 #include <QStringList>
29 #include <QTextStream>
30 #include <BRep_Tool.hxx>
31 #include <BRepAdaptor_Surface.hxx>
32 #include <BRepTopAdaptor_FClass2d.hxx>
34 #include <Geom_Curve.hxx>
36 #include <TopExp_Explorer.hxx>
38 #include <TopoDS_Face.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TopoDS_Wire.hxx>
42 static int aMaxNameId = std::numeric_limits<int>::max();
44 void HYDROData_Tool::WriteStringsToFile( QFile& theFile,
45 const QStringList& theStrings,
46 const QString& theSep )
48 if ( !theFile.isOpen() || theStrings.isEmpty() )
51 QString aWriteStr = theStrings.join( theSep );
52 if ( aWriteStr.isEmpty() )
55 QTextStream anOutStream( &theFile );
56 anOutStream << aWriteStr << theSep << theSep;
59 void HYDROData_Tool::SetMustBeUpdatedObjects(
60 const Handle(HYDROData_Document)& theDoc )
62 bool anIsChanged = true;
64 // iterate until there is no changes because objects on all level of dependency must be updated
69 HYDROData_Iterator anIter( theDoc );
70 for ( ; anIter.More(); anIter.Next() )
72 Handle(HYDROData_Entity) anObject = anIter.Current();
73 if ( anObject.IsNull() || anObject->IsMustBeUpdated() )
76 HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
77 for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
79 Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
80 if ( aRefObject.IsNull() || !aRefObject->IsMustBeUpdated() )
83 anObject->SetToUpdate( true );
91 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
92 const QString& thePrefix,
93 const QStringList& theUsedNames,
94 const bool theIsTryToUsePurePrefix )
96 QStringList aNamesList( theUsedNames );
98 // Collect all used names in the document
99 HYDROData_Iterator anIter( theDoc );
100 for( ; anIter.More(); anIter.Next() )
102 Handle(HYDROData_Entity) anObject = anIter.Current();
103 if( anObject.IsNull() )
106 QString anObjName = anObject->GetName();
107 if ( anObjName.isEmpty() )
110 aNamesList.append( anObjName );
115 if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
119 while( anId < aMaxNameId )
121 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
123 // check that there are no other objects with the same name in the document
124 if ( !aNamesList.contains( aName ) )
132 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
134 if ( theObject.IsNull() )
137 return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
138 theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
141 void HYDROData_Tool::UpdateChildObjectName( const QString& theOldStr,
142 const QString& theNewStr,
143 const Handle(HYDROData_Entity)& theObject )
145 if ( theObject.IsNull() )
148 QString anObjName = theObject->GetName();
149 if ( theOldStr.isEmpty() )
151 while ( anObjName.startsWith( '_' ) )
152 anObjName.remove( 0, 1 );
154 anObjName.prepend( theNewStr + "_" );
156 else if ( anObjName.startsWith( theOldStr ) )
158 anObjName.replace( 0, theOldStr.length(), theNewStr );
163 theObject->SetName( anObjName );
166 QString HYDROData_Tool::GenerateNameForPython( const MapOfTreatedObjects& theTreatedObjects,
167 const QString& thePrefix )
169 QString aName = thePrefix;
170 if ( !theTreatedObjects.contains( aName ) )
174 while( anId < aMaxNameId )
176 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
178 // check that there are no other objects with the same name
179 if ( !theTreatedObjects.contains( aName ) )
185 //======================================================================================================
186 TopAbs_State HYDROData_Tool::ComputePointState( const gp_XY& theXY, const TopoDS_Face& theFace )
188 TopAbs_State aState(TopAbs_UNKNOWN);
189 if(theFace.IsNull()) return aState;
190 Standard_Real aTol = BRep_Tool::Tolerance(theFace);
191 BRepAdaptor_Surface Ads ( theFace, Standard_False );
192 Standard_Real toluv = Min ( Ads.UResolution(aTol), Ads.VResolution(aTol) );
193 const gp_Pln& aPlane = Ads.Surface().Plane();
194 gp_Pnt aPnt(theXY.X(), theXY.Y(), 0.);
195 Standard_Real aU1, aV1;
196 ElSLib::Parameters(aPlane,aPnt, aU1, aV1);
197 BRepTopAdaptor_FClass2d aClassifier( theFace, toluv );
198 aState = aClassifier.Perform( gp_Pnt2d(aU1, aV1), Standard_False );
202 double HYDROData_Tool::GetAltitudeForEdge( const TopoDS_Edge& theEdge,
203 const gp_XY& thePoint,
204 double theParameterTolerance,
205 double theSquareDistanceTolerance,
206 double theInvalidAltitude )
208 double aFirst, aLast;
209 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
210 if( aCurve.IsNull() )
211 return theInvalidAltitude;
213 gp_Pnt aFirstPnt, aLastPnt;
215 aCurve->D0( aFirst, aFirstPnt );
216 aCurve->D0( aLast, aLastPnt );
218 gp_Pnt2d aFirstPnt2d( aFirstPnt.X(), aFirstPnt.Y() );
219 gp_Pnt2d aLastPnt2d( aLastPnt.X(), aLastPnt.Y() );
221 double aFirstDist = 0;
222 double aLastDist = aFirstPnt2d.SquareDistance( aLastPnt2d );
223 double aNecDist = aFirstPnt2d.SquareDistance( thePoint );
225 while( fabs( aLast - aFirst ) > theParameterTolerance )
227 double aMid = ( aFirst + aLast ) / 2;
229 aCurve->D0( aMid, aMidPnt );
230 double aDist = aFirstPnt2d.SquareDistance( gp_Pnt2d( aMidPnt.X(), aMidPnt.Y() ) );
232 if( aDist < aNecDist )
238 double aMid = ( aFirst + aLast ) / 2;
240 aCurve->D0( aMid, aMidPnt );
242 gp_Pnt2d aMidPnt2d( aMidPnt.X(), aMidPnt.Y() );
243 if( aMidPnt2d.SquareDistance( thePoint ) < theSquareDistanceTolerance )
246 return theInvalidAltitude;
249 double HYDROData_Tool::GetAltitudeForWire( const TopoDS_Wire& theWire,
250 const gp_XY& thePoint,
251 double theParameterTolerance,
252 double theSquareDistanceTolerance,
253 double theInvalidAltitude )
255 TopExp_Explorer anExp( theWire, TopAbs_EDGE );
256 for( ; anExp.More(); anExp.Next() )
258 double anAltitude = GetAltitudeForEdge( TopoDS::Edge( anExp.Current() ), thePoint,
259 theParameterTolerance, theSquareDistanceTolerance, theInvalidAltitude );
260 if( anAltitude != theInvalidAltitude )
263 return theInvalidAltitude;
266 TopoDS_Shape HYDROData_Tool::getFirstShapeFromGroup( const HYDROData_SequenceOfObjects& theGroups,
267 const int theGroupId )
269 TopoDS_Shape aResShape;
270 if ( theGroupId < 1 || theGroupId > theGroups.Length() )
273 Handle(HYDROData_ShapesGroup) aGroup =
274 Handle(HYDROData_ShapesGroup)::DownCast( theGroups.Value( theGroupId ) );
275 if ( aGroup.IsNull() )
278 TopTools_SequenceOfShape aGroupShapes;
279 aGroup->GetShapes( aGroupShapes );
281 if ( !aGroupShapes.IsEmpty() )
282 aResShape = aGroupShapes.First();
287 TCollection_ExtendedString HYDROData_Tool::toExtString( const QString& theStr )
289 TCollection_ExtendedString aRes;
290 if( !theStr.isEmpty() )
292 Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
293 memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
294 ((short*)extStr)[theStr.length()] = '\0';
295 aRes = TCollection_ExtendedString( extStr );
301 QString HYDROData_Tool::toQString( const TCollection_ExtendedString& theStr )
303 return QString( (QChar*)theStr.ToExtString(), theStr.Length() );
306 std::ostream& operator<<( std::ostream& theStream, const QString& theText )
308 theStream << theText.toStdString();
312 std::ostream& operator<<( std::ostream& theStream, const QColor& theColor )
314 theStream << "[" << theColor.red() << ", " << theColor.green() << ", " << theColor.blue() << "]";
318 std::ostream& operator<<( std::ostream& theStream, const TopoDS_Shape& theShape )
320 theStream << "[" << theShape.TShape().operator->() << "]";
324 std::ostream& operator<<( std::ostream& theStream, const TopoDS_Face& theFace )
326 theStream << "[" << theFace.TShape().operator->() << "]";