]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_ResultPart.cpp
Salome HOME
Parts results and activation/deactivation management (show/hide also)
[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       // make the current feature the last in any case: to update shapes defore deactivation too
78       FeaturePtr aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
79         ModelAPI_Feature::group(), aDoc->size(ModelAPI_Feature::group()) - 1));
80       aDoc->setCurrentFeature(aLastFeature, false);
81       if (theFlag) { // disable, so make all features disabled too
82         // update the shape just before the deactivation: it will be used outside of part
83         myShape.Nullify();
84         shape();
85         aDoc->setCurrentFeature(FeaturePtr(), false);
86       }
87     }
88     return true;
89   }
90   return false;
91 }
92
93 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
94 {
95   if (myShape.IsNull()) {
96     DocumentPtr aDoc = Model_ResultPart::partDoc();
97     if (aDoc.get()) {
98       const std::string& aBodyGroup = ModelAPI_ResultBody::group();
99       TopoDS_Compound aResultComp;
100       BRep_Builder aBuilder;
101       aBuilder.MakeCompound(aResultComp);
102       int aNumSubs = 0;
103       for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
104         ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
105         if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
106           TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
107           if (!aShape.IsNull()) {
108             aBuilder.Add(aResultComp, aShape);
109             aNumSubs++;
110           }
111         }
112       }
113       if (aNumSubs) {
114         myShape = aResultComp;
115       }
116     }
117   }
118   if (myShape.IsNull())
119     return std::shared_ptr<GeomAPI_Shape>();
120   std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
121   aResult->setImpl(new TopoDS_Shape(myShape));
122   return aResult;
123 }
124
125 #include <Model_Document.h>
126 #include <TNaming_Tool.hxx>
127 #include <TNaming_NamedShape.hxx>
128 #include <TNaming_Iterator.hxx>
129 #include <TDataStd_Name.hxx>
130
131 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape)
132 {
133   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
134   // getting an access to the document of part
135   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
136   if (!aDoc.get()) // the part document is not presented for the moment
137     return "";
138   TDF_Label anAccessLabel = aDoc->generalLabel();
139
140   std::string aName;
141   // check if the subShape is already in DF
142   Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, anAccessLabel);
143   Handle(TDataStd_Name) anAttr;
144   if(!aNS.IsNull() && !aNS->IsEmpty()) { // in the document    
145     if(aNS->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
146       aName = TCollection_AsciiString(anAttr->Get()).ToCString();
147       if(!aName.empty()) {          
148         const TDF_Label& aLabel = aDoc->findNamingName(aName);
149
150         static const std::string aPostFix("_");
151         TNaming_Iterator anItL(aNS);
152         for(int i = 1; anItL.More(); anItL.Next(), i++) {
153           if(anItL.NewShape() == aShape) {
154             aName += aPostFix;
155             aName += TCollection_AsciiString (i).ToCString();
156             break;
157           }
158         }
159       } 
160     }
161   }
162   return aName;
163 }
164
165 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(const std::string& theName)
166 {
167   /// TODO: not implemented yet
168   return std::shared_ptr<GeomAPI_Shape>();
169 }
170
171
172 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
173   std::string& theDefault)
174 {
175   theSection = "Visualization";
176   theName = "result_part_color";
177   theDefault = DEFAULT_COLOR();
178 }