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