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_PolylineOperator.h>
20 #include <HYDROData_Document.h>
21 #include <BRepBuilderAPI_MakeEdge2d.hxx>
22 #include <BRepBuilderAPI_MakeWire.hxx>
23 #include <TopoDS_Edge.hxx>
24 #include <TopoDS_Wire.hxx>
26 template<class T> void append( std::vector<T>& theList, const std::vector<T>& theList2 )
28 int aSize = theList.size();
29 int aNewSize = aSize + theList2.size();
34 theList.resize( aNewSize );
35 for( int i=aSize, j=0; i<aNewSize; i++, j++ )
36 theList[i] = theList2[j];
40 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
41 const TCollection_AsciiString& theNamePrefix,
42 const Handle( HYDROData_PolylineXY )& thePolyline,
43 const gp_Pnt2d& thePoint ) const
45 std::vector<gp_Pnt2d> aPointsList( 1 );
46 aPointsList[0] = thePoint;
47 std::vector<Handle( Geom2d_Curve )> aCurves = GetCurves( thePolyline );
49 for( int i=0, n=aCurves.size(); i<n; i++ )
51 std::vector<Handle( Geom2d_Curve )> aCurvesList = Split( aCurves[i], aPointsList );
52 bool isLocalOK = CreatePolylines( theDoc, theNamePrefix, aCurvesList );
53 isOK = isOK && isLocalOK;
58 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
59 const TCollection_AsciiString& theNamePrefix,
60 const Handle( HYDROData_PolylineXY )& thePolyline,
61 const Handle( HYDROData_PolylineXY )& theTool ) const
63 std::vector<Handle( Geom2d_Curve )> aCurves = GetCurves( thePolyline );
64 std::vector<Handle( Geom2d_Curve )> aToolCurves = GetCurves( theTool );
66 for( int i=0, n=aCurves.size(); i<n; i++ )
67 for( int j=0, m=aToolCurves.size(); j<m; j++ )
69 std::vector<gp_Pnt2d> aPointsList = Intersection( aCurves[i], aToolCurves[j] );
70 std::vector<Handle( Geom2d_Curve )> aCurvesList = Split( aCurves[i], aPointsList );
71 bool isLocalOK = CreatePolylines( theDoc, theNamePrefix, aCurvesList );
72 isOK = isOK && isLocalOK;
77 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
78 const TCollection_AsciiString& theNamePrefix,
79 const HYDROData_SequenceOfObjects& thePolylines )
81 int f = thePolylines.Lower(), l = thePolylines.Upper();
83 std::vector<Handle( Geom2d_Curve )> anAllCurves;
84 for( int i=f; i<=l; i++ )
86 Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
87 std::vector<Handle( Geom2d_Curve )> aCurves = GetCurves( aPolyline );
88 append( anAllCurves, aCurves );
91 for( int i=0, n=anAllCurves.size(); i<n; i++ )
93 std::vector<gp_Pnt2d> aCompletePointsList;
94 for( int j=0; j<n; j++ )
98 std::vector<gp_Pnt2d> aPointsList = Intersection( anAllCurves[i], anAllCurves[j] );
99 append( aCompletePointsList, aPointsList );
101 std::vector<Handle( Geom2d_Curve )> aCurvesList = Split( anAllCurves[i], aCompletePointsList );
102 bool isLocalOK = CreatePolylines( theDoc, theNamePrefix, aCurvesList );
103 isOK = isOK && isLocalOK;
108 bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
109 const TCollection_AsciiString& theName,
110 const HYDROData_SequenceOfObjects& thePolylines )
116 std::vector<Handle( Geom2d_Curve )> HYDROData_PolylineOperator::GetCurves( const Handle( HYDROData_PolylineXY )& thePolyline )
118 std::vector<Handle( Geom2d_Curve )> aResult;
123 std::vector<gp_Pnt2d> HYDROData_PolylineOperator::Intersection( const Handle( Geom2d_Curve )& theCurve,
124 const Handle( Geom2d_Curve )& theTool )
126 std::vector<gp_Pnt2d> aResult;
131 std::vector<Handle( Geom2d_Curve )> HYDROData_PolylineOperator::Split( const Handle( Geom2d_Curve )& theCurve,
132 const std::vector<gp_Pnt2d>& thePoints )
134 std::vector<Handle( Geom2d_Curve )> aResult;
139 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
140 const TCollection_AsciiString& theNamePrefix,
141 const std::vector<Handle( Geom2d_Curve )>& theCurves )
143 if( theDoc.IsNull() )
146 int n = theCurves.size();
147 for( int i=0; i<n; i++ )
149 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge2d( theCurves[i] ).Edge();
150 BRepBuilderAPI_MakeWire aMakeWire;
151 aMakeWire.Add( anEdge );
153 Handle( HYDROData_PolylineXY ) aPolyline =
154 Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
155 if( aPolyline.IsNull() )
158 aPolyline->SetShape( aMakeWire.Wire() );