Salome HOME
27.10.2014. Naming data structure for Extrusion feature.
[modules/shaper.git] / src / Model / Model_ResultBody.cpp
1 // File:        Model_ResultBody.cpp
2 // Created:     08 Jul 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_ResultBody.h>
6 #include <Model_Data.h>
7 #include <TNaming_Builder.hxx>
8 #include <TNaming_NamedShape.hxx>
9 #include <TopoDS_Shape.hxx>
10 #include <TDF_ChildIterator.hxx>
11 #include <GeomAPI_Shape.h>
12
13 Model_ResultBody::Model_ResultBody()
14 {
15 }
16
17 void Model_ResultBody::store(const boost::shared_ptr<GeomAPI_Shape>& theShape)
18 {
19   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
20   if (aData) {
21     TDF_Label& aShapeLab = aData->shapeLab();
22     // clean builders
23     clean();   
24     // store the new shape as primitive
25     TNaming_Builder aBuilder(aShapeLab);
26     if (!theShape)
27       return;  // bad shape
28     TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
29     if (aShape.IsNull())
30       return;  // null shape inside
31
32     aBuilder.Generated(aShape);
33   }
34 }
35
36 void Model_ResultBody::storeGenerated(const boost::shared_ptr<GeomAPI_Shape>& theFromShape,
37                                           const boost::shared_ptr<GeomAPI_Shape>& theToShape)
38 {
39   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
40   if (aData) {
41     TDF_Label& aShapeLab = aData->shapeLab();
42     // clean builders
43     clean();   
44     // store the new shape as primitive
45     TNaming_Builder aBuilder(aShapeLab);
46     if (!theFromShape || !theToShape)
47       return;  // bad shape
48     TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
49     if (aShapeBasis.IsNull())
50       return;  // null shape inside
51         TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
52     if (aShapeNew.IsNull())
53       return;  // null shape inside
54     aBuilder.Generated(aShapeBasis, aShapeNew);
55   }
56 }
57
58 void Model_ResultBody::storeModified(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
59                                           const boost::shared_ptr<GeomAPI_Shape>& theNewShape)
60 {
61   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
62   if (aData) {
63     TDF_Label& aShapeLab = aData->shapeLab();
64     // clean builders
65     clean();   
66     // store the new shape as primitive
67     TNaming_Builder aBuilder(aShapeLab);
68     if (!theOldShape || !theNewShape)
69       return;  // bad shape
70     TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
71     if (aShapeOld.IsNull())
72       return;  // null shape inside
73         TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
74     if (aShapeNew.IsNull())
75       return;  // null shape inside
76     aBuilder.Generated(aShapeOld, aShapeNew);
77   }
78 }
79
80 boost::shared_ptr<GeomAPI_Shape> Model_ResultBody::shape()
81 {
82   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
83   if (aData) {
84     TDF_Label& aShapeLab = aData->shapeLab();
85     Handle(TNaming_NamedShape) aName;
86     if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
87       TopoDS_Shape aShape = aName->Get();
88       if (!aShape.IsNull()) {
89         boost::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
90         aRes->setImpl(new TopoDS_Shape(aShape));
91         return aRes;
92       }
93     }
94   }
95   return boost::shared_ptr<GeomAPI_Shape>();
96 }
97
98 boost::shared_ptr<ModelAPI_Feature> Model_ResultBody::owner()
99 {
100   return myOwner;
101 }
102
103 void Model_ResultBody::clean()
104 {
105   std::vector<TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
106   for(; aBuilder != myBuilders.end(); aBuilder++)
107     delete *aBuilder;
108 }
109
110 Model_ResultBody::~Model_ResultBody()
111 {
112   clean();
113 }
114
115 TNaming_Builder* Model_ResultBody::builder(const int theTag)
116 {
117   if (myBuilders.size() < (unsigned int)theTag) {
118     myBuilders.insert(myBuilders.end(), theTag - myBuilders.size() + 1, NULL);
119   }
120   if (!myBuilders[theTag]) {
121     boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
122     myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
123   }
124   return myBuilders[theTag];
125 }
126
127 void Model_ResultBody::generated(
128   const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
129 {
130   TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
131   builder(theTag)->Generated(aShape);
132 }
133
134 void Model_ResultBody::generated(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
135     const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
136 {
137   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
138   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
139   builder(theTag)->Generated(anOldShape, aNewShape);
140 }
141
142
143 void Model_ResultBody::modified(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
144     const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
145 {
146   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
147   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
148   builder(theTag)->Modify(anOldShape, aNewShape);
149 }
150
151 void Model_ResultBody::deleted(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
152     const int theTag)
153 {
154   TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
155   builder(theTag)->Delete(aShape);
156 }