Salome HOME
Define color for part result
[modules/shaper.git] / src / Model / Model_ResultPart.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_ResultPart.cpp
4 // Created:     07 Jul 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include <Model_ResultPart.h>
8 #include <ModelAPI_Data.h>
9 #include <ModelAPI_AttributeDocRef.h>
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_Feature.h>
12 #include <ModelAPI_ResultBody.h>
13
14 #include <TopoDS_Compound.hxx>
15 #include <BRep_Builder.hxx>
16
17 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
18 {
19   return data()->document("PartDocument")->value();
20 }
21
22 std::shared_ptr<ModelAPI_Feature> Model_ResultPart::owner()
23 {
24   return std::shared_ptr<ModelAPI_Feature>();  // return empty pointer
25 }
26
27 Model_ResultPart::Model_ResultPart()
28 {
29   myIsDisabled = true; // by default it is not initialized and false to be after created
30   setIsConcealed(false);
31 }
32
33 void Model_ResultPart::setData(std::shared_ptr<ModelAPI_Data> theData)
34 {
35   ModelAPI_Result::setData(theData);
36   if (theData) {
37     data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId());
38   }
39 }
40
41 void Model_ResultPart::activate()
42 {
43   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
44   
45   if (!aDocRef->value().get()) {  // create (or open) a document if it is not yet created
46     std::shared_ptr<ModelAPI_Document> aDoc = document()->subDocument(data()->name());
47     if (aDoc) {
48       aDocRef->setValue(aDoc);
49     }
50   }
51   if (aDocRef->value().get()) {
52     SessionPtr aMgr = ModelAPI_Session::get();
53     bool isNewTransaction = !aMgr->isOperation();
54     // activation may cause changes in current features in document, so it must be in transaction
55     if (isNewTransaction) {
56       aMgr->startOperation("Activation");
57     }
58     ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
59     if (isNewTransaction) {
60       aMgr->finishOperation();
61     }
62   }
63 }
64
65 bool Model_ResultPart::isActivated() 
66 {
67   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
68   return aDocRef->value().get();
69 }
70
71 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
72     const bool theFlag)
73 {
74   if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
75     DocumentPtr aDoc = Model_ResultPart::partDoc();
76     if (aDoc.get()) {
77       if (theFlag) { // disable, so make all features disabled too
78         aDoc->setCurrentFeature(FeaturePtr(), false);
79       } else { // enabled, so make the current feature the last inside of this document
80         FeaturePtr aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
81           ModelAPI_Feature::group(), aDoc->size(ModelAPI_Feature::group()) - 1));
82         aDoc->setCurrentFeature(aLastFeature, false);
83       }
84     }
85     return true;
86   }
87   return false;
88 }
89
90 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
91 {
92   DocumentPtr aDoc = Model_ResultPart::partDoc();
93   if (aDoc.get()) {
94     const std::string& aBodyGroup = ModelAPI_ResultBody::group();
95     TopoDS_Compound aResultComp;
96     BRep_Builder aBuilder;
97     aBuilder.MakeCompound(aResultComp);
98     int aNumSubs = 0;
99     for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
100       ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
101       if (aBody.get() && aBody->shape().get()) {
102         TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
103         if (!aShape.IsNull()) {
104           aBuilder.Add(aResultComp, aShape);
105           aNumSubs++;
106         }
107       }
108     }
109     if (aNumSubs) {
110       std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
111       aResult->setImpl(new TopoDS_Shape(aResultComp));
112       return aResult;
113     }
114   }
115   return std::shared_ptr<GeomAPI_Shape>();
116 }
117
118
119 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
120   std::string& theDefault)
121 {
122   theSection = "Visualization";
123   theName = "result_part_color";
124   theDefault = DEFAULT_COLOR();
125 }