Salome HOME
Issue #19890: Shape displayed with some transparency after symmetry
[modules/shaper.git] / src / Model / Model_ResultPart.cpp
1 // Copyright (C) 2014-2020  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <Model_ResultPart.h>
21 #include <ModelAPI_Data.h>
22 #include <Model_Data.h>
23 #include <ModelAPI_AttributeDocRef.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Feature.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_AttributeIntArray.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_AttributeReference.h>
30 #include <ModelAPI_AttributeDouble.h>
31 #include <Model_Document.h>
32 #include <Model_Application.h>
33 #include <Events_Loop.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_Tools.h>
36
37 #include <GeomAPI_Trsf.h>
38
39 #include <GeomAlgoAPI_Transform.h>
40
41 #include <Locale_Convert.h>
42
43 #include <TNaming_Tool.hxx>
44 #include <TNaming_NamedShape.hxx>
45 #include <TDataStd_Name.hxx>
46 #include <TopoDS_Compound.hxx>
47 #include <BRep_Builder.hxx>
48 #include <TopExp_Explorer.hxx>
49
50 #define baseRef() \
51   std::dynamic_pointer_cast<Model_ResultPart>(data()->reference(BASE_REF_ID())->value())
52
53 void Model_ResultPart::initAttributes()
54 {
55   // append the color attribute. It is empty, the attribute will be filled by a request
56   AttributeDocRefPtr aDocRef = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(
57     data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId()));
58   data()->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
59   data()->addAttribute(BASE_REF_ID(), ModelAPI_AttributeReference::typeId());
60   data()->addAttribute(DEFLECTION_ID(), ModelAPI_AttributeDouble::typeId());
61   data()->addAttribute(TRANSPARENCY_ID(), ModelAPI_AttributeDouble::typeId());
62   data()->addAttribute(ISO_LINES_ID(), ModelAPI_AttributeIntArray::typeId());
63
64   if (aDocRef->isInitialized() && // initialized immediately means already exist and will be loaded
65       !Model_Application::getApplication()->hasDocument(aDocRef->docId()))
66     Model_Application::getApplication()->setLoadByDemand(data()->name(), aDocRef->docId());
67 }
68
69 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
70 {
71   if (myTrsf.get() && baseRef().get()) { // the second condition is due to #2035
72     return baseRef()->partDoc();
73   }
74   if (!data()->isValid())
75     return DocumentPtr();
76   DocumentPtr aRes = data()->document(DOC_REF())->value();
77   return aRes;
78 }
79
80 Model_ResultPart::Model_ResultPart()
81 {
82 }
83
84 void Model_ResultPart::activate()
85 {
86   if (myTrsf.get()) {
87     baseRef()->activate();
88     return;
89   }
90
91   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
92
93   // activation may cause changes in current features in document, so it must be in transaction
94   bool isNewTransaction = false;
95   SessionPtr aMgr = ModelAPI_Session::get();
96   if (!aMgr->isOperation()) {
97     // open transaction even document is not created to set current docs in setActiveDocument
98     std::string aMsg = "Activation " + Locale::Convert::toString(data()->name());
99     aMgr->startOperation(aMsg);
100     isNewTransaction = true;
101   }
102   if (!aDocRef->value().get()) {  // create (or open) a document if it is not yet created
103     Handle(Model_Application) anApp = Model_Application::getApplication();
104     if (anApp->isLoadByDemand(data()->name(), aDocRef->docId())) {
105       anApp->loadDocument(data()->name(), aDocRef->docId()); // if it is just new part, load fails
106     } else {
107       anApp->createDocument(aDocRef->docId());
108     }
109     std::shared_ptr<ModelAPI_Document> aDoc = aDocRef->value();
110     if (aDoc.get()) {
111       aDoc->synchronizeTransactions();
112       aDocRef->setValue(aDoc);
113     }
114   }
115   if (aDocRef->value().get()) {
116     ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
117   }
118   if (isNewTransaction) {
119     aMgr->finishOperation();
120   }
121 }
122
123
124 void Model_ResultPart::loadPart()
125 {
126   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
127   if (!aDocRef->value().get()) {  // create (or open) a document if it is not yet created
128     Handle(Model_Application) anApp = Model_Application::getApplication();
129     if (anApp->isLoadByDemand(data()->name(), aDocRef->docId())) {
130       anApp->loadDocument(data()->name(), aDocRef->docId()); // if it is just new part, load fails
131     }
132     else {
133       anApp->createDocument(aDocRef->docId());
134     }
135   }
136 }
137
138
139 std::shared_ptr<ModelAPI_ResultPart> Model_ResultPart::original()
140 {
141   if (myTrsf.get() && baseRef().get()) {  // the second condition is due to #2035
142     return baseRef()->original();
143   }
144   return std::dynamic_pointer_cast<ModelAPI_ResultPart>(data()->owner());
145 }
146
147 bool Model_ResultPart::isActivated()
148 {
149   if (myTrsf.get()) {
150     if (!baseRef().get()) // may be on close
151       return false;
152     return baseRef()->isActivated();
153   }
154
155   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
156   return aDocRef->value().get() != NULL;
157 }
158
159 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
160     const bool theFlag)
161 {
162   if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
163     if (!myTrsf.get()) { // disable of base result part
164       DocumentPtr aDoc = Model_ResultPart::partDoc();
165       if (aDoc.get() && aDoc->isOpened()) {
166         // make the current feature the last in any case: to update shapes before deactivation too
167         int aSize = aDoc->size(ModelAPI_Feature::group());
168         FeaturePtr aLastFeature;
169         if (aSize)
170           aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
171             ModelAPI_Feature::group(), aSize - 1));
172
173         aDoc->setCurrentFeature(aLastFeature, false);
174         if (theFlag) { // disable, so make all features disabled too
175           // update the shape just before the deactivation: it will be used outside of part
176           updateShape();
177           shape();
178           aDoc->setCurrentFeature(FeaturePtr(), false);
179           // in order to update OB sub-elements of document before the document closing
180           Events_Loop* aLoop = Events_Loop::loop();
181           aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
182           aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
183           aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
184         }
185       }
186     }
187     return true;
188   }
189   return false;
190 }
191
192 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
193 {
194   std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
195   if (myShape.IsNull()) { // shape is not produced yet, create it
196     SessionPtr aMgr = ModelAPI_Session::get();
197     bool aToSendUpdate = aMgr->isOperation(); // inside of operation may send an update event
198     if (myTrsf.get()) { // get shape of the base result and apply the transformation
199       ResultPtr anOrigResult = baseRef();
200       std::shared_ptr<GeomAPI_Shape> anOrigShape = anOrigResult->shape();
201       if (anOrigShape.get()) {
202         GeomTrsfPtr aTrsf = std::make_shared<GeomAPI_Trsf>(new gp_Trsf(*myTrsf));
203         GeomAlgoAPI_Transform aTransform(anOrigShape, aTrsf);
204         aResult = aTransform.shape();
205       }
206       if (!myShape.IsNull() && aToSendUpdate) {
207         static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
208         ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
209       }
210       return aResult;
211     } else {
212       DocumentPtr aDoc = Model_ResultPart::partDoc();
213       if (aDoc.get() && aDoc->isOpened()) {
214         const std::string& aBodyGroup = ModelAPI_ResultBody::group();
215         TopoDS_Compound aResultComp;
216         BRep_Builder aBuilder;
217         aBuilder.MakeCompound(aResultComp);
218         int aNumSubs = 0;
219         for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
220           ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
221           // "object" method filters out disabled and concealed anyway, so don't check
222           if (aBody.get() && aBody->data()->isValid() && aBody->shape().get()) {
223             TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
224             if (!aShape.IsNull()) {
225               aBuilder.Add(aResultComp, aShape);
226               aNumSubs++;
227             }
228           }
229         }
230         if (aNumSubs) {
231           myShape = aResultComp;
232         }
233       }
234     }
235     if (!myShape.IsNull() && aToSendUpdate) {
236       static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
237       ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
238     }
239   }
240   if (!myShape.IsNull()) {
241     aResult->setImpl(new TopoDS_Shape(myShape));
242   }
243   return aResult;
244 }
245
246 // Returns true is transformation matrices are equal
247 static bool IsEqualTrsf(gp_Trsf& theT1, gp_Trsf theT2) {
248   for(int aRow = 1; aRow < 4; aRow++) {
249     for(int aCol = 1; aCol < 5; aCol++) {
250       double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
251       if (Abs(aDiff) > 1.e-9)
252         return false;
253     }
254   }
255   return true;
256 }
257
258 std::wstring Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
259   int& theIndex)
260 {
261   theIndex = 0; // not initialized
262
263   if (myTrsf.get()) { // if this is moved copy of part => return the name of original shape
264     ResultPartPtr anOrigResult = baseRef();
265     // searching in the origin the shape equal to the given but with myTrsf
266     TopoDS_Shape aSelection = theShape->impl<TopoDS_Shape>();
267     gp_Trsf aSelTrsf = aSelection.Location().Transformation();
268     TopoDS_Shape anOrigMain = anOrigResult->shape()->impl<TopoDS_Shape>();
269     if (!aSelection.IsNull() && !anOrigMain.IsNull()) {
270       TopExp_Explorer anExp(anOrigMain, aSelection.ShapeType());
271       for(; anExp.More(); anExp.Next()) {
272         if (anExp.Current().IsPartner(aSelection)) {
273           TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()));
274           //if (anOrigMoved.IsSame(aSelection)) {
275           if (IsEqualTrsf(aSelTrsf, anOrigMoved.Location().Transformation())) {
276             std::shared_ptr<GeomAPI_Shape> anOrigSel(new GeomAPI_Shape);
277             anOrigSel->setImpl(new TopoDS_Shape(anExp.Current()));
278             return anOrigResult->nameInPart(anOrigSel, theIndex);
279           }
280         }
281       }
282     }
283     // something is not right
284     return L"";
285   }
286
287   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
288   if (aShape.IsNull())
289     return L"";
290
291   // getting an access to the document of part
292   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
293   if (!aDoc.get()) // the part document is not presented for the moment
294     return L"";
295   MAYBE_UNUSED TDF_Label anAccessLabel = aDoc->generalLabel();
296   // make the selection attribute anyway:
297   // otherwise just by name it is not stable to search the result
298   std::wstring aName;
299   // for this the context result is needed
300   ResultPtr aContext;
301   const std::string& aBodyGroup = ModelAPI_ResultBody::group();
302   for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
303     ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
304     if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
305       TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
306       // check is body contain the selected sub-shape
307       for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
308         if (aShape.IsSame(anExp.Current())) {
309           aContext = aBody;
310           break;
311         }
312       }
313     }
314   }
315   if (aContext.get()) {
316     AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
317     aSelAttr->append(aContext, theShape);
318     theIndex = aSelAttr->size();
319     AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
320     return aNewAttr->namingName();
321   }
322   return aName;
323 }
324
325 bool Model_ResultPart::updateInPart(const int theIndex)
326 {
327   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
328   if (aDoc.get()) {
329     AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
330     AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
331     if (aThisAttr.get()) {
332       if (aThisAttr->update()) {
333         bool aRemove = false;
334         aThisAttr->updateInHistory(aRemove);
335         return true; // it was updated
336       }
337     }
338   }
339   return false; // something is wrong
340 }
341
342 gp_Trsf Model_ResultPart::sumTrsf() {
343   gp_Trsf aResult;
344   if (myTrsf) {
345     aResult = *myTrsf;
346     aResult = aResult * baseRef()->sumTrsf();
347   }
348   return aResult;
349 }
350
351 bool Model_ResultPart::combineGeometrical(const int theIndex, std::wstring& theNewName)
352 {
353   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
354   if (aDoc.get()) {
355     AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
356     AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
357     if (aThisAttr.get()) {
358       aThisAttr->combineGeometrical();
359       if (aThisAttr->value().get()) {
360         int anIndex;
361         theNewName = nameInPart(aThisAttr->value(), anIndex);
362         return true;
363       }
364     }
365   }
366   return false; // something is wrong
367 }
368
369 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(
370   const std::wstring& theName, const std::string& theType, int& theIndex)
371 {
372   theIndex = 0; // not found yet
373   std::shared_ptr<GeomAPI_Shape> aResult;
374   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
375   if (!aDoc.get()) // the part document is not presented for the moment
376     return aResult;
377
378   AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
379   // check this selection is already there: reuse it
380   int aSize = aSelAttr->size();
381   for(int a = 0; a < aSize; a++) {
382     if (aSelAttr->value(a)->namingName() == theName) {
383       theIndex = a;
384       return aSelAttr->value(a)->value();
385     }
386   }
387
388   aSelAttr->append(theName, theType);
389   theIndex = aSelAttr->size();
390   aResult = aSelAttr->value(theIndex - 1)->value();
391   if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
392     gp_Trsf aSumTrsf = sumTrsf();
393     TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
394     aResult->setImpl(new TopoDS_Shape(anOrigMoved));
395   }
396   return aResult;
397 }
398
399 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::selectionValue(const int theIndex)
400 {
401   std::shared_ptr<GeomAPI_Shape> aResult;
402   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
403   if (!aDoc.get()) // the part document is not presented for the moment
404     return aResult;
405
406   AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
407   aResult = aSelAttr->value(theIndex - 1)->value();
408   if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
409     gp_Trsf aSumTrsf = sumTrsf();
410     TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
411     aResult->setImpl(new TopoDS_Shape(anOrigMoved));
412   }
413   return aResult;
414 }
415
416 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
417   std::string& theDefault)
418 {
419   theSection = "Visualization";
420   theName = "result_part_color";
421   theDefault = DEFAULT_COLOR();
422 }
423
424 void Model_ResultPart::updateShape()
425 {
426   myShape.Nullify();
427   myTrsf.reset();
428 }
429
430 void Model_ResultPart::setTrsf(std::shared_ptr<ModelAPI_Result> theThis,
431     const std::shared_ptr<GeomAPI_Trsf>& theTransformation)
432 {
433   updateShape();
434   if (theTransformation.get()) {
435     myTrsf = std::shared_ptr<gp_Trsf>(new gp_Trsf(theTransformation->impl<gp_Trsf>()));
436   }
437   // the result must be explicitly updated
438   static Events_Loop* aLoop = Events_Loop::loop();
439   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
440   ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update
441 }
442
443 std::shared_ptr<GeomAPI_Trsf> Model_ResultPart::summaryTrsf()
444 {
445   GeomTrsfPtr aResult(new GeomAPI_Trsf);
446   aResult->setImpl<gp_Trsf>(new gp_Trsf(sumTrsf()));
447   return aResult;
448 }