]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_ResultCompSolid.cpp
Salome HOME
Merge tag 'V_1.3.1' into HEAD
[modules/shaper.git] / src / Model / Model_ResultCompSolid.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_ResultCompSolid.cpp
4 // Created:     20 Jul 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include <Model_ResultCompSolid.h>
8
9 #include <ModelAPI_AttributeRefList.h>
10 #include <ModelAPI_Object.h>
11
12 #include <Model_BodyBuilder.h>
13
14 #include <Model_Document.h>
15
16 Model_ResultCompSolid::Model_ResultCompSolid()
17 {
18   myBuilder = new Model_BodyBuilder(this);
19 }
20
21 Model_ResultCompSolid::~Model_ResultCompSolid()
22 {
23 }
24
25 void Model_ResultCompSolid::initAttributes()
26 {
27   data()->addAttribute(Model_ResultCompSolid::BODIES_ID(), ModelAPI_AttributeRefList::typeId());
28 }
29
30 std::shared_ptr<ModelAPI_ResultBody> Model_ResultCompSolid::addResult(const int theIndex)
31 {
32   std::shared_ptr<ModelAPI_ResultBody> aBody = document()->createBody(data(), theIndex);
33   if (aBody.get()) {
34     data()->reflist(Model_ResultCompSolid::BODIES_ID())->append(aBody);
35   }
36   return aBody;
37 }
38
39 int Model_ResultCompSolid::numberOfSubs(bool forTree) const
40 {
41   if (forTree)
42     return 0;
43   return data()->reflist(Model_ResultCompSolid::BODIES_ID())->size();
44 }
45
46 std::shared_ptr<ModelAPI_ResultBody> Model_ResultCompSolid::subResult(const int theIndex,
47                                                                       bool forTree) const
48 {
49   if (forTree) {
50     std::shared_ptr<ModelAPI_ResultBody> aBody;
51     return aBody;
52   }
53
54   ObjectPtr anObj = data()->reflist(Model_ResultCompSolid::BODIES_ID())->object(theIndex);
55   return std::dynamic_pointer_cast<ModelAPI_ResultBody>(anObj);
56 }
57
58 /*int Model_ResultCompSolid::subResultId(const int theIndex) const
59 {
60   return subResult(theIndex)->data()->featureId();
61 }*/
62
63 bool Model_ResultCompSolid::isSub(ObjectPtr theObject) const
64 {
65   // check is this feature of result
66   ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObject);
67   /*if (!aFeature) {
68     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
69     if (aRes)
70       aFeature = document()->feature(aRes);
71   }*/
72   if (aResult) {
73     return data()->reflist(Model_ResultCompSolid::BODIES_ID())->isInList(aResult);
74   }
75   return false;
76 }
77
78 void Model_ResultCompSolid::removeResult(std::shared_ptr<ModelAPI_ResultBody> theResult)
79 {
80   if (!data()->isValid()) // sketch is already removed (case on undo of sketch), sync is not needed
81     return;
82
83   std::list<ObjectPtr> aSubs = data()->reflist(Model_ResultCompSolid::BODIES_ID())->list();
84
85   std::list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
86   bool isRemoved = false;
87   bool aHasEmtpyResult = false;
88   for(; aSubIt != aLastIt && !isRemoved; aSubIt++) {
89     std::shared_ptr<ModelAPI_ResultBody> aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSubIt);
90     if (aResult.get() != NULL && aResult == theResult) {
91       data()->reflist(Model_ResultCompSolid::BODIES_ID())->remove(aResult);
92       isRemoved = true;
93     }
94     else if (aResult.get() == NULL)
95       aHasEmtpyResult = true;
96   }
97   // if the object is not found in the sketch sub-elements, that means that the object is removed already.
98   // Find the first empty element and remove it
99   if (!isRemoved && aHasEmtpyResult)
100     data()->reflist(Model_ResultCompSolid::BODIES_ID())->remove(ObjectPtr());
101 }