1 // Created on: 1993-07-19
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
22 #include <Standard_Stream.hxx>
23 #include <GeomTools_CurveSet.ixx>
24 #include <GeomTools.hxx>
25 #include <GeomTools_UndefinedTypeHandler.hxx>
27 #include <Geom_Circle.hxx>
28 #include <Geom_Line.hxx>
29 #include <Geom_Ellipse.hxx>
30 #include <Geom_Parabola.hxx>
31 #include <Geom_Hyperbola.hxx>
32 #include <Geom_BezierCurve.hxx>
33 #include <Geom_BSplineCurve.hxx>
34 #include <Geom_TrimmedCurve.hxx>
35 #include <Geom_OffsetCurve.hxx>
38 #include <gp_Circ.hxx>
39 #include <gp_Elips.hxx>
40 #include <gp_Parab.hxx>
41 #include <gp_Hypr.hxx>
43 #include <TColStd_Array1OfReal.hxx>
44 #include <TColStd_Array1OfInteger.hxx>
45 #include <TColgp_Array1OfPnt.hxx>
46 #include <Standard_Failure.hxx>
47 #include <Standard_ErrorHandler.hxx>
48 #include <Message_ProgressSentry.hxx>
60 //=======================================================================
61 //function : GeomTools_CurveSet
63 //=======================================================================
65 GeomTools_CurveSet::GeomTools_CurveSet()
70 //=======================================================================
73 //=======================================================================
75 void GeomTools_CurveSet::Clear()
81 //=======================================================================
84 //=======================================================================
86 Standard_Integer GeomTools_CurveSet::Add(const Handle(Geom_Curve)& C)
88 return (C.IsNull()) ? 0 : myMap.Add(C);
92 //=======================================================================
95 //=======================================================================
97 Handle(Geom_Curve) GeomTools_CurveSet::Curve
98 (const Standard_Integer I)const
100 if (I <= 0 || I > myMap.Extent())
101 return Handle(Geom_Curve)();
102 return Handle(Geom_Curve)::DownCast(myMap(I));
106 //=======================================================================
109 //=======================================================================
111 Standard_Integer GeomTools_CurveSet::Index
112 (const Handle(Geom_Curve)& S)const
114 return S.IsNull() ? 0 : myMap.FindIndex(S);
118 //=======================================================================
121 //=======================================================================
123 static void Print(const gp_Pnt P,
124 Standard_OStream& OS,
125 const Standard_Boolean compact)
128 if (!compact) OS << ",";
131 if (!compact) OS << ",";
137 //=======================================================================
140 //=======================================================================
142 static void Print(const gp_Dir D,
143 Standard_OStream& OS,
144 const Standard_Boolean compact)
147 if (!compact) OS << ",";
150 if (!compact) OS << ",";
157 //=======================================================================
160 //=======================================================================
162 static void Print(const Handle(Geom_Line)& L,
163 Standard_OStream& OS,
164 const Standard_Boolean compact)
172 if (!compact) OS << "\n Origin :";
173 Print(C.Location(),OS,compact);
174 if (!compact) OS << "\n Axis :";
175 Print(C.Direction(),OS,compact);
176 if (!compact) OS << "\n";
180 //=======================================================================
183 //=======================================================================
185 static void Print(const Handle(Geom_Circle)& CC,
186 Standard_OStream& OS,
187 const Standard_Boolean compact)
194 gp_Circ C = CC->Circ();
195 if (!compact) OS << "\n Center :";
196 Print(C.Location(),OS,compact);
197 if (!compact) OS << "\n Axis :";
198 Print(C.Axis().Direction(),OS,compact);
199 if (!compact) OS << "\n XAxis :";
200 Print(C.XAxis().Direction(),OS,compact);
201 if (!compact) OS << "\n YAxis :";
202 Print(C.YAxis().Direction(),OS,compact);
203 if (!compact) OS << "\n Radius :";
205 if (!compact) OS << "\n";
209 //=======================================================================
212 //=======================================================================
214 static void Print(const Handle(Geom_Ellipse)& E,
215 Standard_OStream& OS,
216 const Standard_Boolean compact)
219 OS << ELLIPSE << " ";
223 gp_Elips C = E->Elips();
224 if (!compact) OS << "\n Center :";
225 Print(C.Location(),OS,compact);
226 if (!compact) OS << "\n Axis :";
227 Print(C.Axis().Direction(),OS,compact);
228 if (!compact) OS << "\n XAxis :";
229 Print(C.XAxis().Direction(),OS,compact);
230 if (!compact) OS << "\n YAxis :";
231 Print(C.YAxis().Direction(),OS,compact);
232 if (!compact) OS << "\n Radii :";
233 OS << C.MajorRadius();
234 if (!compact) OS << ",";
236 OS << C.MinorRadius();
237 if (!compact) OS << "\n";
241 //=======================================================================
244 //=======================================================================
246 static void Print(const Handle(Geom_Parabola)& P,
247 Standard_OStream& OS,
248 const Standard_Boolean compact)
251 OS << PARABOLA << " ";
255 gp_Parab C = P->Parab();
256 if (!compact) OS << "\n Center :";
257 Print(C.Location(),OS,compact);
258 if (!compact) OS << "\n Axis :";
259 Print(C.Axis().Direction(),OS,compact);
260 if (!compact) OS << "\n XAxis :";
261 Print(C.XAxis().Direction(),OS,compact);
262 if (!compact) OS << "\n YAxis :";
263 Print(C.YAxis().Direction(),OS,compact);
264 if (!compact) OS << "\n Focal :";
266 if (!compact) OS << "\n";
270 //=======================================================================
273 //=======================================================================
275 static void Print(const Handle(Geom_Hyperbola)& H,
276 Standard_OStream& OS,
277 const Standard_Boolean compact)
280 OS << HYPERBOLA << " ";
284 gp_Hypr C = H->Hypr();
285 if (!compact) OS << "\n Center :";
286 Print(C.Location(),OS,compact);
287 if (!compact) OS << "\n Axis :";
288 Print(C.Axis().Direction(),OS,compact);
289 if (!compact) OS << "\n XAxis :";
290 Print(C.XAxis().Direction(),OS,compact);
291 if (!compact) OS << "\n YAxis :";
292 Print(C.YAxis().Direction(),OS,compact);
293 if (!compact) OS << "\n Radii :";
294 OS << C.MajorRadius();
295 if (!compact) OS << ",";
297 OS << C.MinorRadius();
298 if (!compact) OS << "\n";
302 //=======================================================================
305 //=======================================================================
307 static void Print(const Handle(Geom_BezierCurve)& B,
308 Standard_OStream& OS,
309 const Standard_Boolean compact)
316 Standard_Boolean rational = B->IsRational();
318 OS << (rational ? 1 : 0) << " ";
325 Standard_Integer i,degree = B->Degree();
326 if (!compact) OS << "\n Degree :";
329 for (i = 1; i <= degree+1; i++) {
330 if (!compact) OS << "\n "<<setw(2)<<i<<" : ";
331 Print(B->Pole(i),OS,compact);
333 OS << " " << B->Weight(i);
338 if (!compact) OS << "\n";
341 //=======================================================================
344 //=======================================================================
346 static void Print(const Handle(Geom_BSplineCurve)& B,
347 Standard_OStream& OS,
348 const Standard_Boolean compact)
351 OS << BSPLINE << " ";
353 OS << "BSplineCurve";
356 Standard_Boolean rational = B->IsRational();
358 OS << (rational ? 1 : 0) << " ";
364 Standard_Boolean periodic = B->IsPeriodic();
366 OS << (periodic ? 1 : 0)<< " ";
373 Standard_Integer i,degree,nbpoles,nbknots;
374 degree = B->Degree();
375 nbpoles = B->NbPoles();
376 nbknots = B->NbKnots();
377 if (!compact) OS << "\n Degree ";
380 if (!compact) OS << ",";
383 if (!compact) OS << " Poles,";
385 OS << nbknots << " ";
386 if (!compact) OS << " Knots\n";
388 if (!compact) OS << "Poles :\n";
389 for (i = 1; i <= nbpoles; i++) {
390 if (!compact) OS << "\n "<<setw(2)<<i<<" : ";
392 Print(B->Pole(i),OS,compact);
394 OS << " " << B->Weight(i);
398 if (!compact) OS << "Knots :\n";
399 for (i = 1; i <= nbknots; i++) {
400 if (!compact) OS << "\n "<<setw(2)<<i<<" : ";
401 OS << " " << B->Knot(i) << " " << B->Multiplicity(i);
405 if (!compact) OS << "\n";
408 //=======================================================================
411 //=======================================================================
413 static void Print(const Handle(Geom_TrimmedCurve)& C,
414 Standard_OStream& OS,
415 const Standard_Boolean compact)
418 OS << TRIMMED << " ";
420 OS << "Trimmed curve\n";
421 if (!compact) OS << "Parameters : ";
422 OS << C->FirstParameter() << " " << C->LastParameter() << "\n";
423 if (!compact) OS << "Basis curve :\n";
424 GeomTools_CurveSet::PrintCurve(C->BasisCurve(),OS,compact);
427 //=======================================================================
430 //=======================================================================
432 static void Print(const Handle(Geom_OffsetCurve)& C,
433 Standard_OStream& OS,
434 const Standard_Boolean compact)
440 if (!compact) OS << "Offset : ";
441 OS << C->Offset() << "\n";
442 if (!compact) OS << "Direction : ";
443 Print(C->Direction(),OS,compact);
445 if (!compact) OS << "Basis curve :\n";
446 GeomTools_CurveSet::PrintCurve(C->BasisCurve(),OS,compact);
449 //=======================================================================
452 //=======================================================================
454 void GeomTools_CurveSet::PrintCurve(const Handle(Geom_Curve)& C,
455 Standard_OStream& OS,
456 const Standard_Boolean compact)
458 Handle(Standard_Type) TheType = C->DynamicType();
460 if ( TheType ==STANDARD_TYPE(Geom_Line)) {
461 Print(Handle(Geom_Line)::DownCast(C),OS,compact);
463 else if ( TheType == STANDARD_TYPE(Geom_Circle)) {
464 Print(Handle(Geom_Circle)::DownCast(C),OS,compact);
466 else if ( TheType == STANDARD_TYPE(Geom_Ellipse)) {
467 Print(Handle(Geom_Ellipse)::DownCast(C),OS,compact);
469 else if ( TheType == STANDARD_TYPE(Geom_Parabola)) {
470 Print(Handle(Geom_Parabola)::DownCast(C),OS,compact);
472 else if ( TheType == STANDARD_TYPE(Geom_Hyperbola)) {
473 Print(Handle(Geom_Hyperbola)::DownCast(C),OS,compact);
475 else if ( TheType == STANDARD_TYPE(Geom_BezierCurve)) {
476 Print(Handle(Geom_BezierCurve)::DownCast(C),OS,compact);
478 else if ( TheType == STANDARD_TYPE(Geom_BSplineCurve)) {
479 Print(Handle(Geom_BSplineCurve)::DownCast(C),OS,compact);
481 else if ( TheType == STANDARD_TYPE(Geom_TrimmedCurve)) {
482 Print(Handle(Geom_TrimmedCurve)::DownCast(C),OS,compact);
484 else if ( TheType == STANDARD_TYPE(Geom_OffsetCurve)) {
485 Print(Handle(Geom_OffsetCurve)::DownCast(C),OS,compact);
488 GeomTools::GetUndefinedTypeHandler()->PrintCurve(C,OS,compact);
490 // OS << "****** UNKNOWN CURVE TYPE ******\n";
492 // cout << "****** UNKNOWN CURVE TYPE ******" << endl;
496 //=======================================================================
499 //=======================================================================
501 void GeomTools_CurveSet::Dump(Standard_OStream& OS)const
503 Standard_Integer i, nbsurf = myMap.Extent();
504 OS << "\n -------\n";
505 OS << "Dump of "<< nbsurf << " Curves ";
506 OS << "\n -------\n\n";
508 for (i = 1; i <= nbsurf; i++) {
509 OS << setw(4) << i << " : ";
510 PrintCurve(Handle(Geom_Curve)::DownCast(myMap(i)),OS,Standard_False);
515 //=======================================================================
518 //=======================================================================
520 void GeomTools_CurveSet::Write(Standard_OStream& OS)const
522 std::streamsize prec = OS.precision(17);
524 Standard_Integer i, nbcurve = myMap.Extent();
525 OS << "Curves "<< nbcurve << "\n";
527 Handle(Message_ProgressIndicator) progress = GetProgress();
528 Message_ProgressSentry PS(progress, "3D Curves", 0, nbcurve, 1);
530 for (i = 1; i <= nbcurve && PS.More(); i++, PS.Next()) {
531 if ( !progress.IsNull() )
534 PrintCurve(Handle(Geom_Curve)::DownCast(myMap(i)),OS,Standard_True);
541 //=======================================================================
544 //=======================================================================
546 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Pnt& P)
548 Standard_Real X=0.,Y=0.,Z=0.;
554 //=======================================================================
557 //=======================================================================
559 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Dir& D)
561 Standard_Real X=0.,Y=0.,Z=0.;
568 //=======================================================================
569 //function : ReadCurve
571 //=======================================================================
573 static Standard_IStream& operator>>(Standard_IStream& IS,
574 Handle(Geom_Line)& L)
579 L = new Geom_Line(P,AX);
583 //=======================================================================
584 //function : ReadCurve
586 //=======================================================================
588 static Standard_IStream& operator>>(Standard_IStream& IS,
589 Handle(Geom_Circle)& C)
592 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
594 IS >> P >> A >> AX >> AY >> R;
595 C = new Geom_Circle(gp_Ax2(P,A,AX),R);
599 //=======================================================================
600 //function : ReadCurve
602 //=======================================================================
604 static Standard_IStream& operator>>(Standard_IStream& IS,
605 Handle(Geom_Ellipse)& E)
608 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
609 Standard_Real R1=0.,R2=0.;
610 IS >> P >> A >> AX >> AY >> R1 >> R2;
611 E = new Geom_Ellipse(gp_Ax2(P,A,AX),R1,R2);
615 //=======================================================================
616 //function : ReadCurve
618 //=======================================================================
620 static Standard_IStream& operator>>(Standard_IStream& IS,
621 Handle(Geom_Parabola)& C)
624 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
626 IS >> P >> A >> AX >> AY >> R1;
627 C = new Geom_Parabola(gp_Ax2(P,A,AX),R1);
631 //=======================================================================
632 //function : ReadCurve
634 //=======================================================================
636 static Standard_IStream& operator>>(Standard_IStream& IS,
637 Handle(Geom_Hyperbola)& H)
640 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
641 Standard_Real R1=0.,R2=0.;
642 IS >> P >> A >> AX >> AY >> R1 >> R2;
643 H = new Geom_Hyperbola(gp_Ax2(P,A,AX),R1,R2);
647 //=======================================================================
648 //function : ReadCurve
650 //=======================================================================
652 static Standard_IStream& operator>>(Standard_IStream& IS,
653 Handle(Geom_BezierCurve)& B)
655 Standard_Boolean rational=Standard_False;
659 Standard_Integer i=0,degree=0;
662 TColgp_Array1OfPnt poles(1,degree+1);
663 TColStd_Array1OfReal weights(1,degree+1);
665 for (i = 1; i <= degree+1; i++) {
672 B = new Geom_BezierCurve(poles,weights);
674 B = new Geom_BezierCurve(poles);
679 //=======================================================================
680 //function : ReadCurve
682 //=======================================================================
684 static Standard_IStream& operator>>(Standard_IStream& IS,
685 Handle(Geom_BSplineCurve)& B)
688 Standard_Boolean rational=Standard_False,periodic=Standard_False;
689 IS >> rational >> periodic;
692 Standard_Integer i=0,degree=0,nbpoles=0,nbknots=0;
693 IS >> degree >> nbpoles >> nbknots;
695 TColgp_Array1OfPnt poles(1,nbpoles);
696 TColStd_Array1OfReal weights(1,nbpoles);
698 for (i = 1; i <= nbpoles; i++) {
704 TColStd_Array1OfReal knots(1,nbknots);
705 TColStd_Array1OfInteger mults(1,nbknots);
707 for (i = 1; i <= nbknots; i++) {
708 IS >> knots(i) >> mults(i);
712 B = new Geom_BSplineCurve(poles,weights,knots,mults,degree,periodic);
714 B = new Geom_BSplineCurve(poles,knots,mults,degree,periodic);
719 //=======================================================================
720 //function : ReadCurve
722 //=======================================================================
724 static Standard_IStream& operator>>(Standard_IStream& IS,
725 Handle(Geom_TrimmedCurve)& C)
727 Standard_Real p1=0.,p2=0.;
729 Handle(Geom_Curve) BC;
730 GeomTools_CurveSet::ReadCurve(IS,BC);
731 C = new Geom_TrimmedCurve(BC,p1,p2);
735 //=======================================================================
736 //function : ReadCurve
738 //=======================================================================
740 static Standard_IStream& operator>>(Standard_IStream& IS,
741 Handle(Geom_OffsetCurve)& C)
747 Handle(Geom_Curve) BC;
748 GeomTools_CurveSet::ReadCurve(IS,BC);
749 C = new Geom_OffsetCurve(BC,p,D);
753 //=======================================================================
754 //function : ReadCurve
756 //=======================================================================
758 Standard_IStream& GeomTools_CurveSet::ReadCurve(Standard_IStream& IS,
759 Handle(Geom_Curve)& C)
761 Standard_Integer ctype;
770 Handle(Geom_Line) CC;
778 Handle(Geom_Circle) CC;
786 Handle(Geom_Ellipse) CC;
794 Handle(Geom_Parabola) CC;
802 Handle(Geom_Hyperbola) CC;
810 Handle(Geom_BezierCurve) CC;
818 Handle(Geom_BSplineCurve) CC;
826 Handle(Geom_TrimmedCurve) CC;
834 Handle(Geom_OffsetCurve) CC;
842 Handle(Geom_Curve) CC;
843 GeomTools::GetUndefinedTypeHandler()->ReadCurve(ctype,IS,CC);
848 catch(Standard_Failure) {
850 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
851 cout <<"EXCEPTION in GeomTools_CurveSet::ReadCurve(..)!!!" << endl;
852 cout << anExc << endl;
859 //=======================================================================
862 //=======================================================================
864 void GeomTools_CurveSet::Read(Standard_IStream& IS)
868 if (strcmp(buffer,"Curves")) {
869 cout << "Not a Curve table"<<endl;
873 Handle(Geom_Curve) C;
874 Standard_Integer i, nbcurve;
877 Handle(Message_ProgressIndicator) progress = GetProgress();
878 Message_ProgressSentry PS(progress, "3D Curves", 0, nbcurve, 1);
879 for (i = 1; i <= nbcurve && PS.More(); i++, PS.Next()) {
880 if ( !progress.IsNull() )
883 GeomTools_CurveSet::ReadCurve(IS,C);
888 //=======================================================================
889 //function : GetProgress
891 //=======================================================================
893 Handle(Message_ProgressIndicator) GeomTools_CurveSet::GetProgress() const
898 //=======================================================================
899 //function : SetProgress
901 //=======================================================================
903 void GeomTools_CurveSet::SetProgress(const Handle(Message_ProgressIndicator)& PR)