Salome HOME
Added a selection attribute and naming basic mechanisms
[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 <GeomAPI_Shape.h>
11
12 Model_ResultBody::Model_ResultBody()
13 {
14 }
15
16 void Model_ResultBody::store(const boost::shared_ptr<GeomAPI_Shape>& theShape)
17 {
18   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
19   if (aData) {
20     TDF_Label& aShapeLab = aData->shapeLab();
21     // remove the previous history
22     clean();
23     aShapeLab.ForgetAllAttributes();
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 boost::shared_ptr<GeomAPI_Shape> Model_ResultBody::shape()
37 {
38   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
39   if (aData) {
40     TDF_Label& aShapeLab = aData->shapeLab();
41     Handle(TNaming_NamedShape) aName;
42     if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
43       TopoDS_Shape aShape = aName->Get();
44       if (!aShape.IsNull()) {
45         boost::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
46         aRes->setImpl(new TopoDS_Shape(aShape));
47         return aRes;
48       }
49     }
50   }
51   return boost::shared_ptr<GeomAPI_Shape>();
52 }
53
54 boost::shared_ptr<ModelAPI_Feature> Model_ResultBody::owner()
55 {
56   return myOwner;
57 }
58
59 void Model_ResultBody::clean()
60 {
61   std::vector<TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
62   for(; aBuilder != myBuilders.end(); aBuilder++)
63     delete *aBuilder;
64 }
65
66 Model_ResultBody::~Model_ResultBody()
67 {
68   clean();
69 }
70
71 TNaming_Builder* Model_ResultBody::builder(const int theTag)
72 {
73   if (myBuilders.size() < (unsigned int)theTag) {
74     myBuilders.insert(myBuilders.end(), theTag - myBuilders.size() + 1, NULL);
75   }
76   if (!myBuilders[theTag]) {
77     boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
78     myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
79   }
80   return myBuilders[theTag];
81 }
82
83 void Model_ResultBody::generated(
84   const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
85 {
86   TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
87   builder(theTag)->Generated(aShape);
88 }
89
90 void Model_ResultBody::generated(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
91     const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
92 {
93   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
94   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
95   builder(theTag)->Generated(anOldShape, aNewShape);
96 }
97
98
99 void Model_ResultBody::modified(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
100     const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
101 {
102   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
103   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
104   builder(theTag)->Modify(anOldShape, aNewShape);
105 }
106
107 void Model_ResultBody::deleted(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
108     const int theTag)
109 {
110   TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
111   builder(theTag)->Delete(aShape);
112 }