Salome HOME
f5c8cb223d1d0438fc78fc0210628e840328add0
[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     // remove the previous history
23     clean();
24     aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
25     for(TDF_ChildIterator anIter(aShapeLab); anIter.More(); anIter.Next()) {
26       anIter.Value().ForgetAllAttributes();
27     }
28     // store the new shape as primitive
29     TNaming_Builder aBuilder(aShapeLab);
30     if (!theShape)
31       return;  // bad shape
32     TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
33     if (aShape.IsNull())
34       return;  // null shape inside
35
36     aBuilder.Generated(aShape);
37   }
38 }
39
40 boost::shared_ptr<GeomAPI_Shape> Model_ResultBody::shape()
41 {
42   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
43   if (aData) {
44     TDF_Label& aShapeLab = aData->shapeLab();
45     Handle(TNaming_NamedShape) aName;
46     if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
47       TopoDS_Shape aShape = aName->Get();
48       if (!aShape.IsNull()) {
49         boost::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
50         aRes->setImpl(new TopoDS_Shape(aShape));
51         return aRes;
52       }
53     }
54   }
55   return boost::shared_ptr<GeomAPI_Shape>();
56 }
57
58 boost::shared_ptr<ModelAPI_Feature> Model_ResultBody::owner()
59 {
60   return myOwner;
61 }
62
63 void Model_ResultBody::clean()
64 {
65   std::vector<TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
66   for(; aBuilder != myBuilders.end(); aBuilder++)
67     delete *aBuilder;
68 }
69
70 Model_ResultBody::~Model_ResultBody()
71 {
72   clean();
73 }
74
75 TNaming_Builder* Model_ResultBody::builder(const int theTag)
76 {
77   if (myBuilders.size() < (unsigned int)theTag) {
78     myBuilders.insert(myBuilders.end(), theTag - myBuilders.size() + 1, NULL);
79   }
80   if (!myBuilders[theTag]) {
81     boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
82     myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
83   }
84   return myBuilders[theTag];
85 }
86
87 void Model_ResultBody::generated(
88   const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
89 {
90   TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
91   builder(theTag)->Generated(aShape);
92 }
93
94 void Model_ResultBody::generated(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
95     const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
96 {
97   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
98   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
99   builder(theTag)->Generated(anOldShape, aNewShape);
100 }
101
102
103 void Model_ResultBody::modified(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
104     const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
105 {
106   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
107   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
108   builder(theTag)->Modify(anOldShape, aNewShape);
109 }
110
111 void Model_ResultBody::deleted(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
112     const int theTag)
113 {
114   TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
115   builder(theTag)->Delete(aShape);
116 }