0024023: Revamp the OCCT Handle -- general
[occt.git] / src / BinTools / BinTools_ShapeSet.cxx
CommitLineData
b311480e 1// Created on: 2004-05-11
2// Created by: Sergey ZARITCHNY
973c2be1 3// Copyright (c) 2004-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
16#include <BinTools_ShapeSet.ixx>
17#include <Standard_ErrorHandler.hxx>
18#include <Precision.hxx>
19#include <TColStd_HArray1OfReal.hxx>
20#include <TColStd_HArray1OfInteger.hxx>
21#include <TColgp_Array1OfPnt2d.hxx>
22#include <gp_Trsf.hxx>
23#include <Poly_PolygonOnTriangulation.hxx>
24#include <Poly_Polygon3D.hxx>
25#include <Poly_Triangulation.hxx>
c04c30b3 26#include <Poly_Polygon2D.hxx>
7fd59977 27#include <BRepTools.hxx>
28#include <BRep_Tool.hxx>
29#include <BRep_TVertex.hxx>
30#include <BRep_TEdge.hxx>
31#include <BRep_ListIteratorOfListOfPointRepresentation.hxx>
32#include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
33#include <BRep_PointRepresentation.hxx>
34#include <BRep_CurveRepresentation.hxx>
35#include <BRep_CurveOnClosedSurface.hxx>
36#include <BRep_Polygon3D.hxx>
37#include <BRep_PolygonOnTriangulation.hxx>
38#include <BRep_PointOnCurve.hxx>
39#include <BRep_PointOnCurveOnSurface.hxx>
40#include <BRep_PointOnSurface.hxx>
41#include <BRep_GCurve.hxx>
42#include <BRep_TFace.hxx>
43#include <TopoDS.hxx>
44#include <TopoDS_Iterator.hxx>
45#include <TopoDS_Vertex.hxx>
46#include <BinTools.hxx>
47#include <BinTools_Curve2dSet.hxx>
48#include <BinTools_CurveSet.hxx>
49#include <BinTools_SurfaceSet.hxx>
ec357c5c 50#include <BRep_CurveOnSurface.hxx>
7fd59977 51#include <string.h>
52//#define MDTV_DEB 1
53const char* Version_1 = "Open CASCADE Topology V1 (c)";
54const char* Version_2 = "Open CASCADE Topology V2 (c)";
55const char* Version_3 = "Open CASCADE Topology V3 (c)";
56//=======================================================================
57//function : operator << (gp_Pnt)
58//purpose :
59//=======================================================================
60
61static Standard_OStream& operator <<(Standard_OStream& OS, const gp_Pnt P)
62{
63 BinTools::PutReal(OS, P.X());
64 BinTools::PutReal(OS, P.Y());
65 BinTools::PutReal(OS, P.Z());
66 return OS;
67}
68//=======================================================================
69//function : BinTools_ShapeSet
70//purpose :
71//=======================================================================
72
73BinTools_ShapeSet::BinTools_ShapeSet(const Standard_Boolean isWithTriangles)
eafb234b 74 :myFormatNb(3), myWithTriangles(isWithTriangles)
7fd59977 75{}
76
77//=======================================================================
78//function : Delete
79//purpose :
80//=======================================================================
81
82void BinTools_ShapeSet::Delete()
83{}
84
85//=======================================================================
d780e37a 86//function : SetWithTriangles
87//purpose :
88//=======================================================================
89void BinTools_ShapeSet::SetWithTriangles(const Standard_Boolean isWithTriangles)
90{
91 myWithTriangles = isWithTriangles;
92}
93
94//=======================================================================
7fd59977 95//function : SetFormatNb
96//purpose :
97//=======================================================================
98void BinTools_ShapeSet::SetFormatNb(const Standard_Integer theFormatNb)
99{
100 myFormatNb = theFormatNb;
101}
102
103//=======================================================================
104//function : FormatNb
105//purpose :
106//=======================================================================
107Standard_Integer BinTools_ShapeSet::FormatNb() const
108{
109 return myFormatNb;
110}
111
112//=======================================================================
113//function : Clear
114//purpose :
115//=======================================================================
116
117void BinTools_ShapeSet::Clear()
118{
119 mySurfaces.Clear();
120 myCurves.Clear();
121 myCurves2d.Clear();
122 myPolygons3D.Clear();
123 myPolygons2D.Clear();
124 myNodes.Clear();
125 myTriangulations.Clear();
126 myShapes.Clear();
127 myLocations.Clear();
128}
129//=======================================================================
130//function : Add
131//purpose :
132//=======================================================================
133
134Standard_Integer BinTools_ShapeSet::Add(const TopoDS_Shape& theShape)
135{
136 if (theShape.IsNull()) return 0;
137 myLocations.Add(theShape.Location());
138 TopoDS_Shape aS2 = theShape;
139 aS2.Location(TopLoc_Location());
140 Standard_Integer anIndex = myShapes.FindIndex(aS2);
141 if (anIndex == 0) {
142 AddGeometry(aS2);
143 for (TopoDS_Iterator its(aS2,Standard_False,Standard_False);its.More(); its.Next())
144 Add(its.Value());
145 anIndex = myShapes.Add(aS2);
146 }
147 return anIndex;
148}
149
150
151//=======================================================================
152//function : Shape
153//purpose :
154//=======================================================================
155
156const TopoDS_Shape& BinTools_ShapeSet::Shape(const Standard_Integer theIndx)const
157{
158 return myShapes(theIndx);
159}
160
161//=======================================================================
162//function : Index
163//purpose :
164//=======================================================================
165
166Standard_Integer BinTools_ShapeSet::Index(const TopoDS_Shape& theShape) const
167{
168 return myShapes.FindIndex(theShape);
169}
170
171//=======================================================================
172//function : Locations
173//purpose :
174//=======================================================================
175
176const BinTools_LocationSet& BinTools_ShapeSet::Locations()const
177{
178 return myLocations;
179}
180
181
182//=======================================================================
183//function : ChangeLocations
184//purpose :
185//=======================================================================
186
187BinTools_LocationSet& BinTools_ShapeSet::ChangeLocations()
188{
189 return myLocations;
190}
191
192//=======================================================================
193//function : AddGeometry
194//purpose :
195//=======================================================================
196
197void BinTools_ShapeSet::AddGeometry(const TopoDS_Shape& S)
198{
199 // Add the geometry
200
201 if (S.ShapeType() == TopAbs_VERTEX) {
202
203 Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(S.TShape());
204 BRep_ListIteratorOfListOfPointRepresentation itrp(TV->Points());
205
206 while (itrp.More()) {
207 const Handle(BRep_PointRepresentation)& PR = itrp.Value();
208
209 if (PR->IsPointOnCurve()) {
210 myCurves.Add(PR->Curve());
211 }
212
213 else if (PR->IsPointOnCurveOnSurface()) {
214 myCurves2d.Add(PR->PCurve());
215 mySurfaces.Add(PR->Surface());
216 }
217
218 else if (PR->IsPointOnSurface()) {
219 mySurfaces.Add(PR->Surface());
220 }
221
222 ChangeLocations().Add(PR->Location());
223 itrp.Next();
224 }
225
226 }
227 else if (S.ShapeType() == TopAbs_EDGE) {
228
229 // Add the curve geometry
230 Handle(BRep_TEdge) TE = Handle(BRep_TEdge)::DownCast(S.TShape());
231 BRep_ListIteratorOfListOfCurveRepresentation itrc(TE->Curves());
232
233 while (itrc.More()) {
234 const Handle(BRep_CurveRepresentation)& CR = itrc.Value();
235 if (CR->IsCurve3D()) {
236 if (!CR->Curve3D().IsNull()) {
237 myCurves.Add(CR->Curve3D());
238 ChangeLocations().Add(CR->Location());
239 }
240 }
241 else if (CR->IsCurveOnSurface()) {
242 mySurfaces.Add(CR->Surface());
243 myCurves2d.Add(CR->PCurve());
244 ChangeLocations().Add(CR->Location());
245 if (CR->IsCurveOnClosedSurface())
246 myCurves2d.Add(CR->PCurve2());
247 }
248 else if (CR->IsRegularity()) {
249 mySurfaces.Add(CR->Surface());
250 ChangeLocations().Add(CR->Location());
251 mySurfaces.Add(CR->Surface2());
252 ChangeLocations().Add(CR->Location2());
253 }
254 else if (myWithTriangles) {
255 if (CR->IsPolygon3D()) {
256 if (!CR->Polygon3D().IsNull()) {
257 myPolygons3D.Add(CR->Polygon3D());
258 ChangeLocations().Add(CR->Location());
259 }
260 }
261 else if (CR->IsPolygonOnTriangulation()) {
262 myTriangulations.Add(CR->Triangulation());
263 myNodes.Add(CR->PolygonOnTriangulation());
264 ChangeLocations().Add(CR->Location());
265 if (CR->IsPolygonOnClosedTriangulation())
266 myNodes.Add(CR->PolygonOnTriangulation2());
267 }
268 else if (CR->IsPolygonOnSurface()) {
269 mySurfaces.Add(CR->Surface());
270 myPolygons2D.Add(CR->Polygon());
271 ChangeLocations().Add(CR->Location());
272 if (CR->IsPolygonOnClosedSurface())
273 myPolygons2D.Add(CR->Polygon2());
274 }
275 }
276 itrc.Next();
277 }
278 }
279
280 else if (S.ShapeType() == TopAbs_FACE) {
281
282 // Add the surface geometry
283 Handle(BRep_TFace) TF = Handle(BRep_TFace)::DownCast(S.TShape());
284 if (!TF->Surface().IsNull()) mySurfaces.Add(TF->Surface());
285
286 if (myWithTriangles) {
287 Handle(Poly_Triangulation) Tr = TF->Triangulation();
288 if (!Tr.IsNull()) myTriangulations.Add(Tr);
289 }
290
291 ChangeLocations().Add(TF->Location());
292 }
293}
294
295//=======================================================================
296//function : WriteGeometry
297//purpose :
298//=======================================================================
299
300void BinTools_ShapeSet::WriteGeometry(Standard_OStream& OS)const
301{
302 myCurves2d.Write(OS);
303 myCurves.Write(OS);
304 WritePolygon3D(OS);
305 WritePolygonOnTriangulation(OS);
306 mySurfaces.Write(OS);
307 WriteTriangulation(OS);
308}
309
310//=======================================================================
311//function : Write
312//purpose :
313//=======================================================================
314
315void BinTools_ShapeSet::Write(Standard_OStream& OS)const
316{
317
318 // write the copyright
319 if (myFormatNb == 3)
320 OS << "\n" << Version_3 << endl;
321 else if (myFormatNb == 2)
322 OS << "\n" << Version_2 << endl;
323 else
324 OS << "\n" << Version_1 << endl;
325
326 //-----------------------------------------
327 // write the locations
328 //-----------------------------------------
329
330 myLocations.Write(OS);
331
332 //-----------------------------------------
333 // write the geometry
334 //-----------------------------------------
335
336 WriteGeometry(OS);
337
338 //-----------------------------------------
339 // write the shapes
340 //-----------------------------------------
341
342 Standard_Integer i, nbShapes = myShapes.Extent();
343
344 OS << "\nTShapes " << nbShapes << "\n";
345
346 // subshapes are written first
347 for (i = 1; i <= nbShapes; i++) {
348
349 const TopoDS_Shape& S = myShapes(i);
350
351 // Type
352 OS << (Standard_Byte)S.ShapeType();
353
354 // Geometry
355 WriteGeometry(S,OS);
356
357 // Flags
358 BinTools::PutBool(OS, S.Free()? 1:0);
359 BinTools::PutBool(OS, S.Modified()? 1:0);
360 BinTools::PutBool(OS, S.Checked()? 1:0);
361 BinTools::PutBool(OS, S.Orientable()? 1:0);
362 BinTools::PutBool(OS, S.Closed()? 1:0);
363 BinTools::PutBool(OS, S.Infinite()? 1:0);
364 BinTools::PutBool(OS, S.Convex()? 1:0);
365
366 // sub-shapes
367
368 TopoDS_Iterator its(S,Standard_False,Standard_False);
369 while (its.More()) {
370 Write(its.Value(),OS);
371 its.Next();
372 }
373 Write(TopoDS_Shape(),OS); // Null shape to end the list
374 }
375
376}
377
378//=======================================================================
379//function : Read
380//purpose :
381//=======================================================================
382
383void BinTools_ShapeSet::Read(Standard_IStream& IS)
384{
385
386 Clear();
387
388 // Check the version
389 char vers[101];
390 do {
391 IS.getline(vers,100,'\n');
392 // BUC60769 PTV 18.10.2000: remove possible '\r' at the end of the line
393
60be1f9b 394 for (Standard_Size lv = (strlen(vers)- 1); lv > 1 && (vers[lv] == '\r' || vers[lv] == '\n') ;lv--)
7fd59977 395 vers[lv] = '\0';
396
397 } while ( ! IS.fail() && strcmp(vers,Version_1) && strcmp(vers,Version_2) &&
398 strcmp(vers,Version_3));
399 if (IS.fail()) {
400 cout << "BinTools_ShapeSet::Read: File was not written with this version of the topology"<<endl;
401 return;
402 }
403
404 if (strcmp(vers,Version_3) == 0) SetFormatNb(3);
405 else if (strcmp(vers,Version_2) == 0) SetFormatNb(2);
406 else SetFormatNb(1);
407
408 //-----------------------------------------
409 // read the locations
410 //-----------------------------------------
411
412 myLocations.Read(IS);
413 //-----------------------------------------
414 // read the geometry
415 //-----------------------------------------
416
417 ReadGeometry(IS);
418
419 //-----------------------------------------
420 // read the shapes
421 //-----------------------------------------
422
423 char buffer[255];
424 IS >> buffer;
425 if (IS.fail() || strcmp(buffer,"TShapes")) {
426 Standard_SStream aMsg;
427 aMsg << "BinTools_ShapeSet::Read: Not a TShape table"<<endl;
428 Standard_Failure::Raise(aMsg);
429 return;
430 }
431
432 Standard_Integer i, nbShapes;
433 IS >> nbShapes;
434 IS.get();//remove lf
435
436 for (i = 1; i <= nbShapes; i++) {
437
438 TopoDS_Shape S;
439
440 //Read type and create empty shape.
441
442 TopAbs_ShapeEnum T = (TopAbs_ShapeEnum) IS.get();
443
444 ReadGeometry(T,IS,S);
445
446 // Set the flags
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);
455
456 // sub-shapes
457 TopoDS_Shape SS;
458 do {
459 Read(SS,IS,nbShapes);
460 if (!SS.IsNull())
461 AddShapes(S,SS);
462 } while(!SS.IsNull());
463
464 S.Free(aFree);
465 S.Modified(aMod);
466 if (myFormatNb >= 2)
467 S.Checked(aChecked);
468 else
469 S.Checked (Standard_False); // force check at reading..
470 S.Orientable(anOrient);
471 S.Closed (aClosed);
472 S.Infinite (anInf);
473 S.Convex (aConv);
474 // check
475
476 if (myFormatNb == 1)
477 if(T == TopAbs_FACE) {
478 const TopoDS_Face& F = TopoDS::Face(S);
479 BRepTools::Update(F);
480 }
481 myShapes.Add(S);
482 }
483}
484
485//=======================================================================
486//function : Write
487//purpose :
488//=======================================================================
489
490void BinTools_ShapeSet::Write(const TopoDS_Shape& S, Standard_OStream& OS)const
491{
492 if (S.IsNull())
493
494 OS << '*';
495 else {
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()));
500 }
501}
502
503//=======================================================================
504//function : Read
505//purpose :
506//=======================================================================
507
508void BinTools_ShapeSet::Read(TopoDS_Shape& S, Standard_IStream& IS,
509 const Standard_Integer nbshapes)const
510{
511 Standard_Character aChar;
512 IS >> aChar;
513 if(aChar == '*')
514 S = TopoDS_Shape();
515 else {
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);
522
523 Standard_Integer l;
524 BinTools::GetInteger(IS, l);
525 S.Location(myLocations.Location(l));
526 }
527}
528
529//=======================================================================
530//function : ReadGeometry
531//purpose :
532//=======================================================================
533
534void BinTools_ShapeSet::ReadGeometry(Standard_IStream& IS)
535{
536 myCurves2d.Read(IS);
537 myCurves.Read(IS);
538 ReadPolygon3D(IS);
539 ReadPolygonOnTriangulation(IS);
540 mySurfaces.Read(IS);
541 ReadTriangulation(IS);
542}
543
544//=======================================================================
545//function : WriteGeometry
546//purpose :
547//=======================================================================
548
549void BinTools_ShapeSet::WriteGeometry(const TopoDS_Shape& S,
550 Standard_OStream& OS)const
551{
552// Write the geometry
553 try {
554 OCC_CATCH_SIGNALS
555 if (S.ShapeType() == TopAbs_VERTEX) {
556
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);
561 OS << p;
0797d9d3 562#ifdef OCCT_DEBUG_POS
563 std::streamoff aPos;
7fd59977 564#endif
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()) {
0797d9d3 571#ifdef OCCT_DEBUG_POS
7fd59977 572 aPos = OS.tellp();
573#endif
574 OS << (Standard_Byte)1; // 1
575 BinTools::PutReal(OS, PR->Parameter());
576 BinTools::PutInteger(OS, myCurves.Index(PR->Curve()));
577 }
578
579 else if (PR->IsPointOnCurveOnSurface()) {
0797d9d3 580#ifdef OCCT_DEBUG_POS
7fd59977 581 aPos = OS.tellp();
582#endif
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()));
587 }
588
589 else if (PR->IsPointOnSurface()) {
0797d9d3 590#ifdef OCCT_DEBUG_POS
7fd59977 591 aPos = OS.tellp();
592#endif
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()));
597 }
598 BinTools::PutInteger(OS, Locations().Index(PR->Location()));
599 itrp.Next();
600 }
601
602// OS << "0 0\n"; // end representations
603 OS.put((Standard_Byte)0);
604 }
605
606 else if (S.ShapeType() == TopAbs_EDGE) {
607
608 // Write the curve geometry
609
610 Handle(BRep_TEdge) TE = Handle(BRep_TEdge)::DownCast(S.TShape());
611
612 BinTools::PutReal(OS, TE->Tolerance());
613
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);
620
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);
634 }
635 }
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())
640// -2- Curve on surf
641 OS << (Standard_Byte)2;
642 else
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();
649 }
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);
654
655 // Write UV Points for higher performance
656 if (FormatNb() >= 2)
657 {
658 gp_Pnt2d Pf,Pl;
659 if (CR->IsCurveOnClosedSurface()) {
660 Handle(BRep_CurveOnClosedSurface) COCS =
661 Handle(BRep_CurveOnClosedSurface)::DownCast(CR);
662 COCS->UVPoints2(Pf,Pl);
663 }
664 else {
665 Handle(BRep_CurveOnSurface) COS =
666 Handle(BRep_CurveOnSurface)::DownCast(CR);
667 COS->UVPoints(Pf,Pl);
668 }
669 BinTools::PutReal(OS, Pf.X());
670 BinTools::PutReal(OS, Pf.Y());
671 BinTools::PutReal(OS, Pl.X());
672 BinTools::PutReal(OS, Pl.Y());
673 }
674 }
675 else if (CR->IsRegularity()) {
676// -4- Regularity
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()));
683
684 }
685
686 else if (myWithTriangles) {
687 if (CR->IsPolygon3D()) {
688 Handle(BRep_Polygon3D) GC = Handle(BRep_Polygon3D)::DownCast(itrc.Value());
689 if (!GC->Polygon3D().IsNull()) {
690// -5- Polygon3D
691 OS << (Standard_Byte)5;
692 BinTools::PutInteger(OS, myPolygons3D.FindIndex(CR->Polygon3D()));
693 BinTools::PutInteger(OS, Locations().Index(CR->Location()));
694 }
695 }
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;
702 else
703// -7- Polygon on closed triangulation
704 OS << (Standard_Byte)7;
705 BinTools::PutInteger(OS, myNodes.FindIndex(PT->PolygonOnTriangulation()));
706
707 if (CR->IsPolygonOnClosedTriangulation()) {
708 BinTools::PutInteger(OS, myNodes.FindIndex(PT->PolygonOnTriangulation2()));
709 }
710 BinTools::PutInteger(OS, myTriangulations.FindIndex(PT->Triangulation()));
711 BinTools::PutInteger(OS, Locations().Index(CR->Location()));
712 }
713 }
714
715 itrc.Next();
716 }
717// OS << "0\n"; // end of the list of representations
718
719 OS << (Standard_Byte)0;
720 }
721
722 else if (S.ShapeType() == TopAbs_FACE) {
723
724 Handle(BRep_TFace) TF = Handle(BRep_TFace)::DownCast(S.TShape());
725 const TopoDS_Face& F = TopoDS::Face(S);
726
727 if (!(TF->Surface()).IsNull()) {
728 Standard_Boolean aNatRes = (BRep_Tool::NaturalRestriction(F)) ? Standard_True : Standard_False;
729 BinTools::PutBool(OS, aNatRes);
730
731 // Write the surface geometry
732 BinTools::PutReal(OS, TF->Tolerance());
733 BinTools::PutInteger(OS, mySurfaces.Index(TF->Surface()));
734 BinTools::PutInteger(OS, Locations().Index(TF->Location()));
735 }
736 if (myWithTriangles) {
737 if (!(TF->Triangulation()).IsNull()) {
738 OS << (Standard_Byte) 2;
739 // Write the triangulation
740 BinTools::PutInteger(OS, myTriangulations.FindIndex(TF->Triangulation()));
741 } else
742 OS << (Standard_Byte) 1;
743 } else
744 OS << (Standard_Byte) 0;//without triangulation
745 }
746 }
747 catch(Standard_Failure) {
748 Standard_SStream aMsg;
749 aMsg << "EXCEPTION in BinTools_ShapeSet::WriteGeometry(S,OS)" << endl;
750 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
751 aMsg << anExc << endl;
752 Standard_Failure::Raise(aMsg);
753 }
754}
755
756//=======================================================================
757//function : ReadGeometry
758//purpose :
759//=======================================================================
760
761void BinTools_ShapeSet::ReadGeometry(const TopAbs_ShapeEnum T,
762 Standard_IStream& IS,
763 TopoDS_Shape& S)
764{
765 // Read the geometry
766
1d47d8d0 767 Standard_Integer val, c,pc,pc2 = 0,s,s2,l,l2,t, pt, pt2 = 0;
768 Standard_Real tol,X,Y,Z,first,last,p1 = 0.,p2;
7fd59977 769 Standard_Real PfX,PfY,PlX,PlY;
770 gp_Pnt2d aPf, aPl;
771 Standard_Boolean closed, bval;
772 Standard_SStream aMsg;
7fd59977 773 GeomAbs_Shape reg = GeomAbs_C0;
7fd59977 774 try {
775 OCC_CATCH_SIGNALS
776 switch (T) {
777
778
779 //---------
780 // vertex
781 //---------
782
783 case TopAbs_VERTEX :
784 {
785// Standard_Integer aPos = IS.tellg();
786// cout << "\nPOS = " << aPos << endl;
787 TopoDS_Vertex& V = TopoDS::Vertex(S);
788
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 myBuilder.MakeVertex(V,gp_Pnt(X,Y,Z),tol);
795 Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(V.TShape());
796
797 BRep_ListOfPointRepresentation& lpr = TV->ChangePoints();
798 TopLoc_Location L;
799 Standard_Boolean aNewF = (myFormatNb > 2) ? Standard_True : Standard_False;
0797d9d3 800#ifdef OCCT_DEBUG
7fd59977 801 gp_Pnt aPnt = gp_Pnt(X,Y,Z);
802#endif
803 do {
804 if(aNewF) {
805 val = (Standard_Integer)IS.get();//case {0|1|2|3}
806 if (val > 0 && val <= 3)
807 BinTools::GetReal(IS, p1);
808 } else {
60be1f9b 809 streampos aPos = IS.tellg();
7fd59977 810 BinTools::GetReal(IS, p1);
811 val = (Standard_Integer)IS.get();//case {0|1|2|3}
0797d9d3 812#ifdef OCCT_DEBUG
7fd59977 813 cout << "\nVal = " << val <<endl;
814#endif
815 if(val != 1 && val !=2 && val !=3){
816 IS.seekg(aPos);
817 val = (Standard_Integer)IS.get();
818 if (val > 0 && val <= 3)
819 BinTools::GetReal(IS, p1);
820 }
821 }
822 Handle(BRep_PointRepresentation) PR;
823 switch (val) {
824 case 0 :
825 break;
826
827 case 1 :
828 {
829 BinTools::GetInteger(IS, c);
830 if (myCurves.Curve(c).IsNull())
831 break;
832 Handle(BRep_PointOnCurve) POC =
833 new BRep_PointOnCurve(p1,
834 myCurves.Curve(c),
835 L);
836 PR = POC;
837 }
838 break;
839
840 case 2 :
841 {
842 BinTools::GetInteger(IS, pc);
843 BinTools::GetInteger(IS, s);
844 if (myCurves2d.Curve2d(pc).IsNull() ||
845 mySurfaces.Surface(s).IsNull())
846 break;
847
848 Handle(BRep_PointOnCurveOnSurface) POC =
849 new BRep_PointOnCurveOnSurface(p1,
850 myCurves2d.Curve2d(pc),
851 mySurfaces.Surface(s),
852 L);
853 PR = POC;
854 }
855 break;
856
857 case 3 :
858 {
859 BinTools::GetReal(IS, p2);
860 BinTools::GetInteger(IS, s);
861 if (mySurfaces.Surface(s).IsNull())
862 break;
863
864 Handle(BRep_PointOnSurface) POC =
865 new BRep_PointOnSurface(p1,p2,
866 mySurfaces.Surface(s),
867 L);
868 PR = POC;
869 }
870 break;
871
872 default:
873 {
874 aMsg << "BinTools_SurfaceSet::ReadGeometry: UnExpected BRep_PointRepresentation = "<< val <<endl;
875 Standard_Failure::Raise(aMsg);
876 }
877 }
878
879 if (val > 0) {
880 BinTools::GetInteger(IS, l);//Locations index
881
882 if (!PR.IsNull()) {
883 PR->Location(Locations().Location(l));
884 lpr.Append(PR);
885 }
886 }
887 } while (val > 0);
888 }
889 break;
890
891
892 //---------
893 // edge
894 //---------
895
896
897 case TopAbs_EDGE :
898
899 // Create an edge
900 {
901 TopoDS_Edge& E = TopoDS::Edge(S);
902
903 myBuilder.MakeEdge(E);
904
905 // Read the curve geometry
906 BinTools::GetReal(IS, tol);
907 BinTools::GetBool(IS, bval);
908 myBuilder.SameParameter(E, bval);
909
910 BinTools::GetBool(IS, bval);
911 myBuilder.SameRange(E,bval);
912
913 BinTools::GetBool(IS, bval);
914 myBuilder.Degenerated(E,bval);
915
916 do {
917 val = (Standard_Integer)IS.get();//{0|1|2|3|4|5|6|7}
918 // -0- no representation
919 // -1- Curve 3D
920 // -2- Curve on surf
921 // -3- Curve on closed surf
922 // -4- Regularity
923 // -5- Polygon3D
924 // -6- Polygon on triangulation
925 // -7- Polygon on closed triangulation
926
927 switch (val) {
928 case 0:
929 break;
930
931 case 1 : // -1- Curve 3D
932 BinTools::GetInteger(IS, c);
933 BinTools::GetInteger(IS, l);
934 if (!myCurves.Curve(c).IsNull()) {
935 myBuilder.UpdateEdge(E,myCurves.Curve(c),
936 Locations().Location(l),tol);
937 }
938 BinTools::GetReal(IS, first);
939 BinTools::GetReal(IS, last);
940 if (!myCurves.Curve(c).IsNull()) {
941 Standard_Boolean Only3d = Standard_True;
942 myBuilder.Range(E,first,last,Only3d);
943 }
944 break;
945
946
947 case 2 : // -2- Curve on surf
948 case 3 : // -3- Curve on closed surf
949 closed = (val == 3);
950 BinTools::GetInteger(IS, pc);
951 if (closed) {
952 BinTools::GetInteger(IS, pc2);
953 reg = (GeomAbs_Shape)IS.get();
954 }
955
956 // surface, location
957 BinTools::GetInteger(IS, s);
958 BinTools::GetInteger(IS, l);
959
960 // range
961 BinTools::GetReal(IS, first);
962 BinTools::GetReal(IS, last);
963
964 // read UV Points // for XML Persistence higher performance
965 if (FormatNb() >= 2)
966 {
967 BinTools::GetReal(IS, PfX);
968 BinTools::GetReal(IS, PfY);
969 BinTools::GetReal(IS, PlX);
970 BinTools::GetReal(IS, PlY);
971 aPf = gp_Pnt2d(PfX,PfY);
972 aPl = gp_Pnt2d(PlX,PlY);
973 }
974
975 if (myCurves2d.Curve2d(pc).IsNull() ||
976 (closed && myCurves2d.Curve2d(pc2).IsNull()) ||
977 mySurfaces.Surface(s).IsNull())
978 break;
979
980 if (closed) {
981 if (FormatNb() >= 2)
982 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
983 myCurves2d.Curve2d(pc2),
984 mySurfaces.Surface(s),
985 Locations().Location(l),tol,
986 aPf, aPl);
987 else
988 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
989 myCurves2d.Curve2d(pc2),
990 mySurfaces.Surface(s),
991 Locations().Location(l),tol);
992
993 myBuilder.Continuity(E,
994 mySurfaces.Surface(s),
995 mySurfaces.Surface(s),
996 Locations().Location(l),
997 Locations().Location(l),
998 reg);
999 }
1000 else
1001 {
1002 if (FormatNb() >= 2)
1003 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
1004 mySurfaces.Surface(s),
1005 Locations().Location(l),tol,
1006 aPf, aPl);
1007 else
1008 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
1009 mySurfaces.Surface(s),
1010 Locations().Location(l),tol);
1011 }
1012 myBuilder.Range(E,
1013 mySurfaces.Surface(s),
1014 Locations().Location(l),
1015 first,last);
1016 break;
1017
1018 case 4 : // -4- Regularity
1019 reg = (GeomAbs_Shape)IS.get();
1020 BinTools::GetInteger(IS, s);
1021 BinTools::GetInteger(IS, l);
1022 BinTools::GetInteger(IS, s2);
1023 BinTools::GetInteger(IS, l2);
1024 if (mySurfaces.Surface(s).IsNull() ||
1025 mySurfaces.Surface(s2).IsNull())
1026 break;
1027 myBuilder.Continuity(E,
1028 mySurfaces.Surface(s),
1029 mySurfaces.Surface(s2),
1030 Locations().Location(l),
1031 Locations().Location(l2),
1032 reg);
1033 break;
1034
1035 case 5 : // -5- Polygon3D
1036 BinTools::GetInteger(IS, c);
1037 BinTools::GetInteger(IS, l);
1038//??? Bug? myBuilder.UpdateEdge(E,Handle(Poly_Polygon3D)::DownCast(myPolygons3D(c)));
1039 myBuilder.UpdateEdge(E,Handle(Poly_Polygon3D)::DownCast(myPolygons3D(c)), Locations().Location(l));
1040 break;
1041
1042 case 6 : // -6- Polygon on triangulation
1043 case 7 : // -7- Polygon on closed triangulation
1044 closed = (val == 7);
1045 BinTools::GetInteger(IS, pt);
1046 if (closed)
1047 BinTools::GetInteger(IS, pt2);
1048
1049 BinTools::GetInteger(IS, t);
1050 BinTools::GetInteger(IS, l);
1051 if (closed) {
1052 myBuilder.UpdateEdge
1053 (E, Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(pt)),
1054 Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(pt2)),
1055 Handle(Poly_Triangulation)::DownCast(myTriangulations(t)),
1056 Locations().Location(l));
1057 }
1058 else {
1059 myBuilder.UpdateEdge
1060 (E,Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(pt)),
1061 Handle(Poly_Triangulation)::DownCast(myTriangulations(t)),
1062 Locations().Location(l));
1063 }
1064 // range
1065 break;
1066 default:
1067 {
1068 aMsg <<"Unexpected Curve Representation ="<< val << endl;
1069 Standard_Failure::Raise(aMsg);
1070 }
1071
1072 }
1073 } while (val > 0);
1074 }
1075 break;
1076
1077
1078 //---------
1079 // wire
1080 //---------
1081
1082 case TopAbs_WIRE :
1083 myBuilder.MakeWire(TopoDS::Wire(S));
1084 break;
1085
1086
1087 //---------
1088 // face
1089 //---------
1090
1091 case TopAbs_FACE :
1092 {
1093 // create a face :
1094 TopoDS_Face& F = TopoDS::Face(S);
1095 myBuilder.MakeFace(F);
1096 BinTools::GetBool(IS, bval); //NaturalRestriction flag
1097 BinTools::GetReal(IS, tol);
1098 BinTools::GetInteger(IS, s); //surface indx
1099 BinTools::GetInteger(IS, l); //location indx
1100 if (!mySurfaces.Surface(s).IsNull()) {
1101 myBuilder.UpdateFace(TopoDS::Face(S),
1102 mySurfaces.Surface(s),
1103 Locations().Location(l),tol);
1104 myBuilder.NaturalRestriction(TopoDS::Face(S),bval );
1105 }
1106
1107 Standard_Byte aByte = (Standard_Byte)IS.get();
1108 // cas triangulation
1109 if(aByte == 2) {
1110 BinTools::GetInteger(IS, s);
1111 myBuilder.UpdateFace(TopoDS::Face(S),
1112 Handle(Poly_Triangulation)::DownCast(myTriangulations(s)));
1113 }
1114 }
1115 break;
1116
1117
1118 //---------
1119 // shell
1120 //---------
1121
1122 case TopAbs_SHELL :
1123 myBuilder.MakeShell(TopoDS::Shell(S));
1124 break;
1125
1126
1127 //---------
1128 // solid
1129 //---------
1130
1131 case TopAbs_SOLID :
1132 myBuilder.MakeSolid(TopoDS::Solid(S));
1133 break;
1134
1135
1136 //---------
1137 // compsolid
1138 //---------
1139
1140 case TopAbs_COMPSOLID :
1141 myBuilder.MakeCompSolid(TopoDS::CompSolid(S));
1142 break;
1143
1144
1145 //---------
1146 // compound
1147 //---------
1148
1149 case TopAbs_COMPOUND :
1150 myBuilder.MakeCompound(TopoDS::Compound(S));
1151 break;
1152
1153 default:
1154 aMsg << "Unexpected topology type = "<< T <<endl;
1155 Standard_Failure::Raise(aMsg);
1156 break;
1157 }
1158 }
1159 catch(Standard_Failure) {
1160 aMsg << "EXCEPTION in BinTools_ShapeSet::ReadGeometry(S,OS)" << endl;
1161 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
1162 aMsg << anExc << endl;
1163 Standard_Failure::Raise(aMsg);
1164 }
1165}
1166
1167
1168
1169//=======================================================================
1170//function : AddShapes
1171//purpose :
1172//=======================================================================
1173
1174void BinTools_ShapeSet::AddShapes(TopoDS_Shape& S1,
1175 const TopoDS_Shape& S2)
1176{
1177 myBuilder.Add(S1,S2);
1178}
1179
1180
1181//=======================================================================
1182//function : WritePolygonOnTriangulation
1183//purpose :
1184//=======================================================================
1185
1186void BinTools_ShapeSet::WritePolygonOnTriangulation(Standard_OStream& OS) const
1187{
1188 Standard_Integer i, j, nbpOntri = myNodes.Extent();
1189
1190 OS << "PolygonOnTriangulations " << nbpOntri << endl;
1191 Handle(Poly_PolygonOnTriangulation) Poly;
1192 Handle(TColStd_HArray1OfReal) Param;
1193 try {
1194 OCC_CATCH_SIGNALS
1195 for (i=1; i<=nbpOntri; i++) {
1196 Poly = Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(i));
1197 const TColStd_Array1OfInteger& Nodes = Poly->Nodes();
1198 BinTools::PutInteger(OS, Nodes.Length());
1199 for (j=1; j <= Nodes.Length(); j++)
1200 BinTools::PutInteger(OS, Nodes.Value(j));
1201
1202 // writing parameters:
1203 Param = Poly->Parameters();
1204
1205 // write the deflection
1206 BinTools::PutReal(OS, Poly->Deflection());
1207 if (!Param.IsNull()) {
1208 BinTools::PutBool(OS, Standard_True);
1209 for (j=1; j <= Param->Length(); j++)
1210 BinTools::PutReal(OS, Param->Value(j));
1211 }
1212 else
1213 BinTools::PutBool(OS, Standard_False);
1214 }
1215 }
1216 catch(Standard_Failure) {
1217 Standard_SStream aMsg;
1218 aMsg << "EXCEPTION in BinTools_ShapeSet::WritePolygonOnTriangulation(..)" << endl;
1219 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
1220 aMsg << anExc << endl;
1221 Standard_Failure::Raise(aMsg);
1222 }
1223}
1224
1225//=======================================================================
1226//function : ReadPolygonOnTriangulation
1227//purpose :
1228//=======================================================================
1229
1230void BinTools_ShapeSet::ReadPolygonOnTriangulation(Standard_IStream& IS)
1231{
1232 char buffer[255];
1233 IS >> buffer;
1234 Standard_SStream aMsg;
1235 if (IS.fail() || (strstr(buffer,"PolygonOnTriangulations") == NULL)) {
1236 aMsg << "BinTools_ShapeSet::ReadPolygonOnTriangulation: Not a PolygonOnTriangulation section" <<endl;
1237 Standard_Failure::Raise(aMsg);
1238 }
1239 Standard_Integer i, j, val, nbpol = 0, nbnodes =0;
1240 Standard_Boolean hasparameters;
1241 Standard_Real par;
1242 Handle(TColStd_HArray1OfReal) Param;
1243 Handle(Poly_PolygonOnTriangulation) Poly;
1244 IS >> nbpol;
1245 IS.get();//remove LF
7fd59977 1246 try {
1247 OCC_CATCH_SIGNALS
1248 for (i=1; i<=nbpol; i++) {
7fd59977 1249 BinTools::GetInteger(IS, nbnodes);
1250
7fd59977 1251 TColStd_Array1OfInteger Nodes(1, nbnodes);
1252 for (j = 1; j <= nbnodes; j++) {
1253 BinTools::GetInteger(IS, val);
1254 Nodes(j) = val;
1255 }
1256 Standard_Real def;
1257 BinTools::GetReal(IS, def);
1258 BinTools::GetBool(IS, hasparameters);
1259 if (hasparameters) {
1260 TColStd_Array1OfReal Param1(1, nbnodes);
1261 for (j = 1; j <= nbnodes; j++) {
1262 BinTools::GetReal(IS, par);
1263 Param1(j) = par;
1264 }
1265 Poly = new Poly_PolygonOnTriangulation(Nodes, Param1);
1266 }
1267 else Poly = new Poly_PolygonOnTriangulation(Nodes);
1268 Poly->Deflection(def);
1269 myNodes.Add(Poly);
1270 }
1271 }
1272 catch(Standard_Failure) {
1273 aMsg << "EXCEPTION in BinTools_ShapeSet::ReadPolygonOnTriangulation(..)" << endl;
1274 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
1275 aMsg << anExc << endl;
1276 Standard_Failure::Raise(aMsg);
1277 }
1278}
1279
1280
1281
1282//=======================================================================
1283//function : WritePolygon3D
1284//purpose :
1285//=======================================================================
1286
1287void BinTools_ShapeSet::WritePolygon3D(Standard_OStream& OS)const
1288{
1289 Standard_Integer i, j, nbpol = myPolygons3D.Extent();
1290 OS << "Polygon3D " << nbpol << endl;
1291 Handle(Poly_Polygon3D) P;
1292 try {
1293 OCC_CATCH_SIGNALS
1294 for (i = 1; i <= nbpol; i++) {
1295 P = Handle(Poly_Polygon3D)::DownCast(myPolygons3D(i));
1296 BinTools::PutInteger(OS, P->NbNodes());
1297 BinTools::PutBool(OS, P->HasParameters()? 1:0);
1298
1299 // write the deflection
1300 BinTools::PutReal(OS, P->Deflection());
1301
1302 // write the nodes
1303 Standard_Integer i1, nbNodes = P->NbNodes();
1304 const TColgp_Array1OfPnt& Nodes = P->Nodes();
1305 for (j = 1; j <= nbNodes; j++) {
1306 BinTools::PutReal(OS, Nodes(j).X());
1307 BinTools::PutReal(OS, Nodes(j).Y());
1308 BinTools::PutReal(OS, Nodes(j).Z());
1309 }
1310 if (P->HasParameters()) {
1311 const TColStd_Array1OfReal& Param = P->Parameters();
1312 for ( i1 = 1; i1 <= nbNodes; i1++ ) {
1313 BinTools::PutReal(OS, Param(i1));
1314 }
1315 }
1316 }
1317 }
1318 catch(Standard_Failure) {
1319 Standard_SStream aMsg;
1320 aMsg << "EXCEPTION in BinTools_ShapeSet::WritePolygon3D(..)" << endl;
1321 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
1322 aMsg << anExc << endl;
1323 Standard_Failure::Raise(aMsg);
1324 }
1325}
1326//=======================================================================
1327//function : ReadPolygon3D
1328//purpose :
1329//=======================================================================
1330
1331void BinTools_ShapeSet::ReadPolygon3D(Standard_IStream& IS)
1332{
1333 char buffer[255];
1334 Standard_Integer i, j, p, nbpol=0, nbnodes =0;
1335 Standard_Boolean hasparameters = Standard_False;
1336 Standard_Real d, x, y, z;
1337 IS >> buffer;
1338 Standard_SStream aMsg;
1339
1340 if (IS.fail() || strstr(buffer,"Polygon3D") == NULL) {
1341 aMsg << "BinTools_ShapeSet::ReadPolygon3D: Not a Polygon3D section" <<endl;
0797d9d3 1342#ifdef OCCT_DEBUG
7fd59977 1343 cout <<"Buffer: " << buffer << endl;
1344#endif
1345 Standard_Failure::Raise(aMsg);
1346 }
1347 Handle(Poly_Polygon3D) P;
1348 IS >> nbpol;
1349 IS.get();//remove LF
1350
1351 try {
1352 OCC_CATCH_SIGNALS
1353 for (i=1; i<=nbpol; i++) {
1354 BinTools::GetInteger(IS, nbnodes);
1355 BinTools::GetBool(IS, hasparameters);
1356 TColgp_Array1OfPnt Nodes(1, nbnodes);
1357 BinTools::GetReal(IS, d);
1358 for (j = 1; j <= nbnodes; j++) {
1359 BinTools::GetReal(IS, x);
1360 BinTools::GetReal(IS, y);
1361 BinTools::GetReal(IS, z);
1362 Nodes(j).SetCoord(x,y,z);
1363 }
1364 if (hasparameters) {
1365 TColStd_Array1OfReal Param(1,nbnodes);
1366 for (p = 1; p <= nbnodes; p++)
1367 BinTools::GetReal(IS, Param(p));
1368
1369 P = new Poly_Polygon3D(Nodes, Param);
1370 }
1371 else P = new Poly_Polygon3D(Nodes);
1372 P->Deflection(d);
1373 myPolygons3D.Add(P);
1374 }
1375 }
1376 catch(Standard_Failure) {
1377 aMsg << "EXCEPTION in BinTools_ShapeSet::ReadPolygon3D(..)" << endl;
1378 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
1379 aMsg << anExc << endl;
1380 Standard_Failure::Raise(aMsg);
1381 }
1382}
1383
1384
1385//=======================================================================
1386//function : WriteTriangulation
1387//purpose :
1388//=======================================================================
1389
1390void BinTools_ShapeSet::WriteTriangulation(Standard_OStream& OS) const
1391{
1392 Standard_Integer i, j, nbNodes, nbtri = myTriangulations.Extent();
1393 Standard_Integer nbTriangles = 0, n1, n2, n3;
1394 OS << "Triangulations " << nbtri << endl;
1395 Handle(Poly_Triangulation) T;
1396 try {
1397 OCC_CATCH_SIGNALS
1398 for (i = 1; i <= nbtri; i++) {
1399 T = Handle(Poly_Triangulation)::DownCast(myTriangulations(i));
1400 BinTools::PutInteger(OS, T->NbNodes());
1401 BinTools::PutInteger(OS, T->NbTriangles());
1402 BinTools::PutBool(OS, T->HasUVNodes()? 1:0);
1403 // write the deflection
1404 BinTools::PutReal(OS, T->Deflection());
1405
1406 // write the 3d nodes
1407 nbNodes = T->NbNodes();
1408 const TColgp_Array1OfPnt& Nodes = T->Nodes();
1409 for (j = 1; j <= nbNodes; j++) {
1410 BinTools::PutReal(OS, Nodes(j).X());
1411 BinTools::PutReal(OS, Nodes(j).Y());
1412 BinTools::PutReal(OS, Nodes(j).Z());
1413 }
1414
1415 if (T->HasUVNodes()) {
1416 const TColgp_Array1OfPnt2d& UVNodes = T->UVNodes();
1417 for (j = 1; j <= nbNodes; j++) {
1418 BinTools::PutReal(OS, UVNodes(j).X());
1419 BinTools::PutReal(OS, UVNodes(j).Y());
1420 }
1421 }
1422 nbTriangles = T->NbTriangles();
1423 const Poly_Array1OfTriangle& Triangles = T->Triangles();
1424 for (j = 1; j <= nbTriangles; j++) {
1425 Triangles(j).Get(n1, n2, n3);
1426 BinTools::PutInteger(OS, n1);
1427 BinTools::PutInteger(OS, n2);
1428 BinTools::PutInteger(OS, n3);
1429 }
1430 }
1431 }
1432 catch(Standard_Failure) {
1433 Standard_SStream aMsg;
1434 aMsg << "EXCEPTION in BinTools_ShapeSet::WriteTriangulation(..)" << endl;
1435 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
1436 aMsg << anExc << endl;
1437 Standard_Failure::Raise(aMsg);
1438 }
1439}
1440
1441//=======================================================================
1442//function : ReadTriangulation
1443//purpose :
1444//=======================================================================
1445
1446void BinTools_ShapeSet::ReadTriangulation(Standard_IStream& IS)
1447{
1448 char buffer[255];
1449 Standard_Integer i, j, nbtri =0;
1450 Standard_Real d, x, y, z;
1451 Standard_Integer nbNodes =0, nbTriangles=0;
1452 Standard_Boolean hasUV = Standard_False;
1453
1454 Handle(Poly_Triangulation) T;
1455 IS >> buffer;
1456
1457 Standard_SStream aMsg;
1458 if (IS.fail() || (strstr(buffer,"Triangulations") == NULL)) {
1459 aMsg << "BinTools_ShapeSet::Triangulation: Not a Triangulation section" <<endl;
1460 Standard_Failure::Raise(aMsg);
1461 }
1462 IS >> nbtri;
1463 IS.get();// remove LF
1464
1465 try {
1466 OCC_CATCH_SIGNALS
1467 for (i=1; i<=nbtri; i++) {
1468 BinTools::GetInteger(IS, nbNodes);
1469 BinTools::GetInteger(IS, nbTriangles);
1470 TColgp_Array1OfPnt Nodes(1, nbNodes);
1471 BinTools::GetBool(IS, hasUV);
1472 TColgp_Array1OfPnt2d UVNodes(1, nbNodes);
1473 BinTools::GetReal(IS, d); //deflection
1474 for (j = 1; j <= nbNodes; j++) {
1475 BinTools::GetReal(IS, x);
1476 BinTools::GetReal(IS, y);
1477 BinTools::GetReal(IS, z);
1478 Nodes(j).SetCoord(x,y,z);
1479 }
1480
1481 if (hasUV) {
1482 for (j = 1; j <= nbNodes; j++) {
1483 BinTools::GetReal(IS, x);
1484 BinTools::GetReal(IS, y);
1485 UVNodes(j).SetCoord(x,y);
1486 }
1487 }
1488
1489 // read the triangles
1490 Standard_Integer n1,n2,n3;
1491 Poly_Array1OfTriangle Triangles(1, nbTriangles);
1492 for (j = 1; j <= nbTriangles; j++) {
1493 BinTools::GetInteger(IS, n1);
1494 BinTools::GetInteger(IS, n2);
1495 BinTools::GetInteger(IS, n3);
1496 Triangles(j).Set(n1,n2,n3);
1497 }
1498
1499 if (hasUV) T = new Poly_Triangulation(Nodes,UVNodes,Triangles);
1500 else T = new Poly_Triangulation(Nodes,Triangles);
1501 T->Deflection(d);
1502 myTriangulations.Add(T);
1503 }
1504 }
1505 catch(Standard_Failure) {
1506 aMsg << "EXCEPTION in BinTools_ShapeSet::ReadTriangulation(..)" << endl;
1507 Handle(Standard_Failure) anExc = Standard_Failure::Caught();
1508 aMsg << anExc << endl;
1509 Standard_Failure::Raise(aMsg);
1510 }
1511}
1512
1513//=======================================================================
1514//function : NbShapes
1515//purpose :
1516//=======================================================================
1517
1518Standard_Integer BinTools_ShapeSet::NbShapes() const
1519{
1520 return myShapes.Extent();
1521}