1 // Created on: 1998-03-12
2 // Created by: Roman LYGIN
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <ShapeUpgrade_SplitCurve3d.ixx>
18 #include <Geom_BSplineCurve.hxx>
19 #include <Precision.hxx>
20 #include <ShapeUpgrade.hxx>
21 #include <TColStd_ListIteratorOfListOfReal.hxx>
22 #include <TColStd_HSequenceOfReal.hxx>
23 #include <TColGeom_HArray1OfCurve.hxx>
24 #include <Geom_TrimmedCurve.hxx>
25 #include <Geom_OffsetCurve.hxx>
26 #include <Standard_ErrorHandler.hxx>
27 #include <Standard_Failure.hxx>
28 #include <ShapeExtend.hxx>
29 #include <Precision.hxx>
30 #include <Geom_BezierCurve.hxx>
31 #include <ShapeAnalysis_Curve.hxx>
32 #include <Geom_Curve.hxx>
34 //=======================================================================
35 //function : ShapeUpgrade_SplitCurve3d
37 //=======================================================================
39 ShapeUpgrade_SplitCurve3d::ShapeUpgrade_SplitCurve3d()
43 //=======================================================================
46 //=======================================================================
48 void ShapeUpgrade_SplitCurve3d::Init(const Handle(Geom_Curve)& C)
50 Init (C, C->FirstParameter(), C->LastParameter());
53 //=======================================================================
56 //=======================================================================
58 void ShapeUpgrade_SplitCurve3d::Init(const Handle(Geom_Curve)& C,
59 const Standard_Real First,
60 const Standard_Real Last)
62 // if (ShapeUpgrade::Debug()) cout << "SplitCurve3d::Init"<<endl;
63 Handle(Geom_Curve) CopyOfC = Handle(Geom_Curve)::DownCast(C->Copy());
65 Standard_Real precision = Precision::PConfusion();
66 Standard_Real firstPar = First;
67 Standard_Real lastPar = Last;
68 Handle (Geom_Curve) aCurve = myCurve;
69 if(aCurve->IsKind(STANDARD_TYPE(Geom_TrimmedCurve)))
70 aCurve=Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
71 // 15.11.2002 PTV OCC966
72 if(!ShapeAnalysis_Curve::IsPeriodic(C)) {
73 Standard_Real fP = aCurve->FirstParameter();
74 Standard_Real lP = aCurve->LastParameter();
75 if(Abs(firstPar-fP) < precision)
77 if(Abs(lastPar-lP) < precision)
81 cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
87 cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
91 if( (lastPar-firstPar) < precision)
92 lastPar=firstPar+2*precision;
95 ShapeUpgrade_SplitCurve::Init (firstPar, lastPar);
97 // first, we make a copy of C to prevent modification:
98 // if (ShapeUpgrade::Debug()) cout << ". copy of the curve"<<endl;
103 //=======================================================================
106 //=======================================================================
108 void ShapeUpgrade_SplitCurve3d::Build(const Standard_Boolean Segment)
110 // if (ShapeUpgrade::Debug()) cout<<"ShapeUpgrade_SplitCurve3d::Build"<<endl;
111 Standard_Real First = mySplitValues->Value(1);
112 Standard_Real Last = mySplitValues->Value(mySplitValues->Length());
114 if (mySplitValues->Length() >2)
115 myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
116 if (myCurve->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
117 Handle(Geom_TrimmedCurve) tmp = Handle(Geom_TrimmedCurve)::DownCast (myCurve);
118 Handle(Geom_Curve) BasCurve = tmp->BasisCurve();
119 ShapeUpgrade_SplitCurve3d spc;
120 spc.Init(BasCurve,First,Last);
121 spc.SetSplitValues(mySplitValues);
123 myNbCurves = spc.GetCurves()->Length();
124 myResultingCurves=new TColGeom_HArray1OfCurve (1,myNbCurves);
125 if(myNbCurves == 1) {
126 Handle(Geom_TrimmedCurve) NewTrimCurve =
127 new Geom_TrimmedCurve(spc.GetCurves()->Value(1),First,Last);
128 myResultingCurves->SetValue(1,NewTrimCurve);
131 myResultingCurves = spc.GetCurves();
132 myStatus |= spc.myStatus;
135 else if (myCurve->IsKind(STANDARD_TYPE(Geom_OffsetCurve))) {
136 Handle(Geom_OffsetCurve) tmp = Handle(Geom_OffsetCurve)::DownCast (myCurve);
137 Handle(Geom_Curve) BasCurve = tmp->BasisCurve();
138 Standard_Real Offset = tmp->Offset();
139 gp_Dir Direct = tmp->Direction();
140 ShapeUpgrade_SplitCurve3d spc;
141 spc.Init(BasCurve,First,Last);
142 spc.SetSplitValues(mySplitValues);
144 myNbCurves = spc.GetCurves()->Length();
145 myResultingCurves = new TColGeom_HArray1OfCurve (1,myNbCurves);
146 for(Standard_Integer i = 1; i <= myNbCurves; i++) {
147 Handle(Geom_OffsetCurve) NewOffsetCurve =
148 new Geom_OffsetCurve(spc.GetCurves()->Value(i),Offset,Direct);
149 myResultingCurves->SetValue(i,NewOffsetCurve);
151 myStatus |= spc.myStatus;
155 //pdn fix on BuildCurve 3d
156 // 15.11.2002 PTV OCC966
157 if(!ShapeAnalysis_Curve::IsPeriodic(myCurve)) {
158 //pdn exceptons only on non periodic curves
159 Standard_Real precision = Precision::PConfusion();
160 Standard_Real firstPar = myCurve->FirstParameter();
161 Standard_Real lastPar = myCurve->LastParameter();
162 if(Abs(First-firstPar) < precision)
164 if(Abs(Last-lastPar) < precision)
166 if(First < firstPar){
168 cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
171 mySplitValues->SetValue(1,First);
175 cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
178 mySplitValues->SetValue(mySplitValues->Length(),Last);
182 myNbCurves = mySplitValues->Length() -1;
183 myResultingCurves = new TColGeom_HArray1OfCurve (1, myNbCurves);
184 if (myNbCurves == 1) {
185 Standard_Boolean filled = Standard_True;
186 if ( Abs ( myCurve->FirstParameter() - First ) < Precision::PConfusion() &&
187 Abs ( myCurve->LastParameter() - Last ) < Precision::PConfusion() )
188 myResultingCurves->SetValue(1,myCurve);
190 else if ( ! Segment || (!myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve)) &&
191 !myCurve->IsKind (STANDARD_TYPE (Geom_BezierCurve))) || ! Status ( ShapeExtend_DONE2 ) ) {
192 /* if(myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve)) ||
193 myCurve->IsKind (STANDARD_TYPE (Geom_BezierCurve) )) {
194 Handle(Geom_Curve) theNewCurve = Handle(Geom_Curve)::DownCast(myCurve->Copy());
197 if (myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve)))
198 Handle(Geom_BSplineCurve)::DownCast(theNewCurve)->Segment (First, Last);
199 else if (myCurve->IsKind (STANDARD_TYPE (Geom_BezierCurve)))
200 Handle(Geom_BezierCurve)::DownCast(theNewCurve)->Segment (First, Last);
202 catch (Standard_Failure) {
204 cout << "Warning: ShapeUpgrade_Split3dCurve::Build(): Exception in Segment :";
205 Standard_Failure::Caught()->Print(cout); cout << endl;
207 theNewCurve = new Geom_TrimmedCurve(Handle(Geom_Curve)::DownCast(myCurve->Copy()),First,Last);
209 myResultingCurves->SetValue (1, theNewCurve);
212 Handle(Geom_TrimmedCurve) theNewCurve = new Geom_TrimmedCurve(Handle(Geom_Curve)::DownCast(myCurve->Copy()),First,Last);
213 myResultingCurves->SetValue (1, theNewCurve);
216 else filled = Standard_False;
217 if ( filled ) return;
219 if (myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve))) {
220 Handle(Geom_BSplineCurve) BsCurve = Handle(Geom_BSplineCurve)::DownCast(myCurve->Copy());
221 Standard_Integer FirstInd =BsCurve->FirstUKnotIndex(),
222 LastInd = BsCurve->LastUKnotIndex();
223 Standard_Integer j = FirstInd;
224 for(Standard_Integer ii =1; ii <= mySplitValues->Length(); ii++) {
225 Standard_Real spval = mySplitValues->Value(ii);
226 for(; j <=LastInd;j++) {
227 if( spval > BsCurve->Knot(j) + Precision::PConfusion()) continue;
228 if( spval < BsCurve->Knot(j) - Precision::PConfusion()) break;
229 mySplitValues->SetValue(ii,BsCurve->Knot(j));
231 if(j == LastInd) break;
235 for (Standard_Integer i = 1; i <= myNbCurves; i++) {
236 // skl : in the next block I change "First","Last" to "Firstt","Lastt"
237 Standard_Real Firstt = mySplitValues->Value(i), Lastt = mySplitValues->Value(i+1);
238 Handle(Geom_Curve) theNewCurve;
240 if (myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve)) ||
241 myCurve->IsKind (STANDARD_TYPE (Geom_BezierCurve))) {
242 theNewCurve = Handle(Geom_Curve)::DownCast(myCurve->Copy());
245 if (myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve)))
246 Handle(Geom_BSplineCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
247 else if (myCurve->IsKind (STANDARD_TYPE (Geom_BezierCurve)))
248 Handle(Geom_BezierCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
249 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
251 catch (Standard_Failure) {
253 cout << "Warning: ShapeUpgrade_Split3dCurve::Build(): Exception in Segment :";
254 Standard_Failure::Caught()->Print(cout); cout << endl;
256 theNewCurve = new Geom_TrimmedCurve(Handle(Geom_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);
259 else theNewCurve = new Geom_TrimmedCurve(Handle(Geom_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);
261 myResultingCurves->SetValue (i, theNewCurve);
266 //=======================================================================
267 //function : GetCurves
269 //=======================================================================
271 const Handle(TColGeom_HArray1OfCurve)& ShapeUpgrade_SplitCurve3d::GetCurves() const
273 return myResultingCurves;