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_Curve2dSet.ixx>
24 #include <GeomTools.hxx>
25 #include <GeomTools_UndefinedTypeHandler.hxx>
27 #include <Geom2d_Circle.hxx>
28 #include <Geom2d_Line.hxx>
29 #include <Geom2d_Ellipse.hxx>
30 #include <Geom2d_Parabola.hxx>
31 #include <Geom2d_Hyperbola.hxx>
32 #include <Geom2d_BezierCurve.hxx>
33 #include <Geom2d_BSplineCurve.hxx>
34 #include <Geom2d_TrimmedCurve.hxx>
35 #include <Geom2d_OffsetCurve.hxx>
37 #include <gp_Lin2d.hxx>
38 #include <gp_Circ2d.hxx>
39 #include <gp_Elips2d.hxx>
40 #include <gp_Parab2d.hxx>
41 #include <gp_Hypr2d.hxx>
43 #include <TColStd_Array1OfReal.hxx>
44 #include <TColStd_Array1OfInteger.hxx>
45 #include <TColgp_Array1OfPnt2d.hxx>
46 #include <Standard_Failure.hxx>
47 #include <Standard_ErrorHandler.hxx>
48 #include <Message_ProgressSentry.hxx>
61 //=======================================================================
62 //function : GeomTools_Curve2dSet
64 //=======================================================================
66 GeomTools_Curve2dSet::GeomTools_Curve2dSet()
71 //=======================================================================
74 //=======================================================================
76 void GeomTools_Curve2dSet::Clear()
82 //=======================================================================
85 //=======================================================================
87 Standard_Integer GeomTools_Curve2dSet::Add(const Handle(Geom2d_Curve)& S)
93 //=======================================================================
96 //=======================================================================
98 Handle(Geom2d_Curve) GeomTools_Curve2dSet::Curve2d
99 (const Standard_Integer I)const
101 if (I <= 0 || I > myMap.Extent())
102 return Handle(Geom2d_Curve)();
104 return Handle(Geom2d_Curve)::DownCast(myMap(I));
108 //=======================================================================
111 //=======================================================================
113 Standard_Integer GeomTools_Curve2dSet::Index
114 (const Handle(Geom2d_Curve)& S)const
116 return myMap.FindIndex(S);
119 //=======================================================================
122 //=======================================================================
124 static void Print(const gp_Pnt2d P,
125 Standard_OStream& OS,
126 const Standard_Boolean compact)
129 if (!compact) OS << ",";
135 //=======================================================================
138 //=======================================================================
140 static void Print(const gp_Dir2d D,
141 Standard_OStream& OS,
142 const Standard_Boolean compact)
145 if (!compact) OS << ",";
151 //=======================================================================
154 //=======================================================================
156 static void Print(const Handle(Geom2d_Line)& L,
157 Standard_OStream& OS,
158 const Standard_Boolean compact)
165 gp_Lin2d C2d = L->Lin2d();
166 if (!compact) OS << "\n Origin :";
167 Print(C2d.Location(),OS,compact);
168 if (!compact) OS << "\n Axis :";
169 Print(C2d.Direction(),OS,compact);
170 if (!compact) OS << "\n";
174 //=======================================================================
177 //=======================================================================
179 static void Print(const Handle(Geom2d_Circle)& C,
180 Standard_OStream& OS,
181 const Standard_Boolean compact)
188 gp_Circ2d C2d = C->Circ2d();
189 if (!compact) OS << "\n Center :";
190 Print(C2d.Location(),OS,compact);
191 if (!compact) OS << "\n XAxis :";
192 Print(C2d.XAxis().Direction(),OS,compact);
193 if (!compact) OS << "\n YAxis :";
194 Print(C2d.YAxis().Direction(),OS,compact);
195 if (!compact) OS << "\n Radius :";
197 if (!compact) OS << "\n";
201 //=======================================================================
204 //=======================================================================
206 static void Print(const Handle(Geom2d_Ellipse)& E,
207 Standard_OStream& OS,
208 const Standard_Boolean compact)
211 OS << ELLIPSE << " ";
215 gp_Elips2d C2d = E->Elips2d();
216 if (!compact) OS << "\n Center :";
217 Print(C2d.Location(),OS,compact);
218 if (!compact) OS << "\n XAxis :";
219 Print(C2d.XAxis().Direction(),OS,compact);
220 if (!compact) OS << "\n YAxis :";
221 Print(C2d.YAxis().Direction(),OS,compact);
222 if (!compact) OS << "\n Radii :";
223 OS << C2d.MajorRadius();
224 if (!compact) OS << ",";
226 OS << C2d.MinorRadius();
227 if (!compact) OS << "\n";
231 //=======================================================================
234 //=======================================================================
236 static void Print(const Handle(Geom2d_Parabola)& P,
237 Standard_OStream& OS,
238 const Standard_Boolean compact)
241 OS << PARABOLA << " ";
245 gp_Parab2d C2d = P->Parab2d();
246 if (!compact) OS << "\n Center :";
247 Print(C2d.Location(),OS,compact);
248 if (!compact) OS << "\n XAxis :";
249 Print(C2d.Axis().XAxis().Direction(),OS,compact);
250 if (!compact) OS << "\n YAxis :";
251 Print(C2d.Axis().YAxis().Direction(),OS,compact);
252 if (!compact) OS << "\n Focal :";
254 if (!compact) OS << "\n";
258 //=======================================================================
261 //=======================================================================
263 static void Print(const Handle(Geom2d_Hyperbola)& H,
264 Standard_OStream& OS,
265 const Standard_Boolean compact)
268 OS << HYPERBOLA << " ";
272 gp_Hypr2d C2d = H->Hypr2d();
273 if (!compact) OS << "\n Center :";
274 Print(C2d.Location(),OS,compact);
275 if (!compact) OS << "\n XAxis :";
276 Print(C2d.XAxis().Direction(),OS,compact);
277 if (!compact) OS << "\n YAxis :";
278 Print(C2d.YAxis().Direction(),OS,compact);
279 if (!compact) OS << "\n Radii :";
280 OS << C2d.MajorRadius();
281 if (!compact) OS << ",";
283 OS << C2d.MinorRadius();
284 if (!compact) OS << "\n";
288 //=======================================================================
291 //=======================================================================
293 static void Print(const Handle(Geom2d_BezierCurve)& B,
294 Standard_OStream& OS,
295 const Standard_Boolean compact)
302 Standard_Boolean rational = B->IsRational();
304 OS << (rational ? 1 : 0) << " ";
311 Standard_Integer i,degree = B->Degree();
312 if (!compact) OS << "\n Degree :";
315 for (i = 1; i <= degree+1; i++) {
316 if (!compact) OS << "\n "<<setw(2)<<i<<" : ";
317 Print(B->Pole(i),OS,compact);
319 OS << " " << B->Weight(i);
323 if (!compact) OS << "\n";
327 //=======================================================================
330 //=======================================================================
332 static void Print(const Handle(Geom2d_BSplineCurve)& B,
333 Standard_OStream& OS,
334 const Standard_Boolean compact)
337 OS << BSPLINE << " ";
339 OS << "BSplineCurve";
342 Standard_Boolean rational = B->IsRational();
344 OS << (rational ? 1 : 0) << " ";
350 Standard_Boolean periodic = B->IsPeriodic();
352 OS << (periodic ? 1 : 0) << " ";
359 Standard_Integer i,degree,nbpoles,nbknots;
360 degree = B->Degree();
361 nbpoles = B->NbPoles();
362 nbknots = B->NbKnots();
363 if (!compact) OS << "\n Degree ";
366 if (!compact) OS << ",";
369 if (!compact) OS << " Poles,";
371 OS << nbknots << " ";
372 if (!compact) OS << " Knots";
374 if (!compact) OS << "Poles :\n";
375 for (i = 1; i <= nbpoles; i++) {
376 if (!compact) OS << "\n "<<setw(2)<<i<<" : ";
378 Print(B->Pole(i),OS,compact);
380 OS << " " << B->Weight(i);
384 if (!compact) OS << "Knots :\n";
385 for (i = 1; i <= nbknots; i++) {
386 if (!compact) OS << "\n "<<setw(2)<<i<<" : ";
388 OS << B->Knot(i) << " " << B->Multiplicity(i);
390 if (!compact) OS << "\n";
394 //=======================================================================
397 //=======================================================================
399 static void Print(const Handle(Geom2d_TrimmedCurve)& C,
400 Standard_OStream& OS,
401 const Standard_Boolean compact)
404 OS << TRIMMED << " ";
406 OS << "Trimmed curve\n";
407 if (!compact) OS << "Parameters : ";
408 OS << C->FirstParameter() << " " << C->LastParameter() << "\n";
409 if (!compact) OS << "Basis curve :\n";
410 GeomTools_Curve2dSet::PrintCurve2d(C->BasisCurve(),OS,compact);
413 //=======================================================================
416 //=======================================================================
418 static void Print(const Handle(Geom2d_OffsetCurve)& C,
419 Standard_OStream& OS,
420 const Standard_Boolean compact)
426 if (!compact) OS << "Offset : ";
427 OS << C->Offset() << "\n";
428 if (!compact) OS << "Basis curve :\n";
429 GeomTools_Curve2dSet::PrintCurve2d(C->BasisCurve(),OS,compact);
432 //=======================================================================
433 //function : PrintCurve2d
435 //=======================================================================
437 void GeomTools_Curve2dSet::PrintCurve2d(const Handle(Geom2d_Curve)& C,
438 Standard_OStream& OS,
439 const Standard_Boolean compact)
441 Handle(Standard_Type) TheType = C->DynamicType();
443 if ( TheType == STANDARD_TYPE(Geom2d_Circle)) {
444 Print(Handle(Geom2d_Circle)::DownCast(C),OS,compact);
446 else if ( TheType ==STANDARD_TYPE(Geom2d_Line)) {
447 Print(Handle(Geom2d_Line)::DownCast(C),OS,compact);
449 else if ( TheType == STANDARD_TYPE(Geom2d_Ellipse)) {
450 Print(Handle(Geom2d_Ellipse)::DownCast(C),OS,compact);
452 else if ( TheType == STANDARD_TYPE(Geom2d_Parabola)) {
453 Print(Handle(Geom2d_Parabola)::DownCast(C),OS,compact);
455 else if ( TheType == STANDARD_TYPE(Geom2d_Hyperbola)) {
456 Print(Handle(Geom2d_Hyperbola)::DownCast(C),OS,compact);
458 else if ( TheType == STANDARD_TYPE(Geom2d_BezierCurve)) {
459 Print(Handle(Geom2d_BezierCurve)::DownCast(C),OS,compact);
461 else if ( TheType == STANDARD_TYPE(Geom2d_BSplineCurve)) {
462 Print(Handle(Geom2d_BSplineCurve)::DownCast(C),OS,compact);
464 else if ( TheType == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
465 Print(Handle(Geom2d_TrimmedCurve)::DownCast(C),OS,compact);
467 else if ( TheType == STANDARD_TYPE(Geom2d_OffsetCurve)) {
468 Print(Handle(Geom2d_OffsetCurve)::DownCast(C),OS,compact);
471 GeomTools::GetUndefinedTypeHandler()->PrintCurve2d(C,OS,compact);
473 // OS << "****** UNKNOWN CURVE2d TYPE ******\n";
475 // cout << "****** UNKNOWN CURVE2d TYPE ******" << endl;
479 //=======================================================================
482 //=======================================================================
484 void GeomTools_Curve2dSet::Dump(Standard_OStream& OS)const
486 Standard_Integer i, nbsurf = myMap.Extent();
487 OS << "\n -------\n";
488 OS << "Dump of "<< nbsurf << " Curve2ds ";
489 OS << "\n -------\n\n";
491 for (i = 1; i <= nbsurf; i++) {
492 OS << setw(4) << i << " : ";
493 PrintCurve2d(Handle(Geom2d_Curve)::DownCast(myMap(i)),OS,Standard_False);
498 //=======================================================================
501 //=======================================================================
503 void GeomTools_Curve2dSet::Write(Standard_OStream& OS)const
505 int prec = OS.precision(17);
507 Standard_Integer i, nbsurf = myMap.Extent();
508 OS << "Curve2ds "<< nbsurf << "\n";
510 Handle(Message_ProgressIndicator) progress = GetProgress();
511 Message_ProgressSentry PS(progress, "2D Curves", 0, nbsurf, 1);
513 for (i = 1; i <= nbsurf && PS.More(); i++, PS.Next()) {
514 if ( !myProgress.IsNull() )
516 PrintCurve2d(Handle(Geom2d_Curve)::DownCast(myMap(i)),OS,Standard_True);
523 //=======================================================================
524 //function : ReadPnt2d
526 //=======================================================================
528 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Pnt2d& P)
530 Standard_Real X=0.,Y=0.;
536 //=======================================================================
537 //function : ReadDir2d
539 //=======================================================================
541 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Dir2d& D)
543 Standard_Real X=0.,Y=0.;
550 //=======================================================================
551 //function : ReadCurve2d
553 //=======================================================================
555 static Standard_IStream& operator>>(Standard_IStream& IS,
556 Handle(Geom2d_Line)& L)
561 L = new Geom2d_Line(P,AX);
565 //=======================================================================
566 //function : ReadCurve2d
568 //=======================================================================
570 static Standard_IStream& operator>>(Standard_IStream& IS,
571 Handle(Geom2d_Circle)& C)
574 gp_Dir2d AX(1.,0.),AY(1.,0.);
576 IS >> P >> AX >> AY >> R;
577 C = new Geom2d_Circle(gp_Ax22d(P,AX,AY),R);
581 //=======================================================================
582 //function : ReadCurve2d
584 //=======================================================================
586 static Standard_IStream& operator>>(Standard_IStream& IS,
587 Handle(Geom2d_Ellipse)& E)
590 gp_Dir2d AX(1.,0.),AY(1.,0.);
591 Standard_Real R1=0.,R2=0.;
592 IS >> P >> AX >> AY >> R1 >> R2;
593 E = new Geom2d_Ellipse(gp_Ax22d(P,AX,AY),R1,R2);
597 //=======================================================================
598 //function : ReadCurve2d
600 //=======================================================================
602 static Standard_IStream& operator>>(Standard_IStream& IS,
603 Handle(Geom2d_Parabola)& C)
606 gp_Dir2d AX(1.,0.),AY(1.,0.);
608 IS >> P >> AX >> AY >> R1;
609 C = new Geom2d_Parabola(gp_Ax22d(P,AX,AY),R1);
613 //=======================================================================
614 //function : ReadCurve2d
616 //=======================================================================
618 static Standard_IStream& operator>>(Standard_IStream& IS,
619 Handle(Geom2d_Hyperbola)& H)
622 gp_Dir2d AX(1.,0.),AY(1.,0.);
623 Standard_Real R1=0.,R2=0.;
624 IS >> P >> AX >> AY >> R1 >> 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++) {
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++) {
685 TColStd_Array1OfReal knots(1,nbknots);
686 TColStd_Array1OfInteger mults(1,nbknots);
688 for (i = 1; i <= nbknots; i++) {
689 IS >> knots(i) >> mults(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.;
710 Handle(Geom2d_Curve) BC;
711 GeomTools_Curve2dSet::ReadCurve2d(IS,BC);
712 C = new Geom2d_TrimmedCurve(BC,p1,p2);
716 //=======================================================================
717 //function : ReadCurve2d
719 //=======================================================================
721 static Standard_IStream& operator>>(Standard_IStream& IS,
722 Handle(Geom2d_OffsetCurve)& C)
726 Handle(Geom2d_Curve) BC;
727 GeomTools_Curve2dSet::ReadCurve2d(IS,BC);
728 C = new Geom2d_OffsetCurve(BC,p);
732 //=======================================================================
733 //function : ReadCurve2d
735 //=======================================================================
737 Standard_IStream& GeomTools_Curve2dSet::ReadCurve2d(Standard_IStream& IS,
738 Handle(Geom2d_Curve)& C)
740 Standard_Integer ctype;
749 Handle(Geom2d_Line) CC;
757 Handle(Geom2d_Circle) CC;
765 Handle(Geom2d_Ellipse) CC;
773 Handle(Geom2d_Parabola) CC;
781 Handle(Geom2d_Hyperbola) CC;
789 Handle(Geom2d_BezierCurve) CC;
797 Handle(Geom2d_BSplineCurve) CC;
805 Handle(Geom2d_TrimmedCurve) CC;
813 Handle(Geom2d_OffsetCurve) CC;
821 Handle(Geom2d_Curve) CC;
822 GeomTools::GetUndefinedTypeHandler()->ReadCurve2d(ctype,IS,CC);
828 catch(Standard_Failure) {
830 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
831 cout <<"EXCEPTION in GeomTools_Curve2dSet::ReadCurve2d(..)!!!" << endl;
832 cout << anExc << endl;
839 //=======================================================================
842 //=======================================================================
844 void GeomTools_Curve2dSet::Read(Standard_IStream& IS)
848 if (strcmp(buffer,"Curve2ds")) {
849 cout << "Not a Curve2d table"<<endl;
853 Handle(Geom2d_Curve) C;
854 Standard_Integer i, nbcurve;
857 Handle(Message_ProgressIndicator) progress = GetProgress();
858 Message_ProgressSentry PS(progress, "2D Curves", 0, nbcurve, 1);
860 for (i = 1; i <= nbcurve && PS.More(); i++, PS.Next()) {
861 if ( !myProgress.IsNull() )
864 GeomTools_Curve2dSet::ReadCurve2d(IS,C);
869 //=======================================================================
870 //function : GetProgress
872 //=======================================================================
874 Handle(Message_ProgressIndicator) GeomTools_Curve2dSet::GetProgress() const
879 //=======================================================================
880 //function : SetProgress
882 //=======================================================================
884 void GeomTools_Curve2dSet::SetProgress(const Handle(Message_ProgressIndicator)& PR)