Salome HOME
For the issue #1156 : do not cause Placement error on activation of previous 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 <ModelAPI_AttributeReference.h>
17 #include <Model_Document.h>
18 #include <Events_Loop.h>
19 #include <ModelAPI_Events.h>
20
21 #include <TNaming_Tool.hxx>
22 #include <TNaming_NamedShape.hxx>
23 #include <TNaming_Iterator.hxx>
24 #include <TDataStd_Name.hxx>
25 #include <TopoDS_Compound.hxx>
26 #include <BRep_Builder.hxx>
27 #include <TopExp_Explorer.hxx>
28
29 #define baseRef() std::dynamic_pointer_cast<Model_ResultPart>(data()->reference(BASE_REF_ID())->value())
30
31 void Model_ResultPart::initAttributes()
32 {
33   // append the color attribute. It is empty, the attribute will be filled by a request
34   data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId());
35   data()->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
36   data()->addAttribute(BASE_REF_ID(), ModelAPI_AttributeReference::typeId());
37 }
38
39 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
40 {
41   if (myTrsf.get()) {
42     return baseRef()->partDoc();
43   }
44   DocumentPtr aRes = data()->document(DOC_REF())->value();
45   if (!aRes.get() && myIsInLoad) { // trying to get this document from the session
46     aRes = document()->subDocument(data()->name());
47   }
48   return aRes;
49 }
50
51 Model_ResultPart::Model_ResultPart()
52 {
53   myIsInLoad = false;
54 }
55
56 void Model_ResultPart::activate()
57 {
58   if (myTrsf.get()) {
59     baseRef()->activate();
60     return;
61   }
62
63   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
64   
65   // activation may cause changes in current features in document, so it must be in transaction
66   bool isNewTransaction = false;
67   SessionPtr aMgr = ModelAPI_Session::get();
68   if (!aDocRef->value().get()) {  // create (or open) a document if it is not yet created
69     myIsInLoad = true;
70     if (!aMgr->isOperation()) {
71       aMgr->startOperation("Activation");
72       isNewTransaction = true;
73     }
74     std::shared_ptr<ModelAPI_Document> aDoc = document()->subDocument(data()->name());
75     myIsInLoad = false;
76     if (aDoc) {
77       aDoc->synchronizeTransactions();
78       aDocRef->setValue(aDoc);
79     }
80   }
81   if (aDocRef->value().get()) {
82     ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
83   }
84   if (isNewTransaction) {
85     aMgr->finishOperation();
86   }
87 }
88
89 std::shared_ptr<ModelAPI_ResultPart> Model_ResultPart::original()
90 {
91   if (myTrsf.get()) {
92     return baseRef()->original();
93   }
94   return std::dynamic_pointer_cast<ModelAPI_ResultPart>(data()->owner());
95 }
96
97 bool Model_ResultPart::isActivated() 
98 {
99   if (myTrsf.get()) {
100     if (!baseRef().get()) // may be on close
101       return false;
102     return baseRef()->isActivated();
103   }
104
105   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
106   return aDocRef->value().get() != NULL;
107 }
108
109 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
110     const bool theFlag)
111 {
112   if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
113     if (!myTrsf.get()) { // disable of base result part
114       DocumentPtr aDoc = Model_ResultPart::partDoc();
115       if (aDoc.get() && aDoc->isOpened()) {
116         std::shared_ptr<Model_Document> anIntDoc = std::dynamic_pointer_cast<Model_Document>(aDoc);
117         // make the current feature the last in any case: to update shapes defore deactivation too
118         FeaturePtr aLastFeature = anIntDoc->lastFeature();
119         aDoc->setCurrentFeature(aLastFeature, false);
120         if (theFlag) { // disable, so make all features disabled too
121           // update the shape just before the deactivation: it will be used outside of part
122           updateShape();
123           shape();
124           aDoc->setCurrentFeature(FeaturePtr(), false);
125           // in order to update OB sub-elements of document before the document closing
126           Events_Loop* aLoop = Events_Loop::loop();
127           aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
128           aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
129           aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
130         }
131       }
132     }
133     return true;
134   }
135   return false;
136 }
137
138 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
139 {
140   std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
141   if (myTrsf.get()) { // get shape of the base result and apply the transformation
142     ResultPtr anOrigResult = baseRef();
143     std::shared_ptr<GeomAPI_Shape> anOrigShape = anOrigResult->shape();
144     if (anOrigShape.get()) {
145       TopoDS_Shape aShape = anOrigShape->impl<TopoDS_Shape>();
146       if (!aShape.IsNull()) {
147         aShape.Move(*(myTrsf.get()));
148         aResult->setImpl(new TopoDS_Shape(aShape));
149       }
150     }
151     return aResult;
152   }
153   if (myShape.IsNull()) { // shape is not produced yet, create it
154     DocumentPtr aDoc = Model_ResultPart::partDoc();
155     if (aDoc.get() && aDoc->isOpened()) {
156       const std::string& aBodyGroup = ModelAPI_ResultBody::group();
157       TopoDS_Compound aResultComp;
158       BRep_Builder aBuilder;
159       aBuilder.MakeCompound(aResultComp);
160       int aNumSubs = 0;
161       for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
162         ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
163         // "object" method filters out disabled and concealed anyway, so don't check
164         if (aBody.get() && aBody->shape().get()) {
165           TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
166           if (!aShape.IsNull()) {
167             aBuilder.Add(aResultComp, aShape);
168             aNumSubs++;
169           }
170         }
171       }
172       if (aNumSubs) {
173         myShape = aResultComp;
174       }
175     }
176   }
177   if (!myShape.IsNull())
178     aResult->setImpl(new TopoDS_Shape(myShape));
179   return aResult;
180 }
181
182 // Returns true is transformation matrices are equal
183 static bool IsEqualTrsf(gp_Trsf& theT1, gp_Trsf theT2) {
184   for(int aRow = 1; aRow < 4; aRow++) {
185     for(int aCol = 1; aCol < 5; aCol++) {
186       double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
187       if (Abs(aDiff) > 1.e-9)
188         return false;
189     }
190   }
191   return true;
192 }
193
194 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
195   int& theIndex)
196 {
197   theIndex = 0; // not initialized
198
199   if (myTrsf.get()) { // if this is moved copy of part => return the name of original shape
200     ResultPartPtr anOrigResult = baseRef();
201     // searching in the origin the shape equal to the given but with myTrsf
202     TopoDS_Shape aSelection = theShape->impl<TopoDS_Shape>();
203     gp_Trsf aSelTrsf = aSelection.Location().Transformation();
204     TopoDS_Shape anOrigMain = anOrigResult->shape()->impl<TopoDS_Shape>();
205     if (!aSelection.IsNull() && !anOrigMain.IsNull()) {
206       TopExp_Explorer anExp(anOrigMain, aSelection.ShapeType());
207       for(; anExp.More(); anExp.Next()) {
208         if (anExp.Current().IsPartner(aSelection)) {
209           TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()));
210           //if (anOrigMoved.IsSame(aSelection)) {
211           if (IsEqualTrsf(aSelTrsf, anOrigMoved.Location().Transformation())) {
212             std::shared_ptr<GeomAPI_Shape> anOrigSel(new GeomAPI_Shape);
213             anOrigSel->setImpl(new TopoDS_Shape(anExp.Current()));
214             return anOrigResult->nameInPart(anOrigSel, theIndex);
215           }
216         }
217       }
218     }
219     // something is not right
220     return "";
221   }
222
223   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
224   if (aShape.IsNull())
225     return "";
226
227   // getting an access to the document of part
228   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
229   if (!aDoc.get()) // the part document is not presented for the moment
230     return "";
231   TDF_Label anAccessLabel = aDoc->generalLabel();
232   // make the selection attribute anyway: otherwise just by name it is not stable to search the result
233   std::string aName;
234   // for this the context result is needed
235   ResultPtr aContext;
236   const std::string& aBodyGroup = ModelAPI_ResultBody::group();
237   for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
238     ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
239     if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
240       TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
241       // check is body contain the selected sub-shape
242       for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
243         if (aShape.IsEqual(anExp.Current())) {
244           aContext = aBody;
245           break;
246         }
247       }
248     }
249   }
250   if (aContext.get()) {
251     AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
252     aSelAttr->append(aContext, theShape);
253     theIndex = aSelAttr->size();
254     AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
255     return aNewAttr->namingName();
256   }
257   return aName;
258 }
259
260 bool Model_ResultPart::updateInPart(const int theIndex)
261 {
262   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
263   if (aDoc.get()) {
264     AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
265     AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
266     if (aThisAttr.get()) {
267       return aThisAttr->update();
268     }
269   }
270   return false; // something is wrong
271 }
272
273 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(
274   const std::string& theName, const std::string& theType, int& theIndex)
275 {
276   theIndex = 0; // not found yet
277   std::shared_ptr<GeomAPI_Shape> aResult;
278   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
279   if (!aDoc.get()) // the part document is not presented for the moment
280     return aResult;
281
282   AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
283   // check this selection is already there: reuse it
284   int aSize = aSelAttr->size();
285   for(int a = 0; a < aSize; a++) {
286     if (aSelAttr->value(a)->namingName() == theName) {
287       theIndex = a;
288       return aSelAttr->value(a)->value();
289     }
290   }
291
292   aSelAttr->append(theName, theType);
293   theIndex = aSelAttr->size();
294   aResult = aSelAttr->value(theIndex - 1)->value();
295   return aResult;
296 }
297
298 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::selectionValue(const int theIndex)
299 {
300   std::shared_ptr<GeomAPI_Shape> aResult;
301   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
302   if (!aDoc.get()) // the part document is not presented for the moment
303     return aResult;
304
305   AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
306   aResult = aSelAttr->value(theIndex - 1)->value();
307   return aResult;
308 }
309
310 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
311   std::string& theDefault)
312 {
313   theSection = "Visualization";
314   theName = "result_part_color";
315   theDefault = DEFAULT_COLOR();
316 }
317
318 void Model_ResultPart::updateShape()
319 {
320   myShape.Nullify();
321   myTrsf.reset();
322 }
323
324 void Model_ResultPart::setTrsf(std::shared_ptr<ModelAPI_Result> theThis, 
325     const std::shared_ptr<GeomAPI_Trsf>& theTransformation)
326 {
327   updateShape();
328   if (theTransformation.get()) {
329     myTrsf = std::shared_ptr<gp_Trsf>(new gp_Trsf(theTransformation->impl<gp_Trsf>()));
330   }
331   // the result must be explicitly updated
332   static Events_Loop* aLoop = Events_Loop::loop();
333   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
334   ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update
335 }