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