2 #include "HYDROData_Tool.h"
4 #include "HYDROData_ArtificialObject.h"
5 #include "HYDROData_Image.h"
6 #include "HYDROData_Iterator.h"
7 #include "HYDROData_NaturalObject.h"
9 #include <TopTools_SequenceOfShape.hxx>
10 #include <TopExp_Explorer.hxx>
13 #include <QStringList>
14 #include <QTextStream>
23 #include <Geom_Plane.hxx>
24 #include <BRep_Tool.hxx>
25 #include <BRepBuilderAPI_FindPlane.hxx>
27 #include <TopoDS_Vertex.hxx>
28 #include <TopoDS_Wire.hxx>
29 #include <TopoDS_Face.hxx>
30 #include <TopoDS_Iterator.hxx>
31 #include <TopTools_MapOfShape.hxx>
32 #include <TColgp_SequenceOfVec.hxx>
33 #include <TopTools_ShapeMapHasher.hxx>
34 #undef _NCollection_MapHasher
36 static int aMaxNameId = std::numeric_limits<int>::max();
38 void HYDROData_Tool::WriteStringsToFile( QFile& theFile,
39 const QStringList& theStrings,
40 const QString& theSep )
42 if ( !theFile.isOpen() || theStrings.isEmpty() )
45 QString aWriteStr = theStrings.join( theSep );
46 if ( aWriteStr.isEmpty() )
49 QTextStream anOutStream( &theFile );
50 anOutStream << aWriteStr << theSep << theSep;
53 void HYDROData_Tool::SetMustBeUpdatedObjects(
54 const Handle(HYDROData_Document)& theDoc )
56 bool anIsChanged = true;
58 // iterate until there is no changes because objects on all level of dependency must be updated
63 HYDROData_Iterator anIter( theDoc );
64 for ( ; anIter.More(); anIter.Next() )
66 Handle(HYDROData_Entity) anObject = anIter.Current();
67 if ( anObject.IsNull() || anObject->IsMustBeUpdated() )
70 HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
71 for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
73 Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
74 if ( aRefObject.IsNull() || !aRefObject->IsMustBeUpdated() )
77 anObject->SetToUpdate( true );
85 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
86 const QString& thePrefix,
87 const QStringList& theUsedNames,
88 const bool theIsTryToUsePurePrefix )
90 QStringList aNamesList( theUsedNames );
92 // Collect all used names in the document
93 HYDROData_Iterator anIter( theDoc );
94 for( ; anIter.More(); anIter.Next() )
96 Handle(HYDROData_Entity) anObject = anIter.Current();
97 if( anObject.IsNull() )
100 QString anObjName = anObject->GetName();
101 if ( anObjName.isEmpty() )
104 aNamesList.append( anObjName );
109 if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
113 while( anId < aMaxNameId )
115 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
117 // check that there are no other objects with the same name in the document
118 if ( !aNamesList.contains( aName ) )
126 Handle(HYDROData_Entity) HYDROData_Tool::FindObjectByName( const Handle(HYDROData_Document)& theDoc,
127 const QString& theName,
128 const ObjectKind theObjectKind )
130 Handle(HYDROData_Entity) anObject;
131 if ( theName.isEmpty() || theDoc.IsNull() )
134 QStringList aNamesList;
135 aNamesList << theName;
137 HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( theDoc, aNamesList, theObjectKind );
138 if( aSeqOfObjs.IsEmpty() )
141 anObject = aSeqOfObjs.First();
145 HYDROData_SequenceOfObjects HYDROData_Tool::FindObjectsByNames( const Handle(HYDROData_Document)& theDoc,
146 const QStringList& theNames,
147 const ObjectKind theObjectKind )
149 HYDROData_SequenceOfObjects aResSeq;
150 if( theDoc.IsNull() )
153 QStringList aNamesList = theNames;
155 HYDROData_Iterator anIter( theDoc, theObjectKind );
156 for( ; anIter.More(); anIter.Next() )
158 Handle(HYDROData_Entity) anObject = anIter.Current();
159 if( anObject.IsNull() )
162 QString anObjName = anObject->GetName();
163 if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
166 aResSeq.Append( anObject );
168 aNamesList.removeAll( anObjName );
169 if ( aNamesList.isEmpty() )
176 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
178 if ( theObject.IsNull() )
181 return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
182 theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
185 void HYDROData_Tool::UpdateChildObjectName( const QString& theOldStr,
186 const QString& theNewStr,
187 const Handle(HYDROData_Entity)& theObject )
189 if ( theObject.IsNull() )
192 QString anObjName = theObject->GetName();
193 if ( theOldStr.isEmpty() )
195 while ( anObjName.startsWith( '_' ) )
196 anObjName.remove( 0, 1 );
198 anObjName.prepend( theNewStr + "_" );
200 else if ( anObjName.startsWith( theOldStr ) )
202 anObjName.replace( 0, theOldStr.length(), theNewStr );
207 theObject->SetName( anObjName );
210 QString HYDROData_Tool::GenerateNameForPython( const MapOfTreatedObjects& theTreatedObjects,
211 const QString& thePrefix )
213 QString aName = thePrefix;
214 if ( !theTreatedObjects.contains( aName ) )
218 while( anId < aMaxNameId )
220 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
222 // check that there are no other objects with the same name
223 if ( !theTreatedObjects.contains( aName ) )
229 //======================================================================================================
230 // the Face to be: planer, whitout holes
232 TopAbs_State HYDROData_Tool::ComputePointState( const gp_XY& thePnt2d, const TopoDS_Face& theFace )
234 TopAbs_State aState(TopAbs_UNKNOWN);
235 if(theFace.IsNull()) return aState;
238 TopoDS_Wire aWire; int nb(0);
239 TopoDS_Iterator it(theFace);
240 for(;it.More();it.Next()) {
241 aWire = TopoDS::Wire(it.Value());
244 if(nb > 1 || aWire.IsNull()) return aState;
247 BRepBuilderAPI_FindPlane fndPlane (theFace, Precision::Confusion());
249 aPlane = fndPlane.Plane()->Pln();
252 aNormal = gp_Vec(aPlane.Axis().Direction());
253 if(theFace.Orientation() == TopAbs_REVERSED)
255 gp_Pnt aPoint = gp_Pnt (thePnt2d.X(), thePnt2d.Y(), 0);
256 TColgp_SequenceOfVec aSeq;
257 TopTools_MapOfShape aMap;
258 it.Initialize(aWire);
259 for (;it.More(); it.Next()) {
260 const TopoDS_Vertex& aVx = TopExp::FirstVertex(TopoDS::Edge(it.Value()), Standard_True);
263 const gp_Pnt& aCurPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVx));
264 if(aPoint.IsEqual(aCurPnt,Precision::Confusion())) {
268 gp_Vec aVec (aPoint, aCurPnt);
271 Standard_Real anAng(0.0);
272 for(int i = 1;i < aSeq.Length();i++) {
273 const gp_Vec& aV1 = aSeq.Value(i);
274 const gp_Vec& aV2 = aSeq.Value(i+1);
275 anAng += aV1.AngleWithRef(aV2, aNormal);
277 anAng += aSeq.Value(aSeq.Length()).AngleWithRef(aSeq.Value(1), aNormal);
278 if(abs(anAng) > Precision::Angular())
284 BRepTopAdaptor_FClass2d aClassifier( theFace, Precision::Confusion() );
285 aState = aClassifier.Perform( gp_Pnt2d( thePoint ), Standard_False );