0033022: Coding - get rid of unused headers [ShapeBuild to STEPControl]
[occt.git] / src / ShapePersistent / ShapePersistent_Geom.hxx
1 // Copyright (c) 2015 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #ifndef _ShapePersistent_Geom_HeaderFile
16 #define _ShapePersistent_Geom_HeaderFile
17
18 #include <Standard_NotImplemented.hxx>
19 #include <Standard_NullObject.hxx>
20
21 #include <StdObjMgt_SharedObject.hxx>
22 #include <StdObjMgt_TransientPersistentMap.hxx>
23
24 #include <StdObject_gp_Curves.hxx>
25 #include <StdObject_gp_Surfaces.hxx>
26 #include <StdObject_gp_Trsfs.hxx>
27
28 #include <Geom_CartesianPoint.hxx>
29 #include <Geom_Direction.hxx>
30 #include <Geom_VectorWithMagnitude.hxx>
31 #include <Geom_Axis1Placement.hxx>
32 #include <Geom_Axis2Placement.hxx>
33 #include <Geom_Transformation.hxx>
34 #include <Geom_Surface.hxx>
35
36 class ShapePersistent_Geom : public StdObjMgt_SharedObject
37 {
38 public:
39   class Geometry : public StdObjMgt_Persistent
40   {
41   public:
42     //! Read persistent data from a file.
43     Standard_EXPORT virtual void Read (StdObjMgt_ReadData& theReadData);
44     //! Write persistent data to a file.
45     Standard_EXPORT virtual void Write (StdObjMgt_WriteData& theWriteData) const;
46     //! Gets persistent objects
47     Standard_EXPORT virtual void PChildren(SequenceOfPersistent& theChildren) const;
48     //! Returns persistent type name
49     virtual Standard_CString PName() const { return "PGeom_Geometry"; }
50   };
51
52 protected:
53   template <class Transient>
54   struct geometryBase : public DelayedBase<Geometry, Transient> 
55   {
56     //! Write persistent data to a file.
57     virtual void Write (StdObjMgt_WriteData&) const
58     {
59       Standard_NotImplemented::Raise("ShapePersistent_Geom::geometryBase::Write - not implemented");
60     }
61     //! Gets persistent child objects
62     virtual void PChildren (StdObjMgt_Persistent::SequenceOfPersistent&) const { }
63     //! Returns persistent type name
64     virtual Standard_CString PName() const
65     { 
66       Standard_NotImplemented::Raise("ShapePersistent_Geom::geometryBase::PName - not implemented");
67       return ""; 
68     }
69   };
70
71   template <class Base, class PData>
72   class subBase : public Base
73   {
74   public:
75     //! Read persistent data from a file.
76     virtual void Read (StdObjMgt_ReadData& theReadData)
77       { PData().Read (theReadData); }
78     //! Write persistent data to a file.
79     virtual void Write (StdObjMgt_WriteData& theWriteData) const
80       { PData().Write(theWriteData); }
81     //! Gets persistent child objects
82     virtual void PChildren (StdObjMgt_Persistent::SequenceOfPersistent&) const
83     {
84       Standard_NotImplemented::Raise("ShapePersistent_Geom::subBase::PChildren - not implemented");
85     }
86     //! Returns persistent type name
87     virtual Standard_CString PName() const
88     { 
89       Standard_NotImplemented::Raise("ShapePersistent_Geom::subBase::PName - not implemented");
90       return ""; 
91     }
92   };
93
94   template <class Base, class GpData>
95   struct subBase_gp : public Base
96   {
97   public:
98     //! Read persistent data from a file.
99     virtual void Read (StdObjMgt_ReadData&) { }
100     //! Write persistent data to a file.
101     virtual void Write (StdObjMgt_WriteData&) const { }
102     //! Gets persistent child objects
103     virtual void PChildren (StdObjMgt_Persistent::SequenceOfPersistent&) const { }
104     //! Returns persistent type name
105     virtual Standard_CString PName() const
106     {
107       Standard_NotImplemented::Raise("ShapePersistent_Geom::subBase_gp::PName - not implemented");
108       return "";
109     }
110   };
111
112   template <class Base>
113   struct subBase_empty : Base  
114   { 
115     //! Returns persistent type name
116     virtual Standard_CString PName() const
117     {
118       Standard_NotImplemented::Raise("ShapePersistent_Geom::subBase_empty::PName - not implemented");
119       return "";
120     }
121   };
122
123   template <class Base, class Target, class Data = void>
124   class instance : public Base
125   {
126   public:
127     //! Read persistent data from a file.
128     virtual void Read (StdObjMgt_ReadData& theReadData)
129     {
130       Data aData;
131       theReadData >> aData;
132       this->myTransient = new Target(aData);
133     }
134     //! Gets persistent child objects
135     virtual void PChildren(StdObjMgt_Persistent::SequenceOfPersistent&) const { }
136     //! Write persistent data to a file.
137     virtual void Write(StdObjMgt_WriteData&) const
138     {
139       Standard_NotImplemented::Raise("ShapePersistent_Geom::instance::Write - not implemented");
140     }
141     //! Returns persistent type name
142     virtual Standard_CString PName() const
143     {
144       Standard_NotImplemented::Raise("ShapePersistent_Geom::instance::PName - not implemented");
145       return "";
146     }
147   };
148
149 private:
150   typedef geometryBase<Geom_Geometry> basic;
151
152 public:
153   typedef subBase_empty<basic>                                 Point;
154   typedef instance<Point, Geom_CartesianPoint, gp_Pnt>         CartesianPoint;
155
156   typedef subBase_gp<basic, gp_Vec>                            Vector;
157   typedef instance<Vector, Geom_Direction          , gp_Dir>   Direction;
158   typedef instance<Vector, Geom_VectorWithMagnitude, gp_Vec>   VectorWithMagnitude;
159
160   typedef subBase_gp<basic, gp_Ax1>                            AxisPlacement;
161   typedef instance<AxisPlacement, Geom_Axis1Placement, gp_Ax1> Axis1Placement;
162   typedef instance<AxisPlacement, Geom_Axis2Placement>         Axis2Placement;
163
164   typedef instance <SharedBase<Geom_Transformation>,
165                     Geom_Transformation,
166                     gp_Trsf>                                   Transformation;
167
168   typedef geometryBase<Geom_Curve>                             Curve;
169   typedef geometryBase<Geom_Surface>                           Surface;
170
171 public:
172   //! Create a persistent object for a curve
173   Standard_EXPORT static Handle(Curve) Translate (const Handle(Geom_Curve)& theCurve,
174                                                   StdObjMgt_TransientPersistentMap& theMap);
175   //! Create a persistent object for a curve
176   Standard_EXPORT static Handle(Surface) Translate (const Handle(Geom_Surface)& theSurf,
177                                                     StdObjMgt_TransientPersistentMap& theMap);
178 };
179
180 //=======================================================================
181 // Point 
182 //=======================================================================
183
184 template<>
185 inline Standard_CString ShapePersistent_Geom::subBase_empty<ShapePersistent_Geom::basic>
186   ::PName() const { return "PGeom_Point"; }
187
188 //=======================================================================
189 // CartesianPoint
190 //=======================================================================
191
192 template<>
193 inline Standard_CString ShapePersistent_Geom::instance<ShapePersistent_Geom::Point,
194                                                        Geom_CartesianPoint,
195                                                        gp_Pnt>
196   ::PName() const { return "PGeom_CartesianPoint"; }
197
198 template<>
199 inline void ShapePersistent_Geom::instance<ShapePersistent_Geom::Point,
200                                            Geom_CartesianPoint,
201                                            gp_Pnt>
202   ::Write(StdObjMgt_WriteData& theWriteData) const
203 {
204   Handle(Geom_CartesianPoint) aMyGeom =
205     Handle(Geom_CartesianPoint)::DownCast(myTransient);
206   theWriteData << aMyGeom->Pnt();
207 }
208
209 //=======================================================================
210 // Vector
211 //=======================================================================
212
213 template<>
214 inline Standard_CString ShapePersistent_Geom::subBase_gp<ShapePersistent_Geom::basic,
215                                                          gp_Vec>
216   ::PName() const { return "PGeom_Vector"; }
217
218 //=======================================================================
219 // Direction
220 //=======================================================================
221
222 template<>
223 inline Standard_CString ShapePersistent_Geom::instance<ShapePersistent_Geom::Direction,
224                                                        Geom_Direction,
225                                                        gp_Dir>
226   ::PName() const { return "PGeom_Direction"; }
227
228 template<>
229 inline void ShapePersistent_Geom::instance<ShapePersistent_Geom::Direction,
230                                            Geom_Direction,
231                                            gp_Dir>
232   ::Write(StdObjMgt_WriteData& theWriteData) const
233 {
234   Handle(Geom_Direction) aMyGeom =
235     Handle(Geom_Direction)::DownCast(myTransient);
236   theWriteData << aMyGeom->Dir();
237 }
238
239 //=======================================================================
240 // VectorWithMagnitude
241 //=======================================================================
242
243 template<>
244 inline Standard_CString ShapePersistent_Geom::instance<ShapePersistent_Geom::VectorWithMagnitude,
245                                                        Geom_VectorWithMagnitude,
246                                                        gp_Vec>
247   ::PName() const { return "PGeom_VectorWithMagnitude"; }
248
249 template<>
250 inline void ShapePersistent_Geom::instance<ShapePersistent_Geom::VectorWithMagnitude,
251                                            Geom_VectorWithMagnitude,
252                                            gp_Vec>
253   ::Write(StdObjMgt_WriteData& theWriteData) const
254 {
255   Handle(Geom_VectorWithMagnitude) aMyGeom =
256     Handle(Geom_VectorWithMagnitude)::DownCast(myTransient);
257   theWriteData << aMyGeom->Vec();
258 }
259
260 //=======================================================================
261 // AxisPlacement 
262 //=======================================================================
263
264 template<>
265 inline Standard_CString ShapePersistent_Geom::subBase_gp<ShapePersistent_Geom::basic,
266                                                          gp_Ax1>
267   ::PName() const { return "PGeom_AxisPlacement"; }
268
269 //=======================================================================
270 // Axis1Placement
271 //=======================================================================
272
273 template<>
274 inline Standard_CString ShapePersistent_Geom::instance<ShapePersistent_Geom::Axis1Placement,
275                                                        Geom_Axis1Placement,
276                                                        gp_Ax1>
277   ::PName() const { return "PGeom_Axis1Placement"; }
278
279 template<>
280 inline void ShapePersistent_Geom::instance<ShapePersistent_Geom::Axis1Placement,
281                                            Geom_Axis1Placement,
282                                            gp_Ax1>
283   ::Write(StdObjMgt_WriteData& theWriteData) const
284 {
285   Handle(Geom_Axis1Placement) aMyGeom =
286     Handle(Geom_Axis1Placement)::DownCast(myTransient);
287   write(theWriteData, aMyGeom->Ax1());
288 }
289
290 //=======================================================================
291 // Axis2Placement 
292 //=======================================================================
293
294 template<>
295 inline Standard_CString ShapePersistent_Geom::instance<ShapePersistent_Geom::AxisPlacement,
296                                                        Geom_Axis2Placement>
297   ::PName() const { return "PGeom_Axis2Placement"; }
298
299 template<>
300 inline void ShapePersistent_Geom::instance<ShapePersistent_Geom::AxisPlacement,
301                                            Geom_Axis2Placement>
302   ::Read (StdObjMgt_ReadData& theReadData)
303 {
304   gp_Ax1 anAxis;
305   gp_Dir anXDirection;
306
307   theReadData >> anAxis >> anXDirection;
308
309   myTransient = new Geom_Axis2Placement(anAxis.Location(),
310     anAxis.Direction(),
311     anXDirection);
312 }
313
314 template<>
315 inline void ShapePersistent_Geom::instance<ShapePersistent_Geom::AxisPlacement,
316                                            Geom_Axis2Placement>
317   ::Write (StdObjMgt_WriteData& theWriteData) const
318 {
319   Handle(Geom_Axis2Placement) aMyGeom =
320     Handle(Geom_Axis2Placement)::DownCast(myTransient);
321   const gp_Ax1& anAxis = aMyGeom->Axis();
322   const gp_Dir& anXDirection = aMyGeom->Direction();
323   write(theWriteData, anAxis) << anXDirection;
324 }
325
326 //=======================================================================
327 // Transformation
328 //=======================================================================
329
330 template<>
331 inline Standard_CString ShapePersistent_Geom::instance<ShapePersistent_Geom::Transformation,
332                                                        Geom_Transformation,
333                                                        gp_Trsf>
334   ::PName() const { return "PGeom_Transformation"; }
335
336 template<>
337 inline void ShapePersistent_Geom::instance<ShapePersistent_Geom::Transformation,
338                                            Geom_Transformation,
339                                            gp_Trsf>
340   ::Write (StdObjMgt_WriteData& theWriteData) const
341 {
342   theWriteData << myTransient->Trsf();
343 }
344
345 //=======================================================================
346 // Geometry
347 //=======================================================================
348
349 template<>
350 inline Standard_CString ShapePersistent_Geom::geometryBase<Geom_Geometry>
351   ::PName() const { return "PGeom_Geometry"; }
352
353 //=======================================================================
354 // Curve
355 //=======================================================================
356
357 template<>
358 inline Standard_CString ShapePersistent_Geom::geometryBase<Geom_Curve>
359   ::PName() const { return "PGeom_Curve"; }
360
361 //=======================================================================
362 // Surface
363 //=======================================================================
364
365 template<>
366 inline Standard_CString ShapePersistent_Geom::geometryBase<Geom_Surface>
367   ::PName() const { return "PGeom_Surface"; }
368
369 #endif