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);
507 for (i = 1; i <= nbsurf && PS.More(); i++, PS.Next()) {
508 PrintCurve2d(Handle(Geom2d_Curve)::DownCast(myMap(i)),OS,Standard_True);
514 //=======================================================================
515 //function : ReadPnt2d
517 //=======================================================================
519 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Pnt2d& P)
521 Standard_Real X=0.,Y=0.;
522 GeomTools::GetReal(IS, X);
523 GeomTools::GetReal(IS, Y);
528 //=======================================================================
529 //function : ReadDir2d
531 //=======================================================================
533 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Dir2d& D)
535 Standard_Real X=0.,Y=0.;
536 GeomTools::GetReal(IS, X);
537 GeomTools::GetReal(IS, Y);
543 //=======================================================================
544 //function : ReadCurve2d
546 //=======================================================================
548 static Standard_IStream& operator>>(Standard_IStream& IS,
549 Handle(Geom2d_Line)& L)
554 L = new Geom2d_Line(P,AX);
558 //=======================================================================
559 //function : ReadCurve2d
561 //=======================================================================
563 static Standard_IStream& operator>>(Standard_IStream& IS,
564 Handle(Geom2d_Circle)& C)
567 gp_Dir2d AX(1.,0.),AY(1.,0.);
570 GeomTools::GetReal(IS, R);
571 C = new Geom2d_Circle(gp_Ax22d(P,AX,AY),R);
575 //=======================================================================
576 //function : ReadCurve2d
578 //=======================================================================
580 static Standard_IStream& operator>>(Standard_IStream& IS,
581 Handle(Geom2d_Ellipse)& E)
584 gp_Dir2d AX(1.,0.),AY(1.,0.);
585 Standard_Real R1=0.,R2=0.;
587 GeomTools::GetReal(IS, R1);
588 GeomTools::GetReal(IS, R2);
589 E = new Geom2d_Ellipse(gp_Ax22d(P,AX,AY),R1,R2);
593 //=======================================================================
594 //function : ReadCurve2d
596 //=======================================================================
598 static Standard_IStream& operator>>(Standard_IStream& IS,
599 Handle(Geom2d_Parabola)& C)
602 gp_Dir2d AX(1.,0.),AY(1.,0.);
605 GeomTools::GetReal(IS, R1);
606 C = new Geom2d_Parabola(gp_Ax22d(P,AX,AY),R1);
610 //=======================================================================
611 //function : ReadCurve2d
613 //=======================================================================
615 static Standard_IStream& operator>>(Standard_IStream& IS,
616 Handle(Geom2d_Hyperbola)& H)
619 gp_Dir2d AX(1.,0.),AY(1.,0.);
620 Standard_Real R1=0.,R2=0.;
622 GeomTools::GetReal(IS, R1);
623 GeomTools::GetReal(IS, R2);
624 H = new Geom2d_Hyperbola(gp_Ax22d(P,AX,AY),R1,R2);
628 //=======================================================================
629 //function : ReadCurve2d
631 //=======================================================================
633 static Standard_IStream& operator>>(Standard_IStream& IS,
634 Handle(Geom2d_BezierCurve)& B)
636 Standard_Boolean rational=Standard_False;
640 Standard_Integer i=0,degree=0;
643 TColgp_Array1OfPnt2d poles(1,degree+1);
644 TColStd_Array1OfReal weights(1,degree+1);
646 for (i = 1; i <= degree+1; i++) {
649 GeomTools::GetReal(IS, weights(i));
653 B = new Geom2d_BezierCurve(poles,weights);
655 B = new Geom2d_BezierCurve(poles);
660 //=======================================================================
661 //function : ReadCurve2d
663 //=======================================================================
665 static Standard_IStream& operator>>(Standard_IStream& IS,
666 Handle(Geom2d_BSplineCurve)& B)
668 Standard_Boolean rational=Standard_False,periodic=Standard_False;
669 IS >> rational >> periodic;
672 Standard_Integer i=0,degree=0,nbpoles=0,nbknots=0;
673 IS >> degree >> nbpoles >> nbknots;
675 TColgp_Array1OfPnt2d poles(1,nbpoles);
676 TColStd_Array1OfReal weights(1,nbpoles);
678 for (i = 1; i <= nbpoles; i++) {
681 GeomTools::GetReal(IS, weights(i));
684 TColStd_Array1OfReal knots(1,nbknots);
685 TColStd_Array1OfInteger mults(1,nbknots);
687 for (i = 1; i <= nbknots; i++) {
688 GeomTools::GetReal(IS, knots(i));
693 B = new Geom2d_BSplineCurve(poles,weights,knots,mults,degree,periodic);
695 B = new Geom2d_BSplineCurve(poles,knots,mults,degree,periodic);
700 //=======================================================================
701 //function : ReadCurve2d
703 //=======================================================================
705 static Standard_IStream& operator>>(Standard_IStream& IS,
706 Handle(Geom2d_TrimmedCurve)& C)
708 Standard_Real p1=0.,p2=0.;
709 GeomTools::GetReal(IS, p1);
710 GeomTools::GetReal(IS, p2);
711 Handle(Geom2d_Curve) BC;
712 GeomTools_Curve2dSet::ReadCurve2d(IS,BC);
713 C = new Geom2d_TrimmedCurve(BC,p1,p2);
717 //=======================================================================
718 //function : ReadCurve2d
720 //=======================================================================
722 static Standard_IStream& operator>>(Standard_IStream& IS,
723 Handle(Geom2d_OffsetCurve)& C)
726 GeomTools::GetReal(IS, p);
727 Handle(Geom2d_Curve) BC;
728 GeomTools_Curve2dSet::ReadCurve2d(IS,BC);
729 C = new Geom2d_OffsetCurve(BC,p);
733 //=======================================================================
734 //function : ReadCurve2d
736 //=======================================================================
738 Standard_IStream& GeomTools_Curve2dSet::ReadCurve2d(Standard_IStream& IS,
739 Handle(Geom2d_Curve)& C)
741 Standard_Integer ctype;
750 Handle(Geom2d_Line) CC;
758 Handle(Geom2d_Circle) CC;
766 Handle(Geom2d_Ellipse) CC;
774 Handle(Geom2d_Parabola) CC;
782 Handle(Geom2d_Hyperbola) CC;
790 Handle(Geom2d_BezierCurve) CC;
798 Handle(Geom2d_BSplineCurve) CC;
806 Handle(Geom2d_TrimmedCurve) CC;
814 Handle(Geom2d_OffsetCurve) CC;
822 Handle(Geom2d_Curve) CC;
823 GeomTools::GetUndefinedTypeHandler()->ReadCurve2d(ctype,IS,CC);
829 catch(Standard_Failure) {
831 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
832 cout <<"EXCEPTION in GeomTools_Curve2dSet::ReadCurve2d(..)!!!" << endl;
833 cout << anExc << endl;
840 //=======================================================================
843 //=======================================================================
845 void GeomTools_Curve2dSet::Read(Standard_IStream& IS)
849 if (strcmp(buffer,"Curve2ds")) {
850 cout << "Not a Curve2d table"<<endl;
854 Handle(Geom2d_Curve) C;
855 Standard_Integer i, nbcurve;
858 Handle(Message_ProgressIndicator) progress = GetProgress();
859 Message_ProgressSentry PS(progress, "2D Curves", 0, nbcurve, 1);
860 for (i = 1; i <= nbcurve && PS.More(); i++, PS.Next()) {
861 GeomTools_Curve2dSet::ReadCurve2d(IS,C);
866 //=======================================================================
867 //function : GetProgress
869 //=======================================================================
871 Handle(Message_ProgressIndicator) GeomTools_Curve2dSet::GetProgress() const
876 //=======================================================================
877 //function : SetProgress
879 //=======================================================================
881 void GeomTools_Curve2dSet::SetProgress(const Handle(Message_ProgressIndicator)& PR)