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_TopoCurve.h>
21 #include <HYDROData_Document.h>
22 #include <BRepAdaptor_Curve.hxx>
23 #include <BRep_Builder.hxx>
24 #include <BRep_Tool.hxx>
25 #include <BRepBuilderAPI_MakeEdge2d.hxx>
26 #include <BRepBuilderAPI_MakeEdge.hxx>
27 #include <BRepBuilderAPI_MakeWire.hxx>
28 #include <Extrema_ExtCC.hxx>
29 #include <Extrema_ExtPC.hxx>
30 #include <GeomAPI_Interpolate.hxx>
31 #include <NCollection_Vector.hxx>
32 #include <Precision.hxx>
33 #include <ShapeAnalysis_TransferParametersProj.hxx>
34 #include <ShapeBuild_Edge.hxx>
35 #include <TColgp_Array1OfVec.hxx>
36 #include <TColgp_HArray1OfPnt.hxx>
37 #include <TColStd_HArray1OfBoolean.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Wire.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
46 template<class T> void append( std::vector<T>& theList, const std::vector<T>& theList2 )
48 int aSize = theList.size();
49 int aNewSize = aSize + theList2.size();
54 theList.resize( aNewSize );
55 for( int i=aSize, j=0; i<aNewSize; i++, j++ )
56 theList[i] = theList2[j];
59 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
60 const Handle( HYDROData_PolylineXY )& thePolyline,
61 const gp_Pnt2d& thePoint,
62 double theTolerance ) const
64 std::vector<gp_Pnt2d> aPointsList( 1 );
65 aPointsList[0] = thePoint;
66 std::vector<TopoDS_Wire> aCurves;
67 GetWires(thePolyline, aCurves);
69 for( int i=0, n=aCurves.size(); i<n; i++ )
71 std::vector<TopoDS_Shape> aCurvesList;
72 Split(aCurves[i], thePoint, theTolerance, aCurvesList);
73 bool isLocalOK = CreatePolylines( theDoc, thePolyline->GetName(), aCurvesList, true );
74 isOK = isOK && isLocalOK;
79 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
80 const Handle( HYDROData_PolylineXY )& thePolyline,
81 const Handle( HYDROData_PolylineXY )& theTool,
82 double theTolerance ) const
84 HYDROData_SequenceOfObjects aSeq;
85 aSeq.Append( theTool );
86 return split( theDoc, thePolyline, aSeq, theTolerance, -1 );
89 bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
90 const HYDROData_SequenceOfObjects& thePolylines,
93 int f = thePolylines.Lower(), l = thePolylines.Upper();
94 for( int i=f; i<=l; i++ )
96 Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
97 if( !split( theDoc, aPolyline, thePolylines, theTolerance, i ) )
103 bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
104 const QString& theName,
105 const HYDROData_SequenceOfObjects& thePolylines,
106 bool isConnectByNewSegment,
107 double theTolerance )
109 std::deque<HYDROData_TopoCurve> aMergedCurves;
110 HYDROData_SequenceOfObjects::Iterator aPIt(thePolylines);
111 for (; aPIt.More(); aPIt.Next())
113 Handle(HYDROData_PolylineXY) aPolyline =
114 Handle(HYDROData_PolylineXY)::DownCast(aPIt.Value());
115 std::vector<TopoDS_Wire> aWires;
116 GetWires(aPolyline, aWires);
117 for (std::vector<TopoDS_Wire>::const_iterator aWIt = aWires.begin(),
118 aLastWIt = aWires.end(); aWIt != aLastWIt; ++aWIt)
120 const Standard_Boolean aResult = !isConnectByNewSegment ?
121 HYDROData_TopoCurve::Merge(theTolerance, *aWIt, aMergedCurves) :
122 HYDROData_TopoCurve::Connect(theTolerance, *aWIt, aMergedCurves);
130 TopoDS_Compound aWireSet;
131 BRep_Builder aBuilder;
132 aBuilder.MakeCompound(aWireSet);
133 std::deque<HYDROData_TopoCurve>::iterator aCIt = aMergedCurves.begin();
134 std::deque<HYDROData_TopoCurve>::iterator aLastCIt = aMergedCurves.end();
135 for (; aCIt != aLastCIt; ++aCIt)
137 if (!aCIt->IsEmpty())
139 aBuilder.Add(aWireSet, aCIt->Wire());
143 std::vector<TopoDS_Shape> aPolylines(1);
144 aPolylines[0] = aWireSet;
145 CreatePolylines(theDoc, theName, aPolylines, false);
149 bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
150 const Handle( HYDROData_PolylineXY )& thePolyline,
151 const HYDROData_SequenceOfObjects& theTools,
153 int theIgnoreIndex ) const
155 std::vector<TopoDS_Wire> aCurves;
156 GetWires(thePolyline, aCurves);
157 std::vector<TopoDS_Wire> aToolCurves;
158 for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
159 if( i!=theIgnoreIndex )
161 Handle( HYDROData_PolylineXY ) aToolPolyline =
162 Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
163 std::vector<TopoDS_Wire> aTCurves;
164 GetWires(aToolPolyline, aTCurves);
165 append( aToolCurves, aTCurves);
168 const int aPSCount = aCurves.size();
169 const int aTSCount = aToolCurves.size();
170 std::vector<TopoDS_Shape> aResult;
171 for (int aPSI = 0; aPSI < aPSCount; ++aPSI)
173 HYDROData_TopoCurve aCurve;
174 if (!aCurve.Initialize(aCurves[aPSI]))
179 std::deque<std::list<double> > aParams;
180 for (int aTSI = 0; aTSI < aTSCount; ++aTSI)
182 aCurve.Intersect(aToolCurves[aTSI], aParams);
185 std::deque<HYDROData_TopoCurve> aSplittedCurves;
186 aCurve.Cut(aParams, aSplittedCurves);
187 std::deque<HYDROData_TopoCurve>::const_iterator aCIt =
188 aSplittedCurves.begin();
189 std::deque<HYDROData_TopoCurve>::const_iterator aLastCIt =
190 aSplittedCurves.end();
191 for (; aCIt != aLastCIt; ++aCIt)
193 aResult.push_back(aCIt->Wire());
197 CreatePolylines(theDoc, thePolyline->GetName(), aResult, true);
201 void HYDROData_PolylineOperator::GetWires(
202 const Handle( HYDROData_PolylineXY )& thePolyline,
203 std::vector<TopoDS_Wire>& theWires)
205 TopoDS_Shape aShape = thePolyline->GetShape();
206 if( aShape.ShapeType()==TopAbs_WIRE )
208 theWires.push_back( TopoDS::Wire( aShape ) );
212 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
213 for( ; anExp.More(); anExp.Next() )
215 theWires.push_back( TopoDS::Wire( anExp.Current() ) );
220 void HYDROData_PolylineOperator::Split(
221 const TopoDS_Wire& theWire,
222 const gp_Pnt2d& thePoint,
224 std::vector<TopoDS_Shape>& theWires)
226 HYDROData_TopoCurve aCurve;
227 if (!aCurve.Initialize(theWire))
229 theWires.push_back(theWire);
233 const gp_XYZ aP(thePoint.X(), thePoint.Y(), 0);
234 std::list<TopoDS_Edge>::const_iterator aEPos;
236 aCurve.Project(aP, aEPos, aParam);
237 HYDROData_TopoCurve aCurve1, aCurve2;
238 aCurve.Cut(aEPos, aParam, aCurve1, aCurve2);
239 theWires.push_back(aCurve1.Wire());
240 if (!aCurve2.IsEmpty())
242 theWires.push_back(aCurve2.Wire());
246 bool HYDROData_PolylineOperator::CreatePolylines( const Handle( HYDROData_Document )& theDoc,
247 const QString& theNamePrefix,
248 const std::vector<TopoDS_Shape>& theShapes,
251 if( theDoc.IsNull() )
254 int n = theShapes.size();
256 for( int i=0; i<n; i++ )
258 Handle( HYDROData_PolylineXY ) aPolyline =
259 Handle( HYDROData_PolylineXY )::DownCast( theDoc->CreateObject( KIND_POLYLINEXY ) );
260 if( aPolyline.IsNull() )
263 aPolyline->SetShape( theShapes[i] );
267 QString aNewName = theNamePrefix + "_" + QString::number( anIndex );
268 if( theDoc->FindObjectByName( aNewName ).IsNull() ) // the object with such a name is not found
269 aPolyline->SetName( aNewName );
274 aPolyline->SetName( theNamePrefix );