311828051330feb3bd89ba35cfc9aa4b714d5167
[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 void CColoredShapes::Remove(const TopoDS_Shape& aShape)
27 {
28         m_colorMap.UnBind(aShape);
29         for ( TopoDS_ListIteratorOfListOfShape iter(m_shapeList); iter.More(); iter.Next() ) {
30                 if(iter.Value() == aShape) {
31                         m_shapeList.Remove(iter);
32                         break;
33                 }
34         }
35 }
36
37 IMPLEMENT_SERIAL(CColoredShapes, CObject,1);
38
39 // This schema contains all the Persistent Geometry and Topology
40 #include <ShapeSchema.hxx>  
41
42 // Tools to store TopoDS_Shape
43 #include <MgtBRep.hxx>
44 #include <PTopoDS_HShape.hxx>
45 #include <PTColStd_TransientPersistentMap.hxx>
46 #include <TopoDS_Shape.hxx>
47
48 // Tools to put Persistent Object in an archive
49 #include <FSD_Archive.hxx>
50 #include <Storage_Data.hxx>
51 #include <Storage_HSeqOfRoot.hxx>
52 #include <Storage_Root.hxx>
53 #include <PTColStd_PersistentTransientMap.hxx>
54
55 void CColoredShapes::Serialize(CArchive & ar)
56 {
57         CObject::Serialize(ar);
58
59         // an I/O driver
60         FSD_Archive f( &ar );
61
62         // the applicative Schema containing Persistent Topology and Geometry
63         // Note that it inherits from the class Storage_Schema
64         Handle(ShapeSchema) s = new ShapeSchema;
65
66         if ( ar.IsStoring() ) 
67         {   
68                 // Write number of shapes to be serialized
69
70                 ar << (int)m_colorMap.Extent();
71         
72                 for ( TopoDS_ListIteratorOfListOfShape iter(m_shapeList); iter.More(); iter.Next() )
73                 {
74                         //Create the persistent Shape
75                         PTColStd_TransientPersistentMap aMap;
76
77                         Handle(PTopoDS_HShape) aPShape = 
78                                 MgtBRep::Translate(iter.Value(), aMap, MgtBRep_WithoutTriangle);
79
80                         // Store the Persistent shape in the archive
81                         Handle(Storage_Data) d = new Storage_Data;
82                         d->AddRoot("ObjectName", aPShape);
83                         s->Write( f, d);
84
85                         // Check
86                         if (d->ErrorStatus() != Storage_VSOk) 
87                         {
88                                 ::MessageBox(NULL, " Error while writing... ", " Error ",MB_OK) ;
89                         }
90         
91                         // Store the color in the archive
92                         ar << (Standard_Integer)m_colorMap.Find(iter.Value());
93                 }
94         }
95         else
96         {
97                 // Get numbe of stored shapes
98                 int nbShapes;
99                 ar >> nbShapes;
100
101                 Standard_Integer tmp;
102                 Quantity_NameOfColor theColor;
103
104                 // Reading all shapes
105                 for ( int i = 0; i < nbShapes; i++ )
106                 {
107                         // Read the Persistent Shape from the archive
108                         Handle(Storage_Data) d = s->Read( f );
109                         Handle(Storage_HSeqOfRoot)  roots = d->Roots();
110                         Handle(Standard_Persistent) p;
111                         Handle(Storage_Root) r;
112                         Handle(PTopoDS_HShape) aPShape;
113                         TopoDS_Shape theShape;
114
115                         r = roots->Value(1);
116                         p = r->Object();
117                         aPShape  = Handle(PTopoDS_HShape)::DownCast(p);
118
119                         // Create the shape
120                         PTColStd_PersistentTransientMap aMap;
121
122                         MgtBRep::Translate(aPShape,aMap,theShape,MgtBRep_WithoutTriangle);
123
124                         m_shapeList.Append(theShape);
125                         
126                         // Read the Color from the archive
127                         ar >> tmp;
128                         theColor = (Quantity_NameOfColor) tmp;
129                         m_colorMap.Bind(theShape, theColor);
130                 }       
131         }
132 }
133
134 void CColoredShapes::Display(Handle(AIS_InteractiveContext)& anAIScontext)
135 {
136         for ( TopoDS_ListIteratorOfListOfShape iter(m_shapeList); iter.More(); iter.Next() )
137         {
138                 Handle(AIS_Shape) ais = new AIS_Shape(iter.Value());
139                 anAIScontext->SetColor(ais, (Quantity_NameOfColor)m_colorMap.Find(iter.Value()));
140                 anAIScontext->SetMaterial(ais, Graphic3d_NOM_GOLD, Standard_False);
141                 anAIScontext->Display(ais, Standard_False);
142         }
143 }