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