Salome HOME
Issue #2615 : EDF 2018-3 Sub-results states
[modules/shaper.git] / src / Model / Model_ResultPart.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <Model_ResultPart.h>
22 #include <ModelAPI_Data.h>
23 #include <Model_Data.h>
24 #include <ModelAPI_AttributeDocRef.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_ResultBody.h>
28 #include <ModelAPI_AttributeIntArray.h>
29 #include <ModelAPI_AttributeSelectionList.h>
30 #include <ModelAPI_AttributeReference.h>
31 #include <ModelAPI_AttributeDouble.h>
32 #include <Model_Document.h>
33 #include <Model_Application.h>
34 #include <Events_Loop.h>
35 #include <ModelAPI_Events.h>
36
37 #include <GeomAPI_Trsf.h>
38
39 #include <TNaming_Tool.hxx>
40 #include <TNaming_NamedShape.hxx>
41 #include <TDataStd_Name.hxx>
42 #include <TopoDS_Compound.hxx>
43 #include <BRep_Builder.hxx>
44 #include <TopExp_Explorer.hxx>
45
46 #define baseRef() \
47   std::dynamic_pointer_cast<Model_ResultPart>(data()->reference(BASE_REF_ID())->value())
48
49 void Model_ResultPart::initAttributes()
50 {
51   // append the color attribute. It is empty, the attribute will be filled by a request
52   AttributeDocRefPtr aDocRef = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(
53     data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId()));
54   data()->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
55   data()->addAttribute(BASE_REF_ID(), ModelAPI_AttributeReference::typeId());
56   data()->addAttribute(DEFLECTION_ID(), ModelAPI_AttributeDouble::typeId());
57   data()->addAttribute(TRANSPARENCY_ID(), ModelAPI_AttributeDouble::typeId());
58
59   if (aDocRef->isInitialized() && // initialized immideately means already exist and will be loaded
60       !Model_Application::getApplication()->hasDocument(aDocRef->docId()))
61     Model_Application::getApplication()->setLoadByDemand(data()->name(), aDocRef->docId());
62 }
63
64 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
65 {
66   if (myTrsf.get() && baseRef().get()) { // the second condition is due to #2035
67     return baseRef()->partDoc();
68   }
69   DocumentPtr aRes = data()->document(DOC_REF())->value();
70   return aRes;
71 }
72
73 Model_ResultPart::Model_ResultPart()
74 {
75 }
76
77 void Model_ResultPart::activate()
78 {
79   if (myTrsf.get()) {
80     baseRef()->activate();
81     return;
82   }
83
84   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
85
86   // activation may cause changes in current features in document, so it must be in transaction
87   bool isNewTransaction = false;
88   SessionPtr aMgr = ModelAPI_Session::get();
89   if (!aMgr->isOperation()) {
90     // open transaction even document is not created to set current docs in setActiveDocument
91     aMgr->startOperation("Activation");
92     isNewTransaction = true;
93   }
94   if (!aDocRef->value().get()) {  // create (or open) a document if it is not yet created
95     Handle(Model_Application) anApp = Model_Application::getApplication();
96     if (anApp->isLoadByDemand(data()->name(), aDocRef->docId())) {
97       anApp->loadDocument(data()->name(), aDocRef->docId()); // if it is just new part, load fails
98     } else {
99       anApp->createDocument(aDocRef->docId());
100     }
101     std::shared_ptr<ModelAPI_Document> aDoc = aDocRef->value();
102     if (aDoc.get()) {
103       aDoc->synchronizeTransactions();
104       aDocRef->setValue(aDoc);
105     }
106   }
107   if (aDocRef->value().get()) {
108     ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
109   }
110   if (isNewTransaction) {
111     aMgr->finishOperation();
112   }
113 }
114
115 std::shared_ptr<ModelAPI_ResultPart> Model_ResultPart::original()
116 {
117   if (myTrsf.get() && baseRef().get()) {  // the second condition is due to #2035
118     return baseRef()->original();
119   }
120   return std::dynamic_pointer_cast<ModelAPI_ResultPart>(data()->owner());
121 }
122
123 bool Model_ResultPart::isActivated()
124 {
125   if (myTrsf.get()) {
126     if (!baseRef().get()) // may be on close
127       return false;
128     return baseRef()->isActivated();
129   }
130
131   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
132   return aDocRef->value().get() != NULL;
133 }
134
135 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
136     const bool theFlag)
137 {
138   if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
139     if (!myTrsf.get()) { // disable of base result part
140       DocumentPtr aDoc = Model_ResultPart::partDoc();
141       if (aDoc.get() && aDoc->isOpened()) {
142         // make the current feature the last in any case: to update shapes defore deactivation too
143         int aSize = aDoc->size(ModelAPI_Feature::group());
144         FeaturePtr aLastFeature;
145         if (aSize)
146           aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
147             ModelAPI_Feature::group(), aSize - 1));
148
149         aDoc->setCurrentFeature(aLastFeature, false);
150         if (theFlag) { // disable, so make all features disabled too
151           // update the shape just before the deactivation: it will be used outside of part
152           updateShape();
153           shape();
154           aDoc->setCurrentFeature(FeaturePtr(), false);
155           // in order to update OB sub-elements of document before the document closing
156           Events_Loop* aLoop = Events_Loop::loop();
157           aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
158           aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
159           aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
160         }
161       }
162     }
163     return true;
164   }
165   return false;
166 }
167
168 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
169 {
170   std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
171   if (myShape.IsNull()) { // shape is not produced yet, create it
172     if (myTrsf.get()) { // get shape of the base result and apply the transformation
173       ResultPtr anOrigResult = baseRef();
174       std::shared_ptr<GeomAPI_Shape> anOrigShape = anOrigResult->shape();
175       if (anOrigShape.get()) {
176         TopoDS_Shape aShape = anOrigShape->impl<TopoDS_Shape>();
177         if (!aShape.IsNull()) {
178           aShape.Move(*(myTrsf.get()));
179           myShape = aShape;
180           aResult->setImpl(new TopoDS_Shape(aShape));
181         }
182       }
183       return aResult;
184     } else {
185       DocumentPtr aDoc = Model_ResultPart::partDoc();
186       if (aDoc.get() && aDoc->isOpened()) {
187         const std::string& aBodyGroup = ModelAPI_ResultBody::group();
188         TopoDS_Compound aResultComp;
189         BRep_Builder aBuilder;
190         aBuilder.MakeCompound(aResultComp);
191         int aNumSubs = 0;
192         for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
193           ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
194           // "object" method filters out disabled and concealed anyway, so don't check
195           if (aBody.get() && aBody->data()->isValid() && aBody->shape().get()) {
196             TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
197             if (!aShape.IsNull()) {
198               aBuilder.Add(aResultComp, aShape);
199               aNumSubs++;
200             }
201           }
202         }
203         if (aNumSubs) {
204           myShape = aResultComp;
205         }
206       }
207     }
208   }
209   if (!myShape.IsNull())
210     aResult->setImpl(new TopoDS_Shape(myShape));
211   return aResult;
212 }
213
214 // Returns true is transformation matrices are equal
215 static bool IsEqualTrsf(gp_Trsf& theT1, gp_Trsf theT2) {
216   for(int aRow = 1; aRow < 4; aRow++) {
217     for(int aCol = 1; aCol < 5; aCol++) {
218       double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
219       if (Abs(aDiff) > 1.e-9)
220         return false;
221     }
222   }
223   return true;
224 }
225
226 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
227   int& theIndex)
228 {
229   theIndex = 0; // not initialized
230
231   if (myTrsf.get()) { // if this is moved copy of part => return the name of original shape
232     ResultPartPtr anOrigResult = baseRef();
233     // searching in the origin the shape equal to the given but with myTrsf
234     TopoDS_Shape aSelection = theShape->impl<TopoDS_Shape>();
235     gp_Trsf aSelTrsf = aSelection.Location().Transformation();
236     TopoDS_Shape anOrigMain = anOrigResult->shape()->impl<TopoDS_Shape>();
237     if (!aSelection.IsNull() && !anOrigMain.IsNull()) {
238       TopExp_Explorer anExp(anOrigMain, aSelection.ShapeType());
239       for(; anExp.More(); anExp.Next()) {
240         if (anExp.Current().IsPartner(aSelection)) {
241           TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()));
242           //if (anOrigMoved.IsSame(aSelection)) {
243           if (IsEqualTrsf(aSelTrsf, anOrigMoved.Location().Transformation())) {
244             std::shared_ptr<GeomAPI_Shape> anOrigSel(new GeomAPI_Shape);
245             anOrigSel->setImpl(new TopoDS_Shape(anExp.Current()));
246             return anOrigResult->nameInPart(anOrigSel, theIndex);
247           }
248         }
249       }
250     }
251     // something is not right
252     return "";
253   }
254
255   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
256   if (aShape.IsNull())
257     return "";
258
259   // getting an access to the document of part
260   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
261   if (!aDoc.get()) // the part document is not presented for the moment
262     return "";
263   TDF_Label anAccessLabel = aDoc->generalLabel();
264   // make the selection attribute anyway:
265   // otherwise just by name it is not stable to search the result
266   std::string aName;
267   // for this the context result is needed
268   ResultPtr aContext;
269   const std::string& aBodyGroup = ModelAPI_ResultBody::group();
270   for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
271     ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
272     if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
273       TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
274       // check is body contain the selected sub-shape
275       for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
276         if (aShape.IsEqual(anExp.Current())) {
277           aContext = aBody;
278           break;
279         }
280       }
281     }
282   }
283   if (aContext.get()) {
284     AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
285     aSelAttr->append(aContext, theShape);
286     theIndex = aSelAttr->size();
287     AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
288     return aNewAttr->namingName();
289   }
290   return aName;
291 }
292
293 bool Model_ResultPart::updateInPart(const int theIndex)
294 {
295   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
296   if (aDoc.get()) {
297     AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
298     AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
299     if (aThisAttr.get()) {
300       return aThisAttr->update();
301     }
302   }
303   return false; // something is wrong
304 }
305
306 gp_Trsf Model_ResultPart::sumTrsf() {
307   gp_Trsf aResult;
308   if (myTrsf) {
309     aResult = *myTrsf;
310     aResult = aResult * baseRef()->sumTrsf();
311   }
312   return aResult;
313 }
314
315 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(
316   const std::string& theName, const std::string& theType, int& theIndex)
317 {
318   theIndex = 0; // not found yet
319   std::shared_ptr<GeomAPI_Shape> aResult;
320   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
321   if (!aDoc.get()) // the part document is not presented for the moment
322     return aResult;
323
324   AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
325   // check this selection is already there: reuse it
326   int aSize = aSelAttr->size();
327   for(int a = 0; a < aSize; a++) {
328     if (aSelAttr->value(a)->namingName() == theName) {
329       theIndex = a;
330       return aSelAttr->value(a)->value();
331     }
332   }
333
334   aSelAttr->append(theName, theType);
335   theIndex = aSelAttr->size();
336   aResult = aSelAttr->value(theIndex - 1)->value();
337   if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
338     gp_Trsf aSumTrsf = sumTrsf();
339     TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
340     aResult->setImpl(new TopoDS_Shape(anOrigMoved));
341   }
342   return aResult;
343 }
344
345 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::selectionValue(const int theIndex)
346 {
347   std::shared_ptr<GeomAPI_Shape> aResult;
348   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
349   if (!aDoc.get()) // the part document is not presented for the moment
350     return aResult;
351
352   AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
353   aResult = aSelAttr->value(theIndex - 1)->value();
354   if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
355     gp_Trsf aSumTrsf = sumTrsf();
356     TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
357     aResult->setImpl(new TopoDS_Shape(anOrigMoved));
358   }
359   return aResult;
360 }
361
362 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
363   std::string& theDefault)
364 {
365   theSection = "Visualization";
366   theName = "result_part_color";
367   theDefault = DEFAULT_COLOR();
368 }
369
370 void Model_ResultPart::updateShape()
371 {
372   myShape.Nullify();
373   myTrsf.reset();
374 }
375
376 void Model_ResultPart::setTrsf(std::shared_ptr<ModelAPI_Result> theThis,
377     const std::shared_ptr<GeomAPI_Trsf>& theTransformation)
378 {
379   updateShape();
380   if (theTransformation.get()) {
381     myTrsf = std::shared_ptr<gp_Trsf>(new gp_Trsf(theTransformation->impl<gp_Trsf>()));
382   }
383   // the result must be explicitly updated
384   static Events_Loop* aLoop = Events_Loop::loop();
385   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
386   ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update
387 }