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