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