Salome HOME
Initial implementation of work of "To add all elements that share the same topology...
[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     SessionPtr aMgr = ModelAPI_Session::get();
173     bool aToSendUpdate = aMgr->isOperation(); // inside of operation may send an update evnet
174     if (myTrsf.get()) { // get shape of the base result and apply the transformation
175       ResultPtr anOrigResult = baseRef();
176       std::shared_ptr<GeomAPI_Shape> anOrigShape = anOrigResult->shape();
177       if (anOrigShape.get()) {
178         TopoDS_Shape aShape = anOrigShape->impl<TopoDS_Shape>();
179         if (!aShape.IsNull()) {
180           aShape.Move(*(myTrsf.get()));
181           myShape = aShape;
182           aResult->setImpl(new TopoDS_Shape(aShape));
183         }
184       }
185       if (!myShape.IsNull() && aToSendUpdate) {
186         static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
187         ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
188       }
189       return aResult;
190     } else {
191       DocumentPtr aDoc = Model_ResultPart::partDoc();
192       if (aDoc.get() && aDoc->isOpened()) {
193         const std::string& aBodyGroup = ModelAPI_ResultBody::group();
194         TopoDS_Compound aResultComp;
195         BRep_Builder aBuilder;
196         aBuilder.MakeCompound(aResultComp);
197         int aNumSubs = 0;
198         for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
199           ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
200           // "object" method filters out disabled and concealed anyway, so don't check
201           if (aBody.get() && aBody->data()->isValid() && aBody->shape().get()) {
202             TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
203             if (!aShape.IsNull()) {
204               aBuilder.Add(aResultComp, aShape);
205               aNumSubs++;
206             }
207           }
208         }
209         if (aNumSubs) {
210           myShape = aResultComp;
211         }
212       }
213     }
214     if (!myShape.IsNull() && aToSendUpdate) {
215       static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
216       ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
217     }
218   }
219   if (!myShape.IsNull()) {
220     aResult->setImpl(new TopoDS_Shape(myShape));
221   }
222   return aResult;
223 }
224
225 // Returns true is transformation matrices are equal
226 static bool IsEqualTrsf(gp_Trsf& theT1, gp_Trsf theT2) {
227   for(int aRow = 1; aRow < 4; aRow++) {
228     for(int aCol = 1; aCol < 5; aCol++) {
229       double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
230       if (Abs(aDiff) > 1.e-9)
231         return false;
232     }
233   }
234   return true;
235 }
236
237 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
238   int& theIndex)
239 {
240   theIndex = 0; // not initialized
241
242   if (myTrsf.get()) { // if this is moved copy of part => return the name of original shape
243     ResultPartPtr anOrigResult = baseRef();
244     // searching in the origin the shape equal to the given but with myTrsf
245     TopoDS_Shape aSelection = theShape->impl<TopoDS_Shape>();
246     gp_Trsf aSelTrsf = aSelection.Location().Transformation();
247     TopoDS_Shape anOrigMain = anOrigResult->shape()->impl<TopoDS_Shape>();
248     if (!aSelection.IsNull() && !anOrigMain.IsNull()) {
249       TopExp_Explorer anExp(anOrigMain, aSelection.ShapeType());
250       for(; anExp.More(); anExp.Next()) {
251         if (anExp.Current().IsPartner(aSelection)) {
252           TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()));
253           //if (anOrigMoved.IsSame(aSelection)) {
254           if (IsEqualTrsf(aSelTrsf, anOrigMoved.Location().Transformation())) {
255             std::shared_ptr<GeomAPI_Shape> anOrigSel(new GeomAPI_Shape);
256             anOrigSel->setImpl(new TopoDS_Shape(anExp.Current()));
257             return anOrigResult->nameInPart(anOrigSel, theIndex);
258           }
259         }
260       }
261     }
262     // something is not right
263     return "";
264   }
265
266   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
267   if (aShape.IsNull())
268     return "";
269
270   // getting an access to the document of part
271   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
272   if (!aDoc.get()) // the part document is not presented for the moment
273     return "";
274   TDF_Label anAccessLabel = aDoc->generalLabel();
275   // make the selection attribute anyway:
276   // otherwise just by name it is not stable to search the result
277   std::string aName;
278   // for this the context result is needed
279   ResultPtr aContext;
280   const std::string& aBodyGroup = ModelAPI_ResultBody::group();
281   for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
282     ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
283     if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
284       TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
285       // check is body contain the selected sub-shape
286       for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
287         if (aShape.IsEqual(anExp.Current())) {
288           aContext = aBody;
289           break;
290         }
291       }
292     }
293   }
294   if (aContext.get()) {
295     AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
296     aSelAttr->append(aContext, theShape);
297     theIndex = aSelAttr->size();
298     AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
299     return aNewAttr->namingName();
300   }
301   return aName;
302 }
303
304 bool Model_ResultPart::updateInPart(const int theIndex)
305 {
306   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
307   if (aDoc.get()) {
308     AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
309     AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
310     if (aThisAttr.get()) {
311       return aThisAttr->update();
312     }
313   }
314   return false; // something is wrong
315 }
316
317 gp_Trsf Model_ResultPart::sumTrsf() {
318   gp_Trsf aResult;
319   if (myTrsf) {
320     aResult = *myTrsf;
321     aResult = aResult * baseRef()->sumTrsf();
322   }
323   return aResult;
324 }
325
326 bool Model_ResultPart::combineGeometrical(const int theIndex, std::string& theNewName)
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       aThisAttr->combineGeometrical();
334       if (aThisAttr->value().get()) {
335         int anIndex;
336         theNewName = nameInPart(aThisAttr->value(), anIndex);
337         return true;
338       }
339     }
340   }
341   return false; // something is wrong
342 }
343
344 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(
345   const std::string& theName, const std::string& theType, int& theIndex)
346 {
347   theIndex = 0; // not found yet
348   std::shared_ptr<GeomAPI_Shape> aResult;
349   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
350   if (!aDoc.get()) // the part document is not presented for the moment
351     return aResult;
352
353   AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
354   // check this selection is already there: reuse it
355   int aSize = aSelAttr->size();
356   for(int a = 0; a < aSize; a++) {
357     if (aSelAttr->value(a)->namingName() == theName) {
358       theIndex = a;
359       return aSelAttr->value(a)->value();
360     }
361   }
362
363   aSelAttr->append(theName, theType);
364   theIndex = aSelAttr->size();
365   aResult = aSelAttr->value(theIndex - 1)->value();
366   if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
367     gp_Trsf aSumTrsf = sumTrsf();
368     TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
369     aResult->setImpl(new TopoDS_Shape(anOrigMoved));
370   }
371   return aResult;
372 }
373
374 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::selectionValue(const int theIndex)
375 {
376   std::shared_ptr<GeomAPI_Shape> aResult;
377   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
378   if (!aDoc.get()) // the part document is not presented for the moment
379     return aResult;
380
381   AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
382   aResult = aSelAttr->value(theIndex - 1)->value();
383   if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
384     gp_Trsf aSumTrsf = sumTrsf();
385     TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
386     aResult->setImpl(new TopoDS_Shape(anOrigMoved));
387   }
388   return aResult;
389 }
390
391 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
392   std::string& theDefault)
393 {
394   theSection = "Visualization";
395   theName = "result_part_color";
396   theDefault = DEFAULT_COLOR();
397 }
398
399 void Model_ResultPart::updateShape()
400 {
401   myShape.Nullify();
402   myTrsf.reset();
403 }
404
405 void Model_ResultPart::setTrsf(std::shared_ptr<ModelAPI_Result> theThis,
406     const std::shared_ptr<GeomAPI_Trsf>& theTransformation)
407 {
408   updateShape();
409   if (theTransformation.get()) {
410     myTrsf = std::shared_ptr<gp_Trsf>(new gp_Trsf(theTransformation->impl<gp_Trsf>()));
411   }
412   // the result must be explicitly updated
413   static Events_Loop* aLoop = Events_Loop::loop();
414   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
415   ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update
416 }