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>
43 static int aMaxNameId = std::numeric_limits<int>::max();
45 void HYDROData_Tool::WriteStringsToFile( QFile& theFile,
46 const QStringList& theStrings,
47 const QString& theSep )
49 if ( !theFile.isOpen() || theStrings.isEmpty() )
52 QString aWriteStr = theStrings.join( theSep );
53 if ( aWriteStr.isEmpty() )
56 QTextStream anOutStream( &theFile );
57 anOutStream << aWriteStr << theSep << theSep;
60 void HYDROData_Tool::SetMustBeUpdatedObjects(
61 const Handle(HYDROData_Document)& theDoc )
63 bool anIsChanged = true;
65 // iterate until there is no changes because objects on all level of dependency must be updated
70 HYDROData_Iterator anIter( theDoc );
71 for ( ; anIter.More(); anIter.Next() )
73 Handle(HYDROData_Entity) anObject = anIter.Current();
74 if ( anObject.IsNull() || anObject->IsMustBeUpdated() )
77 HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
78 for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
80 Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
81 if ( aRefObject.IsNull() || !aRefObject->IsMustBeUpdated() )
84 anObject->SetToUpdate( true );
92 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
93 const QString& thePrefix,
94 const QStringList& theUsedNames,
95 const bool theIsTryToUsePurePrefix )
97 QStringList aNamesList( theUsedNames );
99 // Collect all used names in the document
100 HYDROData_Iterator anIter( theDoc );
101 for( ; anIter.More(); anIter.Next() )
103 Handle(HYDROData_Entity) anObject = anIter.Current();
104 if( anObject.IsNull() )
107 QString anObjName = anObject->GetName();
108 if ( anObjName.isEmpty() )
111 aNamesList.append( anObjName );
116 if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
120 while( anId < aMaxNameId )
122 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
124 // check that there are no other objects with the same name in the document
125 if ( !aNamesList.contains( aName ) )
133 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
135 if ( theObject.IsNull() )
138 return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
139 theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
142 void HYDROData_Tool::UpdateChildObjectName( const QString& theOldStr,
143 const QString& theNewStr,
144 const Handle(HYDROData_Entity)& theObject )
146 if ( theObject.IsNull() )
149 QString anObjName = theObject->GetName();
150 if ( theOldStr.isEmpty() )
152 while ( anObjName.startsWith( '_' ) )
153 anObjName.remove( 0, 1 );
155 anObjName.prepend( theNewStr + "_" );
157 else if ( anObjName.startsWith( theOldStr ) )
159 anObjName.replace( 0, theOldStr.length(), theNewStr );
164 theObject->SetName( anObjName );
167 QString HYDROData_Tool::GenerateNameForPython( const MapOfTreatedObjects& theTreatedObjects,
168 const QString& thePrefix )
170 QString aName = thePrefix;
171 if ( !theTreatedObjects.contains( aName ) )
175 while( anId < aMaxNameId )
177 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
179 // check that there are no other objects with the same name
180 if ( !theTreatedObjects.contains( aName ) )
186 //======================================================================================================
187 TopAbs_State HYDROData_Tool::ComputePointState( const gp_XY& theXY, const TopoDS_Face& theFace )
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 );
203 double HYDROData_Tool::GetAltitudeForEdge( const TopoDS_Edge& theEdge,
204 const gp_XY& thePoint,
205 double theParameterTolerance,
206 double theSquareDistanceTolerance,
207 double theInvalidAltitude )
209 double aFirst, aLast;
210 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
211 if( aCurve.IsNull() )
212 return theInvalidAltitude;
214 gp_Pnt aFirstPnt, aLastPnt;
216 aCurve->D0( aFirst, aFirstPnt );
217 aCurve->D0( aLast, aLastPnt );
219 gp_Pnt2d aFirstPnt2d( aFirstPnt.X(), aFirstPnt.Y() );
220 gp_Pnt2d aLastPnt2d( aLastPnt.X(), aLastPnt.Y() );
222 double aFirstDist = 0;
223 double aLastDist = aFirstPnt2d.SquareDistance( aLastPnt2d );
224 double aNecDist = aFirstPnt2d.SquareDistance( thePoint );
226 while( fabs( aLast - aFirst ) > theParameterTolerance )
228 double aMid = ( aFirst + aLast ) / 2;
230 aCurve->D0( aMid, aMidPnt );
231 double aDist = aFirstPnt2d.SquareDistance( gp_Pnt2d( aMidPnt.X(), aMidPnt.Y() ) );
233 if( aDist < aNecDist )
239 double aMid = ( aFirst + aLast ) / 2;
241 aCurve->D0( aMid, aMidPnt );
243 gp_Pnt2d aMidPnt2d( aMidPnt.X(), aMidPnt.Y() );
244 if( aMidPnt2d.SquareDistance( thePoint ) < theSquareDistanceTolerance )
247 return theInvalidAltitude;
250 double HYDROData_Tool::GetAltitudeForWire( const TopoDS_Wire& theWire,
251 const gp_XY& thePoint,
252 double theParameterTolerance,
253 double theSquareDistanceTolerance,
254 double theInvalidAltitude )
256 TopExp_Explorer anExp( theWire, TopAbs_EDGE );
257 for( ; anExp.More(); anExp.Next() )
259 double anAltitude = GetAltitudeForEdge( TopoDS::Edge( anExp.Current() ), thePoint,
260 theParameterTolerance, theSquareDistanceTolerance, theInvalidAltitude );
261 if( anAltitude != theInvalidAltitude )
264 return theInvalidAltitude;
267 TopoDS_Shape HYDROData_Tool::getFirstShapeFromGroup( const HYDROData_SequenceOfObjects& theGroups,
268 const int theGroupId )
270 TopoDS_Shape aResShape;
271 if ( theGroupId < 1 || theGroupId > theGroups.Length() )
274 Handle(HYDROData_ShapesGroup) aGroup =
275 Handle(HYDROData_ShapesGroup)::DownCast( theGroups.Value( theGroupId ) );
276 if ( aGroup.IsNull() )
279 TopTools_SequenceOfShape aGroupShapes;
280 aGroup->GetShapes( aGroupShapes );
282 if ( !aGroupShapes.IsEmpty() )
283 aResShape = aGroupShapes.First();
288 TCollection_ExtendedString HYDROData_Tool::toExtString( const QString& theStr )
290 TCollection_ExtendedString aRes;
291 if( !theStr.isEmpty() )
293 Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
294 memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
295 ((short*)extStr)[theStr.length()] = '\0';
296 aRes = TCollection_ExtendedString( extStr );
302 QString HYDROData_Tool::toQString( const TCollection_ExtendedString& theStr )
304 return QString( (QChar*)theStr.ToExtString(), theStr.Length() );
307 std::ostream& operator<<( std::ostream& theStream, const QString& theText )
309 theStream << theText.toStdString();
313 std::ostream& operator<<( std::ostream& theStream, const QColor& theColor )
315 theStream << "[" << theColor.red() << ", " << theColor.green() << ", " << theColor.blue() << "]";
319 std::ostream& operator<<( std::ostream& theStream, const TopoDS_Shape& theShape )
321 theStream << "[" << theShape.TShape().operator->() << "]";
325 std::ostream& operator<<( std::ostream& theStream, const TopoDS_Face& theFace )
327 theStream << "[" << theFace.TShape().operator->() << "]";