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
9 // under the terms of the GNU Lesser General Public 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_CurveSet.ixx>
19 #include <GeomTools.hxx>
20 #include <GeomTools_UndefinedTypeHandler.hxx>
22 #include <Geom_Circle.hxx>
23 #include <Geom_Line.hxx>
24 #include <Geom_Ellipse.hxx>
25 #include <Geom_Parabola.hxx>
26 #include <Geom_Hyperbola.hxx>
27 #include <Geom_BezierCurve.hxx>
28 #include <Geom_BSplineCurve.hxx>
29 #include <Geom_TrimmedCurve.hxx>
30 #include <Geom_OffsetCurve.hxx>
33 #include <gp_Circ.hxx>
34 #include <gp_Elips.hxx>
35 #include <gp_Parab.hxx>
36 #include <gp_Hypr.hxx>
38 #include <TColStd_Array1OfReal.hxx>
39 #include <TColStd_Array1OfInteger.hxx>
40 #include <TColgp_Array1OfPnt.hxx>
41 #include <Standard_Failure.hxx>
42 #include <Standard_ErrorHandler.hxx>
43 #include <Message_ProgressSentry.hxx>
55 //=======================================================================
56 //function : GeomTools_CurveSet
58 //=======================================================================
60 GeomTools_CurveSet::GeomTools_CurveSet()
65 //=======================================================================
68 //=======================================================================
70 void GeomTools_CurveSet::Clear()
76 //=======================================================================
79 //=======================================================================
81 Standard_Integer GeomTools_CurveSet::Add(const Handle(Geom_Curve)& C)
83 return (C.IsNull()) ? 0 : myMap.Add(C);
87 //=======================================================================
90 //=======================================================================
92 Handle(Geom_Curve) GeomTools_CurveSet::Curve
93 (const Standard_Integer I)const
95 if (I <= 0 || I > myMap.Extent())
96 return Handle(Geom_Curve)();
97 return Handle(Geom_Curve)::DownCast(myMap(I));
101 //=======================================================================
104 //=======================================================================
106 Standard_Integer GeomTools_CurveSet::Index
107 (const Handle(Geom_Curve)& S)const
109 return S.IsNull() ? 0 : myMap.FindIndex(S);
113 //=======================================================================
116 //=======================================================================
118 static void Print(const gp_Pnt P,
119 Standard_OStream& OS,
120 const Standard_Boolean compact)
123 if (!compact) OS << ",";
126 if (!compact) OS << ",";
132 //=======================================================================
135 //=======================================================================
137 static void Print(const gp_Dir D,
138 Standard_OStream& OS,
139 const Standard_Boolean compact)
142 if (!compact) OS << ",";
145 if (!compact) OS << ",";
152 //=======================================================================
155 //=======================================================================
157 static void Print(const Handle(Geom_Line)& L,
158 Standard_OStream& OS,
159 const Standard_Boolean compact)
167 if (!compact) OS << "\n Origin :";
168 Print(C.Location(),OS,compact);
169 if (!compact) OS << "\n Axis :";
170 Print(C.Direction(),OS,compact);
171 if (!compact) OS << "\n";
175 //=======================================================================
178 //=======================================================================
180 static void Print(const Handle(Geom_Circle)& CC,
181 Standard_OStream& OS,
182 const Standard_Boolean compact)
189 gp_Circ C = CC->Circ();
190 if (!compact) OS << "\n Center :";
191 Print(C.Location(),OS,compact);
192 if (!compact) OS << "\n Axis :";
193 Print(C.Axis().Direction(),OS,compact);
194 if (!compact) OS << "\n XAxis :";
195 Print(C.XAxis().Direction(),OS,compact);
196 if (!compact) OS << "\n YAxis :";
197 Print(C.YAxis().Direction(),OS,compact);
198 if (!compact) OS << "\n Radius :";
200 if (!compact) OS << "\n";
204 //=======================================================================
207 //=======================================================================
209 static void Print(const Handle(Geom_Ellipse)& E,
210 Standard_OStream& OS,
211 const Standard_Boolean compact)
214 OS << ELLIPSE << " ";
218 gp_Elips C = E->Elips();
219 if (!compact) OS << "\n Center :";
220 Print(C.Location(),OS,compact);
221 if (!compact) OS << "\n Axis :";
222 Print(C.Axis().Direction(),OS,compact);
223 if (!compact) OS << "\n XAxis :";
224 Print(C.XAxis().Direction(),OS,compact);
225 if (!compact) OS << "\n YAxis :";
226 Print(C.YAxis().Direction(),OS,compact);
227 if (!compact) OS << "\n Radii :";
228 OS << C.MajorRadius();
229 if (!compact) OS << ",";
231 OS << C.MinorRadius();
232 if (!compact) OS << "\n";
236 //=======================================================================
239 //=======================================================================
241 static void Print(const Handle(Geom_Parabola)& P,
242 Standard_OStream& OS,
243 const Standard_Boolean compact)
246 OS << PARABOLA << " ";
250 gp_Parab C = P->Parab();
251 if (!compact) OS << "\n Center :";
252 Print(C.Location(),OS,compact);
253 if (!compact) OS << "\n Axis :";
254 Print(C.Axis().Direction(),OS,compact);
255 if (!compact) OS << "\n XAxis :";
256 Print(C.XAxis().Direction(),OS,compact);
257 if (!compact) OS << "\n YAxis :";
258 Print(C.YAxis().Direction(),OS,compact);
259 if (!compact) OS << "\n Focal :";
261 if (!compact) OS << "\n";
265 //=======================================================================
268 //=======================================================================
270 static void Print(const Handle(Geom_Hyperbola)& H,
271 Standard_OStream& OS,
272 const Standard_Boolean compact)
275 OS << HYPERBOLA << " ";
279 gp_Hypr C = H->Hypr();
280 if (!compact) OS << "\n Center :";
281 Print(C.Location(),OS,compact);
282 if (!compact) OS << "\n Axis :";
283 Print(C.Axis().Direction(),OS,compact);
284 if (!compact) OS << "\n XAxis :";
285 Print(C.XAxis().Direction(),OS,compact);
286 if (!compact) OS << "\n YAxis :";
287 Print(C.YAxis().Direction(),OS,compact);
288 if (!compact) OS << "\n Radii :";
289 OS << C.MajorRadius();
290 if (!compact) OS << ",";
292 OS << C.MinorRadius();
293 if (!compact) OS << "\n";
297 //=======================================================================
300 //=======================================================================
302 static void Print(const Handle(Geom_BezierCurve)& B,
303 Standard_OStream& OS,
304 const Standard_Boolean compact)
311 Standard_Boolean rational = B->IsRational();
313 OS << (rational ? 1 : 0) << " ";
320 Standard_Integer i,degree = B->Degree();
321 if (!compact) OS << "\n Degree :";
324 for (i = 1; i <= degree+1; i++) {
325 if (!compact) OS << "\n "<<setw(2)<<i<<" : ";
326 Print(B->Pole(i),OS,compact);
328 OS << " " << B->Weight(i);
333 if (!compact) OS << "\n";
336 //=======================================================================
339 //=======================================================================
341 static void Print(const Handle(Geom_BSplineCurve)& B,
342 Standard_OStream& OS,
343 const Standard_Boolean compact)
346 OS << BSPLINE << " ";
348 OS << "BSplineCurve";
351 Standard_Boolean rational = B->IsRational();
353 OS << (rational ? 1 : 0) << " ";
359 Standard_Boolean periodic = B->IsPeriodic();
361 OS << (periodic ? 1 : 0)<< " ";
368 Standard_Integer i,degree,nbpoles,nbknots;
369 degree = B->Degree();
370 nbpoles = B->NbPoles();
371 nbknots = B->NbKnots();
372 if (!compact) OS << "\n Degree ";
375 if (!compact) OS << ",";
378 if (!compact) OS << " Poles,";
380 OS << nbknots << " ";
381 if (!compact) OS << " Knots\n";
383 if (!compact) OS << "Poles :\n";
384 for (i = 1; i <= nbpoles; i++) {
385 if (!compact) OS << "\n "<<setw(2)<<i<<" : ";
387 Print(B->Pole(i),OS,compact);
389 OS << " " << B->Weight(i);
393 if (!compact) OS << "Knots :\n";
394 for (i = 1; i <= nbknots; i++) {
395 if (!compact) OS << "\n "<<setw(2)<<i<<" : ";
396 OS << " " << B->Knot(i) << " " << B->Multiplicity(i);
400 if (!compact) OS << "\n";
403 //=======================================================================
406 //=======================================================================
408 static void Print(const Handle(Geom_TrimmedCurve)& C,
409 Standard_OStream& OS,
410 const Standard_Boolean compact)
413 OS << TRIMMED << " ";
415 OS << "Trimmed curve\n";
416 if (!compact) OS << "Parameters : ";
417 OS << C->FirstParameter() << " " << C->LastParameter() << "\n";
418 if (!compact) OS << "Basis curve :\n";
419 GeomTools_CurveSet::PrintCurve(C->BasisCurve(),OS,compact);
422 //=======================================================================
425 //=======================================================================
427 static void Print(const Handle(Geom_OffsetCurve)& C,
428 Standard_OStream& OS,
429 const Standard_Boolean compact)
435 if (!compact) OS << "Offset : ";
436 OS << C->Offset() << "\n";
437 if (!compact) OS << "Direction : ";
438 Print(C->Direction(),OS,compact);
440 if (!compact) OS << "Basis curve :\n";
441 GeomTools_CurveSet::PrintCurve(C->BasisCurve(),OS,compact);
444 //=======================================================================
447 //=======================================================================
449 void GeomTools_CurveSet::PrintCurve(const Handle(Geom_Curve)& C,
450 Standard_OStream& OS,
451 const Standard_Boolean compact)
453 Handle(Standard_Type) TheType = C->DynamicType();
455 if ( TheType ==STANDARD_TYPE(Geom_Line)) {
456 Print(Handle(Geom_Line)::DownCast(C),OS,compact);
458 else if ( TheType == STANDARD_TYPE(Geom_Circle)) {
459 Print(Handle(Geom_Circle)::DownCast(C),OS,compact);
461 else if ( TheType == STANDARD_TYPE(Geom_Ellipse)) {
462 Print(Handle(Geom_Ellipse)::DownCast(C),OS,compact);
464 else if ( TheType == STANDARD_TYPE(Geom_Parabola)) {
465 Print(Handle(Geom_Parabola)::DownCast(C),OS,compact);
467 else if ( TheType == STANDARD_TYPE(Geom_Hyperbola)) {
468 Print(Handle(Geom_Hyperbola)::DownCast(C),OS,compact);
470 else if ( TheType == STANDARD_TYPE(Geom_BezierCurve)) {
471 Print(Handle(Geom_BezierCurve)::DownCast(C),OS,compact);
473 else if ( TheType == STANDARD_TYPE(Geom_BSplineCurve)) {
474 Print(Handle(Geom_BSplineCurve)::DownCast(C),OS,compact);
476 else if ( TheType == STANDARD_TYPE(Geom_TrimmedCurve)) {
477 Print(Handle(Geom_TrimmedCurve)::DownCast(C),OS,compact);
479 else if ( TheType == STANDARD_TYPE(Geom_OffsetCurve)) {
480 Print(Handle(Geom_OffsetCurve)::DownCast(C),OS,compact);
483 GeomTools::GetUndefinedTypeHandler()->PrintCurve(C,OS,compact);
485 // OS << "****** UNKNOWN CURVE TYPE ******\n";
487 // cout << "****** UNKNOWN CURVE TYPE ******" << endl;
491 //=======================================================================
494 //=======================================================================
496 void GeomTools_CurveSet::Dump(Standard_OStream& OS)const
498 Standard_Integer i, nbsurf = myMap.Extent();
499 OS << "\n -------\n";
500 OS << "Dump of "<< nbsurf << " Curves ";
501 OS << "\n -------\n\n";
503 for (i = 1; i <= nbsurf; i++) {
504 OS << setw(4) << i << " : ";
505 PrintCurve(Handle(Geom_Curve)::DownCast(myMap(i)),OS,Standard_False);
510 //=======================================================================
513 //=======================================================================
515 void GeomTools_CurveSet::Write(Standard_OStream& OS)const
517 std::streamsize prec = OS.precision(17);
519 Standard_Integer i, nbcurve = myMap.Extent();
520 OS << "Curves "<< nbcurve << "\n";
522 Handle(Message_ProgressIndicator) progress = GetProgress();
523 Message_ProgressSentry PS(progress, "3D Curves", 0, nbcurve, 1);
525 for (i = 1; i <= nbcurve && PS.More(); i++, PS.Next()) {
526 if ( !progress.IsNull() )
529 PrintCurve(Handle(Geom_Curve)::DownCast(myMap(i)),OS,Standard_True);
536 //=======================================================================
539 //=======================================================================
541 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Pnt& P)
543 Standard_Real X=0.,Y=0.,Z=0.;
549 //=======================================================================
552 //=======================================================================
554 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Dir& D)
556 Standard_Real X=0.,Y=0.,Z=0.;
563 //=======================================================================
564 //function : ReadCurve
566 //=======================================================================
568 static Standard_IStream& operator>>(Standard_IStream& IS,
569 Handle(Geom_Line)& L)
574 L = new Geom_Line(P,AX);
578 //=======================================================================
579 //function : ReadCurve
581 //=======================================================================
583 static Standard_IStream& operator>>(Standard_IStream& IS,
584 Handle(Geom_Circle)& C)
587 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
589 IS >> P >> A >> AX >> AY >> R;
590 C = new Geom_Circle(gp_Ax2(P,A,AX),R);
594 //=======================================================================
595 //function : ReadCurve
597 //=======================================================================
599 static Standard_IStream& operator>>(Standard_IStream& IS,
600 Handle(Geom_Ellipse)& E)
603 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
604 Standard_Real R1=0.,R2=0.;
605 IS >> P >> A >> AX >> AY >> R1 >> R2;
606 E = new Geom_Ellipse(gp_Ax2(P,A,AX),R1,R2);
610 //=======================================================================
611 //function : ReadCurve
613 //=======================================================================
615 static Standard_IStream& operator>>(Standard_IStream& IS,
616 Handle(Geom_Parabola)& C)
619 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
621 IS >> P >> A >> AX >> AY >> R1;
622 C = new Geom_Parabola(gp_Ax2(P,A,AX),R1);
626 //=======================================================================
627 //function : ReadCurve
629 //=======================================================================
631 static Standard_IStream& operator>>(Standard_IStream& IS,
632 Handle(Geom_Hyperbola)& H)
635 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
636 Standard_Real R1=0.,R2=0.;
637 IS >> P >> A >> AX >> AY >> R1 >> R2;
638 H = new Geom_Hyperbola(gp_Ax2(P,A,AX),R1,R2);
642 //=======================================================================
643 //function : ReadCurve
645 //=======================================================================
647 static Standard_IStream& operator>>(Standard_IStream& IS,
648 Handle(Geom_BezierCurve)& B)
650 Standard_Boolean rational=Standard_False;
654 Standard_Integer i=0,degree=0;
657 TColgp_Array1OfPnt poles(1,degree+1);
658 TColStd_Array1OfReal weights(1,degree+1);
660 for (i = 1; i <= degree+1; i++) {
667 B = new Geom_BezierCurve(poles,weights);
669 B = new Geom_BezierCurve(poles);
674 //=======================================================================
675 //function : ReadCurve
677 //=======================================================================
679 static Standard_IStream& operator>>(Standard_IStream& IS,
680 Handle(Geom_BSplineCurve)& B)
683 Standard_Boolean rational=Standard_False,periodic=Standard_False;
684 IS >> rational >> periodic;
687 Standard_Integer i=0,degree=0,nbpoles=0,nbknots=0;
688 IS >> degree >> nbpoles >> nbknots;
690 TColgp_Array1OfPnt poles(1,nbpoles);
691 TColStd_Array1OfReal weights(1,nbpoles);
693 for (i = 1; i <= nbpoles; i++) {
699 TColStd_Array1OfReal knots(1,nbknots);
700 TColStd_Array1OfInteger mults(1,nbknots);
702 for (i = 1; i <= nbknots; i++) {
703 IS >> knots(i) >> mults(i);
707 B = new Geom_BSplineCurve(poles,weights,knots,mults,degree,periodic);
709 B = new Geom_BSplineCurve(poles,knots,mults,degree,periodic);
714 //=======================================================================
715 //function : ReadCurve
717 //=======================================================================
719 static Standard_IStream& operator>>(Standard_IStream& IS,
720 Handle(Geom_TrimmedCurve)& C)
722 Standard_Real p1=0.,p2=0.;
724 Handle(Geom_Curve) BC;
725 GeomTools_CurveSet::ReadCurve(IS,BC);
726 C = new Geom_TrimmedCurve(BC,p1,p2);
730 //=======================================================================
731 //function : ReadCurve
733 //=======================================================================
735 static Standard_IStream& operator>>(Standard_IStream& IS,
736 Handle(Geom_OffsetCurve)& C)
742 Handle(Geom_Curve) BC;
743 GeomTools_CurveSet::ReadCurve(IS,BC);
744 C = new Geom_OffsetCurve(BC,p,D);
748 //=======================================================================
749 //function : ReadCurve
751 //=======================================================================
753 Standard_IStream& GeomTools_CurveSet::ReadCurve(Standard_IStream& IS,
754 Handle(Geom_Curve)& C)
756 Standard_Integer ctype;
765 Handle(Geom_Line) CC;
773 Handle(Geom_Circle) CC;
781 Handle(Geom_Ellipse) CC;
789 Handle(Geom_Parabola) CC;
797 Handle(Geom_Hyperbola) CC;
805 Handle(Geom_BezierCurve) CC;
813 Handle(Geom_BSplineCurve) CC;
821 Handle(Geom_TrimmedCurve) CC;
829 Handle(Geom_OffsetCurve) CC;
837 Handle(Geom_Curve) CC;
838 GeomTools::GetUndefinedTypeHandler()->ReadCurve(ctype,IS,CC);
843 catch(Standard_Failure) {
845 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
846 cout <<"EXCEPTION in GeomTools_CurveSet::ReadCurve(..)!!!" << endl;
847 cout << anExc << endl;
854 //=======================================================================
857 //=======================================================================
859 void GeomTools_CurveSet::Read(Standard_IStream& IS)
863 if (strcmp(buffer,"Curves")) {
864 cout << "Not a Curve table"<<endl;
868 Handle(Geom_Curve) C;
869 Standard_Integer i, nbcurve;
872 Handle(Message_ProgressIndicator) progress = GetProgress();
873 Message_ProgressSentry PS(progress, "3D Curves", 0, nbcurve, 1);
874 for (i = 1; i <= nbcurve && PS.More(); i++, PS.Next()) {
875 if ( !progress.IsNull() )
878 GeomTools_CurveSet::ReadCurve(IS,C);
883 //=======================================================================
884 //function : GetProgress
886 //=======================================================================
888 Handle(Message_ProgressIndicator) GeomTools_CurveSet::GetProgress() const
893 //=======================================================================
894 //function : SetProgress
896 //=======================================================================
898 void GeomTools_CurveSet::SetProgress(const Handle(Message_ProgressIndicator)& PR)