1 // Created on: 1993-07-02
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
22 // Modified: 02 Nov 2000: BUC60769. JMB, PTV. In order to be able to read BRep
23 // is run, we need the following modifications.
26 // In method Read(Standard_IStream &IS), during the version
27 // authentification we cut last '\r' in the line (which will
28 // be present if file is in DOS coding)
30 #include <TopTools_ShapeSet.ixx>
32 #include <TopoDS_Iterator.hxx>
33 #include <TopoDS_Shape.hxx>
37 #include <Message_ProgressSentry.hxx>
39 const char* Version = "CASCADE Topology V1, (c) Matra-Datavision";
40 const char* Version2 = "CASCADE Topology V2, (c) Matra-Datavision";
42 //=======================================================================
43 //function : TopTools_ShapeSet
45 //=======================================================================
47 TopTools_ShapeSet::TopTools_ShapeSet() : myFormatNb(1)
51 void TopTools_ShapeSet::Delete()
54 //=======================================================================
55 //function : SetFormatNb
57 //=======================================================================
58 void TopTools_ShapeSet::SetFormatNb(const Standard_Integer theFormatNb)
60 myFormatNb = theFormatNb;
63 //=======================================================================
66 //=======================================================================
67 Standard_Integer TopTools_ShapeSet::FormatNb() const
72 //=======================================================================
75 //=======================================================================
77 void TopTools_ShapeSet::Clear()
84 //=======================================================================
87 //=======================================================================
89 Standard_Integer TopTools_ShapeSet::Add(const TopoDS_Shape& S)
91 if (S.IsNull()) return 0;
92 myLocations.Add(S.Location());
94 S2.Location(TopLoc_Location());
95 Standard_Integer index = myShapes.FindIndex(S2);
99 for (TopoDS_Iterator its(S2,Standard_False,Standard_False);
100 its.More(); its.Next())
102 index = myShapes.Add(S2);
108 //=======================================================================
111 //=======================================================================
113 const TopoDS_Shape& TopTools_ShapeSet::Shape(const Standard_Integer I)const
118 //=======================================================================
121 //=======================================================================
123 Standard_Integer TopTools_ShapeSet::Index(const TopoDS_Shape& S) const
125 return myShapes.FindIndex(S);
128 //=======================================================================
129 //function : Locations
131 //=======================================================================
133 const TopTools_LocationSet& TopTools_ShapeSet::Locations()const
139 //=======================================================================
140 //function : ChangeLocations
142 //=======================================================================
144 TopTools_LocationSet& TopTools_ShapeSet::ChangeLocations()
149 //=======================================================================
150 //function : PrintShapeEnum
152 //=======================================================================
154 static void PrintShapeEnum(const TopAbs_ShapeEnum T,
161 if (C) S << "Ve"; else S << "VERTEX ";
165 if (C) S << "Ed"; else S << "EDGE ";
169 if (C) S << "Wi"; else S << "WIRE ";
173 if (C) S << "Fa"; else S << "FACE ";
177 if (C) S << "Sh"; else S << "SHELL ";
181 if (C) S << "So"; else S << "SOLID ";
184 case TopAbs_COMPSOLID :
185 if (C) S << "CS"; else S << "COMPSOLID";
188 case TopAbs_COMPOUND :
189 if (C) S << "Co"; else S << "COMPOUND ";
193 if (C) S << "Sp"; else S << "SHAPE";
198 //=======================================================================
199 //function : PrintOrientation
201 //=======================================================================
203 static void PrintOrientation(const TopAbs_Orientation O,
209 case TopAbs_FORWARD :
210 if (C) S << "+"; else S << "FORWARD";
213 case TopAbs_REVERSED :
214 if (C) S << "-"; else S << "REVERSED";
217 case TopAbs_INTERNAL :
218 if (C) S << "i"; else S << "INTERNAL";
221 case TopAbs_EXTERNAL :
222 if (C) S << "e"; else S << "EXTERNAL";
227 //=======================================================================
228 //function : DumpExtent
229 //purpose : Dump number of shapes.
230 //=======================================================================
232 Standard_OStream& TopTools_ShapeSet::DumpExtent(Standard_OStream& OS)const
235 Standard_Integer nbVERTEX = 0;
236 Standard_Integer nbEDGE = 0;
237 Standard_Integer nbWIRE = 0;
238 Standard_Integer nbFACE = 0;
239 Standard_Integer nbSHELL = 0;
240 Standard_Integer nbSOLID = 0;
241 Standard_Integer nbCOMPSOLID = 0;
242 Standard_Integer nbCOMPOUND = 0;
243 Standard_Integer nbSHAPE = myShapes.Extent();
245 for (Standard_Integer i = 1; i <= nbSHAPE; i++) {
247 switch(myShapes(i).ShapeType()) {
273 case TopAbs_COMPSOLID :
277 case TopAbs_COMPOUND :
287 OS << " VERTEX : " << nbVERTEX << "\n";
288 OS << " EDGE : " << nbEDGE << "\n";
289 OS << " WIRE : " << nbWIRE << "\n";
290 OS << " FACE : " << nbFACE << "\n";
291 OS << " SHELL : " << nbSHELL << "\n";
292 OS << " SOLID : " << nbSOLID << "\n";
293 OS << " COMPSOLID : " << nbCOMPSOLID << "\n";
294 OS << " COMPOUND : " << nbCOMPOUND << "\n";
295 OS << " SHAPE : " << nbSHAPE << "\n";
300 //=======================================================================
301 //function : DumpExtent
302 //purpose : Dump number of shapes.
303 //=======================================================================
305 void TopTools_ShapeSet::DumpExtent(TCollection_AsciiString& S)const
308 Standard_Integer nbVERTEX = 0;
309 Standard_Integer nbEDGE = 0;
310 Standard_Integer nbWIRE = 0;
311 Standard_Integer nbFACE = 0;
312 Standard_Integer nbSHELL = 0;
313 Standard_Integer nbSOLID = 0;
314 Standard_Integer nbCOMPSOLID = 0;
315 Standard_Integer nbCOMPOUND = 0;
316 Standard_Integer nbSHAPE = myShapes.Extent();
318 for (Standard_Integer i = 1; i <= nbSHAPE; i++) {
320 switch(myShapes(i).ShapeType()) {
346 case TopAbs_COMPSOLID :
350 case TopAbs_COMPOUND :
359 S = S + " VERTEX : " + TCollection_AsciiString(nbVERTEX) + "\n";
360 S = S + " EDGE : " + TCollection_AsciiString(nbEDGE) + "\n";
361 S = S + " WIRE : " + TCollection_AsciiString(nbWIRE) + "\n";
362 S = S + " FACE : " + TCollection_AsciiString(nbFACE) + "\n";
363 S = S + " SHELL : " + TCollection_AsciiString(nbSHELL) + "\n";
364 S = S + " SOLID : " + TCollection_AsciiString(nbSOLID) + "\n";
365 S = S + " COMPSOLID : " + TCollection_AsciiString(nbCOMPSOLID) + "\n";
366 S = S + " COMPOUND : " + TCollection_AsciiString(nbCOMPOUND) + "\n";
367 S = S + " SHAPE : " + TCollection_AsciiString(nbSHAPE) + "\n";
371 //=======================================================================
374 //=======================================================================
376 void TopTools_ShapeSet::Dump(Standard_OStream& OS)const
378 //-----------------------------------------
380 //-----------------------------------------
382 Standard_Integer i, nbShapes = myShapes.Extent();
384 OS << "\nDump of " << nbShapes << " TShapes";
385 OS << "\n\n-----------------\n\n";
387 OS << "Flags : Free, Modified, Checked, Orientable, Closed, Infinite, Convex";
390 for (i = nbShapes; i >= 1; i--) {
392 const TopoDS_Shape& S = myShapes(i);
393 OS << "TShape # " << nbShapes - i + 1 << " : ";
397 PrintShapeEnum(S.ShapeType(),OS,Standard_False);
399 OS << (S.Free() ? 1 : 0);
400 OS << (S.Modified() ? 1 : 0);
401 OS << (S.Checked() ? 1 : 0);
402 OS << (S.Orientable() ? 1 : 0);
403 OS << (S.Closed() ? 1 : 0);
404 OS << (S.Infinite() ? 1 : 0);
405 OS << (S.Convex() ? 1 : 0);
406 OS << " " << (void*) &(*S.TShape()) <<"\n";
410 TopoDS_Iterator its(S,Standard_False,Standard_False);
412 const TopoDS_Shape& sub = its.Value();
413 PrintOrientation(sub.Orientation(),OS,Standard_True);
414 OS<<nbShapes - myShapes.FindIndex(sub.Located(TopLoc_Location())) + 1;
415 if (!sub.Location().IsIdentity())
416 OS << "(L" << myLocations.Index(sub.Location()) << ")";
428 //-----------------------------------------
430 //-----------------------------------------
434 //-----------------------------------------
435 // dump the locations
436 //-----------------------------------------
438 myLocations.Dump(OS);
444 //=======================================================================
447 //=======================================================================
449 void TopTools_ShapeSet::Write(Standard_OStream& OS)
451 // always use C locale for writing shapes
452 std::locale anOldLocale = OS.imbue (std::locale::classic());
454 // use 15-digit precision
455 std::streamsize prec = OS.precision(15);
457 // write the copyright
459 OS << "\n" << Version2 << endl;
461 OS << "\n" << Version << endl;
463 //-----------------------------------------
464 // write the locations
465 //-----------------------------------------
467 if ( !myProgress.IsNull() )
468 myProgress->NewScope ( 10, "Locations" );
469 myLocations.SetProgress(myProgress);
470 myLocations.Write(OS);
471 if ( !myProgress.IsNull() ) {
472 myProgress->EndScope();
476 if (!myProgress.IsNull() && myProgress->UserBreak()) {
477 OS << "Interrupted by the user"<<endl;
478 OS.imbue (anOldLocale);
482 //-----------------------------------------
483 // write the geometry
484 //-----------------------------------------
486 if ( !myProgress.IsNull() )
487 myProgress->NewScope ( 75, "Geometry" );
489 if ( !myProgress.IsNull() ) {
490 myProgress->EndScope();
494 //-----------------------------------------
496 //-----------------------------------------
498 Standard_Integer i, nbShapes = myShapes.Extent();
500 OS << "\nTShapes " << nbShapes << "\n";
502 // subshapes are written first
504 if ( !myProgress.IsNull() )
505 myProgress->NewScope ( 15, "Shapes" );
506 Message_ProgressSentry PS(myProgress, "Shapes", 0, nbShapes, 1);
508 for (i = 1; i <= nbShapes && PS.More(); i++, PS.Next()) {
509 if ( !myProgress.IsNull() )
512 const TopoDS_Shape& S = myShapes(i);
515 PrintShapeEnum(S.ShapeType(),OS,Standard_True);
523 OS << (S.Free() ? 1 : 0);
524 OS << (S.Modified() ? 1 : 0);
525 OS << (S.Checked() ? 1 : 0);
526 OS << (S.Orientable() ? 1 : 0);
527 OS << (S.Closed() ? 1 : 0);
528 OS << (S.Infinite() ? 1 : 0);
529 OS << (S.Convex() ? 1 : 0);
534 Standard_Integer l = 0;
535 TopoDS_Iterator its(S,Standard_False,Standard_False);
537 Write(its.Value(),OS);
545 Write(TopoDS_Shape(),OS); // Null shape to end the list
551 OS.imbue (anOldLocale);
554 if (!myProgress.IsNull()) {
555 myProgress->EndScope();
557 if (myProgress->UserBreak())
558 OS << "Interrupted by the user"<<endl;
562 //=======================================================================
563 //function : ReadShapeEnum
565 //=======================================================================
567 static TopAbs_ShapeEnum ReadShapeEnum(Standard_IStream& IS)
575 return TopAbs_VERTEX;
587 if (buffer[1] == 'h')
593 if (buffer[1] == 'S')
594 return TopAbs_COMPSOLID;
596 return TopAbs_COMPOUND;
599 return TopAbs_COMPOUND;
602 //=======================================================================
605 //=======================================================================
607 void TopTools_ShapeSet::Read(Standard_IStream& IS)
609 // always use C locale for reading shapes
610 std::locale anOldLocale = IS.imbue (std::locale::classic());
617 IS.getline(vers,100,'\n');
618 // BUC60769 PTV 18.10.2000: remove possible '\r' at the end of the line
619 //Standard_Integer lv = strlen(vers);
621 //if(pm = strchr(vers,'\r'))
624 for (Standard_Size lv = (strlen(vers)- 1); lv > 1 && (vers[lv] == '\r' || vers[lv] == '\n') ;lv--)
627 } while ( ! IS.fail() && strcmp(vers,Version) && strcmp(vers,Version2) );
629 cout << "File was not written with this version of the topology"<<endl;
630 IS.imbue (anOldLocale);
633 if (strcmp(vers,Version2) == 0) SetFormatNb(2);
636 //-----------------------------------------
637 // read the locations
638 //-----------------------------------------
641 if (!myProgress.IsNull() && myProgress->UserBreak()) {
642 cout << "Interrupted by the user"<<endl;
643 // on remet le LC_NUMERIC a la precedente valeur
644 IS.imbue (anOldLocale);
647 if ( !myProgress.IsNull() )
648 myProgress->NewScope ( 10, "Locations" );
649 myLocations.SetProgress(myProgress);
650 myLocations.Read(IS);
651 if ( !myProgress.IsNull() ) {
652 myProgress->EndScope();
656 if (!myProgress.IsNull() && myProgress->UserBreak()) {
657 cout << "Interrupted by the user"<<endl;
658 // on remet le LC_NUMERIC a la precedente valeur
659 IS.imbue (anOldLocale);
663 //-----------------------------------------
665 //-----------------------------------------
666 if ( !myProgress.IsNull() )
667 myProgress->NewScope ( 75, "Geometry" );
669 if ( !myProgress.IsNull() ) {
670 myProgress->EndScope();
674 //-----------------------------------------
676 //-----------------------------------------
680 if (strcmp(buffer,"TShapes")) {
681 cout << "Not a TShape table"<<endl;
682 // on remet le LC_NUMERIC a la precedente valeur
683 IS.imbue (anOldLocale);
687 Standard_Integer i, nbShapes;
691 if ( !myProgress.IsNull() )
692 myProgress->NewScope ( 15, "Shapes" );
693 Message_ProgressSentry PS(myProgress, "Shapes", 0, nbShapes, 1);
694 for (i = 1; i <= nbShapes && PS.More(); i++, PS.Next() ) {
695 if ( !myProgress.IsNull() )
699 //Read type and create empty shape.
701 TopAbs_ShapeEnum T = ReadShapeEnum(IS);
702 ReadGeometry(T,IS,S);
710 Read(SS,IS,nbShapes);
713 } while(!SS.IsNull());
715 S.Free (buffer[0] == '1');
716 S.Modified (buffer[1] == '1');
719 S.Checked (buffer[2] == '1');
721 S.Checked (Standard_False); // force check at reading..
723 S.Orientable(buffer[3] == '1');
724 S.Closed (buffer[4] == '1');
725 S.Infinite (buffer[5] == '1');
726 S.Convex (buffer[6] == '1');
735 if (!myProgress.IsNull()) {
736 myProgress->EndScope();
740 // on remet le LC_NUMERIC a la precedente valeur
741 IS.imbue (anOldLocale);
744 //=======================================================================
747 //=======================================================================
749 void TopTools_ShapeSet::Dump(const TopoDS_Shape& S,
750 Standard_OStream& OS)const
752 if (S.IsNull()) OS << "Null shape\n";
753 OS << "Shape : " << myShapes.FindIndex(S.Located(TopLoc_Location()));
755 PrintOrientation(S.Orientation(),OS,Standard_False);
756 if (!S.Location().IsIdentity())
757 OS <<", location : " << myLocations.Index(S.Location());
762 //=======================================================================
765 //=======================================================================
767 void TopTools_ShapeSet::Write(const TopoDS_Shape& S,
768 Standard_OStream& OS)const
770 if (S.IsNull()) OS << "*";
772 PrintOrientation(S.Orientation(),OS,Standard_True);
773 OS << myShapes.Extent() - myShapes.FindIndex(S.Located(TopLoc_Location())) + 1;
774 OS << " " << myLocations.Index(S.Location()) << " ";
778 //=======================================================================
781 //=======================================================================
783 void TopTools_ShapeSet::Read(TopoDS_Shape& S,
784 Standard_IStream& IS)const
786 Read(S,IS,myShapes.Extent());
789 //=======================================================================
792 //=======================================================================
794 void TopTools_ShapeSet::Read(TopoDS_Shape& S,
795 Standard_IStream& IS,
796 const Standard_Integer nbshapes)const
805 if (buffer[0] == '*')
808 S = myShapes(nbshapes - atoi(buffer+1) + 1);
812 S.Orientation(TopAbs_FORWARD);
816 S.Orientation(TopAbs_REVERSED);
820 S.Orientation(TopAbs_INTERNAL);
824 S.Orientation(TopAbs_EXTERNAL);
830 S.Location(myLocations.Location(l));
834 //=======================================================================
835 //function : AddGeometry
837 //=======================================================================
839 void TopTools_ShapeSet::AddGeometry(const TopoDS_Shape&)
843 //=======================================================================
844 //function : DumpGeometry
846 //=======================================================================
848 void TopTools_ShapeSet::DumpGeometry(Standard_OStream&) const
853 //=======================================================================
854 //function : WriteGeometry
856 //=======================================================================
858 void TopTools_ShapeSet::WriteGeometry(Standard_OStream&)
863 //=======================================================================
864 //function : ReadGeometry
866 //=======================================================================
868 void TopTools_ShapeSet::ReadGeometry(Standard_IStream&)
873 //=======================================================================
874 //function : DumpGeometry
876 //=======================================================================
878 void TopTools_ShapeSet::DumpGeometry(const TopoDS_Shape&,
879 Standard_OStream&)const
884 //=======================================================================
885 //function : WriteGeometry
887 //=======================================================================
889 void TopTools_ShapeSet::WriteGeometry(const TopoDS_Shape&,
890 Standard_OStream&)const
895 //=======================================================================
896 //function : ReadGeometry
898 //=======================================================================
900 void TopTools_ShapeSet::ReadGeometry(const TopAbs_ShapeEnum,
909 //=======================================================================
910 //function : AddShapes
912 //=======================================================================
914 void TopTools_ShapeSet::AddShapes(TopoDS_Shape&,
921 //=======================================================================
924 //=======================================================================
926 void TopTools_ShapeSet::Check(const TopAbs_ShapeEnum,
931 //=======================================================================
932 //function : NbShapes
934 //=======================================================================
936 Standard_Integer TopTools_ShapeSet::NbShapes() const
938 return myShapes.Extent();
941 //=======================================================================
942 //function : GetProgress
944 //=======================================================================
946 Handle(Message_ProgressIndicator) TopTools_ShapeSet::GetProgress() const
951 //=======================================================================
952 //function : SetProgress
954 //=======================================================================
956 void TopTools_ShapeSet::SetProgress(const Handle(Message_ProgressIndicator)& PR)