1 // Created on: 2004-05-11
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_Curve2dSet.hxx>
19 #include <BinTools_CurveSet.hxx>
20 #include <BinTools_LocationSet.hxx>
21 #include <BinTools_ShapeSet.hxx>
22 #include <BinTools_SurfaceSet.hxx>
23 #include <BRep_CurveOnClosedSurface.hxx>
24 #include <BRep_CurveOnSurface.hxx>
25 #include <BRep_CurveRepresentation.hxx>
26 #include <BRep_GCurve.hxx>
27 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
28 #include <BRep_ListIteratorOfListOfPointRepresentation.hxx>
29 #include <BRep_PointOnCurve.hxx>
30 #include <BRep_PointOnCurveOnSurface.hxx>
31 #include <BRep_PointOnSurface.hxx>
32 #include <BRep_PointRepresentation.hxx>
33 #include <BRep_Polygon3D.hxx>
34 #include <BRep_PolygonOnTriangulation.hxx>
35 #include <BRep_TEdge.hxx>
36 #include <BRep_TFace.hxx>
37 #include <BRep_Tool.hxx>
38 #include <BRep_TVertex.hxx>
39 #include <BRepTools.hxx>
40 #include <gp_Trsf.hxx>
41 #include <Poly_Polygon2D.hxx>
42 #include <Poly_Polygon3D.hxx>
43 #include <Poly_PolygonOnTriangulation.hxx>
44 #include <Poly_Triangulation.hxx>
45 #include <Precision.hxx>
46 #include <Standard_ErrorHandler.hxx>
47 #include <TColgp_Array1OfPnt2d.hxx>
48 #include <TColStd_HArray1OfInteger.hxx>
49 #include <TColStd_HArray1OfReal.hxx>
51 #include <TopoDS_Iterator.hxx>
52 #include <TopoDS_Shape.hxx>
53 #include <TopoDS_Vertex.hxx>
57 const char* Version_1 = "Open CASCADE Topology V1 (c)";
58 const char* Version_2 = "Open CASCADE Topology V2 (c)";
59 const char* Version_3 = "Open CASCADE Topology V3 (c)";
60 //=======================================================================
61 //function : operator << (gp_Pnt)
63 //=======================================================================
65 static Standard_OStream& operator <<(Standard_OStream& OS, const gp_Pnt P)
67 BinTools::PutReal(OS, P.X());
68 BinTools::PutReal(OS, P.Y());
69 BinTools::PutReal(OS, P.Z());
72 //=======================================================================
73 //function : BinTools_ShapeSet
75 //=======================================================================
77 BinTools_ShapeSet::BinTools_ShapeSet(const Standard_Boolean isWithTriangles)
78 :myFormatNb(3), myWithTriangles(isWithTriangles)
81 //=======================================================================
82 //function : ~BinTools_ShapeSet
84 //=======================================================================
86 BinTools_ShapeSet::~BinTools_ShapeSet()
89 //=======================================================================
90 //function : SetFormatNb
92 //=======================================================================
93 void BinTools_ShapeSet::SetFormatNb(const Standard_Integer theFormatNb)
95 myFormatNb = theFormatNb;
98 //=======================================================================
101 //=======================================================================
102 Standard_Integer BinTools_ShapeSet::FormatNb() const
107 //=======================================================================
110 //=======================================================================
112 void BinTools_ShapeSet::Clear()
117 myPolygons3D.Clear();
118 myPolygons2D.Clear();
120 myTriangulations.Clear();
124 //=======================================================================
127 //=======================================================================
129 Standard_Integer BinTools_ShapeSet::Add(const TopoDS_Shape& theShape)
131 if (theShape.IsNull()) return 0;
132 myLocations.Add(theShape.Location());
133 TopoDS_Shape aS2 = theShape;
134 aS2.Location(TopLoc_Location());
135 Standard_Integer anIndex = myShapes.FindIndex(aS2);
138 for (TopoDS_Iterator its(aS2,Standard_False,Standard_False);its.More(); its.Next())
140 anIndex = myShapes.Add(aS2);
146 //=======================================================================
149 //=======================================================================
151 const TopoDS_Shape& BinTools_ShapeSet::Shape(const Standard_Integer theIndx)const
153 return myShapes(theIndx);
156 //=======================================================================
159 //=======================================================================
161 Standard_Integer BinTools_ShapeSet::Index(const TopoDS_Shape& theShape) const
163 return myShapes.FindIndex(theShape);
166 //=======================================================================
167 //function : Locations
169 //=======================================================================
171 const BinTools_LocationSet& BinTools_ShapeSet::Locations()const
177 //=======================================================================
178 //function : ChangeLocations
180 //=======================================================================
182 BinTools_LocationSet& BinTools_ShapeSet::ChangeLocations()
187 //=======================================================================
188 //function : AddGeometry
190 //=======================================================================
192 void BinTools_ShapeSet::AddGeometry(const TopoDS_Shape& S)
196 if (S.ShapeType() == TopAbs_VERTEX) {
198 Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(S.TShape());
199 BRep_ListIteratorOfListOfPointRepresentation itrp(TV->Points());
201 while (itrp.More()) {
202 const Handle(BRep_PointRepresentation)& PR = itrp.Value();
204 if (PR->IsPointOnCurve()) {
205 myCurves.Add(PR->Curve());
208 else if (PR->IsPointOnCurveOnSurface()) {
209 myCurves2d.Add(PR->PCurve());
210 mySurfaces.Add(PR->Surface());
213 else if (PR->IsPointOnSurface()) {
214 mySurfaces.Add(PR->Surface());
217 ChangeLocations().Add(PR->Location());
222 else if (S.ShapeType() == TopAbs_EDGE) {
224 // Add the curve geometry
225 Handle(BRep_TEdge) TE = Handle(BRep_TEdge)::DownCast(S.TShape());
226 BRep_ListIteratorOfListOfCurveRepresentation itrc(TE->Curves());
228 while (itrc.More()) {
229 const Handle(BRep_CurveRepresentation)& CR = itrc.Value();
230 if (CR->IsCurve3D()) {
231 if (!CR->Curve3D().IsNull()) {
232 myCurves.Add(CR->Curve3D());
233 ChangeLocations().Add(CR->Location());
236 else if (CR->IsCurveOnSurface()) {
237 mySurfaces.Add(CR->Surface());
238 myCurves2d.Add(CR->PCurve());
239 ChangeLocations().Add(CR->Location());
240 if (CR->IsCurveOnClosedSurface())
241 myCurves2d.Add(CR->PCurve2());
243 else if (CR->IsRegularity()) {
244 mySurfaces.Add(CR->Surface());
245 ChangeLocations().Add(CR->Location());
246 mySurfaces.Add(CR->Surface2());
247 ChangeLocations().Add(CR->Location2());
249 else if (myWithTriangles) {
250 if (CR->IsPolygon3D()) {
251 if (!CR->Polygon3D().IsNull()) {
252 myPolygons3D.Add(CR->Polygon3D());
253 ChangeLocations().Add(CR->Location());
256 else if (CR->IsPolygonOnTriangulation()) {
257 myTriangulations.Add(CR->Triangulation());
258 myNodes.Add(CR->PolygonOnTriangulation());
259 ChangeLocations().Add(CR->Location());
260 if (CR->IsPolygonOnClosedTriangulation())
261 myNodes.Add(CR->PolygonOnTriangulation2());
263 else if (CR->IsPolygonOnSurface()) {
264 mySurfaces.Add(CR->Surface());
265 myPolygons2D.Add(CR->Polygon());
266 ChangeLocations().Add(CR->Location());
267 if (CR->IsPolygonOnClosedSurface())
268 myPolygons2D.Add(CR->Polygon2());
275 else if (S.ShapeType() == TopAbs_FACE) {
277 // Add the surface geometry
278 Handle(BRep_TFace) TF = Handle(BRep_TFace)::DownCast(S.TShape());
279 if (!TF->Surface().IsNull()) mySurfaces.Add(TF->Surface());
282 || TF->Surface().IsNull())
284 Handle(Poly_Triangulation) Tr = TF->Triangulation();
285 if (!Tr.IsNull()) myTriangulations.Add(Tr);
288 ChangeLocations().Add(TF->Location());
292 //=======================================================================
293 //function : WriteGeometry
295 //=======================================================================
297 void BinTools_ShapeSet::WriteGeometry(Standard_OStream& OS)const
299 myCurves2d.Write(OS);
302 WritePolygonOnTriangulation(OS);
303 mySurfaces.Write(OS);
304 WriteTriangulation(OS);
307 //=======================================================================
310 //=======================================================================
312 void BinTools_ShapeSet::Write(Standard_OStream& OS)const
315 // write the copyright
317 OS << "\n" << Version_3 << "\n";
318 else if (myFormatNb == 2)
319 OS << "\n" << Version_2 << "\n";
321 OS << "\n" << Version_1 << "\n";
323 //-----------------------------------------
324 // write the locations
325 //-----------------------------------------
327 myLocations.Write(OS);
329 //-----------------------------------------
330 // write the geometry
331 //-----------------------------------------
335 //-----------------------------------------
337 //-----------------------------------------
339 Standard_Integer i, nbShapes = myShapes.Extent();
341 OS << "\nTShapes " << nbShapes << "\n";
343 // subshapes are written first
344 for (i = 1; i <= nbShapes; i++) {
346 const TopoDS_Shape& S = myShapes(i);
349 OS << (Standard_Byte)S.ShapeType();
355 BinTools::PutBool(OS, S.Free()? 1:0);
356 BinTools::PutBool(OS, S.Modified()? 1:0);
357 BinTools::PutBool(OS, S.Checked()? 1:0);
358 BinTools::PutBool(OS, S.Orientable()? 1:0);
359 BinTools::PutBool(OS, S.Closed()? 1:0);
360 BinTools::PutBool(OS, S.Infinite()? 1:0);
361 BinTools::PutBool(OS, S.Convex()? 1:0);
365 TopoDS_Iterator its(S,Standard_False,Standard_False);
367 Write(its.Value(),OS);
370 Write(TopoDS_Shape(),OS); // Null shape to end the list
375 //=======================================================================
378 //=======================================================================
380 void BinTools_ShapeSet::Read(Standard_IStream& IS)
388 IS.getline(vers,100,'\n');
389 // BUC60769 PTV 18.10.2000: remove possible '\r' at the end of the line
391 Standard_Size lv = strlen(vers);
393 for (lv--; lv > 0 && (vers[lv] == '\r' || vers[lv] == '\n'); lv--)
397 } while ( ! IS.fail() && strcmp(vers,Version_1) && strcmp(vers,Version_2) &&
398 strcmp(vers,Version_3));
400 std::cout << "BinTools_ShapeSet::Read: File was not written with this version of the topology"<<std::endl;
404 if (strcmp(vers,Version_3) == 0) SetFormatNb(3);
405 else if (strcmp(vers,Version_2) == 0) SetFormatNb(2);
408 //-----------------------------------------
409 // read the locations
410 //-----------------------------------------
412 myLocations.Read(IS);
413 //-----------------------------------------
415 //-----------------------------------------
419 //-----------------------------------------
421 //-----------------------------------------
425 if (IS.fail() || strcmp(buffer,"TShapes")) {
426 Standard_SStream aMsg;
427 aMsg << "BinTools_ShapeSet::Read: Not a TShape table"<<std::endl;
428 throw Standard_Failure(aMsg.str().c_str());
432 Standard_Integer nbShapes = 0;
436 for (int i = 1; i <= nbShapes; i++) {
440 //Read type and create empty shape.
442 TopAbs_ShapeEnum T = (TopAbs_ShapeEnum) IS.get();
444 ReadGeometry(T,IS,S);
447 Standard_Boolean aFree, aMod, aChecked, anOrient, aClosed, anInf, aConv;
448 BinTools::GetBool(IS, aFree);
449 BinTools::GetBool(IS, aMod);
450 BinTools::GetBool(IS, aChecked);
451 BinTools::GetBool(IS, anOrient);
452 BinTools::GetBool(IS, aClosed);
453 BinTools::GetBool(IS, anInf);
454 BinTools::GetBool(IS, aConv);
459 Read(SS,IS,nbShapes);
462 } while(!SS.IsNull());
469 S.Checked (Standard_False); // force check at reading..
470 S.Orientable(anOrient);
477 if(T == TopAbs_FACE) {
478 const TopoDS_Face& F = TopoDS::Face(S);
479 BRepTools::Update(F);
485 //=======================================================================
488 //=======================================================================
490 void BinTools_ShapeSet::Write(const TopoDS_Shape& S, Standard_OStream& OS)const
496 // {TopAbs_FORWARD, TopAbs_REVERSED, TopAbs_INTERNAL, TopAbs_EXTERNAL}
497 OS << (Standard_Byte) S.Orientation();
498 BinTools::PutInteger(OS, myShapes.Extent() - myShapes.FindIndex(S.Located(TopLoc_Location())) + 1);
499 BinTools::PutInteger(OS, Locations().Index(S.Location()));
503 //=======================================================================
506 //=======================================================================
508 void BinTools_ShapeSet::Read(TopoDS_Shape& S, Standard_IStream& IS,
509 const Standard_Integer nbshapes)const
511 Standard_Character aChar = '\0';
516 TopAbs_Orientation anOrient;
517 anOrient = (TopAbs_Orientation)aChar;
518 Standard_Integer anIndx;
519 BinTools::GetInteger(IS, anIndx);
520 S = myShapes(nbshapes - anIndx + 1);
521 S.Orientation(anOrient);
524 BinTools::GetInteger(IS, l);
525 S.Location(myLocations.Location(l));
529 //=======================================================================
530 //function : ReadGeometry
532 //=======================================================================
534 void BinTools_ShapeSet::ReadGeometry(Standard_IStream& IS)
539 ReadPolygonOnTriangulation(IS);
541 ReadTriangulation(IS);
544 //=======================================================================
545 //function : WriteGeometry
547 //=======================================================================
549 void BinTools_ShapeSet::WriteGeometry(const TopoDS_Shape& S,
550 Standard_OStream& OS)const
552 // Write the geometry
555 if (S.ShapeType() == TopAbs_VERTEX) {
557 // Write the point geometry
558 TopoDS_Vertex V = TopoDS::Vertex(S);
559 BinTools::PutReal(OS, BRep_Tool::Tolerance(V));
560 gp_Pnt p = BRep_Tool::Pnt(V);
562 #ifdef OCCT_DEBUG_POS
565 Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(S.TShape());
566 BRep_ListIteratorOfListOfPointRepresentation itrp(TV->Points());
567 while (itrp.More()) {
568 const Handle(BRep_PointRepresentation)& PR = itrp.Value();
569 // BinTools::PutReal(OS, PR->Parameter());
570 if (PR->IsPointOnCurve()) {
571 #ifdef OCCT_DEBUG_POS
574 OS << (Standard_Byte)1; // 1
575 BinTools::PutReal(OS, PR->Parameter());
576 BinTools::PutInteger(OS, myCurves.Index(PR->Curve()));
579 else if (PR->IsPointOnCurveOnSurface()) {
580 #ifdef OCCT_DEBUG_POS
583 OS << (Standard_Byte)2;// 2
584 BinTools::PutReal(OS, PR->Parameter());
585 BinTools::PutInteger(OS, myCurves2d.Index(PR->PCurve()));
586 BinTools::PutInteger(OS, mySurfaces.Index(PR->Surface()));
589 else if (PR->IsPointOnSurface()) {
590 #ifdef OCCT_DEBUG_POS
593 OS << (Standard_Byte)3;// 3
594 BinTools::PutReal(OS, PR->Parameter2());
595 BinTools::PutReal(OS, PR->Parameter());
596 BinTools::PutInteger(OS, mySurfaces.Index(PR->Surface()));
598 BinTools::PutInteger(OS, Locations().Index(PR->Location()));
602 // OS << "0 0\n"; // end representations
603 OS.put((Standard_Byte)0);
606 else if (S.ShapeType() == TopAbs_EDGE) {
608 // Write the curve geometry
610 Handle(BRep_TEdge) TE = Handle(BRep_TEdge)::DownCast(S.TShape());
612 BinTools::PutReal(OS, TE->Tolerance());
614 Standard_Boolean aVal = (TE->SameParameter()) ? Standard_True : Standard_False;
615 BinTools::PutBool(OS, aVal);
616 aVal = (TE->SameRange()) ? Standard_True : Standard_False;
617 BinTools::PutBool(OS, aVal);
618 aVal = (TE->Degenerated()) ? Standard_True : Standard_False;
619 BinTools::PutBool(OS, aVal);
621 Standard_Real first, last;
622 BRep_ListIteratorOfListOfCurveRepresentation itrc = TE->Curves();
623 while (itrc.More()) {
624 const Handle(BRep_CurveRepresentation)& CR = itrc.Value();
625 if (CR->IsCurve3D()) {
626 if (!CR->Curve3D().IsNull()) {
627 Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itrc.Value());
628 GC->Range(first, last);
629 OS << (Standard_Byte)1;//CURVE_3D;
630 BinTools::PutInteger(OS, myCurves.Index(CR->Curve3D()));
631 BinTools::PutInteger(OS, Locations().Index(CR->Location()));
632 BinTools::PutReal(OS, first);
633 BinTools::PutReal(OS, last);
636 else if (CR->IsCurveOnSurface()) {
637 Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itrc.Value());
638 GC->Range(first, last);
639 if (!CR->IsCurveOnClosedSurface())
641 OS << (Standard_Byte)2;
643 // -3- Curve on closed surf
644 OS << (Standard_Byte)3;
645 BinTools::PutInteger(OS, myCurves2d.Index(CR->PCurve()));
646 if (CR->IsCurveOnClosedSurface()) {//+ int|char
647 BinTools::PutInteger(OS, myCurves2d.Index(CR->PCurve2()));
648 OS << (Standard_Byte)CR->Continuity();
650 BinTools::PutInteger(OS, mySurfaces.Index(CR->Surface()));
651 BinTools::PutInteger(OS, Locations().Index(CR->Location()));
652 BinTools::PutReal(OS, first);
653 BinTools::PutReal(OS, last);
655 // Write UV Points for higher performance
659 if (CR->IsCurveOnClosedSurface()) {
660 Handle(BRep_CurveOnClosedSurface) COCS =
661 Handle(BRep_CurveOnClosedSurface)::DownCast(CR);
662 COCS->UVPoints2(Pf,Pl);
665 Handle(BRep_CurveOnSurface) COS =
666 Handle(BRep_CurveOnSurface)::DownCast(CR);
667 COS->UVPoints(Pf,Pl);
669 BinTools::PutReal(OS, Pf.X());
670 BinTools::PutReal(OS, Pf.Y());
671 BinTools::PutReal(OS, Pl.X());
672 BinTools::PutReal(OS, Pl.Y());
675 else if (CR->IsRegularity()) {
677 OS << (Standard_Byte)4;
678 OS << (Standard_Byte)CR->Continuity();
679 BinTools::PutInteger(OS, mySurfaces.Index(CR->Surface()));
680 BinTools::PutInteger(OS, Locations().Index(CR->Location()));
681 BinTools::PutInteger(OS, mySurfaces.Index(CR->Surface2()));
682 BinTools::PutInteger(OS, Locations().Index(CR->Location2()));
686 else if (myWithTriangles) {
687 if (CR->IsPolygon3D()) {
688 Handle(BRep_Polygon3D) GC = Handle(BRep_Polygon3D)::DownCast(itrc.Value());
689 if (!GC->Polygon3D().IsNull()) {
691 OS << (Standard_Byte)5;
692 BinTools::PutInteger(OS, myPolygons3D.FindIndex(CR->Polygon3D()));
693 BinTools::PutInteger(OS, Locations().Index(CR->Location()));
696 else if (CR->IsPolygonOnTriangulation()) {
697 Handle(BRep_PolygonOnTriangulation) PT =
698 Handle(BRep_PolygonOnTriangulation)::DownCast(itrc.Value());
699 if (!CR->IsPolygonOnClosedTriangulation())
700 // -6- Polygon on triangulation
701 OS << (Standard_Byte)6;
703 // -7- Polygon on closed triangulation
704 OS << (Standard_Byte)7;
705 BinTools::PutInteger(OS, myNodes.FindIndex(PT->PolygonOnTriangulation()));
707 if (CR->IsPolygonOnClosedTriangulation()) {
708 BinTools::PutInteger(OS, myNodes.FindIndex(PT->PolygonOnTriangulation2()));
710 BinTools::PutInteger(OS, myTriangulations.FindIndex(PT->Triangulation()));
711 BinTools::PutInteger(OS, Locations().Index(CR->Location()));
717 // OS << "0\n"; // end of the list of representations
719 OS << (Standard_Byte)0;
722 else if (S.ShapeType() == TopAbs_FACE) {
724 Handle(BRep_TFace) TF = Handle(BRep_TFace)::DownCast(S.TShape());
725 const TopoDS_Face& F = TopoDS::Face(S);
727 // Write the surface geometry
728 Standard_Boolean aNatRes = BRep_Tool::NaturalRestriction(F);
729 BinTools::PutBool (OS, aNatRes);
730 BinTools::PutReal (OS, TF->Tolerance());
731 BinTools::PutInteger (OS, !TF->Surface().IsNull()
732 ? mySurfaces.Index (TF->Surface())
734 BinTools::PutInteger (OS, Locations().Index (TF->Location()));
737 || TF->Surface().IsNull())
739 if (!(TF->Triangulation()).IsNull()) {
740 OS << (Standard_Byte) 2;
741 // Write the triangulation
742 BinTools::PutInteger(OS, myTriangulations.FindIndex(TF->Triangulation()));
744 OS << (Standard_Byte) 1;
746 OS << (Standard_Byte) 0;//without triangulation
749 catch(Standard_Failure const& anException) {
750 Standard_SStream aMsg;
751 aMsg << "EXCEPTION in BinTools_ShapeSet::WriteGeometry(S,OS)" << std::endl;
752 aMsg << anException << std::endl;
753 throw Standard_Failure(aMsg.str().c_str());
757 //=======================================================================
758 //function : ReadGeometry
760 //=======================================================================
762 void BinTools_ShapeSet::ReadGeometry(const TopAbs_ShapeEnum T,
763 Standard_IStream& IS,
768 Standard_Integer val, c,pc,pc2 = 0,s,s2,l,l2,t, pt, pt2 = 0;
769 Standard_Real tol,X,Y,Z,first,last,p1 = 0.,p2;
770 Standard_Real PfX,PfY,PlX,PlY;
772 Standard_Boolean closed, bval;
773 GeomAbs_Shape reg = GeomAbs_C0;
785 // Standard_Integer aPos = IS.tellg();
786 // std::cout << "\nPOS = " << aPos << std::endl;
787 TopoDS_Vertex& V = TopoDS::Vertex(S);
789 // Read the point geometry
790 BinTools::GetReal(IS, tol);
791 BinTools::GetReal(IS, X);
792 BinTools::GetReal(IS, Y);
793 BinTools::GetReal(IS, Z);
794 gp_Pnt aPnt (X, Y, Z);
795 myBuilder.MakeVertex (V, aPnt, tol);
796 Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(V.TShape());
798 BRep_ListOfPointRepresentation& lpr = TV->ChangePoints();
800 Standard_Boolean aNewF = (myFormatNb > 2);
803 val = (Standard_Integer)IS.get();//case {0|1|2|3}
804 if (val > 0 && val <= 3)
805 BinTools::GetReal(IS, p1);
807 std::streampos aPos = IS.tellg();
808 BinTools::GetReal(IS, p1);
809 val = (Standard_Integer)IS.get();//case {0|1|2|3}
811 std::cout << "\nVal = " << val <<std::endl;
813 if(val != 1 && val !=2 && val !=3){
815 val = (Standard_Integer)IS.get();
816 if (val > 0 && val <= 3)
817 BinTools::GetReal(IS, p1);
820 Handle(BRep_PointRepresentation) PR;
827 BinTools::GetInteger(IS, c);
828 if (myCurves.Curve(c).IsNull())
830 Handle(BRep_PointOnCurve) POC =
831 new BRep_PointOnCurve(p1,
840 BinTools::GetInteger(IS, pc);
841 BinTools::GetInteger(IS, s);
842 if (myCurves2d.Curve2d(pc).IsNull() ||
843 mySurfaces.Surface(s).IsNull())
846 Handle(BRep_PointOnCurveOnSurface) POC =
847 new BRep_PointOnCurveOnSurface(p1,
848 myCurves2d.Curve2d(pc),
849 mySurfaces.Surface(s),
857 BinTools::GetReal(IS, p2);
858 BinTools::GetInteger(IS, s);
859 if (mySurfaces.Surface(s).IsNull())
862 Handle(BRep_PointOnSurface) POC =
863 new BRep_PointOnSurface(p1,p2,
864 mySurfaces.Surface(s),
872 Standard_SStream aMsg;
873 aMsg << "BinTools_SurfaceSet::ReadGeometry: UnExpected BRep_PointRepresentation = "<< val <<std::endl;
874 throw Standard_Failure(aMsg.str().c_str());
879 BinTools::GetInteger(IS, l);//Locations index
882 PR->Location(Locations().Location(l));
900 TopoDS_Edge& E = TopoDS::Edge(S);
902 myBuilder.MakeEdge(E);
904 // Read the curve geometry
905 BinTools::GetReal(IS, tol);
906 BinTools::GetBool(IS, bval);
907 myBuilder.SameParameter(E, bval);
909 BinTools::GetBool(IS, bval);
910 myBuilder.SameRange(E,bval);
912 BinTools::GetBool(IS, bval);
913 myBuilder.Degenerated(E,bval);
916 val = (Standard_Integer)IS.get();//{0|1|2|3|4|5|6|7}
917 // -0- no representation
920 // -3- Curve on closed surf
923 // -6- Polygon on triangulation
924 // -7- Polygon on closed triangulation
930 case 1 : // -1- Curve 3D
931 BinTools::GetInteger(IS, c);
932 BinTools::GetInteger(IS, l);
933 if (!myCurves.Curve(c).IsNull()) {
934 myBuilder.UpdateEdge(E,myCurves.Curve(c),
935 Locations().Location(l),tol);
937 BinTools::GetReal(IS, first);
938 BinTools::GetReal(IS, last);
939 if (!myCurves.Curve(c).IsNull()) {
940 Standard_Boolean Only3d = Standard_True;
941 myBuilder.Range(E,first,last,Only3d);
946 case 2 : // -2- Curve on surf
947 case 3 : // -3- Curve on closed surf
949 BinTools::GetInteger(IS, pc);
951 BinTools::GetInteger(IS, pc2);
952 reg = (GeomAbs_Shape)IS.get();
956 BinTools::GetInteger(IS, s);
957 BinTools::GetInteger(IS, l);
960 BinTools::GetReal(IS, first);
961 BinTools::GetReal(IS, last);
963 // read UV Points // for XML Persistence higher performance
966 BinTools::GetReal(IS, PfX);
967 BinTools::GetReal(IS, PfY);
968 BinTools::GetReal(IS, PlX);
969 BinTools::GetReal(IS, PlY);
970 aPf = gp_Pnt2d(PfX,PfY);
971 aPl = gp_Pnt2d(PlX,PlY);
974 if (myCurves2d.Curve2d(pc).IsNull() ||
975 (closed && myCurves2d.Curve2d(pc2).IsNull()) ||
976 mySurfaces.Surface(s).IsNull())
981 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
982 myCurves2d.Curve2d(pc2),
983 mySurfaces.Surface(s),
984 Locations().Location(l),tol,
987 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
988 myCurves2d.Curve2d(pc2),
989 mySurfaces.Surface(s),
990 Locations().Location(l),tol);
992 myBuilder.Continuity(E,
993 mySurfaces.Surface(s),
994 mySurfaces.Surface(s),
995 Locations().Location(l),
996 Locations().Location(l),
1001 if (FormatNb() >= 2)
1002 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
1003 mySurfaces.Surface(s),
1004 Locations().Location(l),tol,
1007 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
1008 mySurfaces.Surface(s),
1009 Locations().Location(l),tol);
1012 mySurfaces.Surface(s),
1013 Locations().Location(l),
1017 case 4 : // -4- Regularity
1018 reg = (GeomAbs_Shape)IS.get();
1019 BinTools::GetInteger(IS, s);
1020 BinTools::GetInteger(IS, l);
1021 BinTools::GetInteger(IS, s2);
1022 BinTools::GetInteger(IS, l2);
1023 if (mySurfaces.Surface(s).IsNull() ||
1024 mySurfaces.Surface(s2).IsNull())
1026 myBuilder.Continuity(E,
1027 mySurfaces.Surface(s),
1028 mySurfaces.Surface(s2),
1029 Locations().Location(l),
1030 Locations().Location(l2),
1034 case 5 : // -5- Polygon3D
1035 BinTools::GetInteger(IS, c);
1036 BinTools::GetInteger(IS, l);
1037 //??? Bug? myBuilder.UpdateEdge(E,myPolygons3D(c));
1038 myBuilder.UpdateEdge(E, myPolygons3D(c), Locations().Location(l));
1041 case 6 : // -6- Polygon on triangulation
1042 case 7 : // -7- Polygon on closed triangulation
1043 closed = (val == 7);
1044 BinTools::GetInteger(IS, pt);
1046 BinTools::GetInteger(IS, pt2);
1048 BinTools::GetInteger(IS, t);
1049 BinTools::GetInteger(IS, l);
1052 myBuilder.UpdateEdge (E, myNodes(pt), myNodes(pt2), myTriangulations(t), Locations().Location(l));
1056 myBuilder.UpdateEdge (E, myNodes(pt), myTriangulations(t), Locations().Location(l));
1062 Standard_SStream aMsg;
1063 aMsg <<"Unexpected Curve Representation ="<< val << std::endl;
1064 throw Standard_Failure(aMsg.str().c_str());
1078 myBuilder.MakeWire(TopoDS::Wire(S));
1089 TopoDS_Face& F = TopoDS::Face(S);
1090 myBuilder.MakeFace(F);
1091 BinTools::GetBool(IS, bval); //NaturalRestriction flag
1092 BinTools::GetReal(IS, tol);
1093 BinTools::GetInteger(IS, s); //surface indx
1094 BinTools::GetInteger(IS, l); //location indx
1095 myBuilder.UpdateFace (F,
1096 s > 0 ? mySurfaces.Surface(s) : Handle(Geom_Surface)(),
1097 Locations().Location(l),
1099 myBuilder.NaturalRestriction (F, bval);
1101 Standard_Byte aByte = (Standard_Byte)IS.get();
1102 // cas triangulation
1104 BinTools::GetInteger(IS, s);
1105 myBuilder.UpdateFace(TopoDS::Face(S), myTriangulations(s));
1116 myBuilder.MakeShell(TopoDS::Shell(S));
1125 myBuilder.MakeSolid(TopoDS::Solid(S));
1133 case TopAbs_COMPSOLID :
1134 myBuilder.MakeCompSolid(TopoDS::CompSolid(S));
1142 case TopAbs_COMPOUND :
1143 myBuilder.MakeCompound(TopoDS::Compound(S));
1148 Standard_SStream aMsg;
1149 aMsg << "Unexpected topology type = "<< T <<std::endl;
1150 throw Standard_Failure(aMsg.str().c_str());
1155 catch(Standard_Failure const& anException) {
1156 Standard_SStream aMsg;
1157 aMsg << "EXCEPTION in BinTools_ShapeSet::ReadGeometry(S,OS)" << std::endl;
1158 aMsg << anException << std::endl;
1159 throw Standard_Failure(aMsg.str().c_str());
1165 //=======================================================================
1166 //function : AddShapes
1168 //=======================================================================
1170 void BinTools_ShapeSet::AddShapes(TopoDS_Shape& S1,
1171 const TopoDS_Shape& S2)
1173 myBuilder.Add(S1,S2);
1177 //=======================================================================
1178 //function : WritePolygonOnTriangulation
1180 //=======================================================================
1181 void BinTools_ShapeSet::WritePolygonOnTriangulation(Standard_OStream& OS) const
1183 const Standard_Integer aNbPol = myNodes.Extent();
1184 OS << "PolygonOnTriangulations " << aNbPol << "\n";
1188 for (Standard_Integer aPolIter = 1; aPolIter <= aNbPol; ++aPolIter)
1190 const Handle(Poly_PolygonOnTriangulation)& aPoly = myNodes.FindKey (aPolIter);
1191 const TColStd_Array1OfInteger& aNodes = aPoly->Nodes();
1192 BinTools::PutInteger(OS, aNodes.Length());
1193 for (Standard_Integer aNodeIter = 1; aNodeIter <= aNodes.Length(); ++aNodeIter)
1195 BinTools::PutInteger(OS, aNodes.Value (aNodeIter));
1198 // write the deflection
1199 BinTools::PutReal(OS, aPoly->Deflection());
1201 // writing parameters
1202 if (const Handle(TColStd_HArray1OfReal)& aParam = aPoly->Parameters())
1204 BinTools::PutBool(OS, Standard_True);
1205 for (Standard_Integer aNodeIter = 1; aNodeIter <= aParam->Length(); ++aNodeIter)
1207 BinTools::PutReal(OS, aParam->Value (aNodeIter));
1212 BinTools::PutBool(OS, Standard_False);
1216 catch (Standard_Failure const& anException)
1218 Standard_SStream aMsg;
1219 aMsg << "EXCEPTION in BinTools_ShapeSet::WritePolygonOnTriangulation(..)\n" << anException << "\n";
1220 throw Standard_Failure(aMsg.str().c_str());
1224 //=======================================================================
1225 //function : ReadPolygonOnTriangulation
1227 //=======================================================================
1228 void BinTools_ShapeSet::ReadPolygonOnTriangulation(Standard_IStream& IS)
1232 if (IS.fail() || (strstr(aHeader,"PolygonOnTriangulations") == NULL))
1234 throw Standard_Failure("BinTools_ShapeSet::ReadPolygonOnTriangulation: Not a PolygonOnTriangulation section");
1237 Standard_Integer aNbPol = 0;
1239 IS.get();//remove LF
1243 for (Standard_Integer aPolIter = 1; aPolIter <= aNbPol; ++aPolIter)
1245 Standard_Integer aNbNodes = 0;
1246 BinTools::GetInteger(IS, aNbNodes);
1247 Handle(Poly_PolygonOnTriangulation) aPoly = new Poly_PolygonOnTriangulation (aNbNodes, Standard_False);
1248 TColStd_Array1OfInteger& aNodes = aPoly->ChangeNodes();
1249 for (Standard_Integer aNodeIter = 1; aNodeIter <= aNbNodes; ++aNodeIter)
1251 BinTools::GetInteger(IS, aNodes.ChangeValue (aNodeIter));
1254 Standard_Real aDefl = 0.0;
1255 BinTools::GetReal(IS, aDefl);
1256 aPoly->Deflection (aDefl);
1258 Standard_Boolean hasParameters = Standard_False;
1259 BinTools::GetBool(IS, hasParameters);
1262 Handle(TColStd_HArray1OfReal) aParams = new TColStd_HArray1OfReal (1, aNbNodes);
1263 for (Standard_Integer aNodeIter = 1; aNodeIter <= aNbNodes; ++aNodeIter)
1265 BinTools::GetReal(IS, aParams->ChangeValue (aNodeIter));
1267 aPoly->SetParameters (aParams);
1269 myNodes.Add (aPoly);
1272 catch (Standard_Failure const& anException)
1274 Standard_SStream aMsg;
1275 aMsg << "EXCEPTION in BinTools_ShapeSet::ReadPolygonOnTriangulation(..)\n" << anException << "\n";
1276 throw Standard_Failure(aMsg.str().c_str());
1280 //=======================================================================
1281 //function : WritePolygon3D
1283 //=======================================================================
1284 void BinTools_ShapeSet::WritePolygon3D(Standard_OStream& OS)const
1286 const Standard_Integer aNbPol = myPolygons3D.Extent();
1287 OS << "Polygon3D " << aNbPol << "\n";
1291 for (Standard_Integer aPolIter = 1; aPolIter <= aNbPol; ++aPolIter)
1293 const Handle(Poly_Polygon3D)& aPoly = myPolygons3D.FindKey (aPolIter);
1294 BinTools::PutInteger(OS, aPoly->NbNodes());
1295 BinTools::PutBool(OS, aPoly->HasParameters());
1297 // write the deflection
1298 BinTools::PutReal(OS, aPoly->Deflection());
1301 const Standard_Integer aNbNodes = aPoly->NbNodes();
1302 const TColgp_Array1OfPnt& aNodes = aPoly->Nodes();
1303 for (Standard_Integer aNodeIter = 1; aNodeIter <= aNbNodes; ++aNodeIter)
1305 const gp_Pnt& aPnt = aNodes.Value (aNodeIter);
1306 BinTools::PutReal(OS, aPnt.X());
1307 BinTools::PutReal(OS, aPnt.Y());
1308 BinTools::PutReal(OS, aPnt.Z());
1310 if (aPoly->HasParameters())
1312 const TColStd_Array1OfReal& aParam = aPoly->Parameters();
1313 for (Standard_Integer aNodeIter = 1; aNodeIter <= aNbNodes; ++aNodeIter)
1315 BinTools::PutReal(OS, aParam.Value (aNodeIter));
1320 catch (Standard_Failure const& anException)
1322 Standard_SStream aMsg;
1323 aMsg << "EXCEPTION in BinTools_ShapeSet::WritePolygon3D(..)\n" << anException << "\n";
1324 throw Standard_Failure(aMsg.str().c_str());
1327 //=======================================================================
1328 //function : ReadPolygon3D
1330 //=======================================================================
1331 void BinTools_ShapeSet::ReadPolygon3D(Standard_IStream& IS)
1336 if (IS.fail() || strstr(aHeader,"Polygon3D") == NULL)
1339 std::cout <<"Buffer: " << aHeader << std::endl;
1341 throw Standard_Failure("BinTools_ShapeSet::ReadPolygon3D: Not a Polygon3D section");
1344 Standard_Integer aNbPol = 0;
1346 IS.get();//remove LF
1350 for (Standard_Integer aPolIter = 1; aPolIter <= aNbPol; ++aPolIter)
1352 Standard_Integer aNbNodes = 0;
1353 Standard_Boolean hasParameters = Standard_False;
1354 Standard_Real aDefl = 0.0;
1355 BinTools::GetInteger(IS, aNbNodes);
1356 BinTools::GetBool(IS, hasParameters);
1357 BinTools::GetReal(IS, aDefl);
1359 Handle(Poly_Polygon3D) aPoly = new Poly_Polygon3D (aNbNodes, hasParameters);
1360 aPoly->Deflection (aDefl);
1362 TColgp_Array1OfPnt& aNodes = aPoly->ChangeNodes();
1363 for (Standard_Integer aNodeIter = 1; aNodeIter <= aNbNodes; ++aNodeIter)
1365 gp_XYZ& aPnt = aNodes.ChangeValue (aNodeIter).ChangeCoord();
1366 BinTools::GetReal(IS, aPnt.ChangeCoord (1));
1367 BinTools::GetReal(IS, aPnt.ChangeCoord (2));
1368 BinTools::GetReal(IS, aPnt.ChangeCoord (3));
1372 TColStd_Array1OfReal& aParam = aPoly->ChangeParameters();
1373 for (Standard_Integer aNodeIter = 1; aNodeIter <= aNbNodes; ++aNodeIter)
1375 BinTools::GetReal(IS, aParam.ChangeValue (aNodeIter));
1379 myPolygons3D.Add (aPoly);
1382 catch (Standard_Failure const& anException)
1384 Standard_SStream aMsg;
1385 aMsg << "EXCEPTION in BinTools_ShapeSet::ReadPolygon3D(..)\n" << anException << "\n";
1386 throw Standard_Failure(aMsg.str().c_str());
1391 //=======================================================================
1392 //function : WriteTriangulation
1394 //=======================================================================
1395 void BinTools_ShapeSet::WriteTriangulation(Standard_OStream& OS) const
1397 const Standard_Integer aNbTriangulations = myTriangulations.Extent();
1398 OS << "Triangulations " << aNbTriangulations << "\n";
1403 for (Standard_Integer aTriangulationIter = 1; aTriangulationIter <= aNbTriangulations; ++aTriangulationIter)
1405 const Handle(Poly_Triangulation)& aTriangulation = myTriangulations.FindKey (aTriangulationIter);
1406 const Standard_Integer aNbNodes = aTriangulation->NbNodes();
1407 const Standard_Integer aNbTriangles = aTriangulation->NbTriangles();
1408 BinTools::PutInteger(OS, aNbNodes);
1409 BinTools::PutInteger(OS, aNbTriangles);
1410 BinTools::PutBool(OS, aTriangulation->HasUVNodes() ? 1 : 0);
1411 BinTools::PutReal(OS, aTriangulation->Deflection());
1413 // write the 3d nodes
1414 const TColgp_Array1OfPnt& aNodes = aTriangulation->Nodes();
1415 for (Standard_Integer aNodeIter = 1; aNodeIter <= aNbNodes; ++aNodeIter)
1417 const gp_Pnt& aPnt = aNodes.Value (aNodeIter);
1418 BinTools::PutReal(OS, aPnt.X());
1419 BinTools::PutReal(OS, aPnt.Y());
1420 BinTools::PutReal(OS, aPnt.Z());
1423 if (aTriangulation->HasUVNodes())
1425 const TColgp_Array1OfPnt2d& aUVNodes = aTriangulation->UVNodes();
1426 for (Standard_Integer aNodeIter = 1; aNodeIter <= aNbNodes; ++aNodeIter)
1428 const gp_Pnt2d aUV = aUVNodes.Value (aNodeIter);
1429 BinTools::PutReal(OS, aUV.X());
1430 BinTools::PutReal(OS, aUV.Y());
1434 const Poly_Array1OfTriangle& aTriangles = aTriangulation->Triangles();
1435 for (Standard_Integer aTriIter = 1; aTriIter <= aNbTriangles; ++aTriIter)
1437 const Poly_Triangle& aTri = aTriangles.Value (aTriIter);
1438 BinTools::PutInteger(OS, aTri.Value (1));
1439 BinTools::PutInteger(OS, aTri.Value (2));
1440 BinTools::PutInteger(OS, aTri.Value (3));
1444 catch (Standard_Failure const& anException)
1446 Standard_SStream aMsg;
1447 aMsg << "EXCEPTION in BinTools_ShapeSet::WriteTriangulation(..)\n" << anException << "\n";
1448 throw Standard_Failure(aMsg.str().c_str());
1452 //=======================================================================
1453 //function : ReadTriangulation
1455 //=======================================================================
1456 void BinTools_ShapeSet::ReadTriangulation (Standard_IStream& IS)
1460 if (IS.fail() || (strstr(aHeader, "Triangulations") == NULL))
1462 throw Standard_Failure("BinTools_ShapeSet::Triangulation: Not a Triangulation section");
1465 Standard_Integer aNbTriangulations = 0;
1466 IS >> aNbTriangulations;
1467 IS.get();// remove LF
1472 for (Standard_Integer aTriangulationIter = 1; aTriangulationIter <= aNbTriangulations; ++aTriangulationIter)
1474 Standard_Integer aNbNodes = 0, aNbTriangles = 0;
1475 Standard_Boolean hasUV = Standard_False;
1476 Standard_Real aDefl = 0.0;
1477 BinTools::GetInteger(IS, aNbNodes);
1478 BinTools::GetInteger(IS, aNbTriangles);
1479 BinTools::GetBool(IS, hasUV);
1480 BinTools::GetReal(IS, aDefl); //deflection
1481 Handle(Poly_Triangulation) aTriangulation = new Poly_Triangulation (aNbNodes, aNbTriangles, hasUV);
1482 aTriangulation->Deflection (aDefl);
1484 TColgp_Array1OfPnt& aNodes = aTriangulation->ChangeNodes();
1485 for (Standard_Integer aNodeIter = 1; aNodeIter <= aNbNodes; ++aNodeIter)
1487 Standard_Real* anXYZ = aNodes.ChangeValue (aNodeIter).ChangeCoord().ChangeData();
1488 BinTools::GetReal(IS, anXYZ[0]);
1489 BinTools::GetReal(IS, anXYZ[1]);
1490 BinTools::GetReal(IS, anXYZ[2]);
1495 TColgp_Array1OfPnt2d& aUVNodes = aTriangulation->ChangeUVNodes();
1496 for (Standard_Integer aNodeIter = 1; aNodeIter <= aNbNodes; ++aNodeIter)
1498 gp_XY& anUV = aUVNodes.ChangeValue (aNodeIter).ChangeCoord();
1499 BinTools::GetReal(IS, anUV.ChangeCoord (1));
1500 BinTools::GetReal(IS, anUV.ChangeCoord (2));
1504 // read the triangles
1505 Poly_Array1OfTriangle& aTriangles = aTriangulation->ChangeTriangles();
1506 for (Standard_Integer aTriIter = 1; aTriIter <= aNbTriangles; ++aTriIter)
1508 Poly_Triangle& aTri = aTriangles.ChangeValue (aTriIter);
1509 BinTools::GetInteger(IS, aTri.ChangeValue (1));
1510 BinTools::GetInteger(IS, aTri.ChangeValue (2));
1511 BinTools::GetInteger(IS, aTri.ChangeValue (3));
1514 myTriangulations.Add (aTriangulation);
1517 catch (Standard_Failure const& anException)
1519 Standard_SStream aMsg;
1520 aMsg << "EXCEPTION in BinTools_ShapeSet::ReadTriangulation(..)\n" << anException << "\n";
1521 throw Standard_Failure(aMsg.str().c_str());
1525 //=======================================================================
1526 //function : NbShapes
1528 //=======================================================================
1530 Standard_Integer BinTools_ShapeSet::NbShapes() const
1532 return myShapes.Extent();