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