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