1 // Created on: 2004-05-20
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2004-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <BinTools.hxx>
18 #include <BinTools_CurveSet.hxx>
19 #include <Geom_BezierCurve.hxx>
20 #include <Geom_BSplineCurve.hxx>
21 #include <Geom_Circle.hxx>
22 #include <Geom_Curve.hxx>
23 #include <Geom_Ellipse.hxx>
24 #include <Geom_Hyperbola.hxx>
25 #include <Geom_Line.hxx>
26 #include <Geom_OffsetCurve.hxx>
27 #include <Geom_Parabola.hxx>
28 #include <Geom_TrimmedCurve.hxx>
29 #include <gp_Circ.hxx>
30 #include <gp_Elips.hxx>
31 #include <gp_Hypr.hxx>
33 #include <gp_Parab.hxx>
34 #include <Standard_ErrorHandler.hxx>
35 #include <Standard_Failure.hxx>
36 #include <Standard_OutOfRange.hxx>
37 #include <TColgp_Array1OfPnt.hxx>
38 #include <TColStd_Array1OfInteger.hxx>
39 #include <TColStd_Array1OfReal.hxx>
40 #include <Message_ProgressSentry.hxx>
52 //=======================================================================
53 //function : BinTools_CurveSet
55 //=======================================================================
57 BinTools_CurveSet::BinTools_CurveSet()
62 //=======================================================================
65 //=======================================================================
67 void BinTools_CurveSet::Clear()
73 //=======================================================================
76 //=======================================================================
78 Standard_Integer BinTools_CurveSet::Add(const Handle(Geom_Curve)& C)
80 return (C.IsNull()) ? 0 : myMap.Add(C);
84 //=======================================================================
87 //=======================================================================
89 Handle(Geom_Curve) BinTools_CurveSet::Curve
90 (const Standard_Integer I)const
93 Handle(Geom_Curve) dummy;
97 return Handle(Geom_Curve)::DownCast(myMap(I));
101 //=======================================================================
104 //=======================================================================
106 Standard_Integer BinTools_CurveSet::Index
107 (const Handle(Geom_Curve)& S)const
109 return S.IsNull() ? 0 : myMap.FindIndex(S);
113 //=======================================================================
114 //function : operator << (gp_Pnt)
116 //=======================================================================
118 static Standard_OStream& operator <<(Standard_OStream& OS, const gp_Pnt P)
120 BinTools::PutReal(OS, P.X());
121 BinTools::PutReal(OS, P.Y());
122 BinTools::PutReal(OS, P.Z());
126 //=======================================================================
127 //function : operator << (gp_Dir)
129 //=======================================================================
131 static Standard_OStream& operator <<(Standard_OStream& OS, const gp_Dir D)
133 BinTools::PutReal(OS, D.X());
134 BinTools::PutReal(OS, D.Y());
135 BinTools::PutReal(OS, D.Z());
140 //=======================================================================
141 //function : operator << (Geom_Line)
143 //=======================================================================
145 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_Line)& L)
147 OS << (Standard_Byte)LINE;
149 OS << C.Location();//Pnt
150 OS << C.Direction();//Dir
155 //=======================================================================
156 //function : operator <<(Geom_Circle)
158 //=======================================================================
160 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_Circle)& CC)
162 OS << (Standard_Byte)CIRCLE;
163 gp_Circ C = CC->Circ();
165 OS << C.Axis().Direction();
166 OS << C.XAxis().Direction();
167 OS << C.YAxis().Direction();
168 BinTools::PutReal(OS, C.Radius());
172 //=======================================================================
173 //function : operator <<(Geom_Ellipse)
175 //=======================================================================
177 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_Ellipse)& E)
179 OS << (Standard_Byte)ELLIPSE;
180 gp_Elips C = E->Elips();
182 OS << C.Axis().Direction();
183 OS << C.XAxis().Direction();
184 OS << C.YAxis().Direction();
185 BinTools::PutReal(OS, C.MajorRadius());
186 BinTools::PutReal(OS, C.MinorRadius());
190 //=======================================================================
191 //function : operator <<(Geom_Parabola)
193 //=======================================================================
195 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_Parabola)& P)
197 OS << (Standard_Byte)PARABOLA;
198 gp_Parab C = P->Parab();
200 OS << C.Axis().Direction();
201 OS << C.XAxis().Direction();
202 OS << C.YAxis().Direction();
203 BinTools::PutReal(OS, C.Focal());
207 //=======================================================================
208 //function : operator <<(Geom_Hyperbola)
210 //=======================================================================
212 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_Hyperbola)& H)
214 OS << (Standard_Byte)HYPERBOLA;
215 gp_Hypr C = H->Hypr();
217 OS << C.Axis().Direction();
218 OS << C.XAxis().Direction();
219 OS << C.YAxis().Direction();
220 BinTools::PutReal(OS, C.MajorRadius());
221 BinTools::PutReal(OS, C.MinorRadius());
225 //=======================================================================
226 //function : operator <<(Geom_BezierCurve)
228 //=======================================================================
230 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_BezierCurve)& B)
232 OS << (Standard_Byte)BEZIER;
233 Standard_Boolean aRational = B->IsRational() ? 1:0;
234 BinTools::PutBool(OS, aRational); //rational
236 Standard_Integer i,aDegree = B->Degree();
237 BinTools::PutExtChar(OS, (Standard_ExtCharacter)aDegree); //<< Degree
238 for (i = 1; i <= aDegree+1; i++) {
239 OS << B->Pole(i); //Pnt
241 BinTools::PutReal(OS, B->Weight(i));//Real
246 //=======================================================================
247 //function : operator <<(Geom_BSplineCurve)
249 //=======================================================================
251 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_BSplineCurve)& B)
253 OS << (Standard_Byte)BSPLINE;
254 Standard_Boolean aRational = B->IsRational() ? 1:0;
255 BinTools::PutBool(OS, aRational); //rational
256 Standard_Boolean aPeriodic = B->IsPeriodic() ? 1:0;
257 BinTools::PutBool(OS, aPeriodic); //periodic
259 Standard_Integer i,aDegree,aNbPoles,aNbKnots;
260 aDegree = B->Degree();
261 aNbPoles = B->NbPoles();
262 aNbKnots = B->NbKnots();
263 BinTools::PutExtChar(OS, (Standard_ExtCharacter) aDegree);
264 BinTools::PutInteger(OS, aNbPoles);
265 BinTools::PutInteger(OS, aNbKnots);
266 for (i = 1; i <= aNbPoles; i++) {
267 OS << B->Pole(i); // Pnt
269 BinTools::PutReal(OS, B->Weight(i));
272 for (i = 1; i <= aNbKnots; i++) {
273 BinTools::PutReal(OS, B->Knot(i));
274 BinTools::PutInteger(OS, B->Multiplicity(i));
279 //=======================================================================
280 //function : operator <<(Geom_TrimmedCurve)
282 //=======================================================================
284 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_TrimmedCurve)& C)
286 OS << (Standard_Byte)TRIMMED;
287 BinTools::PutReal(OS, C->FirstParameter());
288 BinTools::PutReal(OS, C->LastParameter());
289 BinTools_CurveSet::WriteCurve(C->BasisCurve(),OS);
293 //=======================================================================
294 //function : operator <<(Geom_OffsetCurve)
296 //=======================================================================
298 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_OffsetCurve)& C)
300 OS << (Standard_Byte)OFFSET;
301 BinTools::PutReal(OS,C->Offset());//Offset
302 OS << C->Direction();
303 BinTools_CurveSet::WriteCurve(C->BasisCurve(),OS);
307 //=======================================================================
310 //=======================================================================
312 void BinTools_CurveSet::WriteCurve(const Handle(Geom_Curve)& C,
313 Standard_OStream& OS)
315 Handle(Standard_Type) TheType = C->DynamicType();
318 if ( TheType ==STANDARD_TYPE(Geom_Line)) {
319 OS << Handle(Geom_Line)::DownCast(C);
321 else if ( TheType == STANDARD_TYPE(Geom_Circle)) {
322 OS << Handle(Geom_Circle)::DownCast(C);
324 else if ( TheType == STANDARD_TYPE(Geom_Ellipse)) {
325 OS << Handle(Geom_Ellipse)::DownCast(C);
327 else if ( TheType == STANDARD_TYPE(Geom_Parabola)) {
328 OS << Handle(Geom_Parabola)::DownCast(C);
330 else if ( TheType == STANDARD_TYPE(Geom_Hyperbola)) {
331 OS << Handle(Geom_Hyperbola)::DownCast(C);
333 else if ( TheType == STANDARD_TYPE(Geom_BezierCurve)) {
334 OS << Handle(Geom_BezierCurve)::DownCast(C);
336 else if ( TheType == STANDARD_TYPE(Geom_BSplineCurve)) {
337 OS << Handle(Geom_BSplineCurve)::DownCast(C);
339 else if ( TheType == STANDARD_TYPE(Geom_TrimmedCurve)) {
340 OS << Handle(Geom_TrimmedCurve)::DownCast(C);
342 else if ( TheType == STANDARD_TYPE(Geom_OffsetCurve)) {
343 OS << Handle(Geom_OffsetCurve)::DownCast(C);
346 throw Standard_Failure("UNKNOWN CURVE TYPE");
349 catch(Standard_Failure const& anException) {
350 Standard_SStream aMsg;
351 aMsg << "EXCEPTION in BinTools_CurveSet::WriteCurve(..)" << std::endl;
352 aMsg << anException << std::endl;
353 throw Standard_Failure(aMsg.str().c_str());
357 //=======================================================================
360 //=======================================================================
362 void BinTools_CurveSet::Write (Standard_OStream& OS,
363 const Handle(Message_ProgressIndicator)& theProgress)const
365 Standard_Integer i, nbcurv = myMap.Extent();
366 Message_ProgressSentry aPS(theProgress, "Writing curves", 0, nbcurv, 1);
367 OS << "Curves "<< nbcurv << "\n";
368 for (i = 1; i <= nbcurv &&aPS.More(); i++, aPS.Next()) {
369 WriteCurve(Handle(Geom_Curve)::DownCast(myMap(i)),OS);
374 //=======================================================================
377 //=======================================================================
379 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Pnt& P)
381 Standard_Real X=0.,Y=0.,Z=0.;
382 BinTools::GetReal(IS, X);
383 BinTools::GetReal(IS, Y);
384 BinTools::GetReal(IS, Z);
389 //=======================================================================
392 //=======================================================================
394 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Dir& D)
396 Standard_Real X=0.,Y=0.,Z=0.;
397 BinTools::GetReal(IS, X);
398 BinTools::GetReal(IS, Y);
399 BinTools::GetReal(IS, Z);
405 //=======================================================================
406 //function : ReadCurve
408 //=======================================================================
410 static Standard_IStream& operator>>(Standard_IStream& IS,
411 Handle(Geom_Line)& L)
416 L = new Geom_Line(P,AX);
420 //=======================================================================
421 //function : ReadCurve
423 //=======================================================================
425 static Standard_IStream& operator>>(Standard_IStream& IS,
426 Handle(Geom_Circle)& C)
429 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
431 IS >> P >> A >> AX >> AY;
432 BinTools::GetReal(IS, R);
433 C = new Geom_Circle(gp_Ax2(P,A,AX),R);
437 //=======================================================================
438 //function : ReadCurve
440 //=======================================================================
442 static Standard_IStream& operator>>(Standard_IStream& IS,
443 Handle(Geom_Ellipse)& E)
446 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
447 Standard_Real R1=0.,R2=0.;
448 IS >> P >> A >> AX >> AY;
449 BinTools::GetReal(IS, R1);
450 BinTools::GetReal(IS, R2);
451 E = new Geom_Ellipse(gp_Ax2(P,A,AX),R1,R2);
455 //=======================================================================
456 //function : ReadCurve
458 //=======================================================================
460 static Standard_IStream& operator>>(Standard_IStream& IS,
461 Handle(Geom_Parabola)& C)
464 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
466 IS >> P >> A >> AX >> AY;
467 BinTools::GetReal(IS, R1);
468 C = new Geom_Parabola(gp_Ax2(P,A,AX),R1);
472 //=======================================================================
473 //function : ReadCurve
475 //=======================================================================
477 static Standard_IStream& operator>>(Standard_IStream& IS,
478 Handle(Geom_Hyperbola)& H)
481 gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0);
482 Standard_Real R1=0.,R2=0.;
483 IS >> P >> A >> AX >> AY;
484 BinTools::GetReal(IS, R1);
485 BinTools::GetReal(IS, R2);
486 H = new Geom_Hyperbola(gp_Ax2(P,A,AX),R1,R2);
490 //=======================================================================
491 //function : ReadCurve
493 //=======================================================================
495 static Standard_IStream& operator>>(Standard_IStream& IS,
496 Handle(Geom_BezierCurve)& B)
498 Standard_Boolean rational=Standard_False;
499 BinTools::GetBool(IS, rational);
502 Standard_Integer i=0,degree=0;
504 Standard_ExtCharacter aVal='\0';
505 BinTools::GetExtChar(IS, aVal);
506 degree = (Standard_Integer)aVal;
508 TColgp_Array1OfPnt poles(1,degree+1);
509 TColStd_Array1OfReal weights(1,degree+1);
511 for (i = 1; i <= degree+1; i++) {
515 BinTools::GetReal(IS, weights(i));
519 B = new Geom_BezierCurve(poles,weights);
521 B = new Geom_BezierCurve(poles);
526 //=======================================================================
527 //function : ReadCurve
529 //=======================================================================
531 static Standard_IStream& operator>>(Standard_IStream& IS,
532 Handle(Geom_BSplineCurve)& B)
535 Standard_Boolean rational=Standard_False,periodic=Standard_False;
536 BinTools::GetBool(IS, rational);
537 BinTools::GetBool(IS, periodic);
540 Standard_Integer i=0,degree=0,nbpoles=0,nbknots=0;
541 Standard_ExtCharacter aVal='\0';
542 BinTools::GetExtChar(IS, aVal);
543 degree = (Standard_Integer)aVal;
545 BinTools::GetInteger(IS, nbpoles);
547 BinTools::GetInteger(IS, nbknots);
549 TColgp_Array1OfPnt poles(1,nbpoles);
550 TColStd_Array1OfReal weights(1,nbpoles);
552 for (i = 1; i <= nbpoles; i++) {
555 BinTools::GetReal(IS, weights(i));
558 TColStd_Array1OfReal knots(1,nbknots);
559 TColStd_Array1OfInteger mults(1,nbknots);
561 for (i = 1; i <= nbknots; i++) {
562 BinTools::GetReal(IS, knots(i));
563 BinTools::GetInteger(IS, mults(i));
567 B = new Geom_BSplineCurve(poles,weights,knots,mults,degree,periodic);
569 B = new Geom_BSplineCurve(poles,knots,mults,degree,periodic);
574 //=======================================================================
575 //function : ReadCurve
577 //=======================================================================
579 static Standard_IStream& operator>>(Standard_IStream& IS,
580 Handle(Geom_TrimmedCurve)& C)
582 Standard_Real p1=0.,p2=0.;
583 BinTools::GetReal(IS, p1);//FirstParameter
584 BinTools::GetReal(IS, p2);//LastParameter
585 Handle(Geom_Curve) BC;
586 BinTools_CurveSet::ReadCurve(IS,BC);
587 C = new Geom_TrimmedCurve(BC,p1,p2);
591 //=======================================================================
592 //function : ReadCurve
594 //=======================================================================
596 static Standard_IStream& operator>>(Standard_IStream& IS,
597 Handle(Geom_OffsetCurve)& C)
600 BinTools::GetReal(IS, p);//Offset
603 Handle(Geom_Curve) BC;
604 BinTools_CurveSet::ReadCurve(IS,BC);
605 C = new Geom_OffsetCurve(BC,p,D);
609 //=======================================================================
610 //function : ReadCurve
612 //=======================================================================
614 Standard_IStream& BinTools_CurveSet::ReadCurve(Standard_IStream& IS,
615 Handle(Geom_Curve)& C)
619 const Standard_Byte ctype = (Standard_Byte) IS.get();
625 Handle(Geom_Line) CC;
633 Handle(Geom_Circle) CC;
641 Handle(Geom_Ellipse) CC;
649 Handle(Geom_Parabola) CC;
657 Handle(Geom_Hyperbola) CC;
665 Handle(Geom_BezierCurve) CC;
673 Handle(Geom_BSplineCurve) CC;
681 Handle(Geom_TrimmedCurve) CC;
689 Handle(Geom_OffsetCurve) CC;
698 throw Standard_Failure("UNKNOWN CURVE TYPE");
702 catch(Standard_Failure const& anException) {
704 Standard_SStream aMsg;
705 aMsg <<"EXCEPTION in BinTools_CurveSet::ReadCurve(..)" << std::endl;
706 aMsg << anException << std::endl;
707 throw Standard_Failure(aMsg.str().c_str());
712 //=======================================================================
715 //=======================================================================
717 void BinTools_CurveSet::Read (Standard_IStream& IS,
718 const Handle(Message_ProgressIndicator)& theProgress)
722 if (IS.fail() || strcmp(buffer,"Curves")) {
723 Standard_SStream aMsg;
724 aMsg << "BinTools_CurveSet::Read: Not a Curve table"<<std::endl;
726 std::cout <<"CurveSet buffer: " << buffer << std::endl;
728 throw Standard_Failure(aMsg.str().c_str());
732 Handle(Geom_Curve) C;
733 Standard_Integer i, nbcurve;
736 Message_ProgressSentry aPS(theProgress, "Reading curves", 0, nbcurve, 1);
738 IS.get();//remove <lf>
739 for (i = 1; i <= nbcurve && aPS.More(); i++, aPS.Next()) {
740 BinTools_CurveSet::ReadCurve(IS,C);