1 // Created on: 1998-03-12
2 // Created by: Pierre BARRAS
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
22 #include <ShapeUpgrade_SplitCurve2d.ixx>
23 #include <ShapeUpgrade.hxx>
24 #include <Geom2d_BSplineCurve.hxx>
25 #include <TColStd_ListIteratorOfListOfReal.hxx>
26 #include <TColStd_HSequenceOfReal.hxx>
27 #include <TColGeom2d_HArray1OfCurve.hxx>
28 #include <Geom2d_TrimmedCurve.hxx>
29 #include <Geom2d_OffsetCurve.hxx>
30 #include <Standard_ErrorHandler.hxx>
31 #include <Standard_Failure.hxx>
32 #include <ShapeExtend.hxx>
33 #include <Precision.hxx>
34 #include <Geom2d_BezierCurve.hxx>
35 #include <ShapeAnalysis_Curve.hxx>
37 //=======================================================================
38 //function : ShapeUpgrade_SplitCurve2d
40 //=======================================================================
42 ShapeUpgrade_SplitCurve2d::ShapeUpgrade_SplitCurve2d()
46 //=======================================================================
49 //=======================================================================
51 void ShapeUpgrade_SplitCurve2d::Init(const Handle(Geom2d_Curve)& C)
53 Init (C, C->FirstParameter(), C->LastParameter());
56 //=======================================================================
59 //=======================================================================
61 void ShapeUpgrade_SplitCurve2d::Init(const Handle(Geom2d_Curve)& C,
62 const Standard_Real First,
63 const Standard_Real Last)
65 // if (ShapeUpgrade::Debug()) cout << "SplitCurve2d::Init"<<endl;
66 Handle(Geom2d_Curve) CopyOfC = Handle(Geom2d_Curve)::DownCast(C->Copy());
69 Standard_Real precision = Precision::PConfusion();
70 Standard_Real firstPar = First;
71 Standard_Real lastPar = Last;
72 Handle (Geom2d_Curve) aCurve = myCurve;
73 if(aCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)))
74 aCurve=Handle(Geom2d_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
75 // 15.11.2002 PTV OCC966
76 if(!ShapeAnalysis_Curve::IsPeriodic(C)) {
77 Standard_Real fP = aCurve->FirstParameter();
78 Standard_Real lP = aCurve->LastParameter();
79 if(Abs(firstPar-fP) < precision)
81 if(Abs(lastPar-lP) < precision)
85 cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
91 cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
95 if( (lastPar-firstPar) < precision)
96 lastPar=firstPar+2*precision;
99 ShapeUpgrade_SplitCurve::Init (firstPar, lastPar);
101 // first, we make a copy of C to prevent modification:
102 // if (ShapeUpgrade::Debug()) cout << ". copy of the curve"<<endl;
107 //=======================================================================
110 //=======================================================================
112 void ShapeUpgrade_SplitCurve2d::Build(const Standard_Boolean Segment)
114 // if (ShapeUpgrade::Debug()) cout<<"ShapeUpgrade_SplitCurve2d::Build"<<endl;
115 Standard_Real First = mySplitValues->Value(1);
116 Standard_Real Last = mySplitValues->Value(mySplitValues->Length());
118 if (mySplitValues->Length() > 2)
119 myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
120 if (myCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
121 Handle(Geom2d_TrimmedCurve) tmp = Handle(Geom2d_TrimmedCurve)::DownCast (myCurve);
122 Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
123 ShapeUpgrade_SplitCurve2d spc;
124 spc.Init(BasCurve,First,Last);
125 spc.SetSplitValues(mySplitValues);
127 myNbCurves = spc.GetCurves()->Length();
129 myResultingCurves=new TColGeom2d_HArray1OfCurve (1,myNbCurves);
130 if (myNbCurves == 1) {
131 Handle(Geom2d_TrimmedCurve) NewTrimCurve =
132 new Geom2d_TrimmedCurve(spc.GetCurves()->Value(1),First,Last);
133 myResultingCurves->SetValue(1,NewTrimCurve);
136 myResultingCurves = spc.GetCurves();
137 myStatus |= spc.myStatus;
140 else if (myCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve))) {
141 Handle(Geom2d_OffsetCurve) tmp = Handle(Geom2d_OffsetCurve)::DownCast (myCurve);
142 Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
143 Standard_Real Offset = tmp->Offset();
144 ShapeUpgrade_SplitCurve2d spc;
145 spc.Init(BasCurve,First,Last);
146 spc.SetSplitValues(mySplitValues);
148 myNbCurves = spc.GetCurves()->Length();
149 myResultingCurves = new TColGeom2d_HArray1OfCurve (1,myNbCurves);
150 for(Standard_Integer i = 1; i <= myNbCurves; i++) {
151 Handle(Geom2d_OffsetCurve) NewOffsetCurve = new Geom2d_OffsetCurve(spc.GetCurves()->Value(i),Offset);
152 myResultingCurves->SetValue(i,NewOffsetCurve);
154 myStatus |= spc.myStatus;
158 myNbCurves = mySplitValues->Length() -1;
159 myResultingCurves=new TColGeom2d_HArray1OfCurve (1,myNbCurves);
161 Standard_Boolean filled = Standard_True;
162 if ( Abs ( myCurve->FirstParameter() - First ) < Precision::PConfusion() &&
163 Abs ( myCurve->LastParameter() - Last ) < Precision::PConfusion() )
164 myResultingCurves->SetValue(1,myCurve);
166 else if ( ! Segment || (!myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) &&
167 !myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve))) || ! Status ( ShapeExtend_DONE2 ) ) {
168 if(myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) ||
169 myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve) )) {
170 Handle(Geom2d_Curve) theNewCurve = Handle(Geom2d_Curve)::DownCast(myCurve->Copy());
173 if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)))
174 Handle(Geom2d_BSplineCurve)::DownCast(theNewCurve)->Segment (First, Last);
175 else if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve)))
176 Handle(Geom2d_BezierCurve)::DownCast(theNewCurve)->Segment (First, Last);
178 catch (Standard_Failure) {
180 cout << "Warning: ShapeUpgrade_Split2dCurve::Build(): Exception in Segment :";
181 Standard_Failure::Caught()->Print(cout); cout << endl;
183 theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),First,Last);
185 myResultingCurves->SetValue (1, theNewCurve);
189 Handle(Geom2d_TrimmedCurve) theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),First,Last);
190 myResultingCurves->SetValue (1, theNewCurve);
193 else filled = Standard_False;
194 if ( filled ) return;
198 if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve))) {
199 Handle(Geom2d_BSplineCurve) BsCurve = Handle(Geom2d_BSplineCurve)::DownCast(myCurve->Copy());
200 Standard_Integer FirstInd =BsCurve->FirstUKnotIndex(),
201 LastInd = BsCurve->LastUKnotIndex();
202 Standard_Integer j = FirstInd;
203 for(Standard_Integer ii =1 ; ii <= mySplitValues->Length(); ii++) {
204 Standard_Real spval = mySplitValues->Value(ii);
205 for(; j <=LastInd;j++) {
206 if( spval > BsCurve->Knot(j) + Precision::PConfusion()) continue;
207 if( spval < BsCurve->Knot(j) - Precision::PConfusion()) break;
208 mySplitValues->SetValue(ii,BsCurve->Knot(j));
210 if(j == LastInd) break;
214 for (Standard_Integer i = 1; i <= myNbCurves; i++) {
215 // skl : in the next block I change "First","Last" to "Firstt","Lastt"
216 Standard_Real Firstt = mySplitValues->Value(i), Lastt = mySplitValues->Value(i+1);
217 Handle(Geom2d_Curve) theNewCurve;
219 // creates a copy of myCurve before to segment:
220 if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) ||
221 myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve))) {
222 theNewCurve = Handle(Geom2d_Curve)::DownCast(myCurve->Copy());
225 if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)))
226 Handle(Geom2d_BSplineCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
227 else if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve)))
228 Handle(Geom2d_BezierCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
229 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
231 catch (Standard_Failure) {
233 cout << "Warning: ShapeUpgrade_Split2dCurve::Build(): Exception in Segment :";
234 Standard_Failure::Caught()->Print(cout); cout << endl;
236 theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);
240 theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);
242 myResultingCurves->SetValue (i, theNewCurve);
246 //=======================================================================
247 //function : GetCurves
249 //=======================================================================
251 const Handle(TColGeom2d_HArray1OfCurve)& ShapeUpgrade_SplitCurve2d::GetCurves() const
253 return myResultingCurves;