1 // Copyright (c) 2015 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <ShapePersistent_TopoDS.hxx>
15 #include <ShapePersistent_BRep.hxx>
17 #include <BRep_Builder.hxx>
20 #include <TopoDS_Iterator.hxx>
21 #include <TopoDS_Vertex.hxx>
23 #include <Standard_Assert.hxx>
35 //=======================================================================
37 //purpose : Read persistent data from a file
38 //=======================================================================
39 void ShapePersistent_TopoDS::HShape::Read (StdObjMgt_ReadData& theReadData)
41 theReadData >> myEntry;
42 StdObject_Shape::read (theReadData);
45 void ShapePersistent_TopoDS::HShape::Write (StdObjMgt_WriteData& theWriteData) const
47 theWriteData << myEntry;
48 StdObject_Shape::write (theWriteData);
51 void ShapePersistent_TopoDS::HShape::PChildren(SequenceOfPersistent& theChildren) const
53 theChildren.Append(myEntry);
54 StdObject_Shape::PChildren(theChildren);
57 void ShapePersistent_TopoDS::pTBase::setFlags
58 (const Handle(TopoDS_TShape)& theTShape) const
60 theTShape->Free (Standard_False); // Always frozen when coming from DB
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);
69 static inline void AddShape
70 (TopoDS_Shape& theParent, const Handle(StdObjMgt_Persistent)& theRef)
72 Handle(ShapePersistent_TopoDS::HShape) aShape =
73 Handle(ShapePersistent_TopoDS::HShape)::DownCast (theRef);
76 BRep_Builder().Add (theParent, aShape->Import());
79 static inline void AddShape
80 (TopoDS_Shape& theParent, const StdObject_Shape& theShape)
82 BRep_Builder().Add (theParent, theShape.Import());
85 template <class ShapesArray>
86 void ShapePersistent_TopoDS::pTBase::addShapesT
87 (TopoDS_Shape& theParent) const
89 Handle(ShapesArray) aShapes = Handle(ShapesArray)::DownCast (myShapes);
92 typename ShapesArray::Iterator anIter (*aShapes->Array());
93 for (; anIter.More(); anIter.Next())
94 AddShape (theParent, anIter.Value());
98 template void ShapePersistent_TopoDS::pTBase::addShapesT
99 <StdLPersistent_HArray1::Persistent> (TopoDS_Shape& theParent) const;
101 template void ShapePersistent_TopoDS::pTBase::addShapesT
102 <StdPersistent_HArray1::Shape1> (TopoDS_Shape& theParent) const;
104 template <class Target>
105 Handle(TopoDS_TShape)
106 ShapePersistent_TopoDS::pTSimple<Target>::createTShape() const
107 { return new Target; }
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>;
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)
124 Handle(HShape) pHShape;
126 if (theShape.IsNull())
129 pHShape = new HShape;
131 if (theMap.IsBound(theShape.TShape()))
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;
140 pTShape* aPTShape = 0;
141 switch (theShape.ShapeType())
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();
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();
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();
162 Handle(TWire) aPWire = new TWire;
163 pHShape->myTShape = aPWire;
164 aPWire->myPersistent = new TWire::pTObjectT;
165 aPTShape = aPWire->myPersistent.get();
168 Handle(TShell) aPShell = new TShell;
169 pHShape->myTShape = aPShell;
170 aPShell->myPersistent = new TShell::pTObjectT;
171 aPTShape = aPShell->myPersistent.get();
174 Handle(TSolid) aPSolid = new TSolid;
175 pHShape->myTShape = aPSolid;
176 aPSolid->myPersistent = new TSolid::pTObjectT;
177 aPTShape = aPSolid->myPersistent.get();
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();
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();
194 Standard_ASSERT_INVOKE ("Unsupported shape type");
198 // Register in the persistent map
199 theMap.Bind(theShape.TShape(), pHShape->myTShape);
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;
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
216 Standard_Integer nbElem = 0;
217 TopoDS_Iterator anItCount(S);
218 while (anItCount.More()) {
225 Handle(StdLPersistent_HArray1OfPersistent) aShapes =
226 new StdLPersistent_HArray1OfPersistent(1, nbElem);
227 // translate <sub-shapes>
228 TopoDS_Iterator anItTrans(S);
229 for (Standard_Integer i = 1; anItTrans.More(); anItTrans.Next(), ++i) {
230 aShapes->SetValue(i, Translate(anItTrans.Value(), theMap, theTriangleMode));
232 aPTShape->myShapes = StdLPersistent_HArray1::Translate<StdLPersistent_HArray1OfPersistent>
233 ("PTopoDS_HArray1OfHShape", aShapes->Array1());
237 pHShape->myOrient = theShape.Orientation();
238 pHShape->myLocation = StdObject_Location::Translate(theShape.Location(), theMap);