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