Salome HOME
Issue #705 Color of part change color of body
[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 #include <ModelAPI_AttributeIntArray.h>
14 #include <Model_Document.h>
15
16 #include <TNaming_Tool.hxx>
17 #include <TNaming_NamedShape.hxx>
18 #include <TNaming_Iterator.hxx>
19 #include <TDataStd_Name.hxx>
20 #include <TopoDS_Compound.hxx>
21 #include <BRep_Builder.hxx>
22
23 void Model_ResultPart::initAttributes()
24 {
25   // append the color attribute. It is empty, the attribute will be filled by a request
26   DataPtr aData = data();
27   aData->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
28 }
29
30 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
31 {
32   return data()->document("PartDocument")->value();
33 }
34
35 std::shared_ptr<ModelAPI_Feature> Model_ResultPart::owner()
36 {
37   return std::shared_ptr<ModelAPI_Feature>();  // return empty pointer
38 }
39
40 Model_ResultPart::Model_ResultPart()
41 {
42   myIsDisabled = true; // by default it is not initialized and false to be after created
43   setIsConcealed(false);
44 }
45
46 void Model_ResultPart::setData(std::shared_ptr<ModelAPI_Data> theData)
47 {
48   ModelAPI_Result::setData(theData);
49   if (theData) {
50     data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId());
51   }
52 }
53
54 void Model_ResultPart::activate()
55 {
56   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
57   
58   if (!aDocRef->value().get()) {  // create (or open) a document if it is not yet created
59     std::shared_ptr<ModelAPI_Document> aDoc = document()->subDocument(data()->name());
60     if (aDoc) {
61       aDocRef->setValue(aDoc);
62     }
63   }
64   if (aDocRef->value().get()) {
65     SessionPtr aMgr = ModelAPI_Session::get();
66     bool isNewTransaction = !aMgr->isOperation();
67     // activation may cause changes in current features in document, so it must be in transaction
68     if (isNewTransaction) {
69       aMgr->startOperation("Activation");
70     }
71     ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
72     if (isNewTransaction) {
73       aMgr->finishOperation();
74     }
75   }
76 }
77
78 bool Model_ResultPart::isActivated() 
79 {
80   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
81   return aDocRef->value().get();
82 }
83
84 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
85     const bool theFlag)
86 {
87   if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
88     DocumentPtr aDoc = Model_ResultPart::partDoc();
89     if (aDoc.get() && aDoc->isOpened()) {
90       // make the current feature the last in any case: to update shapes defore deactivation too
91       FeaturePtr aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
92         ModelAPI_Feature::group(), aDoc->size(ModelAPI_Feature::group()) - 1));
93       aDoc->setCurrentFeature(aLastFeature, false);
94       if (theFlag) { // disable, so make all features disabled too
95         // update the shape just before the deactivation: it will be used outside of part
96         myShape.Nullify();
97         shape();
98         aDoc->setCurrentFeature(FeaturePtr(), false);
99       }
100     }
101     return true;
102   }
103   return false;
104 }
105
106 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
107 {
108   if (myShape.IsNull()) {
109     DocumentPtr aDoc = Model_ResultPart::partDoc();
110     if (aDoc.get()) {
111       const std::string& aBodyGroup = ModelAPI_ResultBody::group();
112       TopoDS_Compound aResultComp;
113       BRep_Builder aBuilder;
114       aBuilder.MakeCompound(aResultComp);
115       int aNumSubs = 0;
116       for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
117         ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
118         if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
119           TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
120           if (!aShape.IsNull()) {
121             aBuilder.Add(aResultComp, aShape);
122             aNumSubs++;
123           }
124         }
125       }
126       if (aNumSubs) {
127         myShape = aResultComp;
128       }
129     }
130   }
131   if (myShape.IsNull())
132     return std::shared_ptr<GeomAPI_Shape>();
133   std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
134   aResult->setImpl(new TopoDS_Shape(myShape));
135   return aResult;
136 }
137
138 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape)
139 {
140   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
141   // getting an access to the document of part
142   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
143   if (!aDoc.get()) // the part document is not presented for the moment
144     return "";
145   TDF_Label anAccessLabel = aDoc->generalLabel();
146
147   std::string aName;
148   // check if the subShape is already in DF
149   Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, anAccessLabel);
150   Handle(TDataStd_Name) anAttr;
151   if(!aNS.IsNull() && !aNS->IsEmpty()) { // in the document    
152     if(aNS->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
153       aName = TCollection_AsciiString(anAttr->Get()).ToCString();
154       if(!aName.empty()) {          
155         const TDF_Label& aLabel = aDoc->findNamingName(aName);
156
157         static const std::string aPostFix("_");
158         TNaming_Iterator anItL(aNS);
159         for(int i = 1; anItL.More(); anItL.Next(), i++) {
160           if(anItL.NewShape() == aShape) {
161             aName += aPostFix;
162             aName += TCollection_AsciiString (i).ToCString();
163             break;
164           }
165         }
166       } 
167     }
168   }
169   return aName;
170 }
171
172 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(const std::string& theName)
173 {
174   /// TODO: not implemented yet
175   return std::shared_ptr<GeomAPI_Shape>();
176 }
177
178
179 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
180   std::string& theDefault)
181 {
182   theSection = "Visualization";
183   theName = "result_part_color";
184   theDefault = DEFAULT_COLOR();
185 }
186
187 void Model_ResultPart::updateShape()
188 {
189   myShape.Nullify();
190 }