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_ProgressScope.hxx>
26 #include <TCollection_AsciiString.hxx>
27 #include <TopoDS_Iterator.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopTools_LocationSet.hxx>
30 #include <TopTools_ShapeSet.hxx>
31 #include <Standard_Assert.hxx>
33 #include <BRep_TFace.hxx>
37 Standard_CString TopTools_ShapeSet::Version_1 = "CASCADE Topology V1, (c) Matra-Datavision";
38 Standard_CString TopTools_ShapeSet::Version_2 = "CASCADE Topology V2, (c) Matra-Datavision";
40 //=======================================================================
41 //function : TopTools_ShapeSet
43 //=======================================================================
44 TopTools_ShapeSet::TopTools_ShapeSet()
45 : myFormatNb (TopTools_FormatVersion_CURRENT)
49 TopTools_ShapeSet::~TopTools_ShapeSet()
52 //=======================================================================
53 //function : SetFormatNb
55 //=======================================================================
56 void TopTools_ShapeSet::SetFormatNb(const Standard_Integer theFormatNb)
58 Standard_ASSERT_RETURN(theFormatNb == TopTools_FormatVersion_VERSION_1 ||
59 theFormatNb == TopTools_FormatVersion_VERSION_2,
60 "Error: unsupported TopTools version.", );
62 myFormatNb = theFormatNb;
65 //=======================================================================
68 //=======================================================================
69 Standard_Integer TopTools_ShapeSet::FormatNb() const
74 //=======================================================================
77 //=======================================================================
79 void TopTools_ShapeSet::Clear()
86 //=======================================================================
89 //=======================================================================
91 Standard_Integer TopTools_ShapeSet::Add(const TopoDS_Shape& S)
93 if (S.IsNull()) return 0;
94 myLocations.Add(S.Location());
96 S2.Location(TopLoc_Location());
97 Standard_Integer index = myShapes.FindIndex(S2);
101 for (TopoDS_Iterator its(S2,Standard_False,Standard_False);
102 its.More(); its.Next())
104 index = myShapes.Add(S2);
110 //=======================================================================
113 //=======================================================================
115 const TopoDS_Shape& TopTools_ShapeSet::Shape(const Standard_Integer I)const
120 //=======================================================================
123 //=======================================================================
125 Standard_Integer TopTools_ShapeSet::Index(const TopoDS_Shape& S) const
127 return myShapes.FindIndex(S);
130 //=======================================================================
131 //function : Locations
133 //=======================================================================
135 const TopTools_LocationSet& TopTools_ShapeSet::Locations()const
141 //=======================================================================
142 //function : ChangeLocations
144 //=======================================================================
146 TopTools_LocationSet& TopTools_ShapeSet::ChangeLocations()
151 //=======================================================================
152 //function : PrintShapeEnum
154 //=======================================================================
156 static void PrintShapeEnum(const TopAbs_ShapeEnum T,
163 if (C) S << "Ve"; else S << "VERTEX ";
167 if (C) S << "Ed"; else S << "EDGE ";
171 if (C) S << "Wi"; else S << "WIRE ";
175 if (C) S << "Fa"; else S << "FACE ";
179 if (C) S << "Sh"; else S << "SHELL ";
183 if (C) S << "So"; else S << "SOLID ";
186 case TopAbs_COMPSOLID :
187 if (C) S << "CS"; else S << "COMPSOLID";
190 case TopAbs_COMPOUND :
191 if (C) S << "Co"; else S << "COMPOUND ";
195 if (C) S << "Sp"; else S << "SHAPE";
200 //=======================================================================
201 //function : PrintOrientation
203 //=======================================================================
205 static void PrintOrientation(const TopAbs_Orientation O,
211 case TopAbs_FORWARD :
212 if (C) S << "+"; else S << "FORWARD";
215 case TopAbs_REVERSED :
216 if (C) S << "-"; else S << "REVERSED";
219 case TopAbs_INTERNAL :
220 if (C) S << "i"; else S << "INTERNAL";
223 case TopAbs_EXTERNAL :
224 if (C) S << "e"; else S << "EXTERNAL";
229 //=======================================================================
230 //function : DumpExtent
231 //purpose : Dump number of shapes.
232 //=======================================================================
234 Standard_OStream& TopTools_ShapeSet::DumpExtent(Standard_OStream& OS)const
237 Standard_Integer nbVERTEX = 0;
238 Standard_Integer nbEDGE = 0;
239 Standard_Integer nbWIRE = 0;
240 Standard_Integer nbFACE = 0;
241 Standard_Integer nbSHELL = 0;
242 Standard_Integer nbSOLID = 0;
243 Standard_Integer nbCOMPSOLID = 0;
244 Standard_Integer nbCOMPOUND = 0;
245 Standard_Integer nbSHAPE = myShapes.Extent();
247 for (Standard_Integer i = 1; i <= nbSHAPE; i++) {
249 switch(myShapes(i).ShapeType()) {
275 case TopAbs_COMPSOLID :
279 case TopAbs_COMPOUND :
289 OS << " VERTEX : " << nbVERTEX << "\n";
290 OS << " EDGE : " << nbEDGE << "\n";
291 OS << " WIRE : " << nbWIRE << "\n";
292 OS << " FACE : " << nbFACE << "\n";
293 OS << " SHELL : " << nbSHELL << "\n";
294 OS << " SOLID : " << nbSOLID << "\n";
295 OS << " COMPSOLID : " << nbCOMPSOLID << "\n";
296 OS << " COMPOUND : " << nbCOMPOUND << "\n";
297 OS << " SHAPE : " << nbSHAPE << "\n";
302 //=======================================================================
303 //function : DumpExtent
304 //purpose : Dump number of shapes.
305 //=======================================================================
307 void TopTools_ShapeSet::DumpExtent(TCollection_AsciiString& S)const
310 Standard_Integer nbVERTEX = 0;
311 Standard_Integer nbEDGE = 0;
312 Standard_Integer nbWIRE = 0;
313 Standard_Integer nbFACE = 0;
314 Standard_Integer nbSHELL = 0;
315 Standard_Integer nbSOLID = 0;
316 Standard_Integer nbCOMPSOLID = 0;
317 Standard_Integer nbCOMPOUND = 0;
318 Standard_Integer nbSHAPE = myShapes.Extent();
320 for (Standard_Integer i = 1; i <= nbSHAPE; i++) {
322 switch(myShapes(i).ShapeType()) {
348 case TopAbs_COMPSOLID :
352 case TopAbs_COMPOUND :
361 S = S + " VERTEX : " + TCollection_AsciiString(nbVERTEX) + "\n";
362 S = S + " EDGE : " + TCollection_AsciiString(nbEDGE) + "\n";
363 S = S + " WIRE : " + TCollection_AsciiString(nbWIRE) + "\n";
364 S = S + " FACE : " + TCollection_AsciiString(nbFACE) + "\n";
365 S = S + " SHELL : " + TCollection_AsciiString(nbSHELL) + "\n";
366 S = S + " SOLID : " + TCollection_AsciiString(nbSOLID) + "\n";
367 S = S + " COMPSOLID : " + TCollection_AsciiString(nbCOMPSOLID) + "\n";
368 S = S + " COMPOUND : " + TCollection_AsciiString(nbCOMPOUND) + "\n";
369 S = S + " SHAPE : " + TCollection_AsciiString(nbSHAPE) + "\n";
373 //=======================================================================
376 //=======================================================================
378 void TopTools_ShapeSet::Dump(Standard_OStream& OS)const
380 //-----------------------------------------
382 //-----------------------------------------
384 Standard_Integer i, nbShapes = myShapes.Extent();
386 OS << "\nDump of " << nbShapes << " TShapes";
387 OS << "\n\n-----------------\n\n";
389 OS << "Flags : Free, Modified, Checked, Orientable, Closed, Infinite, Convex, Locked";
392 for (i = nbShapes; i >= 1; i--) {
394 const TopoDS_Shape& S = myShapes(i);
395 OS << "TShape # " << nbShapes - i + 1 << " : ";
399 PrintShapeEnum(S.ShapeType(),OS,Standard_False);
401 OS << (S.Free() ? 1 : 0);
402 OS << (S.Modified() ? 1 : 0);
403 OS << (S.Checked() ? 1 : 0);
404 OS << (S.Orientable() ? 1 : 0);
405 OS << (S.Closed() ? 1 : 0);
406 OS << (S.Infinite() ? 1 : 0);
407 OS << (S.Convex() ? 1 : 0);
408 OS << (S.Locked() ? 1 : 0);
409 OS << " " << (void*) &(*S.TShape()) <<"\n";
413 TopoDS_Iterator its(S,Standard_False,Standard_False);
415 const TopoDS_Shape& sub = its.Value();
416 PrintOrientation(sub.Orientation(),OS,Standard_True);
417 OS<<nbShapes - myShapes.FindIndex(sub.Located(TopLoc_Location())) + 1;
418 if (!sub.Location().IsIdentity())
419 OS << "(L" << myLocations.Index(sub.Location()) << ")";
431 //-----------------------------------------
433 //-----------------------------------------
437 //-----------------------------------------
438 // dump the locations
439 //-----------------------------------------
441 myLocations.Dump(OS);
447 //=======================================================================
450 //=======================================================================
452 void TopTools_ShapeSet::Write(Standard_OStream& OS, const Message_ProgressRange& theProgress)
454 // always use C locale for writing shapes
455 std::locale anOldLocale = OS.imbue (std::locale::classic());
457 // use 15-digit precision
458 std::streamsize prec = OS.precision(15);
460 // write the copyright
461 if (myFormatNb == TopTools_FormatVersion_VERSION_2)
463 OS << "\n" << Version_2 << "\n";
467 OS << "\n" << Version_1 << "\n";
470 //-----------------------------------------
471 // write the locations
472 //-----------------------------------------
474 Message_ProgressScope aPS(theProgress, "Writing", 100);
476 myLocations.Write(OS, aPS.Next(10));
478 if (aPS.UserBreak()) {
479 OS << "Interrupted by the user\n";
480 OS.imbue (anOldLocale);
484 //-----------------------------------------
485 // write the geometry
486 //-----------------------------------------
488 WriteGeometry(OS, aPS.Next(75));
490 if (aPS.UserBreak()) {
491 OS << "Interrupted by the user\n";
492 OS.imbue(anOldLocale);
496 //-----------------------------------------
498 //-----------------------------------------
500 Standard_Integer i, nbShapes = myShapes.Extent();
502 OS << "\nTShapes " << nbShapes << "\n";
504 // subshapes are written first
505 Message_ProgressScope aPS1 (aPS.Next(15), "Shapes", nbShapes);
506 for (i = 1; i <= nbShapes && aPS1.More(); i++, aPS1.Next()) {
507 const TopoDS_Shape& S = myShapes(i);
510 PrintShapeEnum(S.ShapeType(),OS,Standard_True);
518 OS << (S.Free() ? 1 : 0);
519 OS << (S.Modified() ? 1 : 0);
520 OS << (S.Checked() ? 1 : 0);
521 OS << (S.Orientable() ? 1 : 0);
522 OS << (S.Closed() ? 1 : 0);
523 OS << (S.Infinite() ? 1 : 0);
524 OS << (S.Convex() ? 1 : 0);
529 Standard_Integer l = 0;
530 TopoDS_Iterator its(S,Standard_False,Standard_False);
532 Write(its.Value(),OS);
540 Write(TopoDS_Shape(),OS); // Null shape to end the list
546 OS.imbue (anOldLocale);
549 OS << "Interrupted by the user\n";
552 //=======================================================================
553 //function : ReadShapeEnum
555 //=======================================================================
557 static TopAbs_ShapeEnum ReadShapeEnum(Standard_IStream& IS)
565 return TopAbs_VERTEX;
577 if (buffer[1] == 'h')
583 if (buffer[1] == 'S')
584 return TopAbs_COMPSOLID;
586 return TopAbs_COMPOUND;
589 return TopAbs_COMPOUND;
592 //=======================================================================
595 //=======================================================================
597 void TopTools_ShapeSet::Read(Standard_IStream& IS, const Message_ProgressRange& theProgress)
599 // always use C locale for reading shapes
600 std::locale anOldLocale = IS.imbue (std::locale::classic());
607 IS.getline(vers,100,'\n');
608 // BUC60769 PTV 18.10.2000: remove possible '\r' at the end of the line
609 //Standard_Integer lv = strlen(vers);
611 //if(pm = strchr(vers,'\r'))
614 Standard_Size lv = strlen(vers);
616 for (lv--; lv > 0 && (vers[lv] == '\r' || vers[lv] == '\n') ;lv--)
621 && strcmp(vers, Version_1)
622 && strcmp(vers, Version_2));
624 std::cout << "File was not written with this version of the topology"<<std::endl;
625 IS.imbue (anOldLocale);
628 else if (strcmp(vers, Version_2) == 0)
630 SetFormatNb(TopTools_FormatVersion_VERSION_2);
634 SetFormatNb(TopTools_FormatVersion_VERSION_1);
637 //-----------------------------------------
638 // read the locations
639 //-----------------------------------------
641 Message_ProgressScope aPS(theProgress, "Reading", 100);
643 myLocations.Read(IS, aPS.Next(10));
645 if (aPS.UserBreak()) {
646 std::cout << "Interrupted by the user"<<std::endl;
647 // on remet le LC_NUMERIC a la precedente valeur
648 IS.imbue (anOldLocale);
652 //-----------------------------------------
654 //-----------------------------------------
655 ReadGeometry(IS, aPS.Next(75));
657 if (aPS.UserBreak()) {
658 std::cout << "Interrupted by the user"<<std::endl;
659 IS.imbue(anOldLocale);
663 //-----------------------------------------
665 //-----------------------------------------
669 if (strcmp(buffer,"TShapes")) {
670 std::cout << "Not a TShape table"<<std::endl;
671 // on remet le LC_NUMERIC a la precedente valeur
672 IS.imbue (anOldLocale);
676 Standard_Integer i, nbShapes;
680 Message_ProgressScope aPS1 (aPS.Next(15), "Shapes", nbShapes);
681 for (i = 1; i <= nbShapes && aPS1.More(); i++, aPS1.Next() ) {
684 //Read type and create empty shape.
686 TopAbs_ShapeEnum T = ReadShapeEnum(IS);
687 ReadGeometry(T,IS,S);
695 Read(SS,IS,nbShapes);
698 } while(!SS.IsNull());
700 S.Free (buffer[0] == '1');
701 S.Modified (buffer[1] == '1');
703 const bool isChecked = myFormatNb == TopTools_FormatVersion_VERSION_2
705 S.Checked (isChecked);
707 S.Orientable(buffer[3] == '1');
708 S.Closed (buffer[4] == '1');
709 S.Infinite (buffer[5] == '1');
710 S.Convex (buffer[6] == '1');
714 if (myFormatNb == TopTools_FormatVersion_VERSION_1)
720 // on remet le LC_NUMERIC a la precedente valeur
721 IS.imbue (anOldLocale);
724 std::cout << "Interrupted by the user" << std::endl;
727 //=======================================================================
730 //=======================================================================
732 void TopTools_ShapeSet::Dump(const TopoDS_Shape& S,
733 Standard_OStream& OS)const
735 if (S.IsNull()) OS << "Null shape\n";
736 OS << "Shape : " << myShapes.FindIndex(S.Located(TopLoc_Location()));
738 PrintOrientation(S.Orientation(),OS,Standard_False);
739 if (!S.Location().IsIdentity())
740 OS <<", location : " << myLocations.Index(S.Location());
745 //=======================================================================
748 //=======================================================================
750 void TopTools_ShapeSet::Write(const TopoDS_Shape& S,
751 Standard_OStream& OS)const
753 if (S.IsNull()) OS << "*";
755 PrintOrientation(S.Orientation(),OS,Standard_True);
756 OS << myShapes.Extent() - myShapes.FindIndex(S.Located(TopLoc_Location())) + 1;
757 OS << " " << myLocations.Index(S.Location()) << " ";
761 //=======================================================================
764 //=======================================================================
766 void TopTools_ShapeSet::Read(TopoDS_Shape& S,
767 Standard_IStream& IS)const
769 Read(S,IS,myShapes.Extent());
772 //=======================================================================
775 //=======================================================================
777 void TopTools_ShapeSet::Read(TopoDS_Shape& S,
778 Standard_IStream& IS,
779 const Standard_Integer nbshapes)const
788 if (buffer[0] == '*')
791 S = myShapes(nbshapes - atoi(buffer+1) + 1);
795 S.Orientation(TopAbs_FORWARD);
799 S.Orientation(TopAbs_REVERSED);
803 S.Orientation(TopAbs_INTERNAL);
807 S.Orientation(TopAbs_EXTERNAL);
813 S.Location(myLocations.Location(l));
817 //=======================================================================
818 //function : AddGeometry
820 //=======================================================================
822 void TopTools_ShapeSet::AddGeometry(const TopoDS_Shape&)
826 //=======================================================================
827 //function : DumpGeometry
829 //=======================================================================
831 void TopTools_ShapeSet::DumpGeometry(Standard_OStream&) const
836 //=======================================================================
837 //function : WriteGeometry
839 //=======================================================================
841 void TopTools_ShapeSet::WriteGeometry(Standard_OStream&, const Message_ProgressRange&)
846 //=======================================================================
847 //function : ReadGeometry
849 //=======================================================================
851 void TopTools_ShapeSet::ReadGeometry(Standard_IStream&, const Message_ProgressRange&)
856 //=======================================================================
857 //function : DumpGeometry
859 //=======================================================================
861 void TopTools_ShapeSet::DumpGeometry(const TopoDS_Shape&,
862 Standard_OStream&)const
867 //=======================================================================
868 //function : WriteGeometry
870 //=======================================================================
872 void TopTools_ShapeSet::WriteGeometry(const TopoDS_Shape&,
873 Standard_OStream&)const
878 //=======================================================================
879 //function : ReadGeometry
881 //=======================================================================
883 void TopTools_ShapeSet::ReadGeometry(const TopAbs_ShapeEnum,
892 //=======================================================================
893 //function : AddShapes
895 //=======================================================================
897 void TopTools_ShapeSet::AddShapes(TopoDS_Shape&,
904 //=======================================================================
907 //=======================================================================
909 void TopTools_ShapeSet::Check(const TopAbs_ShapeEnum,
914 //=======================================================================
915 //function : NbShapes
917 //=======================================================================
919 Standard_Integer TopTools_ShapeSet::NbShapes() const
921 return myShapes.Extent();