Salome HOME
Support of wide string
[modules/shaper.git] / src / Model / Model_ResultPart.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Model_ResultPart.h>
21 #include <ModelAPI_Data.h>
22 #include <Model_Data.h>
23 #include <ModelAPI_AttributeDocRef.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Feature.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_AttributeIntArray.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_AttributeReference.h>
30 #include <ModelAPI_AttributeDouble.h>
31 #include <Model_Document.h>
32 #include <Model_Application.h>
33 #include <Events_Loop.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_Tools.h>
36
37 #include <GeomAPI_Trsf.h>
38
39 #include <TNaming_Tool.hxx>
40 #include <TNaming_NamedShape.hxx>
41 #include <TDataStd_Name.hxx>
42 #include <TopoDS_Compound.hxx>
43 #include <BRep_Builder.hxx>
44 #include <TopExp_Explorer.hxx>
45
46 #define baseRef() \
47   std::dynamic_pointer_cast<Model_ResultPart>(data()->reference(BASE_REF_ID())->value())
48
49 void Model_ResultPart::initAttributes()
50 {
51   // append the color attribute. It is empty, the attribute will be filled by a request
52   AttributeDocRefPtr aDocRef = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(
53     data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId()));
54   data()->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
55   data()->addAttribute(BASE_REF_ID(), ModelAPI_AttributeReference::typeId());
56   data()->addAttribute(DEFLECTION_ID(), ModelAPI_AttributeDouble::typeId());
57   data()->addAttribute(TRANSPARENCY_ID(), ModelAPI_AttributeDouble::typeId());
58   data()->addAttribute(ISO_LINES_ID(), ModelAPI_AttributeIntArray::typeId());
59
60   if (aDocRef->isInitialized() && // initialized immediately means already exist and will be loaded
61       !Model_Application::getApplication()->hasDocument(aDocRef->docId()))
62     Model_Application::getApplication()->setLoadByDemand(data()->name(), aDocRef->docId());
63 }
64
65 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
66 {
67   if (myTrsf.get() && baseRef().get()) { // the second condition is due to #2035
68     return baseRef()->partDoc();
69   }
70   if (!data()->isValid())
71     return DocumentPtr();
72   DocumentPtr aRes = data()->document(DOC_REF())->value();
73   return aRes;
74 }
75
76 Model_ResultPart::Model_ResultPart()
77 {
78 }
79
80 void Model_ResultPart::activate()
81 {
82   if (myTrsf.get()) {
83     baseRef()->activate();
84     return;
85   }
86
87   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
88
89   // activation may cause changes in current features in document, so it must be in transaction
90   bool isNewTransaction = false;
91   SessionPtr aMgr = ModelAPI_Session::get();
92   if (!aMgr->isOperation()) {
93     // open transaction even document is not created to set current docs in setActiveDocument
94     std::string aMsg = "Activation " + ModelAPI_Tools::toString(data()->name());
95     aMgr->startOperation(aMsg);
96     isNewTransaction = true;
97   }
98   if (!aDocRef->value().get()) {  // create (or open) a document if it is not yet created
99     Handle(Model_Application) anApp = Model_Application::getApplication();
100     if (anApp->isLoadByDemand(data()->name(), aDocRef->docId())) {
101       anApp->loadDocument(data()->name(), aDocRef->docId()); // if it is just new part, load fails
102     } else {
103       anApp->createDocument(aDocRef->docId());
104     }
105     std::shared_ptr<ModelAPI_Document> aDoc = aDocRef->value();
106     if (aDoc.get()) {
107       aDoc->synchronizeTransactions();
108       aDocRef->setValue(aDoc);
109     }
110   }
111   if (aDocRef->value().get()) {
112     ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
113   }
114   if (isNewTransaction) {
115     aMgr->finishOperation();
116   }
117 }
118
119
120 void Model_ResultPart::loadPart()
121 {
122   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
123   if (!aDocRef->value().get()) {  // create (or open) a document if it is not yet created
124     Handle(Model_Application) anApp = Model_Application::getApplication();
125     if (anApp->isLoadByDemand(data()->name(), aDocRef->docId())) {
126       anApp->loadDocument(data()->name(), aDocRef->docId()); // if it is just new part, load fails
127     }
128     else {
129       anApp->createDocument(aDocRef->docId());
130     }
131   }
132 }
133
134
135 std::shared_ptr<ModelAPI_ResultPart> Model_ResultPart::original()
136 {
137   if (myTrsf.get() && baseRef().get()) {  // the second condition is due to #2035
138     return baseRef()->original();
139   }
140   return std::dynamic_pointer_cast<ModelAPI_ResultPart>(data()->owner());
141 }
142
143 bool Model_ResultPart::isActivated()
144 {
145   if (myTrsf.get()) {
146     if (!baseRef().get()) // may be on close
147       return false;
148     return baseRef()->isActivated();
149   }
150
151   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
152   return aDocRef->value().get() != NULL;
153 }
154
155 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
156     const bool theFlag)
157 {
158   if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
159     if (!myTrsf.get()) { // disable of base result part
160       DocumentPtr aDoc = Model_ResultPart::partDoc();
161       if (aDoc.get() && aDoc->isOpened()) {
162         // make the current feature the last in any case: to update shapes before deactivation too
163         int aSize = aDoc->size(ModelAPI_Feature::group());
164         FeaturePtr aLastFeature;
165         if (aSize)
166           aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
167             ModelAPI_Feature::group(), aSize - 1));
168
169         aDoc->setCurrentFeature(aLastFeature, false);
170         if (theFlag) { // disable, so make all features disabled too
171           // update the shape just before the deactivation: it will be used outside of part
172           updateShape();
173           shape();
174           aDoc->setCurrentFeature(FeaturePtr(), false);
175           // in order to update OB sub-elements of document before the document closing
176           Events_Loop* aLoop = Events_Loop::loop();
177           aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
178           aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
179           aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
180         }
181       }
182     }
183     return true;
184   }
185   return false;
186 }
187
188 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
189 {
190   std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
191   if (myShape.IsNull()) { // shape is not produced yet, create it
192     SessionPtr aMgr = ModelAPI_Session::get();
193     bool aToSendUpdate = aMgr->isOperation(); // inside of operation may send an update event
194     if (myTrsf.get()) { // get shape of the base result and apply the transformation
195       ResultPtr anOrigResult = baseRef();
196       std::shared_ptr<GeomAPI_Shape> anOrigShape = anOrigResult->shape();
197       if (anOrigShape.get()) {
198         TopoDS_Shape aShape = anOrigShape->impl<TopoDS_Shape>();
199         if (!aShape.IsNull()) {
200           aShape.Move(*(myTrsf.get()));
201           myShape = aShape;
202           aResult->setImpl(new TopoDS_Shape(aShape));
203         }
204       }
205       if (!myShape.IsNull() && aToSendUpdate) {
206         static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
207         ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
208       }
209       return aResult;
210     } else {
211       DocumentPtr aDoc = Model_ResultPart::partDoc();
212       if (aDoc.get() && aDoc->isOpened()) {
213         const std::string& aBodyGroup = ModelAPI_ResultBody::group();
214         TopoDS_Compound aResultComp;
215         BRep_Builder aBuilder;
216         aBuilder.MakeCompound(aResultComp);
217         int aNumSubs = 0;
218         for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
219           ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
220           // "object" method filters out disabled and concealed anyway, so don't check
221           if (aBody.get() && aBody->data()->isValid() && aBody->shape().get()) {
222             TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
223             if (!aShape.IsNull()) {
224               aBuilder.Add(aResultComp, aShape);
225               aNumSubs++;
226             }
227           }
228         }
229         if (aNumSubs) {
230           myShape = aResultComp;
231         }
232       }
233     }
234     if (!myShape.IsNull() && aToSendUpdate) {
235       static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
236       ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
237     }
238   }
239   if (!myShape.IsNull()) {
240     aResult->setImpl(new TopoDS_Shape(myShape));
241   }
242   return aResult;
243 }
244
245 // Returns true is transformation matrices are equal
246 static bool IsEqualTrsf(gp_Trsf& theT1, gp_Trsf theT2) {
247   for(int aRow = 1; aRow < 4; aRow++) {
248     for(int aCol = 1; aCol < 5; aCol++) {
249       double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
250       if (Abs(aDiff) > 1.e-9)
251         return false;
252     }
253   }
254   return true;
255 }
256
257 std::wstring Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
258   int& theIndex)
259 {
260   theIndex = 0; // not initialized
261
262   if (myTrsf.get()) { // if this is moved copy of part => return the name of original shape
263     ResultPartPtr anOrigResult = baseRef();
264     // searching in the origin the shape equal to the given but with myTrsf
265     TopoDS_Shape aSelection = theShape->impl<TopoDS_Shape>();
266     gp_Trsf aSelTrsf = aSelection.Location().Transformation();
267     TopoDS_Shape anOrigMain = anOrigResult->shape()->impl<TopoDS_Shape>();
268     if (!aSelection.IsNull() && !anOrigMain.IsNull()) {
269       TopExp_Explorer anExp(anOrigMain, aSelection.ShapeType());
270       for(; anExp.More(); anExp.Next()) {
271         if (anExp.Current().IsPartner(aSelection)) {
272           TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()));
273           //if (anOrigMoved.IsSame(aSelection)) {
274           if (IsEqualTrsf(aSelTrsf, anOrigMoved.Location().Transformation())) {
275             std::shared_ptr<GeomAPI_Shape> anOrigSel(new GeomAPI_Shape);
276             anOrigSel->setImpl(new TopoDS_Shape(anExp.Current()));
277             return anOrigResult->nameInPart(anOrigSel, theIndex);
278           }
279         }
280       }
281     }
282     // something is not right
283     return L"";
284   }
285
286   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
287   if (aShape.IsNull())
288     return L"";
289
290   // getting an access to the document of part
291   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
292   if (!aDoc.get()) // the part document is not presented for the moment
293     return L"";
294   TDF_Label anAccessLabel = aDoc->generalLabel();
295   // make the selection attribute anyway:
296   // otherwise just by name it is not stable to search the result
297   std::wstring aName;
298   // for this the context result is needed
299   ResultPtr aContext;
300   const std::string& aBodyGroup = ModelAPI_ResultBody::group();
301   for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
302     ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
303     if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
304       TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
305       // check is body contain the selected sub-shape
306       for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
307         if (aShape.IsSame(anExp.Current())) {
308           aContext = aBody;
309           break;
310         }
311       }
312     }
313   }
314   if (aContext.get()) {
315     AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
316     aSelAttr->append(aContext, theShape);
317     theIndex = aSelAttr->size();
318     AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
319     return aNewAttr->namingName();
320   }
321   return aName;
322 }
323
324 bool Model_ResultPart::updateInPart(const int theIndex)
325 {
326   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
327   if (aDoc.get()) {
328     AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
329     AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
330     if (aThisAttr.get()) {
331       return aThisAttr->update();
332     }
333   }
334   return false; // something is wrong
335 }
336
337 gp_Trsf Model_ResultPart::sumTrsf() {
338   gp_Trsf aResult;
339   if (myTrsf) {
340     aResult = *myTrsf;
341     aResult = aResult * baseRef()->sumTrsf();
342   }
343   return aResult;
344 }
345
346 bool Model_ResultPart::combineGeometrical(const int theIndex, std::wstring& theNewName)
347 {
348   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
349   if (aDoc.get()) {
350     AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
351     AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
352     if (aThisAttr.get()) {
353       aThisAttr->combineGeometrical();
354       if (aThisAttr->value().get()) {
355         int anIndex;
356         theNewName = nameInPart(aThisAttr->value(), anIndex);
357         return true;
358       }
359     }
360   }
361   return false; // something is wrong
362 }
363
364 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(
365   const std::wstring& theName, const std::string& theType, int& theIndex)
366 {
367   theIndex = 0; // not found yet
368   std::shared_ptr<GeomAPI_Shape> aResult;
369   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
370   if (!aDoc.get()) // the part document is not presented for the moment
371     return aResult;
372
373   AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
374   // check this selection is already there: reuse it
375   int aSize = aSelAttr->size();
376   for(int a = 0; a < aSize; a++) {
377     if (aSelAttr->value(a)->namingName() == theName) {
378       theIndex = a;
379       return aSelAttr->value(a)->value();
380     }
381   }
382
383   aSelAttr->append(theName, theType);
384   theIndex = aSelAttr->size();
385   aResult = aSelAttr->value(theIndex - 1)->value();
386   if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
387     gp_Trsf aSumTrsf = sumTrsf();
388     TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
389     aResult->setImpl(new TopoDS_Shape(anOrigMoved));
390   }
391   return aResult;
392 }
393
394 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::selectionValue(const int theIndex)
395 {
396   std::shared_ptr<GeomAPI_Shape> aResult;
397   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
398   if (!aDoc.get()) // the part document is not presented for the moment
399     return aResult;
400
401   AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
402   aResult = aSelAttr->value(theIndex - 1)->value();
403   if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
404     gp_Trsf aSumTrsf = sumTrsf();
405     TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
406     aResult->setImpl(new TopoDS_Shape(anOrigMoved));
407   }
408   return aResult;
409 }
410
411 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
412   std::string& theDefault)
413 {
414   theSection = "Visualization";
415   theName = "result_part_color";
416   theDefault = DEFAULT_COLOR();
417 }
418
419 void Model_ResultPart::updateShape()
420 {
421   myShape.Nullify();
422   myTrsf.reset();
423 }
424
425 void Model_ResultPart::setTrsf(std::shared_ptr<ModelAPI_Result> theThis,
426     const std::shared_ptr<GeomAPI_Trsf>& theTransformation)
427 {
428   updateShape();
429   if (theTransformation.get()) {
430     myTrsf = std::shared_ptr<gp_Trsf>(new gp_Trsf(theTransformation->impl<gp_Trsf>()));
431   }
432   // the result must be explicitly updated
433   static Events_Loop* aLoop = Events_Loop::loop();
434   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
435   ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update
436 }
437
438 std::shared_ptr<GeomAPI_Trsf> Model_ResultPart::summaryTrsf()
439 {
440   GeomTrsfPtr aResult(new GeomAPI_Trsf);
441   aResult->setImpl<gp_Trsf>(new gp_Trsf(sumTrsf()));
442   return aResult;
443 }