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