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