1 // Created on: 1993-07-19
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-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 <Standard_Stream.hxx>
18 #include <GeomTools_Curve2dSet.ixx>
19 #include <GeomTools.hxx>
20 #include <GeomTools_UndefinedTypeHandler.hxx>
22 #include <Geom2d_Circle.hxx>
23 #include <Geom2d_Line.hxx>
24 #include <Geom2d_Ellipse.hxx>
25 #include <Geom2d_Parabola.hxx>
26 #include <Geom2d_Hyperbola.hxx>
27 #include <Geom2d_BezierCurve.hxx>
28 #include <Geom2d_BSplineCurve.hxx>
29 #include <Geom2d_TrimmedCurve.hxx>
30 #include <Geom2d_OffsetCurve.hxx>
32 #include <gp_Lin2d.hxx>
33 #include <gp_Circ2d.hxx>
34 #include <gp_Elips2d.hxx>
35 #include <gp_Parab2d.hxx>
36 #include <gp_Hypr2d.hxx>
38 #include <TColStd_Array1OfReal.hxx>
39 #include <TColStd_Array1OfInteger.hxx>
40 #include <TColgp_Array1OfPnt2d.hxx>
41 #include <Standard_Failure.hxx>
42 #include <Standard_ErrorHandler.hxx>
43 #include <Message_ProgressSentry.hxx>
56 //=======================================================================
57 //function : GeomTools_Curve2dSet
59 //=======================================================================
61 GeomTools_Curve2dSet::GeomTools_Curve2dSet()
66 //=======================================================================
69 //=======================================================================
71 void GeomTools_Curve2dSet::Clear()
77 //=======================================================================
80 //=======================================================================
82 Standard_Integer GeomTools_Curve2dSet::Add(const Handle(Geom2d_Curve)& S)
88 //=======================================================================
91 //=======================================================================
93 Handle(Geom2d_Curve) GeomTools_Curve2dSet::Curve2d
94 (const Standard_Integer I)const
96 if (I <= 0 || I > myMap.Extent())
97 return Handle(Geom2d_Curve)();
99 return Handle(Geom2d_Curve)::DownCast(myMap(I));
103 //=======================================================================
106 //=======================================================================
108 Standard_Integer GeomTools_Curve2dSet::Index
109 (const Handle(Geom2d_Curve)& S)const
111 return myMap.FindIndex(S);
114 //=======================================================================
117 //=======================================================================
119 static void Print(const gp_Pnt2d P,
120 Standard_OStream& OS,
121 const Standard_Boolean compact)
124 if (!compact) OS << ",";
130 //=======================================================================
133 //=======================================================================
135 static void Print(const gp_Dir2d D,
136 Standard_OStream& OS,
137 const Standard_Boolean compact)
140 if (!compact) OS << ",";
146 //=======================================================================
149 //=======================================================================
151 static void Print(const Handle(Geom2d_Line)& L,
152 Standard_OStream& OS,
153 const Standard_Boolean compact)
160 gp_Lin2d C2d = L->Lin2d();
161 if (!compact) OS << "\n Origin :";
162 Print(C2d.Location(),OS,compact);
163 if (!compact) OS << "\n Axis :";
164 Print(C2d.Direction(),OS,compact);
165 if (!compact) OS << "\n";
169 //=======================================================================
172 //=======================================================================
174 static void Print(const Handle(Geom2d_Circle)& C,
175 Standard_OStream& OS,
176 const Standard_Boolean compact)
183 gp_Circ2d C2d = C->Circ2d();
184 if (!compact) OS << "\n Center :";
185 Print(C2d.Location(),OS,compact);
186 if (!compact) OS << "\n XAxis :";
187 Print(C2d.XAxis().Direction(),OS,compact);
188 if (!compact) OS << "\n YAxis :";
189 Print(C2d.YAxis().Direction(),OS,compact);
190 if (!compact) OS << "\n Radius :";
192 if (!compact) OS << "\n";
196 //=======================================================================
199 //=======================================================================
201 static void Print(const Handle(Geom2d_Ellipse)& E,
202 Standard_OStream& OS,
203 const Standard_Boolean compact)
206 OS << ELLIPSE << " ";
210 gp_Elips2d C2d = E->Elips2d();
211 if (!compact) OS << "\n Center :";
212 Print(C2d.Location(),OS,compact);
213 if (!compact) OS << "\n XAxis :";
214 Print(C2d.XAxis().Direction(),OS,compact);
215 if (!compact) OS << "\n YAxis :";
216 Print(C2d.YAxis().Direction(),OS,compact);
217 if (!compact) OS << "\n Radii :";
218 OS << C2d.MajorRadius();
219 if (!compact) OS << ",";
221 OS << C2d.MinorRadius();
222 if (!compact) OS << "\n";
226 //=======================================================================
229 //=======================================================================
231 static void Print(const Handle(Geom2d_Parabola)& P,
232 Standard_OStream& OS,
233 const Standard_Boolean compact)
236 OS << PARABOLA << " ";
240 gp_Parab2d C2d = P->Parab2d();
241 if (!compact) OS << "\n Center :";
242 Print(C2d.Location(),OS,compact);
243 if (!compact) OS << "\n XAxis :";
244 Print(C2d.Axis().XAxis().Direction(),OS,compact);
245 if (!compact) OS << "\n YAxis :";
246 Print(C2d.Axis().YAxis().Direction(),OS,compact);
247 if (!compact) OS << "\n Focal :";
249 if (!compact) OS << "\n";
253 //=======================================================================
256 //=======================================================================
258 static void Print(const Handle(Geom2d_Hyperbola)& H,
259 Standard_OStream& OS,
260 const Standard_Boolean compact)
263 OS << HYPERBOLA << " ";
267 gp_Hypr2d C2d = H->Hypr2d();
268 if (!compact) OS << "\n Center :";
269 Print(C2d.Location(),OS,compact);
270 if (!compact) OS << "\n XAxis :";
271 Print(C2d.XAxis().Direction(),OS,compact);
272 if (!compact) OS << "\n YAxis :";
273 Print(C2d.YAxis().Direction(),OS,compact);
274 if (!compact) OS << "\n Radii :";
275 OS << C2d.MajorRadius();
276 if (!compact) OS << ",";
278 OS << C2d.MinorRadius();
279 if (!compact) OS << "\n";
283 //=======================================================================
286 //=======================================================================
288 static void Print(const Handle(Geom2d_BezierCurve)& B,
289 Standard_OStream& OS,
290 const Standard_Boolean compact)
297 Standard_Boolean rational = B->IsRational();
299 OS << (rational ? 1 : 0) << " ";
306 Standard_Integer i,degree = B->Degree();
307 if (!compact) OS << "\n Degree :";
310 for (i = 1; i <= degree+1; i++) {
311 if (!compact) OS << "\n "<<setw(2)<<i<<" : ";
312 Print(B->Pole(i),OS,compact);
314 OS << " " << B->Weight(i);
318 if (!compact) OS << "\n";
322 //=======================================================================
325 //=======================================================================
327 static void Print(const Handle(Geom2d_BSplineCurve)& B,
328 Standard_OStream& OS,
329 const Standard_Boolean compact)
332 OS << BSPLINE << " ";
334 OS << "BSplineCurve";
337 Standard_Boolean rational = B->IsRational();
339 OS << (rational ? 1 : 0) << " ";
345 Standard_Boolean periodic = B->IsPeriodic();
347 OS << (periodic ? 1 : 0) << " ";
354 Standard_Integer i,degree,nbpoles,nbknots;
355 degree = B->Degree();
356 nbpoles = B->NbPoles();
357 nbknots = B->NbKnots();
358 if (!compact) OS << "\n Degree ";
361 if (!compact) OS << ",";
364 if (!compact) OS << " Poles,";
366 OS << nbknots << " ";
367 if (!compact) OS << " Knots";
369 if (!compact) OS << "Poles :\n";
370 for (i = 1; i <= nbpoles; i++) {
371 if (!compact) OS << "\n "<<setw(2)<<i<<" : ";
373 Print(B->Pole(i),OS,compact);
375 OS << " " << B->Weight(i);
379 if (!compact) OS << "Knots :\n";
380 for (i = 1; i <= nbknots; i++) {
381 if (!compact) OS << "\n "<<setw(2)<<i<<" : ";
383 OS << B->Knot(i) << " " << B->Multiplicity(i);
385 if (!compact) OS << "\n";
389 //=======================================================================
392 //=======================================================================
394 static void Print(const Handle(Geom2d_TrimmedCurve)& C,
395 Standard_OStream& OS,
396 const Standard_Boolean compact)
399 OS << TRIMMED << " ";
401 OS << "Trimmed curve\n";
402 if (!compact) OS << "Parameters : ";
403 OS << C->FirstParameter() << " " << C->LastParameter() << "\n";
404 if (!compact) OS << "Basis curve :\n";
405 GeomTools_Curve2dSet::PrintCurve2d(C->BasisCurve(),OS,compact);
408 //=======================================================================
411 //=======================================================================
413 static void Print(const Handle(Geom2d_OffsetCurve)& C,
414 Standard_OStream& OS,
415 const Standard_Boolean compact)
421 if (!compact) OS << "Offset : ";
422 OS << C->Offset() << "\n";
423 if (!compact) OS << "Basis curve :\n";
424 GeomTools_Curve2dSet::PrintCurve2d(C->BasisCurve(),OS,compact);
427 //=======================================================================
428 //function : PrintCurve2d
430 //=======================================================================
432 void GeomTools_Curve2dSet::PrintCurve2d(const Handle(Geom2d_Curve)& C,
433 Standard_OStream& OS,
434 const Standard_Boolean compact)
436 Handle(Standard_Type) TheType = C->DynamicType();
438 if ( TheType == STANDARD_TYPE(Geom2d_Circle)) {
439 Print(Handle(Geom2d_Circle)::DownCast(C),OS,compact);
441 else if ( TheType ==STANDARD_TYPE(Geom2d_Line)) {
442 Print(Handle(Geom2d_Line)::DownCast(C),OS,compact);
444 else if ( TheType == STANDARD_TYPE(Geom2d_Ellipse)) {
445 Print(Handle(Geom2d_Ellipse)::DownCast(C),OS,compact);
447 else if ( TheType == STANDARD_TYPE(Geom2d_Parabola)) {
448 Print(Handle(Geom2d_Parabola)::DownCast(C),OS,compact);
450 else if ( TheType == STANDARD_TYPE(Geom2d_Hyperbola)) {
451 Print(Handle(Geom2d_Hyperbola)::DownCast(C),OS,compact);
453 else if ( TheType == STANDARD_TYPE(Geom2d_BezierCurve)) {
454 Print(Handle(Geom2d_BezierCurve)::DownCast(C),OS,compact);
456 else if ( TheType == STANDARD_TYPE(Geom2d_BSplineCurve)) {
457 Print(Handle(Geom2d_BSplineCurve)::DownCast(C),OS,compact);
459 else if ( TheType == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
460 Print(Handle(Geom2d_TrimmedCurve)::DownCast(C),OS,compact);
462 else if ( TheType == STANDARD_TYPE(Geom2d_OffsetCurve)) {
463 Print(Handle(Geom2d_OffsetCurve)::DownCast(C),OS,compact);
466 GeomTools::GetUndefinedTypeHandler()->PrintCurve2d(C,OS,compact);
468 // OS << "****** UNKNOWN CURVE2d TYPE ******\n";
470 // cout << "****** UNKNOWN CURVE2d TYPE ******" << endl;
474 //=======================================================================
477 //=======================================================================
479 void GeomTools_Curve2dSet::Dump(Standard_OStream& OS)const
481 Standard_Integer i, nbsurf = myMap.Extent();
482 OS << "\n -------\n";
483 OS << "Dump of "<< nbsurf << " Curve2ds ";
484 OS << "\n -------\n\n";
486 for (i = 1; i <= nbsurf; i++) {
487 OS << setw(4) << i << " : ";
488 PrintCurve2d(Handle(Geom2d_Curve)::DownCast(myMap(i)),OS,Standard_False);
493 //=======================================================================
496 //=======================================================================
498 void GeomTools_Curve2dSet::Write(Standard_OStream& OS)const
500 std::streamsize prec = OS.precision(17);
502 Standard_Integer i, nbsurf = myMap.Extent();
503 OS << "Curve2ds "<< nbsurf << "\n";
505 Handle(Message_ProgressIndicator) progress = GetProgress();
506 Message_ProgressSentry PS(progress, "2D Curves", 0, nbsurf, 1);
508 for (i = 1; i <= nbsurf && PS.More(); i++, PS.Next()) {
509 if ( !myProgress.IsNull() )
511 PrintCurve2d(Handle(Geom2d_Curve)::DownCast(myMap(i)),OS,Standard_True);
518 //=======================================================================
519 //function : ReadPnt2d
521 //=======================================================================
523 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Pnt2d& P)
525 Standard_Real X=0.,Y=0.;
531 //=======================================================================
532 //function : ReadDir2d
534 //=======================================================================
536 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Dir2d& D)
538 Standard_Real X=0.,Y=0.;
545 //=======================================================================
546 //function : ReadCurve2d
548 //=======================================================================
550 static Standard_IStream& operator>>(Standard_IStream& IS,
551 Handle(Geom2d_Line)& L)
556 L = new Geom2d_Line(P,AX);
560 //=======================================================================
561 //function : ReadCurve2d
563 //=======================================================================
565 static Standard_IStream& operator>>(Standard_IStream& IS,
566 Handle(Geom2d_Circle)& C)
569 gp_Dir2d AX(1.,0.),AY(1.,0.);
571 IS >> P >> AX >> AY >> R;
572 C = new Geom2d_Circle(gp_Ax22d(P,AX,AY),R);
576 //=======================================================================
577 //function : ReadCurve2d
579 //=======================================================================
581 static Standard_IStream& operator>>(Standard_IStream& IS,
582 Handle(Geom2d_Ellipse)& E)
585 gp_Dir2d AX(1.,0.),AY(1.,0.);
586 Standard_Real R1=0.,R2=0.;
587 IS >> P >> AX >> AY >> R1 >> R2;
588 E = new Geom2d_Ellipse(gp_Ax22d(P,AX,AY),R1,R2);
592 //=======================================================================
593 //function : ReadCurve2d
595 //=======================================================================
597 static Standard_IStream& operator>>(Standard_IStream& IS,
598 Handle(Geom2d_Parabola)& C)
601 gp_Dir2d AX(1.,0.),AY(1.,0.);
603 IS >> P >> AX >> AY >> R1;
604 C = new Geom2d_Parabola(gp_Ax22d(P,AX,AY),R1);
608 //=======================================================================
609 //function : ReadCurve2d
611 //=======================================================================
613 static Standard_IStream& operator>>(Standard_IStream& IS,
614 Handle(Geom2d_Hyperbola)& H)
617 gp_Dir2d AX(1.,0.),AY(1.,0.);
618 Standard_Real R1=0.,R2=0.;
619 IS >> P >> AX >> AY >> R1 >> R2;
620 H = new Geom2d_Hyperbola(gp_Ax22d(P,AX,AY),R1,R2);
624 //=======================================================================
625 //function : ReadCurve2d
627 //=======================================================================
629 static Standard_IStream& operator>>(Standard_IStream& IS,
630 Handle(Geom2d_BezierCurve)& B)
632 Standard_Boolean rational=Standard_False;
636 Standard_Integer i=0,degree=0;
639 TColgp_Array1OfPnt2d poles(1,degree+1);
640 TColStd_Array1OfReal weights(1,degree+1);
642 for (i = 1; i <= degree+1; i++) {
649 B = new Geom2d_BezierCurve(poles,weights);
651 B = new Geom2d_BezierCurve(poles);
656 //=======================================================================
657 //function : ReadCurve2d
659 //=======================================================================
661 static Standard_IStream& operator>>(Standard_IStream& IS,
662 Handle(Geom2d_BSplineCurve)& B)
664 Standard_Boolean rational=Standard_False,periodic=Standard_False;
665 IS >> rational >> periodic;
668 Standard_Integer i=0,degree=0,nbpoles=0,nbknots=0;
669 IS >> degree >> nbpoles >> nbknots;
671 TColgp_Array1OfPnt2d poles(1,nbpoles);
672 TColStd_Array1OfReal weights(1,nbpoles);
674 for (i = 1; i <= nbpoles; i++) {
680 TColStd_Array1OfReal knots(1,nbknots);
681 TColStd_Array1OfInteger mults(1,nbknots);
683 for (i = 1; i <= nbknots; i++) {
684 IS >> knots(i) >> mults(i);
688 B = new Geom2d_BSplineCurve(poles,weights,knots,mults,degree,periodic);
690 B = new Geom2d_BSplineCurve(poles,knots,mults,degree,periodic);
695 //=======================================================================
696 //function : ReadCurve2d
698 //=======================================================================
700 static Standard_IStream& operator>>(Standard_IStream& IS,
701 Handle(Geom2d_TrimmedCurve)& C)
703 Standard_Real p1=0.,p2=0.;
705 Handle(Geom2d_Curve) BC;
706 GeomTools_Curve2dSet::ReadCurve2d(IS,BC);
707 C = new Geom2d_TrimmedCurve(BC,p1,p2);
711 //=======================================================================
712 //function : ReadCurve2d
714 //=======================================================================
716 static Standard_IStream& operator>>(Standard_IStream& IS,
717 Handle(Geom2d_OffsetCurve)& C)
721 Handle(Geom2d_Curve) BC;
722 GeomTools_Curve2dSet::ReadCurve2d(IS,BC);
723 C = new Geom2d_OffsetCurve(BC,p);
727 //=======================================================================
728 //function : ReadCurve2d
730 //=======================================================================
732 Standard_IStream& GeomTools_Curve2dSet::ReadCurve2d(Standard_IStream& IS,
733 Handle(Geom2d_Curve)& C)
735 Standard_Integer ctype;
744 Handle(Geom2d_Line) CC;
752 Handle(Geom2d_Circle) CC;
760 Handle(Geom2d_Ellipse) CC;
768 Handle(Geom2d_Parabola) CC;
776 Handle(Geom2d_Hyperbola) CC;
784 Handle(Geom2d_BezierCurve) CC;
792 Handle(Geom2d_BSplineCurve) CC;
800 Handle(Geom2d_TrimmedCurve) CC;
808 Handle(Geom2d_OffsetCurve) CC;
816 Handle(Geom2d_Curve) CC;
817 GeomTools::GetUndefinedTypeHandler()->ReadCurve2d(ctype,IS,CC);
823 catch(Standard_Failure) {
825 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
826 cout <<"EXCEPTION in GeomTools_Curve2dSet::ReadCurve2d(..)!!!" << endl;
827 cout << anExc << endl;
834 //=======================================================================
837 //=======================================================================
839 void GeomTools_Curve2dSet::Read(Standard_IStream& IS)
843 if (strcmp(buffer,"Curve2ds")) {
844 cout << "Not a Curve2d table"<<endl;
848 Handle(Geom2d_Curve) C;
849 Standard_Integer i, nbcurve;
852 Handle(Message_ProgressIndicator) progress = GetProgress();
853 Message_ProgressSentry PS(progress, "2D Curves", 0, nbcurve, 1);
855 for (i = 1; i <= nbcurve && PS.More(); i++, PS.Next()) {
856 if ( !myProgress.IsNull() )
859 GeomTools_Curve2dSet::ReadCurve2d(IS,C);
864 //=======================================================================
865 //function : GetProgress
867 //=======================================================================
869 Handle(Message_ProgressIndicator) GeomTools_Curve2dSet::GetProgress() const
874 //=======================================================================
875 //function : SetProgress
877 //=======================================================================
879 void GeomTools_Curve2dSet::SetProgress(const Handle(Message_ProgressIndicator)& PR)