cb401ca1aef10b83a202c8b1c889796a0bee561d
[occt.git] / src / ShapePersistent / ShapePersistent_BRep.cxx
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 #include <ShapePersistent_BRep.hxx>
15
16 #include <BRep_PointOnCurve.hxx>
17 #include <BRep_PointOnCurveOnSurface.hxx>
18 #include <BRep_PointOnSurface.hxx>
19 #include <BRep_Curve3D.hxx>
20 #include <BRep_CurveOnSurface.hxx>
21 #include <BRep_CurveOnClosedSurface.hxx>
22 #include <BRep_Polygon3D.hxx>
23 #include <BRep_PolygonOnTriangulation.hxx>
24 #include <BRep_PolygonOnClosedTriangulation.hxx>
25 #include <BRep_PolygonOnSurface.hxx>
26 #include <BRep_PolygonOnClosedSurface.hxx>
27 #include <BRep_CurveOn2Surfaces.hxx>
28
29 #include <BRep_TVertex.hxx>
30 #include <BRep_TEdge.hxx>
31 #include <BRep_TFace.hxx>
32
33
34 enum
35 {
36   ParameterMask   = 1,
37   RangeMask       = 2,
38   DegeneratedMask = 4
39 };
40
41
42 //=======================================================================
43 //function : Read
44 //purpose  : Read persistent data from a file
45 //=======================================================================
46 void ShapePersistent_BRep::PointRepresentation::Read
47   (StdObjMgt_ReadData& theReadData)
48     { theReadData >> myLocation >> myParameter >> myNext; }
49
50 //=======================================================================
51 //function : Import
52 //purpose  : Import transient object from the persistent data
53 //=======================================================================
54 void ShapePersistent_BRep::PointRepresentation::Import
55   (BRep_ListOfPointRepresentation& thePoints) const
56 {
57   thePoints.Clear();
58   Handle(PointRepresentation) aPoint = this;
59   for (; aPoint; aPoint = aPoint->myNext)
60     thePoints.Prepend (aPoint->import());
61 }
62
63 Handle(BRep_PointRepresentation)
64   ShapePersistent_BRep::PointRepresentation::import() const
65     { return NULL; }
66
67 void ShapePersistent_BRep::PointOnCurve::Read
68   (StdObjMgt_ReadData& theReadData)
69 {
70   PointRepresentation::Read (theReadData);
71   theReadData >> myCurve;
72 }
73
74 Handle(BRep_PointRepresentation)
75   ShapePersistent_BRep::PointOnCurve::import() const
76 {
77   if (myCurve.IsNull())
78     return NULL;
79
80   return new BRep_PointOnCurve
81     (myParameter, myCurve->Import(), myLocation.Import());
82 }
83
84 void ShapePersistent_BRep::PointsOnSurface::Read
85   (StdObjMgt_ReadData& theReadData)
86 {
87   PointRepresentation::Read (theReadData);
88   theReadData >> mySurface;
89 }
90
91 void ShapePersistent_BRep::PointOnCurveOnSurface::Read
92   (StdObjMgt_ReadData& theReadData)
93 {
94   PointsOnSurface::Read (theReadData);
95   theReadData >> myPCurve;
96 }
97
98 Handle(BRep_PointRepresentation)
99   ShapePersistent_BRep::PointOnCurveOnSurface::import() const
100 {
101   if (mySurface.IsNull() || myPCurve.IsNull())
102     return NULL;
103
104   return new BRep_PointOnCurveOnSurface
105     (myParameter, myPCurve->Import(), mySurface->Import(), myLocation.Import());
106 }
107
108 void ShapePersistent_BRep::PointOnSurface::Read
109   (StdObjMgt_ReadData& theReadData)
110 {
111   PointsOnSurface::Read (theReadData);
112   theReadData >> myParameter2;
113 }
114
115 Handle(BRep_PointRepresentation)
116   ShapePersistent_BRep::PointOnSurface::import() const
117 {
118   if (mySurface.IsNull())
119     return NULL;
120
121   return new BRep_PointOnSurface
122     (myParameter, myParameter2, mySurface->Import(), myLocation.Import());
123 }
124
125 //=======================================================================
126 //function : Read
127 //purpose  : Read persistent data from a file
128 //=======================================================================
129 void ShapePersistent_BRep::CurveRepresentation::Read
130   (StdObjMgt_ReadData& theReadData)
131     { theReadData >> myLocation >> myNext; }
132
133 //=======================================================================
134 //function : Import
135 //purpose  : Import transient object from the persistent data
136 //=======================================================================
137 void ShapePersistent_BRep::CurveRepresentation::Import
138   (BRep_ListOfCurveRepresentation& theCurves) const
139 {
140   theCurves.Clear();
141   Handle(CurveRepresentation) aCurve = this;
142   for (; aCurve; aCurve = aCurve->myNext)
143     theCurves.Prepend (aCurve->import());
144 }
145
146 Handle(BRep_CurveRepresentation)
147   ShapePersistent_BRep::CurveRepresentation::import() const
148     { return NULL; }
149
150 void ShapePersistent_BRep::GCurve::Read
151   (StdObjMgt_ReadData& theReadData)
152 {
153   CurveRepresentation::Read (theReadData);
154   theReadData >> myFirst >> myLast;
155 }
156
157 void ShapePersistent_BRep::Curve3D::Read
158   (StdObjMgt_ReadData& theReadData)
159 {
160   GCurve::Read (theReadData);
161   theReadData >> myCurve3D;
162 }
163
164 Handle(BRep_CurveRepresentation)
165   ShapePersistent_BRep::Curve3D::import() const
166 {
167   Handle(Geom_Curve) aCurve;
168   if (myCurve3D)
169     aCurve = myCurve3D->Import();
170
171   Handle(BRep_Curve3D) aRepresentation =
172     new BRep_Curve3D (aCurve, myLocation.Import());
173
174   aRepresentation->SetRange (myFirst, myLast);
175   return aRepresentation;
176 }
177
178 void ShapePersistent_BRep::CurveOnSurface::Read
179   (StdObjMgt_ReadData& theReadData)
180 {
181   GCurve::Read (theReadData);
182   theReadData >> myPCurve >> mySurface >> myUV1 >> myUV2;
183 }
184
185 Handle(BRep_CurveRepresentation)
186   ShapePersistent_BRep::CurveOnSurface::import() const
187 {
188   if (myPCurve.IsNull() || mySurface.IsNull())
189     return NULL;
190
191   Handle(BRep_CurveOnSurface) aCurve =
192     new BRep_CurveOnSurface (myPCurve->Import(),
193                              mySurface->Import(),
194                              myLocation.Import());
195
196   aCurve->SetUVPoints (myUV1, myUV2);
197   aCurve->SetRange (myFirst, myLast);
198
199   return aCurve;
200 }
201
202 void ShapePersistent_BRep::CurveOnClosedSurface::Read
203   (StdObjMgt_ReadData& theReadData)
204 {
205   CurveOnSurface::Read (theReadData);
206   theReadData >> myPCurve2 >> myContinuity >> myUV21 >> myUV22;
207 }
208
209 Handle(BRep_CurveRepresentation)
210   ShapePersistent_BRep::CurveOnClosedSurface::import() const
211 {
212   if (myPCurve.IsNull() || mySurface.IsNull() || myPCurve2.IsNull())
213     return NULL;
214
215   Handle(BRep_CurveOnClosedSurface) aCurve =
216     new BRep_CurveOnClosedSurface (myPCurve->Import(),
217                                    myPCurve2->Import(),
218                                    mySurface->Import(),
219                                    myLocation.Import(),
220                                    myContinuity);
221
222   aCurve->SetUVPoints  (myUV1  , myUV2 );
223   aCurve->SetUVPoints2 (myUV21 , myUV22);
224   aCurve->SetRange     (myFirst, myLast);
225
226   return aCurve;
227 }
228
229 void ShapePersistent_BRep::Polygon3D::Read
230   (StdObjMgt_ReadData& theReadData)
231 {
232   CurveRepresentation::Read (theReadData);
233   theReadData >> myPolygon3D;
234 }
235
236 Handle(BRep_CurveRepresentation)
237   ShapePersistent_BRep::Polygon3D::import() const
238 {
239   if (myPolygon3D.IsNull())
240     return NULL;
241
242   return new BRep_Polygon3D (myPolygon3D->Import(), myLocation.Import());
243 }
244
245 void ShapePersistent_BRep::PolygonOnTriangulation::Read
246   (StdObjMgt_ReadData& theReadData)
247 {
248   CurveRepresentation::Read (theReadData);
249   theReadData >> myPolygon >> myTriangulation;
250 }
251
252 Handle(BRep_CurveRepresentation)
253   ShapePersistent_BRep::PolygonOnTriangulation::import() const
254 {
255   if (myPolygon.IsNull() || myTriangulation.IsNull())
256     return NULL;
257
258   return new BRep_PolygonOnTriangulation (myPolygon->Import(),
259                                           myTriangulation->Import(),
260                                           myLocation.Import());
261 }
262
263 void ShapePersistent_BRep::PolygonOnClosedTriangulation::Read
264   (StdObjMgt_ReadData& theReadData)
265 {
266   PolygonOnTriangulation::Read (theReadData);
267   theReadData >> myPolygon2;
268 }
269
270 Handle(BRep_CurveRepresentation)
271   ShapePersistent_BRep::PolygonOnClosedTriangulation::import() const
272 {
273   if (myPolygon.IsNull() || myTriangulation.IsNull() || myPolygon2.IsNull())
274     return NULL;
275
276   return new BRep_PolygonOnClosedTriangulation (myPolygon->Import(),
277                                                 myPolygon2->Import(),
278                                                 myTriangulation->Import(),
279                                                 myLocation.Import());
280 }
281
282 void ShapePersistent_BRep::PolygonOnSurface::Read
283   (StdObjMgt_ReadData& theReadData)
284 {
285   CurveRepresentation::Read (theReadData);
286   theReadData >> myPolygon2D >> mySurface;
287 }
288
289 Handle(BRep_CurveRepresentation)
290   ShapePersistent_BRep::PolygonOnSurface::import() const
291 {
292   if (myPolygon2D.IsNull() || mySurface.IsNull())
293     return NULL;
294
295   return new BRep_PolygonOnSurface (myPolygon2D->Import(),
296                                     mySurface->Import(),
297                                     myLocation.Import());
298 }
299
300 void ShapePersistent_BRep::PolygonOnClosedSurface::Read
301   (StdObjMgt_ReadData& theReadData)
302 {
303   PolygonOnSurface::Read (theReadData);
304   theReadData >> myPolygon2;
305 }
306
307 Handle(BRep_CurveRepresentation)
308   ShapePersistent_BRep::PolygonOnClosedSurface::import() const
309 {
310   if (myPolygon2D.IsNull() || mySurface.IsNull() || myPolygon2.IsNull())
311     return NULL;
312
313   return new BRep_PolygonOnClosedSurface (myPolygon2D->Import(),
314                                           myPolygon2->Import(),
315                                           mySurface->Import(),
316                                           myLocation.Import());
317 }
318
319 void ShapePersistent_BRep::CurveOn2Surfaces::Read
320   (StdObjMgt_ReadData& theReadData)
321 {
322   CurveRepresentation::Read (theReadData);
323   theReadData >> mySurface >> mySurface2 >> myLocation2 >> myContinuity;
324 }
325
326 Handle(BRep_CurveRepresentation)
327   ShapePersistent_BRep::CurveOn2Surfaces::import() const
328 {
329   if (mySurface.IsNull() || mySurface2.IsNull())
330     return NULL;
331
332   return new BRep_CurveOn2Surfaces (mySurface->Import(),
333                                     mySurface2->Import(),
334                                     myLocation.Import(),
335                                     myLocation2.Import(),
336                                     myContinuity);
337 }
338
339 //=======================================================================
340 //function : createTShape
341 //purpose  : Create transient TShape object
342 //=======================================================================
343 Handle(TopoDS_TShape) ShapePersistent_BRep::pTVertex::createTShape() const
344 {
345   Handle(BRep_TVertex) aTVertex = new BRep_TVertex;
346
347   aTVertex->Tolerance (myTolerance);
348   aTVertex->Pnt       (myPnt);
349
350   myPoints->Import (aTVertex->ChangePoints());
351
352   return aTVertex;
353 }
354
355 //=======================================================================
356 //function : createTShape
357 //purpose  : Create transient TShape object
358 //=======================================================================
359 Handle(TopoDS_TShape) ShapePersistent_BRep::pTEdge::createTShape() const
360 {
361   Handle(BRep_TEdge) aTEdge = new BRep_TEdge;
362
363   aTEdge->Tolerance     (myTolerance);
364   aTEdge->SameParameter (myFlags & ParameterMask);
365   aTEdge->SameRange     (myFlags & RangeMask);
366   aTEdge->Degenerated   (myFlags & DegeneratedMask);
367
368   myCurves->Import (aTEdge->ChangeCurves());
369
370   return aTEdge;
371 }
372
373 //=======================================================================
374 //function : createTShape
375 //purpose  : Create transient TShape object
376 //=======================================================================
377 Handle(TopoDS_TShape) ShapePersistent_BRep::pTFace::createTShape() const
378 {
379   Handle(BRep_TFace) aTFace = new BRep_TFace;
380
381   aTFace->NaturalRestriction (myNaturalRestriction);
382   aTFace->Tolerance          (myTolerance);
383   aTFace->Location           (myLocation.Import());
384
385   if (mySurface)
386     aTFace->Surface (mySurface->Import());
387
388   if (myTriangulation)
389     aTFace->Triangulation (myTriangulation->Import());
390
391   return aTFace;
392 }