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