Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / mfc / standard / 05_ImportExport / src / ColoredShapes.cpp
1 // ColoredShapes.cpp: implementation of the CColoredShape class.
2 //
3 //////////////////////////////////////////////////////////////////////
4
5 #include "stdafx.h"
6
7 #include <afxtempl.h>
8
9 #include "ColoredShapes.h"
10
11 //////////////////////////////////////////////////////////////////////
12 // Construction/Destruction
13 //////////////////////////////////////////////////////////////////////
14
15 CColoredShapes::CColoredShapes()
16 {
17 }
18
19
20 void CColoredShapes::Add(const Quantity_NameOfColor aColor, const TopoDS_Shape& aShape)
21 {
22         m_shapeList.Append(aShape);
23         m_colorMap.Bind(aShape, aColor);
24 }
25
26 IMPLEMENT_SERIAL(CColoredShapes, CObject,1);
27
28 // This schema contains all the Persistent Geometry and Topology
29 #include <ShapeSchema.hxx>  
30
31 // Tools to store TopoDS_Shape
32 #include <MgtBRep.hxx>
33 #include <PTopoDS_HShape.hxx>
34 #include <PTColStd_TransientPersistentMap.hxx>
35 #include <TopoDS_Shape.hxx>
36
37 // Tools to put Persistent Object in an archive
38 #include <FSD_Archive.hxx>
39 #include <Storage_Data.hxx>
40 #include <Storage_HSeqOfRoot.hxx>
41 #include <Storage_Root.hxx>
42 #include <PTColStd_PersistentTransientMap.hxx>
43
44 void CColoredShapes::Serialize(CArchive & ar)
45 {
46         CObject::Serialize(ar);
47
48         // an I/O driver
49         FSD_Archive f( &ar );
50
51         // the applicative Schema containing Persistent Topology and Geometry
52         // Note that it inherits from the class Storage_Schema
53         Handle(ShapeSchema) s = new ShapeSchema;
54
55         if ( ar.IsStoring() ) 
56         {   
57                 // Write number of shapes to be serialized
58
59                 ar << (int)m_colorMap.Extent();
60         
61                 for ( TopoDS_ListIteratorOfListOfShape iter(m_shapeList); iter.More(); iter.Next() )
62                 {
63                         //Create the persistent Shape
64                         PTColStd_TransientPersistentMap aMap;
65
66                         Handle(PTopoDS_HShape) aPShape = 
67                                 MgtBRep::Translate(iter.Value(), aMap, MgtBRep_WithoutTriangle);
68
69                         // Store the Persistent shape in the archive
70                         Handle(Storage_Data) d = new Storage_Data;
71                         d->AddRoot("ObjectName", aPShape);
72                         s->Write( f, d);
73
74                         // Check
75                         if (d->ErrorStatus() != Storage_VSOk) 
76                         {
77                                 ::MessageBox(NULL, " Error while writing... ", " Error ",MB_OK) ;
78                         }
79         
80                         // Store the color in the archive
81                         ar << (Standard_Integer)m_colorMap.Find(iter.Value());
82                 }
83         }
84         else
85         {
86                 // Get numbe of stored shapes
87                 int nbShapes;
88                 ar >> nbShapes;
89
90                 Standard_Integer tmp;
91                 Quantity_NameOfColor theColor;
92
93                 // Reading all shapes
94                 for ( int i = 0; i < nbShapes; i++ )
95                 {
96                         // Read the Persistent Shape from the archive
97                         Handle(Storage_Data) d = s->Read( f );
98                         Handle(Storage_HSeqOfRoot)  roots = d->Roots();
99                         Handle(Standard_Persistent) p;
100                         Handle(Storage_Root) r;
101                         Handle(PTopoDS_HShape) aPShape;
102                         TopoDS_Shape theShape;
103
104                         r = roots->Value(1);
105                         p = r->Object();
106                         aPShape  = Handle(PTopoDS_HShape)::DownCast(p);
107
108                         // Create the shape
109                         PTColStd_PersistentTransientMap aMap;
110
111                         MgtBRep::Translate(aPShape,aMap,theShape,MgtBRep_WithoutTriangle);
112
113                         m_shapeList.Append(theShape);
114                         
115                         // Read the Color from the archive
116                         ar >> tmp;
117                         theColor = (Quantity_NameOfColor) tmp;
118                         m_colorMap.Bind(theShape, theColor);
119                 }       
120         }
121 }
122
123 void CColoredShapes::Display(Handle(AIS_InteractiveContext)& anAIScontext)
124 {
125         for ( TopoDS_ListIteratorOfListOfShape iter(m_shapeList); iter.More(); iter.Next() )
126         {
127                 Handle(AIS_Shape) ais = new AIS_Shape(iter.Value());
128                 anAIScontext->SetColor(ais, (Quantity_NameOfColor)m_colorMap.Find(iter.Value()));
129                 anAIScontext->SetMaterial(ais, Graphic3d_NOM_GOLD, Standard_False);
130                 anAIScontext->Display(ais, Standard_False);
131         }
132 }