Salome HOME
#2027 Sketcher Trim Feature: 1. preview/selected attributes in trim; 2. avoid includi...
[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 <ModelAPI_AttributeDouble.h>
18 #include <Model_Document.h>
19 #include <Model_Application.h>
20 #include <Events_Loop.h>
21 #include <ModelAPI_Events.h>
22
23 #include <GeomAPI_Trsf.h>
24
25 #include <TNaming_Tool.hxx>
26 #include <TNaming_NamedShape.hxx>
27 #include <TDataStd_Name.hxx>
28 #include <TopoDS_Compound.hxx>
29 #include <BRep_Builder.hxx>
30 #include <TopExp_Explorer.hxx>
31
32 #define baseRef() \
33   std::dynamic_pointer_cast<Model_ResultPart>(data()->reference(BASE_REF_ID())->value())
34
35 void Model_ResultPart::initAttributes()
36 {
37   // append the color attribute. It is empty, the attribute will be filled by a request
38   AttributeDocRefPtr aDocRef = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(
39     data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId()));
40   data()->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
41   data()->addAttribute(BASE_REF_ID(), ModelAPI_AttributeReference::typeId());
42   data()->addAttribute(DEFLECTION_ID(), ModelAPI_AttributeDouble::typeId());
43
44   if (aDocRef->isInitialized() && // initialized immideately means already exist and will be loaded
45       !Model_Application::getApplication()->hasDocument(aDocRef->docId()))
46     Model_Application::getApplication()->setLoadByDemand(data()->name());
47 }
48
49 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
50 {
51   if (myTrsf.get()) {
52     return baseRef()->partDoc();
53   }
54   DocumentPtr aRes = data()->document(DOC_REF())->value();
55   return aRes;
56 }
57
58 Model_ResultPart::Model_ResultPart()
59 {
60 }
61
62 void Model_ResultPart::activate()
63 {
64   if (myTrsf.get()) {
65     baseRef()->activate();
66     return;
67   }
68
69   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
70
71   // activation may cause changes in current features in document, so it must be in transaction
72   bool isNewTransaction = false;
73   SessionPtr aMgr = ModelAPI_Session::get();
74   if (!aMgr->isOperation()) {
75     // open transaction even document is not created to set current docs in setActiveDocument
76     aMgr->startOperation("Activation");
77     isNewTransaction = true;
78   }
79   if (!aDocRef->value().get()) {  // create (or open) a document if it is not yet created
80     Handle(Model_Application) anApp = Model_Application::getApplication();
81     if (anApp->isLoadByDemand(data()->name())) {
82       anApp->loadDocument(data()->name(), aDocRef->docId()); // if it is just ne part, load may fail
83     } else {
84       anApp->createDocument(aDocRef->docId());
85     }
86     std::shared_ptr<ModelAPI_Document> aDoc = aDocRef->value();
87     if (aDoc.get()) {
88       aDoc->synchronizeTransactions();
89       aDocRef->setValue(aDoc);
90     }
91   }
92   if (aDocRef->value().get()) {
93     ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
94   }
95   if (isNewTransaction) {
96     aMgr->finishOperation();
97   }
98 }
99
100 std::shared_ptr<ModelAPI_ResultPart> Model_ResultPart::original()
101 {
102   if (myTrsf.get()) {
103     return baseRef()->original();
104   }
105   return std::dynamic_pointer_cast<ModelAPI_ResultPart>(data()->owner());
106 }
107
108 bool Model_ResultPart::isActivated()
109 {
110   if (myTrsf.get()) {
111     if (!baseRef().get()) // may be on close
112       return false;
113     return baseRef()->isActivated();
114   }
115
116   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
117   return aDocRef->value().get() != NULL;
118 }
119
120 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
121     const bool theFlag)
122 {
123   if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
124     if (!myTrsf.get()) { // disable of base result part
125       DocumentPtr aDoc = Model_ResultPart::partDoc();
126       if (aDoc.get() && aDoc->isOpened()) {
127         // make the current feature the last in any case: to update shapes defore deactivation too
128         int aSize = aDoc->size(ModelAPI_Feature::group());
129         FeaturePtr aLastFeature;
130         if (aSize)
131           aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
132             ModelAPI_Feature::group(), aSize - 1));
133
134         aDoc->setCurrentFeature(aLastFeature, false);
135         if (theFlag) { // disable, so make all features disabled too
136           // update the shape just before the deactivation: it will be used outside of part
137           updateShape();
138           shape();
139           aDoc->setCurrentFeature(FeaturePtr(), false);
140           // in order to update OB sub-elements of document before the document closing
141           Events_Loop* aLoop = Events_Loop::loop();
142           aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
143           aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
144           aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
145         }
146       }
147     }
148     return true;
149   }
150   return false;
151 }
152
153 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
154 {
155   std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
156   if (myShape.IsNull()) { // shape is not produced yet, create it
157     if (myTrsf.get()) { // get shape of the base result and apply the transformation
158       ResultPtr anOrigResult = baseRef();
159       std::shared_ptr<GeomAPI_Shape> anOrigShape = anOrigResult->shape();
160       if (anOrigShape.get()) {
161         TopoDS_Shape aShape = anOrigShape->impl<TopoDS_Shape>();
162         if (!aShape.IsNull()) {
163           aShape.Move(*(myTrsf.get()));
164           myShape = aShape;
165           aResult->setImpl(new TopoDS_Shape(aShape));
166         }
167       }
168       return aResult;
169     } else {
170       DocumentPtr aDoc = Model_ResultPart::partDoc();
171       if (aDoc.get() && aDoc->isOpened()) {
172         const std::string& aBodyGroup = ModelAPI_ResultBody::group();
173         TopoDS_Compound aResultComp;
174         BRep_Builder aBuilder;
175         aBuilder.MakeCompound(aResultComp);
176         int aNumSubs = 0;
177         for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
178           ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
179           // "object" method filters out disabled and concealed anyway, so don't check
180           if (aBody.get() && aBody->shape().get()) {
181             TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
182             if (!aShape.IsNull()) {
183               aBuilder.Add(aResultComp, aShape);
184               aNumSubs++;
185             }
186           }
187         }
188         if (aNumSubs) {
189           myShape = aResultComp;
190         }
191       }
192     }
193   }
194   if (!myShape.IsNull())
195     aResult->setImpl(new TopoDS_Shape(myShape));
196   return aResult;
197 }
198
199 // Returns true is transformation matrices are equal
200 static bool IsEqualTrsf(gp_Trsf& theT1, gp_Trsf theT2) {
201   for(int aRow = 1; aRow < 4; aRow++) {
202     for(int aCol = 1; aCol < 5; aCol++) {
203       double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
204       if (Abs(aDiff) > 1.e-9)
205         return false;
206     }
207   }
208   return true;
209 }
210
211 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
212   int& theIndex)
213 {
214   theIndex = 0; // not initialized
215
216   if (myTrsf.get()) { // if this is moved copy of part => return the name of original shape
217     ResultPartPtr anOrigResult = baseRef();
218     // searching in the origin the shape equal to the given but with myTrsf
219     TopoDS_Shape aSelection = theShape->impl<TopoDS_Shape>();
220     gp_Trsf aSelTrsf = aSelection.Location().Transformation();
221     TopoDS_Shape anOrigMain = anOrigResult->shape()->impl<TopoDS_Shape>();
222     if (!aSelection.IsNull() && !anOrigMain.IsNull()) {
223       TopExp_Explorer anExp(anOrigMain, aSelection.ShapeType());
224       for(; anExp.More(); anExp.Next()) {
225         if (anExp.Current().IsPartner(aSelection)) {
226           TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()));
227           //if (anOrigMoved.IsSame(aSelection)) {
228           if (IsEqualTrsf(aSelTrsf, anOrigMoved.Location().Transformation())) {
229             std::shared_ptr<GeomAPI_Shape> anOrigSel(new GeomAPI_Shape);
230             anOrigSel->setImpl(new TopoDS_Shape(anExp.Current()));
231             return anOrigResult->nameInPart(anOrigSel, theIndex);
232           }
233         }
234       }
235     }
236     // something is not right
237     return "";
238   }
239
240   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
241   if (aShape.IsNull())
242     return "";
243
244   // getting an access to the document of part
245   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
246   if (!aDoc.get()) // the part document is not presented for the moment
247     return "";
248   TDF_Label anAccessLabel = aDoc->generalLabel();
249   // make the selection attribute anyway:
250   // otherwise just by name it is not stable to search the result
251   std::string aName;
252   // for this the context result is needed
253   ResultPtr aContext;
254   const std::string& aBodyGroup = ModelAPI_ResultBody::group();
255   for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
256     ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
257     if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
258       TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
259       // check is body contain the selected sub-shape
260       for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
261         if (aShape.IsEqual(anExp.Current())) {
262           aContext = aBody;
263           break;
264         }
265       }
266     }
267   }
268   if (aContext.get()) {
269     AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
270     aSelAttr->append(aContext, theShape);
271     theIndex = aSelAttr->size();
272     AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
273     return aNewAttr->namingName();
274   }
275   return aName;
276 }
277
278 bool Model_ResultPart::updateInPart(const int theIndex)
279 {
280   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
281   if (aDoc.get()) {
282     AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
283     AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
284     if (aThisAttr.get()) {
285       return aThisAttr->update();
286     }
287   }
288   return false; // something is wrong
289 }
290
291 gp_Trsf Model_ResultPart::sumTrsf() {
292   gp_Trsf aResult;
293   if (myTrsf) {
294     aResult = *myTrsf;
295     aResult = aResult * baseRef()->sumTrsf();
296   }
297   return aResult;
298 }
299
300 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(
301   const std::string& theName, const std::string& theType, int& theIndex)
302 {
303   theIndex = 0; // not found yet
304   std::shared_ptr<GeomAPI_Shape> aResult;
305   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
306   if (!aDoc.get()) // the part document is not presented for the moment
307     return aResult;
308
309   AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
310   // check this selection is already there: reuse it
311   int aSize = aSelAttr->size();
312   for(int a = 0; a < aSize; a++) {
313     if (aSelAttr->value(a)->namingName() == theName) {
314       theIndex = a;
315       return aSelAttr->value(a)->value();
316     }
317   }
318
319   aSelAttr->append(theName, theType);
320   theIndex = aSelAttr->size();
321   aResult = aSelAttr->value(theIndex - 1)->value();
322   if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
323     gp_Trsf aSumTrsf = sumTrsf();
324     TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
325     aResult->setImpl(new TopoDS_Shape(anOrigMoved));
326   }
327   return aResult;
328 }
329
330 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::selectionValue(const int theIndex)
331 {
332   std::shared_ptr<GeomAPI_Shape> aResult;
333   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
334   if (!aDoc.get()) // the part document is not presented for the moment
335     return aResult;
336
337   AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
338   aResult = aSelAttr->value(theIndex - 1)->value();
339   if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
340     gp_Trsf aSumTrsf = sumTrsf();
341     TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
342     aResult->setImpl(new TopoDS_Shape(anOrigMoved));
343   }
344   return aResult;
345 }
346
347 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
348   std::string& theDefault)
349 {
350   theSection = "Visualization";
351   theName = "result_part_color";
352   theDefault = DEFAULT_COLOR();
353 }
354
355 void Model_ResultPart::updateShape()
356 {
357   myShape.Nullify();
358   myTrsf.reset();
359 }
360
361 void Model_ResultPart::setTrsf(std::shared_ptr<ModelAPI_Result> theThis,
362     const std::shared_ptr<GeomAPI_Trsf>& theTransformation)
363 {
364   updateShape();
365   if (theTransformation.get()) {
366     myTrsf = std::shared_ptr<gp_Trsf>(new gp_Trsf(theTransformation->impl<gp_Trsf>()));
367   }
368   // the result must be explicitly updated
369   static Events_Loop* aLoop = Events_Loop::loop();
370   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
371   ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update
372 }