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