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