ff205346 |
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_TopoDS.hxx> |
ec964372 |
15 | #include <ShapePersistent_BRep.hxx> |
ff205346 |
16 | |
17 | #include <BRep_Builder.hxx> |
18 | |
ec964372 |
19 | #include <TopoDS.hxx> |
20 | #include <TopoDS_Iterator.hxx> |
21 | #include <TopoDS_Vertex.hxx> |
22 | |
23 | #include <Standard_Assert.hxx> |
ff205346 |
24 | |
25 | enum |
26 | { |
27 | ModifiedMask = 2, |
28 | CheckedMask = 4, |
29 | OrientableMask = 8, |
30 | ClosedMask = 16, |
31 | InfiniteMask = 32, |
32 | ConvexMask = 64 |
33 | }; |
34 | |
ff205346 |
35 | //======================================================================= |
36 | //function : Read |
37 | //purpose : Read persistent data from a file |
38 | //======================================================================= |
45d8465e |
39 | void ShapePersistent_TopoDS::HShape::Read (StdObjMgt_ReadData& theReadData) |
ff205346 |
40 | { |
41 | theReadData >> myEntry; |
45d8465e |
42 | StdObject_Shape::read (theReadData); |
ff205346 |
43 | } |
44 | |
ec964372 |
45 | void ShapePersistent_TopoDS::HShape::Write (StdObjMgt_WriteData& theWriteData) const |
46 | { |
47 | theWriteData << myEntry; |
48 | StdObject_Shape::write (theWriteData); |
49 | } |
50 | |
51 | void ShapePersistent_TopoDS::HShape::PChildren(SequenceOfPersistent& theChildren) const |
52 | { |
53 | theChildren.Append(myEntry); |
54 | StdObject_Shape::PChildren(theChildren); |
55 | } |
56 | |
ff205346 |
57 | void ShapePersistent_TopoDS::pTBase::setFlags |
58 | (const Handle(TopoDS_TShape)& theTShape) const |
59 | { |
60 | theTShape->Free (Standard_False); // Always frozen when coming from DB |
dde68833 |
61 | theTShape->Modified ((myFlags & ModifiedMask) != 0); |
62 | theTShape->Checked ((myFlags & CheckedMask) != 0); |
63 | theTShape->Orientable ((myFlags & OrientableMask) != 0); |
64 | theTShape->Closed ((myFlags & ClosedMask) != 0); |
65 | theTShape->Infinite ((myFlags & InfiniteMask) != 0); |
66 | theTShape->Convex ((myFlags & ConvexMask) != 0); |
ff205346 |
67 | } |
68 | |
69 | static inline void AddShape |
70 | (TopoDS_Shape& theParent, const Handle(StdObjMgt_Persistent)& theRef) |
71 | { |
45d8465e |
72 | Handle(ShapePersistent_TopoDS::HShape) aShape = |
73 | Handle(ShapePersistent_TopoDS::HShape)::DownCast (theRef); |
ff205346 |
74 | |
75 | if (aShape) |
76 | BRep_Builder().Add (theParent, aShape->Import()); |
77 | } |
78 | |
79 | static inline void AddShape |
80 | (TopoDS_Shape& theParent, const StdObject_Shape& theShape) |
81 | { |
82 | BRep_Builder().Add (theParent, theShape.Import()); |
83 | } |
84 | |
85 | template <class ShapesArray> |
86 | void ShapePersistent_TopoDS::pTBase::addShapesT |
87 | (TopoDS_Shape& theParent) const |
88 | { |
45d8465e |
89 | Handle(ShapesArray) aShapes = Handle(ShapesArray)::DownCast (myShapes); |
90 | if (aShapes) |
ff205346 |
91 | { |
92 | typename ShapesArray::Iterator anIter (*aShapes->Array()); |
93 | for (; anIter.More(); anIter.Next()) |
94 | AddShape (theParent, anIter.Value()); |
95 | } |
96 | } |
97 | |
98 | template void ShapePersistent_TopoDS::pTBase::addShapesT |
99 | <StdLPersistent_HArray1::Persistent> (TopoDS_Shape& theParent) const; |
100 | |
101 | template void ShapePersistent_TopoDS::pTBase::addShapesT |
102 | <StdPersistent_HArray1::Shape1> (TopoDS_Shape& theParent) const; |
103 | |
104 | template <class Target> |
105 | Handle(TopoDS_TShape) |
106 | ShapePersistent_TopoDS::pTSimple<Target>::createTShape() const |
107 | { return new Target; } |
108 | |
109 | template class ShapePersistent_TopoDS::pTSimple<TopoDS_TWire>; |
110 | template class ShapePersistent_TopoDS::pTSimple<TopoDS_TShell>; |
111 | template class ShapePersistent_TopoDS::pTSimple<TopoDS_TSolid>; |
112 | template class ShapePersistent_TopoDS::pTSimple<TopoDS_TCompSolid>; |
113 | template class ShapePersistent_TopoDS::pTSimple<TopoDS_TCompound>; |
ec964372 |
114 | |
115 | //======================================================================= |
116 | //function : Translate |
117 | //purpose : Creates a persistent object from a shape |
118 | //======================================================================= |
119 | Handle(ShapePersistent_TopoDS::HShape) |
120 | ShapePersistent_TopoDS::Translate (const TopoDS_Shape& theShape, |
121 | StdObjMgt_TransientPersistentMap& theMap, |
122 | ShapePersistent_TriangleMode theTriangleMode) |
123 | { |
124 | Handle(HShape) pHShape; |
125 | |
126 | if (theShape.IsNull()) |
127 | return pHShape; |
128 | |
129 | pHShape = new HShape; |
130 | |
131 | if (theMap.IsBound(theShape.TShape())) |
132 | { |
133 | // found in the registered |
134 | Handle(StdPersistent_TopoDS::TShape) aPShape = |
135 | Handle(StdPersistent_TopoDS::TShape)::DownCast(theMap.Find(theShape.TShape())); |
136 | pHShape->myTShape = aPShape; |
137 | } |
138 | else |
139 | { |
140 | pTShape* aPTShape = 0; |
141 | switch (theShape.ShapeType()) |
142 | { |
143 | case TopAbs_VERTEX: { |
144 | Handle(ShapePersistent_BRep::TVertex) aPVertex = new ShapePersistent_BRep::TVertex; |
145 | pHShape->myTShape = aPVertex; |
146 | aPVertex->myPersistent = ShapePersistent_BRep::Translate(TopoDS::Vertex(theShape), theMap); |
147 | aPTShape = aPVertex->myPersistent.get(); |
148 | } break; |
149 | case TopAbs_EDGE: { |
150 | Handle(ShapePersistent_BRep::TEdge) aPEdge = new ShapePersistent_BRep::TEdge; |
151 | pHShape->myTShape = aPEdge; |
152 | aPEdge->myPersistent = ShapePersistent_BRep::Translate(TopoDS::Edge(theShape), theMap, theTriangleMode); |
153 | aPTShape = aPEdge->myPersistent.get(); |
154 | } break; |
155 | case TopAbs_FACE: { |
156 | Handle(ShapePersistent_BRep::TFace) aPFace = new ShapePersistent_BRep::TFace; |
157 | pHShape->myTShape = aPFace; |
158 | aPFace->myPersistent = ShapePersistent_BRep::Translate(TopoDS::Face(theShape), theMap, theTriangleMode); |
159 | aPTShape = aPFace->myPersistent.get(); |
160 | } break; |
161 | case TopAbs_WIRE: { |
162 | Handle(TWire) aPWire = new TWire; |
163 | pHShape->myTShape = aPWire; |
164 | aPWire->myPersistent = new TWire::pTObjectT; |
165 | aPTShape = aPWire->myPersistent.get(); |
166 | } break; |
167 | case TopAbs_SHELL: { |
168 | Handle(TShell) aPShell = new TShell; |
169 | pHShape->myTShape = aPShell; |
170 | aPShell->myPersistent = new TShell::pTObjectT; |
171 | aPTShape = aPShell->myPersistent.get(); |
172 | } break; |
173 | case TopAbs_SOLID: { |
174 | Handle(TSolid) aPSolid = new TSolid; |
175 | pHShape->myTShape = aPSolid; |
176 | aPSolid->myPersistent = new TSolid::pTObjectT; |
177 | aPTShape = aPSolid->myPersistent.get(); |
178 | } break; |
179 | case TopAbs_COMPSOLID: { |
180 | Handle(TCompSolid) aPCompSolid = new TCompSolid; |
181 | pHShape->myTShape = aPCompSolid; |
182 | aPCompSolid->myPersistent = new TCompSolid::pTObjectT; |
183 | aPTShape = aPCompSolid->myPersistent.get(); |
184 | } break; |
185 | case TopAbs_COMPOUND: { |
186 | Handle(TCompound) aPComp = new TCompound; |
187 | pHShape->myTShape = aPComp; |
188 | aPComp->myPersistent = new TCompound::pTObjectT; |
189 | aPTShape = aPComp->myPersistent.get(); |
190 | } break; |
191 | |
192 | case TopAbs_SHAPE: |
193 | default: |
194 | Standard_ASSERT_INVOKE ("Unsupported shape type"); |
195 | break; |
196 | } |
197 | |
198 | // Register in the persistent map |
199 | theMap.Bind(theShape.TShape(), pHShape->myTShape); |
200 | |
201 | // Shape flags |
202 | Standard_Integer aFlags = 0; |
203 | if (theShape.Modified()) aFlags |= ModifiedMask; |
204 | if (theShape.Checked()) aFlags |= CheckedMask; |
205 | if (theShape.Orientable()) aFlags |= OrientableMask; |
206 | if (theShape.Closed()) aFlags |= ClosedMask; |
207 | if (theShape.Infinite()) aFlags |= InfiniteMask; |
208 | if (theShape.Convex()) aFlags |= ConvexMask; |
209 | aPTShape->myFlags = aFlags; |
210 | |
211 | // Copy current Shape |
212 | TopoDS_Shape S = theShape; |
213 | S.Orientation(TopAbs_FORWARD); |
214 | S.Location(TopLoc_Location()); |
215 | // Count the number of <sub-shape> of the Shape's TShape |
b2d1851c |
216 | Standard_Integer nbElem = S.NbChildren(); |
ec964372 |
217 | if (nbElem > 0) |
218 | { |
219 | Handle(StdLPersistent_HArray1OfPersistent) aShapes = |
220 | new StdLPersistent_HArray1OfPersistent(1, nbElem); |
221 | // translate <sub-shapes> |
222 | TopoDS_Iterator anItTrans(S); |
223 | for (Standard_Integer i = 1; anItTrans.More(); anItTrans.Next(), ++i) { |
224 | aShapes->SetValue(i, Translate(anItTrans.Value(), theMap, theTriangleMode)); |
225 | } |
226 | aPTShape->myShapes = StdLPersistent_HArray1::Translate<StdLPersistent_HArray1OfPersistent> |
227 | ("PTopoDS_HArray1OfHShape", aShapes->Array1()); |
228 | } |
229 | } |
230 | |
231 | pHShape->myOrient = theShape.Orientation(); |
232 | pHShape->myLocation = StdObject_Location::Translate(theShape.Location(), theMap); |
233 | |
234 | return pHShape; |
235 | } |