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