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