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