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