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