1 // Created on: 1993-07-02
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 // Modified: 02 Nov 2000: BUC60769. JMB, PTV. In order to be able to read BRep
18 // is run, we need the following modifications.
21 // In method Read(Standard_IStream &IS), during the version
22 // authentification we cut last '\r' in the line (which will
23 // be present if file is in DOS coding)
25 #include <Message_ProgressIndicator.hxx>
26 #include <Message_ProgressSentry.hxx>
27 #include <TCollection_AsciiString.hxx>
28 #include <TopoDS_Iterator.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopTools_LocationSet.hxx>
31 #include <TopTools_ShapeSet.hxx>
35 static const char* Version = "CASCADE Topology V1, (c) Matra-Datavision";
36 static const char* Version2 = "CASCADE Topology V2, (c) Matra-Datavision";
38 //=======================================================================
39 //function : TopTools_ShapeSet
41 //=======================================================================
43 TopTools_ShapeSet::TopTools_ShapeSet() : myFormatNb(1)
47 TopTools_ShapeSet::~TopTools_ShapeSet()
50 //=======================================================================
51 //function : SetFormatNb
53 //=======================================================================
54 void TopTools_ShapeSet::SetFormatNb(const Standard_Integer theFormatNb)
56 myFormatNb = theFormatNb;
59 //=======================================================================
62 //=======================================================================
63 Standard_Integer TopTools_ShapeSet::FormatNb() const
68 //=======================================================================
71 //=======================================================================
73 void TopTools_ShapeSet::Clear()
80 //=======================================================================
83 //=======================================================================
85 Standard_Integer TopTools_ShapeSet::Add(const TopoDS_Shape& S)
87 if (S.IsNull()) return 0;
88 myLocations.Add(S.Location());
90 S2.Location(TopLoc_Location());
91 Standard_Integer index = myShapes.FindIndex(S2);
95 for (TopoDS_Iterator its(S2,Standard_False,Standard_False);
96 its.More(); its.Next())
98 index = myShapes.Add(S2);
104 //=======================================================================
107 //=======================================================================
109 const TopoDS_Shape& TopTools_ShapeSet::Shape(const Standard_Integer I)const
114 //=======================================================================
117 //=======================================================================
119 Standard_Integer TopTools_ShapeSet::Index(const TopoDS_Shape& S) const
121 return myShapes.FindIndex(S);
124 //=======================================================================
125 //function : Locations
127 //=======================================================================
129 const TopTools_LocationSet& TopTools_ShapeSet::Locations()const
135 //=======================================================================
136 //function : ChangeLocations
138 //=======================================================================
140 TopTools_LocationSet& TopTools_ShapeSet::ChangeLocations()
145 //=======================================================================
146 //function : PrintShapeEnum
148 //=======================================================================
150 static void PrintShapeEnum(const TopAbs_ShapeEnum T,
157 if (C) S << "Ve"; else S << "VERTEX ";
161 if (C) S << "Ed"; else S << "EDGE ";
165 if (C) S << "Wi"; else S << "WIRE ";
169 if (C) S << "Fa"; else S << "FACE ";
173 if (C) S << "Sh"; else S << "SHELL ";
177 if (C) S << "So"; else S << "SOLID ";
180 case TopAbs_COMPSOLID :
181 if (C) S << "CS"; else S << "COMPSOLID";
184 case TopAbs_COMPOUND :
185 if (C) S << "Co"; else S << "COMPOUND ";
189 if (C) S << "Sp"; else S << "SHAPE";
194 //=======================================================================
195 //function : PrintOrientation
197 //=======================================================================
199 static void PrintOrientation(const TopAbs_Orientation O,
205 case TopAbs_FORWARD :
206 if (C) S << "+"; else S << "FORWARD";
209 case TopAbs_REVERSED :
210 if (C) S << "-"; else S << "REVERSED";
213 case TopAbs_INTERNAL :
214 if (C) S << "i"; else S << "INTERNAL";
217 case TopAbs_EXTERNAL :
218 if (C) S << "e"; else S << "EXTERNAL";
223 //=======================================================================
224 //function : DumpExtent
225 //purpose : Dump number of shapes.
226 //=======================================================================
228 Standard_OStream& TopTools_ShapeSet::DumpExtent(Standard_OStream& OS)const
231 Standard_Integer nbVERTEX = 0;
232 Standard_Integer nbEDGE = 0;
233 Standard_Integer nbWIRE = 0;
234 Standard_Integer nbFACE = 0;
235 Standard_Integer nbSHELL = 0;
236 Standard_Integer nbSOLID = 0;
237 Standard_Integer nbCOMPSOLID = 0;
238 Standard_Integer nbCOMPOUND = 0;
239 Standard_Integer nbSHAPE = myShapes.Extent();
241 for (Standard_Integer i = 1; i <= nbSHAPE; i++) {
243 switch(myShapes(i).ShapeType()) {
269 case TopAbs_COMPSOLID :
273 case TopAbs_COMPOUND :
283 OS << " VERTEX : " << nbVERTEX << "\n";
284 OS << " EDGE : " << nbEDGE << "\n";
285 OS << " WIRE : " << nbWIRE << "\n";
286 OS << " FACE : " << nbFACE << "\n";
287 OS << " SHELL : " << nbSHELL << "\n";
288 OS << " SOLID : " << nbSOLID << "\n";
289 OS << " COMPSOLID : " << nbCOMPSOLID << "\n";
290 OS << " COMPOUND : " << nbCOMPOUND << "\n";
291 OS << " SHAPE : " << nbSHAPE << "\n";
296 //=======================================================================
297 //function : DumpExtent
298 //purpose : Dump number of shapes.
299 //=======================================================================
301 void TopTools_ShapeSet::DumpExtent(TCollection_AsciiString& S)const
304 Standard_Integer nbVERTEX = 0;
305 Standard_Integer nbEDGE = 0;
306 Standard_Integer nbWIRE = 0;
307 Standard_Integer nbFACE = 0;
308 Standard_Integer nbSHELL = 0;
309 Standard_Integer nbSOLID = 0;
310 Standard_Integer nbCOMPSOLID = 0;
311 Standard_Integer nbCOMPOUND = 0;
312 Standard_Integer nbSHAPE = myShapes.Extent();
314 for (Standard_Integer i = 1; i <= nbSHAPE; i++) {
316 switch(myShapes(i).ShapeType()) {
342 case TopAbs_COMPSOLID :
346 case TopAbs_COMPOUND :
355 S = S + " VERTEX : " + TCollection_AsciiString(nbVERTEX) + "\n";
356 S = S + " EDGE : " + TCollection_AsciiString(nbEDGE) + "\n";
357 S = S + " WIRE : " + TCollection_AsciiString(nbWIRE) + "\n";
358 S = S + " FACE : " + TCollection_AsciiString(nbFACE) + "\n";
359 S = S + " SHELL : " + TCollection_AsciiString(nbSHELL) + "\n";
360 S = S + " SOLID : " + TCollection_AsciiString(nbSOLID) + "\n";
361 S = S + " COMPSOLID : " + TCollection_AsciiString(nbCOMPSOLID) + "\n";
362 S = S + " COMPOUND : " + TCollection_AsciiString(nbCOMPOUND) + "\n";
363 S = S + " SHAPE : " + TCollection_AsciiString(nbSHAPE) + "\n";
367 //=======================================================================
370 //=======================================================================
372 void TopTools_ShapeSet::Dump(Standard_OStream& OS)const
374 //-----------------------------------------
376 //-----------------------------------------
378 Standard_Integer i, nbShapes = myShapes.Extent();
380 OS << "\nDump of " << nbShapes << " TShapes";
381 OS << "\n\n-----------------\n\n";
383 OS << "Flags : Free, Modified, Checked, Orientable, Closed, Infinite, Convex, Locked";
386 for (i = nbShapes; i >= 1; i--) {
388 const TopoDS_Shape& S = myShapes(i);
389 OS << "TShape # " << nbShapes - i + 1 << " : ";
393 PrintShapeEnum(S.ShapeType(),OS,Standard_False);
395 OS << (S.Free() ? 1 : 0);
396 OS << (S.Modified() ? 1 : 0);
397 OS << (S.Checked() ? 1 : 0);
398 OS << (S.Orientable() ? 1 : 0);
399 OS << (S.Closed() ? 1 : 0);
400 OS << (S.Infinite() ? 1 : 0);
401 OS << (S.Convex() ? 1 : 0);
402 OS << (S.Locked() ? 1 : 0);
403 OS << " " << (void*) &(*S.TShape()) <<"\n";
407 TopoDS_Iterator its(S,Standard_False,Standard_False);
409 const TopoDS_Shape& sub = its.Value();
410 PrintOrientation(sub.Orientation(),OS,Standard_True);
411 OS<<nbShapes - myShapes.FindIndex(sub.Located(TopLoc_Location())) + 1;
412 if (!sub.Location().IsIdentity())
413 OS << "(L" << myLocations.Index(sub.Location()) << ")";
425 //-----------------------------------------
427 //-----------------------------------------
431 //-----------------------------------------
432 // dump the locations
433 //-----------------------------------------
435 myLocations.Dump(OS);
441 //=======================================================================
444 //=======================================================================
446 void TopTools_ShapeSet::Write (Standard_OStream& OS,
447 const Handle(Message_ProgressIndicator) &theProgress)
449 // always use C locale for writing shapes
450 std::locale anOldLocale = OS.imbue (std::locale::classic());
452 // use 15-digit precision
453 std::streamsize prec = OS.precision(15);
455 // write the copyright
457 OS << "\n" << Version2 << "\n";
459 OS << "\n" << Version << "\n";
461 //-----------------------------------------
462 // write the locations
463 //-----------------------------------------
464 Message_ProgressSentry aPS(theProgress, "Writing Shapes", 0, 3, 1);
465 myLocations.Write(OS, theProgress);
470 //-----------------------------------------
471 // write the geometry
472 //-----------------------------------------
474 WriteGeometry(OS, theProgress);
479 //-----------------------------------------
481 //-----------------------------------------
483 Standard_Integer i, nbShapes = myShapes.Extent();
485 OS << "\nTShapes " << nbShapes << "\n";
487 // subshapes are written first
489 Message_ProgressSentry aPSinner(theProgress, "Shapes", 0, nbShapes, 1);
490 for (i = 1; i <= nbShapes && aPSinner.More(); i++, aPSinner.Next()) {
491 const TopoDS_Shape& S = myShapes(i);
494 PrintShapeEnum(S.ShapeType(),OS,Standard_True);
502 OS << (S.Free() ? 1 : 0);
503 OS << (S.Modified() ? 1 : 0);
504 OS << (S.Checked() ? 1 : 0);
505 OS << (S.Orientable() ? 1 : 0);
506 OS << (S.Closed() ? 1 : 0);
507 OS << (S.Infinite() ? 1 : 0);
508 OS << (S.Convex() ? 1 : 0);
513 Standard_Integer l = 0;
514 TopoDS_Iterator its(S,Standard_False,Standard_False);
516 Write(its.Value(),OS);
524 Write(TopoDS_Shape(),OS); // Null shape to end the list
530 OS.imbue (anOldLocale);
533 //=======================================================================
534 //function : ReadShapeEnum
536 //=======================================================================
538 static TopAbs_ShapeEnum ReadShapeEnum(Standard_IStream& IS)
546 return TopAbs_VERTEX;
558 if (buffer[1] == 'h')
564 if (buffer[1] == 'S')
565 return TopAbs_COMPSOLID;
567 return TopAbs_COMPOUND;
570 return TopAbs_COMPOUND;
573 //=======================================================================
576 //=======================================================================
578 void TopTools_ShapeSet::Read (Standard_IStream& IS, const Handle(Message_ProgressIndicator) &theProgress)
580 // always use C locale for reading shapes
581 std::locale anOldLocale = IS.imbue (std::locale::classic());
588 IS.getline(vers,100,'\n');
589 // BUC60769 PTV 18.10.2000: remove possible '\r' at the end of the line
590 //Standard_Integer lv = strlen(vers);
592 //if(pm = strchr(vers,'\r'))
595 Standard_Size lv = strlen(vers);
597 for (lv--; lv > 0 && (vers[lv] == '\r' || vers[lv] == '\n') ;lv--)
601 } while ( ! IS.fail() && strcmp(vers,Version) && strcmp(vers,Version2) );
603 std::cout << "File was not written with this version of the topology"<<std::endl;
604 IS.imbue (anOldLocale);
607 if (strcmp(vers,Version2) == 0) SetFormatNb(2);
610 //-----------------------------------------
611 // read the locations
612 //-----------------------------------------
615 Message_ProgressSentry aPS(theProgress, "Reading", 0, 10, 3);
616 myLocations.Read(IS, theProgress);
620 //-----------------------------------------
622 //-----------------------------------------
623 ReadGeometry(IS, theProgress);
628 //-----------------------------------------
630 //-----------------------------------------
634 if (strcmp(buffer,"TShapes")) {
635 std::cout << "Not a TShape table"<<std::endl;
636 // on remet le LC_NUMERIC a la precedente valeur
637 IS.imbue (anOldLocale);
641 Standard_Integer i, nbShapes;
646 Message_ProgressSentry PS(theProgress, "Shapes", 0, nbShapes, 1);
647 for (i = 1; i <= nbShapes && PS.More(); i++, PS.Next() ) {
650 //Read type and create empty shape.
652 TopAbs_ShapeEnum T = ReadShapeEnum(IS);
653 ReadGeometry(T,IS,S);
661 Read(SS,IS,nbShapes);
664 } while(!SS.IsNull());
666 S.Free (buffer[0] == '1');
667 S.Modified (buffer[1] == '1');
670 S.Checked (buffer[2] == '1');
672 S.Checked (Standard_False); // force check at reading..
674 S.Orientable(buffer[3] == '1');
675 S.Closed (buffer[4] == '1');
676 S.Infinite (buffer[5] == '1');
677 S.Convex (buffer[6] == '1');
686 // on remet le LC_NUMERIC a la precedente valeur
687 IS.imbue (anOldLocale);
690 //=======================================================================
693 //=======================================================================
695 void TopTools_ShapeSet::Dump(const TopoDS_Shape& S,
696 Standard_OStream& OS)const
698 if (S.IsNull()) OS << "Null shape\n";
699 OS << "Shape : " << myShapes.FindIndex(S.Located(TopLoc_Location()));
701 PrintOrientation(S.Orientation(),OS,Standard_False);
702 if (!S.Location().IsIdentity())
703 OS <<", location : " << myLocations.Index(S.Location());
708 //=======================================================================
711 //=======================================================================
713 void TopTools_ShapeSet::Write (const TopoDS_Shape& S, Standard_OStream& OS)const
715 if (S.IsNull()) OS << "*";
717 PrintOrientation(S.Orientation(),OS,Standard_True);
718 OS << myShapes.Extent() - myShapes.FindIndex(S.Located(TopLoc_Location())) + 1;
719 OS << " " << myLocations.Index(S.Location()) << " ";
723 //=======================================================================
726 //=======================================================================
728 void TopTools_ShapeSet::Read (TopoDS_Shape& S, Standard_IStream& IS)const
730 Read(S,IS,myShapes.Extent());
733 //=======================================================================
736 //=======================================================================
738 void TopTools_ShapeSet::Read(TopoDS_Shape& S,
739 Standard_IStream& IS,
740 const Standard_Integer nbshapes)const
749 if (buffer[0] == '*')
752 S = myShapes(nbshapes - atoi(buffer+1) + 1);
756 S.Orientation(TopAbs_FORWARD);
760 S.Orientation(TopAbs_REVERSED);
764 S.Orientation(TopAbs_INTERNAL);
768 S.Orientation(TopAbs_EXTERNAL);
774 S.Location(myLocations.Location(l));
778 //=======================================================================
779 //function : AddGeometry
781 //=======================================================================
783 void TopTools_ShapeSet::AddGeometry(const TopoDS_Shape&)
787 //=======================================================================
788 //function : DumpGeometry
790 //=======================================================================
792 void TopTools_ShapeSet::DumpGeometry(Standard_OStream&) const
797 //=======================================================================
798 //function : WriteGeometry
800 //=======================================================================
802 void TopTools_ShapeSet::WriteGeometry (Standard_OStream&,
803 const Handle(Message_ProgressIndicator) &)
808 //=======================================================================
809 //function : ReadGeometry
811 //=======================================================================
813 void TopTools_ShapeSet::ReadGeometry (Standard_IStream&,
814 const Handle(Message_ProgressIndicator) &)
819 //=======================================================================
820 //function : DumpGeometry
822 //=======================================================================
824 void TopTools_ShapeSet::DumpGeometry(const TopoDS_Shape&,
825 Standard_OStream&)const
830 //=======================================================================
831 //function : WriteGeometry
833 //=======================================================================
835 void TopTools_ShapeSet::WriteGeometry (const TopoDS_Shape&, Standard_OStream&)const
840 //=======================================================================
841 //function : ReadGeometry
843 //=======================================================================
845 void TopTools_ShapeSet::ReadGeometry (const TopAbs_ShapeEnum, Standard_IStream&, TopoDS_Shape&)
852 //=======================================================================
853 //function : AddShapes
855 //=======================================================================
857 void TopTools_ShapeSet::AddShapes(TopoDS_Shape&,
864 //=======================================================================
867 //=======================================================================
869 void TopTools_ShapeSet::Check(const TopAbs_ShapeEnum,
874 //=======================================================================
875 //function : NbShapes
877 //=======================================================================
879 Standard_Integer TopTools_ShapeSet::NbShapes() const
881 return myShapes.Extent();