Salome HOME
Make the movement, placement and rotation 3D features may be applied to the Part...
[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 <Model_Data.h>
10 #include <ModelAPI_AttributeDocRef.h>
11 #include <ModelAPI_Session.h>
12 #include <ModelAPI_Feature.h>
13 #include <ModelAPI_ResultBody.h>
14 #include <ModelAPI_AttributeIntArray.h>
15 #include <ModelAPI_AttributeSelectionList.h>
16 #include <Model_Document.h>
17 #include <Events_Loop.h>
18 #include <ModelAPI_Events.h>
19
20 #include <TNaming_Tool.hxx>
21 #include <TNaming_NamedShape.hxx>
22 #include <TNaming_Iterator.hxx>
23 #include <TDataStd_Name.hxx>
24 #include <TopoDS_Compound.hxx>
25 #include <BRep_Builder.hxx>
26 #include <TopExp_Explorer.hxx>
27
28 void Model_ResultPart::initAttributes()
29 {
30   // append the color attribute. It is empty, the attribute will be filled by a request
31   DataPtr aData = data();
32   aData->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
33 }
34
35 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
36 {
37   return data()->document("PartDocument")->value();
38 }
39
40 std::shared_ptr<ModelAPI_Feature> Model_ResultPart::owner()
41 {
42   return std::shared_ptr<ModelAPI_Feature>();  // return empty pointer
43 }
44
45 Model_ResultPart::Model_ResultPart()
46 {
47   myIsDisabled = true; // by default it is not initialized and false to be after created
48   setIsConcealed(false);
49 }
50
51 void Model_ResultPart::setData(std::shared_ptr<ModelAPI_Data> theData)
52 {
53   ModelAPI_Result::setData(theData);
54   if (theData) {
55     data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId());
56   }
57 }
58
59 void Model_ResultPart::activate()
60 {
61   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
62   
63   if (!aDocRef->value().get()) {  // create (or open) a document if it is not yet created
64     std::shared_ptr<ModelAPI_Document> aDoc = document()->subDocument(data()->name());
65     if (aDoc) {
66       aDocRef->setValue(aDoc);
67     }
68   }
69   if (aDocRef->value().get()) {
70     SessionPtr aMgr = ModelAPI_Session::get();
71     bool isNewTransaction = !aMgr->isOperation();
72     // activation may cause changes in current features in document, so it must be in transaction
73     if (isNewTransaction) {
74       aMgr->startOperation("Activation");
75     }
76     ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
77     if (isNewTransaction) {
78       aMgr->finishOperation();
79     }
80   }
81 }
82
83 bool Model_ResultPart::isActivated() 
84 {
85   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
86   return aDocRef->value().get();
87 }
88
89 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
90     const bool theFlag)
91 {
92   if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
93     DocumentPtr aDoc = Model_ResultPart::partDoc();
94     if (aDoc.get() && aDoc->isOpened()) {
95       // make the current feature the last in any case: to update shapes defore deactivation too
96       FeaturePtr aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
97         ModelAPI_Feature::group(), aDoc->size(ModelAPI_Feature::group()) - 1));
98       aDoc->setCurrentFeature(aLastFeature, false);
99       if (theFlag) { // disable, so make all features disabled too
100         // update the shape just before the deactivation: it will be used outside of part
101         myShape.Nullify();
102         shape();
103         aDoc->setCurrentFeature(FeaturePtr(), false);
104       }
105     }
106     return true;
107   }
108   return false;
109 }
110
111 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
112 {
113   if (myShape.IsNull()) {
114     DocumentPtr aDoc = Model_ResultPart::partDoc();
115     if (aDoc.get()) {
116       const std::string& aBodyGroup = ModelAPI_ResultBody::group();
117       TopoDS_Compound aResultComp;
118       BRep_Builder aBuilder;
119       aBuilder.MakeCompound(aResultComp);
120       int aNumSubs = 0;
121       for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
122         ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
123         if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
124           TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
125           if (!aShape.IsNull()) {
126             aBuilder.Add(aResultComp, aShape);
127             aNumSubs++;
128           }
129         }
130       }
131       if (aNumSubs) {
132         myShape = aResultComp;
133       }
134     }
135   }
136   if (myShape.IsNull())
137     return std::shared_ptr<GeomAPI_Shape>();
138   std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
139   aResult->setImpl(new TopoDS_Shape(myShape));
140   return aResult;
141 }
142
143 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
144   int& theIndex)
145 {
146   theIndex = 0; // not initialized
147   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
148   if (aShape.IsNull())
149     return "";
150   if (data()->isOwner(this)) { // if this is moved copy of part => return the name of original shape
151     FeaturePtr anOrigFeature = 
152       std::dynamic_pointer_cast<ModelAPI_Feature>(data()->attribute(COLOR_ID())->owner());
153     if (anOrigFeature.get()) {
154       if (anOrigFeature->firstResult().get() && anOrigFeature->firstResult()->shape().get()) {
155         TopoDS_Shape anOrigShape = anOrigFeature->firstResult()->shape()->impl<TopoDS_Shape>();
156         if (!anOrigShape.IsNull()) {
157           TopExp_Explorer anExp(anOrigShape, aShape.ShapeType());
158           for(; anExp.More(); anExp.Next()) {
159             if (aShape.IsPartner(anExp.Current())) {
160               std::shared_ptr<GeomAPI_Shape> anOrigGeomShape(new GeomAPI_Shape);
161               anOrigGeomShape->setImpl(new TopoDS_Shape(anExp.Current()));
162
163               return std::dynamic_pointer_cast<Model_ResultPart>(anOrigFeature->firstResult())->
164                 nameInPart(theShape, theIndex);
165             }
166           }
167         }
168       }
169     }
170   }
171   // getting an access to the document of part
172   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
173   if (!aDoc.get()) // the part document is not presented for the moment
174     return "";
175   TDF_Label anAccessLabel = aDoc->generalLabel();
176
177   std::string aName;
178   // check if the subShape is already in DF
179   Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, anAccessLabel);
180   Handle(TDataStd_Name) anAttr;
181   if(!aNS.IsNull() && !aNS->IsEmpty()) { // in the document    
182     if(aNS->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
183       aName = TCollection_AsciiString(anAttr->Get()).ToCString();
184       if(!aName.empty()) {          
185         const TDF_Label& aLabel = aDoc->findNamingName(aName);
186
187         static const std::string aPostFix("_");
188         TNaming_Iterator anItL(aNS);
189         for(int i = 1; anItL.More(); anItL.Next(), i++) {
190           if(anItL.NewShape() == aShape) {
191             aName += aPostFix;
192             aName += TCollection_AsciiString (i).ToCString();
193             break;
194           }
195         }
196       } 
197     }
198   }
199   if (aName.empty()) { // not found, so use the selection mechanism
200     // for this the context result is needed
201     ResultPtr aContext;
202     const std::string& aBodyGroup = ModelAPI_ResultBody::group();
203     for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
204       ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
205       if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
206         TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
207         // check is body contain the selected sub-shape
208         for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
209           if (aShape.IsEqual(anExp.Current())) {
210             aContext = aBody;
211             break;
212           }
213         }
214       }
215     }
216     if (aContext.get()) {
217       AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
218       aSelAttr->append(aContext, theShape);
219       theIndex = aSelAttr->size();
220       AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
221       return aNewAttr->namingName();
222     }
223   }
224   return aName;
225 }
226
227 bool Model_ResultPart::updateInPart(const int theIndex)
228 {
229   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
230   if (aDoc.get()) {
231     AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
232     AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
233     if (aThisAttr.get()) {
234       return aThisAttr->update();
235     }
236   }
237   return false; // something is wrong
238 }
239
240 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(const std::string& theName)
241 {
242   /// TODO: not implemented yet
243   return std::shared_ptr<GeomAPI_Shape>();
244 }
245
246
247 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
248   std::string& theDefault)
249 {
250   theSection = "Visualization";
251   theName = "result_part_color";
252   theDefault = DEFAULT_COLOR();
253 }
254
255 void Model_ResultPart::updateShape()
256 {
257   myShape.Nullify();
258 }
259
260 void Model_ResultPart::setShape(std::shared_ptr<ModelAPI_Result> theThis, 
261     const std::shared_ptr<GeomAPI_Shape>& theTransformed)
262 {
263   myShape = theTransformed->impl<TopoDS_Shape>();
264   // the result must be explicitly updated
265   static Events_Loop* aLoop = Events_Loop::loop();
266   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
267   ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update
268 }