]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_ResultCompSolid.cpp
Salome HOME
cc3a09015ab2a8cf4b32eb5295cf0fab4b3694bd
[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_Document.h>
13
14 Model_ResultCompSolid::Model_ResultCompSolid()
15 {
16 }
17
18 Model_ResultCompSolid::~Model_ResultCompSolid()
19 {
20 }
21
22 void Model_ResultCompSolid::initAttributes()
23 {
24   data()->addAttribute(Model_ResultCompSolid::BODIES_ID(), ModelAPI_AttributeRefList::typeId());
25 }
26
27 std::shared_ptr<ModelAPI_ResultBody> Model_ResultCompSolid::addResult(const int theIndex)
28 {
29   std::shared_ptr<ModelAPI_ResultBody> aBody = document()->createBody(data(), theIndex);
30   if (aBody.get()) {
31     data()->reflist(Model_ResultCompSolid::BODIES_ID())->append(aBody);
32   }
33   return aBody;
34 }
35
36 int Model_ResultCompSolid::numberOfSubs(bool forTree) const
37 {
38   return 0;
39 }
40
41 std::shared_ptr<ModelAPI_ResultBody> Model_ResultCompSolid::subResult(const int theIndex,
42                                                                       bool forTree) const
43 {
44   if (forTree) {
45     std::shared_ptr<ModelAPI_ResultBody> aBody;
46     return aBody;
47   }
48
49   ObjectPtr anObj = data()->reflist(Model_ResultCompSolid::BODIES_ID())->object(theIndex);
50   return std::dynamic_pointer_cast<ModelAPI_ResultBody>(anObj);
51 }
52
53 /*int Model_ResultCompSolid::subResultId(const int theIndex) const
54 {
55   return subResult(theIndex)->data()->featureId();
56 }*/
57
58 bool Model_ResultCompSolid::isSub(ObjectPtr theObject) const
59 {
60   // check is this feature of result
61   ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObject);
62   /*if (!aFeature) {
63     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
64     if (aRes)
65       aFeature = document()->feature(aRes);
66   }*/
67   if (aResult) {
68     return data()->reflist(Model_ResultCompSolid::BODIES_ID())->isInList(aResult);
69   }
70   return false;
71 }
72
73 void Model_ResultCompSolid::removeResult(std::shared_ptr<ModelAPI_ResultBody> theResult)
74 {
75   if (!data()->isValid()) // sketch is already removed (case on undo of sketch), sync is not needed
76     return;
77
78   std::list<ObjectPtr> aSubs = data()->reflist(Model_ResultCompSolid::BODIES_ID())->list();
79
80   std::list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
81   bool isRemoved = false;
82   bool aHasEmtpyResult = false;
83   for(; aSubIt != aLastIt && !isRemoved; aSubIt++) {
84     std::shared_ptr<ModelAPI_ResultBody> aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSubIt);
85     if (aResult.get() != NULL && aResult == theResult) {
86       data()->reflist(Model_ResultCompSolid::BODIES_ID())->remove(aResult);
87       isRemoved = true;
88     }
89     else if (aResult.get() == NULL)
90       aHasEmtpyResult = true;
91   }
92   // if the object is not found in the sketch sub-elements, that means that the object is removed already.
93   // Find the first empty element and remove it
94   if (!isRemoved && aHasEmtpyResult)
95     data()->reflist(Model_ResultCompSolid::BODIES_ID())->remove(ObjectPtr());
96 }