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