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.
18 #include <Geom2d_BezierCurve.hxx>
19 #include <Geom2d_BSplineCurve.hxx>
20 #include <Geom2d_Circle.hxx>
21 #include <Geom2d_Curve.hxx>
22 #include <Geom2d_Ellipse.hxx>
23 #include <Geom2d_Hyperbola.hxx>
24 #include <Geom2d_Line.hxx>
25 #include <Geom2d_OffsetCurve.hxx>
26 #include <Geom2d_Parabola.hxx>
27 #include <Geom2d_TrimmedCurve.hxx>
28 #include <GeomTools.hxx>
29 #include <GeomTools_Curve2dSet.hxx>
30 #include <GeomTools_UndefinedTypeHandler.hxx>
31 #include <gp_Circ2d.hxx>
32 #include <gp_Elips2d.hxx>
33 #include <gp_Hypr2d.hxx>
34 #include <gp_Lin2d.hxx>
35 #include <gp_Parab2d.hxx>
36 #include <Message_ProgressIndicator.hxx>
37 #include <Message_ProgressSentry.hxx>
38 #include <Standard_ErrorHandler.hxx>
39 #include <Standard_Failure.hxx>
40 #include <Standard_OutOfRange.hxx>
41 #include <Standard_Stream.hxx>
42 #include <TColgp_Array1OfPnt2d.hxx>
43 #include <TColStd_Array1OfInteger.hxx>
44 #include <TColStd_Array1OfReal.hxx>
57 //=======================================================================
58 //function : GeomTools_Curve2dSet
60 //=======================================================================
62 GeomTools_Curve2dSet::GeomTools_Curve2dSet()
67 //=======================================================================
70 //=======================================================================
72 void GeomTools_Curve2dSet::Clear()
78 //=======================================================================
81 //=======================================================================
83 Standard_Integer GeomTools_Curve2dSet::Add(const Handle(Geom2d_Curve)& S)
89 //=======================================================================
92 //=======================================================================
94 Handle(Geom2d_Curve) GeomTools_Curve2dSet::Curve2d
95 (const Standard_Integer I)const
97 if (I <= 0 || I > myMap.Extent())
98 return Handle(Geom2d_Curve)();
100 return Handle(Geom2d_Curve)::DownCast(myMap(I));
104 //=======================================================================
107 //=======================================================================
109 Standard_Integer GeomTools_Curve2dSet::Index
110 (const Handle(Geom2d_Curve)& S)const
112 return myMap.FindIndex(S);
115 //=======================================================================
118 //=======================================================================
120 static void Print(const gp_Pnt2d P,
121 Standard_OStream& OS,
122 const Standard_Boolean compact)
125 if (!compact) OS << ",";
131 //=======================================================================
134 //=======================================================================
136 static void Print(const gp_Dir2d D,
137 Standard_OStream& OS,
138 const Standard_Boolean compact)
141 if (!compact) OS << ",";
147 //=======================================================================
150 //=======================================================================
152 static void Print(const Handle(Geom2d_Line)& L,
153 Standard_OStream& OS,
154 const Standard_Boolean compact)
161 gp_Lin2d C2d = L->Lin2d();
162 if (!compact) OS << "\n Origin :";
163 Print(C2d.Location(),OS,compact);
164 if (!compact) OS << "\n Axis :";
165 Print(C2d.Direction(),OS,compact);
166 if (!compact) OS << "\n";
170 //=======================================================================
173 //=======================================================================
175 static void Print(const Handle(Geom2d_Circle)& C,
176 Standard_OStream& OS,
177 const Standard_Boolean compact)
184 gp_Circ2d C2d = C->Circ2d();
185 if (!compact) OS << "\n Center :";
186 Print(C2d.Location(),OS,compact);
187 if (!compact) OS << "\n XAxis :";
188 Print(C2d.XAxis().Direction(),OS,compact);
189 if (!compact) OS << "\n YAxis :";
190 Print(C2d.YAxis().Direction(),OS,compact);
191 if (!compact) OS << "\n Radius :";
193 if (!compact) OS << "\n";
197 //=======================================================================
200 //=======================================================================
202 static void Print(const Handle(Geom2d_Ellipse)& E,
203 Standard_OStream& OS,
204 const Standard_Boolean compact)
207 OS << ELLIPSE << " ";
211 gp_Elips2d C2d = E->Elips2d();
212 if (!compact) OS << "\n Center :";
213 Print(C2d.Location(),OS,compact);
214 if (!compact) OS << "\n XAxis :";
215 Print(C2d.XAxis().Direction(),OS,compact);
216 if (!compact) OS << "\n YAxis :";
217 Print(C2d.YAxis().Direction(),OS,compact);
218 if (!compact) OS << "\n Radii :";
219 OS << C2d.MajorRadius();
220 if (!compact) OS << ",";
222 OS << C2d.MinorRadius();
223 if (!compact) OS << "\n";
227 //=======================================================================
230 //=======================================================================
232 static void Print(const Handle(Geom2d_Parabola)& P,
233 Standard_OStream& OS,
234 const Standard_Boolean compact)
237 OS << PARABOLA << " ";
241 gp_Parab2d C2d = P->Parab2d();
242 if (!compact) OS << "\n Center :";
243 Print(C2d.Location(),OS,compact);
244 if (!compact) OS << "\n XAxis :";
245 Print(C2d.Axis().XAxis().Direction(),OS,compact);
246 if (!compact) OS << "\n YAxis :";
247 Print(C2d.Axis().YAxis().Direction(),OS,compact);
248 if (!compact) OS << "\n Focal :";
250 if (!compact) OS << "\n";
254 //=======================================================================
257 //=======================================================================
259 static void Print(const Handle(Geom2d_Hyperbola)& H,
260 Standard_OStream& OS,
261 const Standard_Boolean compact)
264 OS << HYPERBOLA << " ";
268 gp_Hypr2d C2d = H->Hypr2d();
269 if (!compact) OS << "\n Center :";
270 Print(C2d.Location(),OS,compact);
271 if (!compact) OS << "\n XAxis :";
272 Print(C2d.XAxis().Direction(),OS,compact);
273 if (!compact) OS << "\n YAxis :";
274 Print(C2d.YAxis().Direction(),OS,compact);
275 if (!compact) OS << "\n Radii :";
276 OS << C2d.MajorRadius();
277 if (!compact) OS << ",";
279 OS << C2d.MinorRadius();
280 if (!compact) OS << "\n";
284 //=======================================================================
287 //=======================================================================
289 static void Print(const Handle(Geom2d_BezierCurve)& B,
290 Standard_OStream& OS,
291 const Standard_Boolean compact)
298 Standard_Boolean rational = B->IsRational();
300 OS << (rational ? 1 : 0) << " ";
307 Standard_Integer i,degree = B->Degree();
308 if (!compact) OS << "\n Degree :";
311 for (i = 1; i <= degree+1; i++) {
312 if (!compact) OS << "\n "<<setw(2)<<i<<" : ";
313 Print(B->Pole(i),OS,compact);
315 OS << " " << B->Weight(i);
319 if (!compact) OS << "\n";
323 //=======================================================================
326 //=======================================================================
328 static void Print(const Handle(Geom2d_BSplineCurve)& B,
329 Standard_OStream& OS,
330 const Standard_Boolean compact)
333 OS << BSPLINE << " ";
335 OS << "BSplineCurve";
338 Standard_Boolean rational = B->IsRational();
340 OS << (rational ? 1 : 0) << " ";
346 Standard_Boolean periodic = B->IsPeriodic();
348 OS << (periodic ? 1 : 0) << " ";
355 Standard_Integer i,degree,nbpoles,nbknots;
356 degree = B->Degree();
357 nbpoles = B->NbPoles();
358 nbknots = B->NbKnots();
359 if (!compact) OS << "\n Degree ";
362 if (!compact) OS << ",";
365 if (!compact) OS << " Poles,";
367 OS << nbknots << " ";
368 if (!compact) OS << " Knots";
370 if (!compact) OS << "Poles :\n";
371 for (i = 1; i <= nbpoles; i++) {
372 if (!compact) OS << "\n "<<setw(2)<<i<<" : ";
374 Print(B->Pole(i),OS,compact);
376 OS << " " << B->Weight(i);
380 if (!compact) OS << "Knots :\n";
381 for (i = 1; i <= nbknots; i++) {
382 if (!compact) OS << "\n "<<setw(2)<<i<<" : ";
384 OS << B->Knot(i) << " " << B->Multiplicity(i);
386 if (!compact) OS << "\n";
390 //=======================================================================
393 //=======================================================================
395 static void Print(const Handle(Geom2d_TrimmedCurve)& C,
396 Standard_OStream& OS,
397 const Standard_Boolean compact)
400 OS << TRIMMED << " ";
402 OS << "Trimmed curve\n";
403 if (!compact) OS << "Parameters : ";
404 OS << C->FirstParameter() << " " << C->LastParameter() << "\n";
405 if (!compact) OS << "Basis curve :\n";
406 GeomTools_Curve2dSet::PrintCurve2d(C->BasisCurve(),OS,compact);
409 //=======================================================================
412 //=======================================================================
414 static void Print(const Handle(Geom2d_OffsetCurve)& C,
415 Standard_OStream& OS,
416 const Standard_Boolean compact)
422 if (!compact) OS << "Offset : ";
423 OS << C->Offset() << "\n";
424 if (!compact) OS << "Basis curve :\n";
425 GeomTools_Curve2dSet::PrintCurve2d(C->BasisCurve(),OS,compact);
428 //=======================================================================
429 //function : PrintCurve2d
431 //=======================================================================
433 void GeomTools_Curve2dSet::PrintCurve2d(const Handle(Geom2d_Curve)& C,
434 Standard_OStream& OS,
435 const Standard_Boolean compact)
437 Handle(Standard_Type) TheType = C->DynamicType();
439 if ( TheType == STANDARD_TYPE(Geom2d_Circle)) {
440 Print(Handle(Geom2d_Circle)::DownCast(C),OS,compact);
442 else if ( TheType ==STANDARD_TYPE(Geom2d_Line)) {
443 Print(Handle(Geom2d_Line)::DownCast(C),OS,compact);
445 else if ( TheType == STANDARD_TYPE(Geom2d_Ellipse)) {
446 Print(Handle(Geom2d_Ellipse)::DownCast(C),OS,compact);
448 else if ( TheType == STANDARD_TYPE(Geom2d_Parabola)) {
449 Print(Handle(Geom2d_Parabola)::DownCast(C),OS,compact);
451 else if ( TheType == STANDARD_TYPE(Geom2d_Hyperbola)) {
452 Print(Handle(Geom2d_Hyperbola)::DownCast(C),OS,compact);
454 else if ( TheType == STANDARD_TYPE(Geom2d_BezierCurve)) {
455 Print(Handle(Geom2d_BezierCurve)::DownCast(C),OS,compact);
457 else if ( TheType == STANDARD_TYPE(Geom2d_BSplineCurve)) {
458 Print(Handle(Geom2d_BSplineCurve)::DownCast(C),OS,compact);
460 else if ( TheType == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
461 Print(Handle(Geom2d_TrimmedCurve)::DownCast(C),OS,compact);
463 else if ( TheType == STANDARD_TYPE(Geom2d_OffsetCurve)) {
464 Print(Handle(Geom2d_OffsetCurve)::DownCast(C),OS,compact);
467 GeomTools::GetUndefinedTypeHandler()->PrintCurve2d(C,OS,compact);
469 // OS << "****** UNKNOWN CURVE2d TYPE ******\n";
471 // cout << "****** UNKNOWN CURVE2d TYPE ******" << endl;
475 //=======================================================================
478 //=======================================================================
480 void GeomTools_Curve2dSet::Dump(Standard_OStream& OS)const
482 Standard_Integer i, nbsurf = myMap.Extent();
483 OS << "\n -------\n";
484 OS << "Dump of "<< nbsurf << " Curve2ds ";
485 OS << "\n -------\n\n";
487 for (i = 1; i <= nbsurf; i++) {
488 OS << setw(4) << i << " : ";
489 PrintCurve2d(Handle(Geom2d_Curve)::DownCast(myMap(i)),OS,Standard_False);
494 //=======================================================================
497 //=======================================================================
499 void GeomTools_Curve2dSet::Write(Standard_OStream& OS)const
501 std::streamsize prec = OS.precision(17);
503 Standard_Integer i, nbsurf = myMap.Extent();
504 OS << "Curve2ds "<< nbsurf << "\n";
506 Handle(Message_ProgressIndicator) progress = GetProgress();
507 Message_ProgressSentry PS(progress, "2D Curves", 0, nbsurf, 1);
508 for (i = 1; i <= nbsurf && PS.More(); i++, PS.Next()) {
509 PrintCurve2d(Handle(Geom2d_Curve)::DownCast(myMap(i)),OS,Standard_True);
515 //=======================================================================
516 //function : ReadPnt2d
518 //=======================================================================
520 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Pnt2d& P)
522 Standard_Real X=0.,Y=0.;
523 GeomTools::GetReal(IS, X);
524 GeomTools::GetReal(IS, Y);
529 //=======================================================================
530 //function : ReadDir2d
532 //=======================================================================
534 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Dir2d& D)
536 Standard_Real X=0.,Y=0.;
537 GeomTools::GetReal(IS, X);
538 GeomTools::GetReal(IS, Y);
544 //=======================================================================
545 //function : ReadCurve2d
547 //=======================================================================
549 static Standard_IStream& operator>>(Standard_IStream& IS,
550 Handle(Geom2d_Line)& L)
555 L = new Geom2d_Line(P,AX);
559 //=======================================================================
560 //function : ReadCurve2d
562 //=======================================================================
564 static Standard_IStream& operator>>(Standard_IStream& IS,
565 Handle(Geom2d_Circle)& C)
568 gp_Dir2d AX(1.,0.),AY(1.,0.);
571 GeomTools::GetReal(IS, 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.;
588 GeomTools::GetReal(IS, R1);
589 GeomTools::GetReal(IS, R2);
590 E = new Geom2d_Ellipse(gp_Ax22d(P,AX,AY),R1,R2);
594 //=======================================================================
595 //function : ReadCurve2d
597 //=======================================================================
599 static Standard_IStream& operator>>(Standard_IStream& IS,
600 Handle(Geom2d_Parabola)& C)
603 gp_Dir2d AX(1.,0.),AY(1.,0.);
606 GeomTools::GetReal(IS, R1);
607 C = new Geom2d_Parabola(gp_Ax22d(P,AX,AY),R1);
611 //=======================================================================
612 //function : ReadCurve2d
614 //=======================================================================
616 static Standard_IStream& operator>>(Standard_IStream& IS,
617 Handle(Geom2d_Hyperbola)& H)
620 gp_Dir2d AX(1.,0.),AY(1.,0.);
621 Standard_Real R1=0.,R2=0.;
623 GeomTools::GetReal(IS, R1);
624 GeomTools::GetReal(IS, R2);
625 H = new Geom2d_Hyperbola(gp_Ax22d(P,AX,AY),R1,R2);
629 //=======================================================================
630 //function : ReadCurve2d
632 //=======================================================================
634 static Standard_IStream& operator>>(Standard_IStream& IS,
635 Handle(Geom2d_BezierCurve)& B)
637 Standard_Boolean rational=Standard_False;
641 Standard_Integer i=0,degree=0;
644 TColgp_Array1OfPnt2d poles(1,degree+1);
645 TColStd_Array1OfReal weights(1,degree+1);
647 for (i = 1; i <= degree+1; i++) {
650 GeomTools::GetReal(IS, weights(i));
654 B = new Geom2d_BezierCurve(poles,weights);
656 B = new Geom2d_BezierCurve(poles);
661 //=======================================================================
662 //function : ReadCurve2d
664 //=======================================================================
666 static Standard_IStream& operator>>(Standard_IStream& IS,
667 Handle(Geom2d_BSplineCurve)& B)
669 Standard_Boolean rational=Standard_False,periodic=Standard_False;
670 IS >> rational >> periodic;
673 Standard_Integer i=0,degree=0,nbpoles=0,nbknots=0;
674 IS >> degree >> nbpoles >> nbknots;
676 TColgp_Array1OfPnt2d poles(1,nbpoles);
677 TColStd_Array1OfReal weights(1,nbpoles);
679 for (i = 1; i <= nbpoles; i++) {
682 GeomTools::GetReal(IS, weights(i));
685 TColStd_Array1OfReal knots(1,nbknots);
686 TColStd_Array1OfInteger mults(1,nbknots);
688 for (i = 1; i <= nbknots; i++) {
689 GeomTools::GetReal(IS, knots(i));
694 B = new Geom2d_BSplineCurve(poles,weights,knots,mults,degree,periodic);
696 B = new Geom2d_BSplineCurve(poles,knots,mults,degree,periodic);
701 //=======================================================================
702 //function : ReadCurve2d
704 //=======================================================================
706 static Standard_IStream& operator>>(Standard_IStream& IS,
707 Handle(Geom2d_TrimmedCurve)& C)
709 Standard_Real p1=0.,p2=0.;
710 GeomTools::GetReal(IS, p1);
711 GeomTools::GetReal(IS, p2);
712 Handle(Geom2d_Curve) BC;
713 GeomTools_Curve2dSet::ReadCurve2d(IS,BC);
714 C = new Geom2d_TrimmedCurve(BC,p1,p2);
718 //=======================================================================
719 //function : ReadCurve2d
721 //=======================================================================
723 static Standard_IStream& operator>>(Standard_IStream& IS,
724 Handle(Geom2d_OffsetCurve)& C)
727 GeomTools::GetReal(IS, p);
728 Handle(Geom2d_Curve) BC;
729 GeomTools_Curve2dSet::ReadCurve2d(IS,BC);
730 C = new Geom2d_OffsetCurve(BC,p);
734 //=======================================================================
735 //function : ReadCurve2d
737 //=======================================================================
739 Standard_IStream& GeomTools_Curve2dSet::ReadCurve2d(Standard_IStream& IS,
740 Handle(Geom2d_Curve)& C)
742 Standard_Integer ctype;
751 Handle(Geom2d_Line) CC;
759 Handle(Geom2d_Circle) CC;
767 Handle(Geom2d_Ellipse) CC;
775 Handle(Geom2d_Parabola) CC;
783 Handle(Geom2d_Hyperbola) CC;
791 Handle(Geom2d_BezierCurve) CC;
799 Handle(Geom2d_BSplineCurve) CC;
807 Handle(Geom2d_TrimmedCurve) CC;
815 Handle(Geom2d_OffsetCurve) CC;
823 Handle(Geom2d_Curve) CC;
824 GeomTools::GetUndefinedTypeHandler()->ReadCurve2d(ctype,IS,CC);
830 catch(Standard_Failure) {
832 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
833 cout <<"EXCEPTION in GeomTools_Curve2dSet::ReadCurve2d(..)!!!" << endl;
834 cout << anExc << endl;
841 //=======================================================================
844 //=======================================================================
846 void GeomTools_Curve2dSet::Read(Standard_IStream& IS)
850 if (strcmp(buffer,"Curve2ds")) {
851 cout << "Not a Curve2d table"<<endl;
855 Handle(Geom2d_Curve) C;
856 Standard_Integer i, nbcurve;
859 Handle(Message_ProgressIndicator) progress = GetProgress();
860 Message_ProgressSentry PS(progress, "2D Curves", 0, nbcurve, 1);
861 for (i = 1; i <= nbcurve && PS.More(); i++, PS.Next()) {
862 GeomTools_Curve2dSet::ReadCurve2d(IS,C);
867 //=======================================================================
868 //function : GetProgress
870 //=======================================================================
872 Handle(Message_ProgressIndicator) GeomTools_Curve2dSet::GetProgress() const
877 //=======================================================================
878 //function : SetProgress
880 //=======================================================================
882 void GeomTools_Curve2dSet::SetProgress(const Handle(Message_ProgressIndicator)& PR)