1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <Model_ResultPart.h>
22 #include <ModelAPI_Data.h>
23 #include <Model_Data.h>
24 #include <ModelAPI_AttributeDocRef.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_ResultBody.h>
28 #include <ModelAPI_AttributeIntArray.h>
29 #include <ModelAPI_AttributeSelectionList.h>
30 #include <ModelAPI_AttributeReference.h>
31 #include <ModelAPI_AttributeDouble.h>
32 #include <Model_Document.h>
33 #include <Model_Application.h>
34 #include <Events_Loop.h>
35 #include <ModelAPI_Events.h>
37 #include <GeomAPI_Trsf.h>
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>
47 std::dynamic_pointer_cast<Model_ResultPart>(data()->reference(BASE_REF_ID())->value())
49 void Model_ResultPart::initAttributes()
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());
59 if (aDocRef->isInitialized() && // initialized immediately means already exist and will be loaded
60 !Model_Application::getApplication()->hasDocument(aDocRef->docId()))
61 Model_Application::getApplication()->setLoadByDemand(data()->name(), aDocRef->docId());
64 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
66 if (myTrsf.get() && baseRef().get()) { // the second condition is due to #2035
67 return baseRef()->partDoc();
69 if (!data()->isValid())
71 DocumentPtr aRes = data()->document(DOC_REF())->value();
75 Model_ResultPart::Model_ResultPart()
79 void Model_ResultPart::activate()
82 baseRef()->activate();
86 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
88 // activation may cause changes in current features in document, so it must be in transaction
89 bool isNewTransaction = false;
90 SessionPtr aMgr = ModelAPI_Session::get();
91 if (!aMgr->isOperation()) {
92 // open transaction even document is not created to set current docs in setActiveDocument
93 aMgr->startOperation("Activation");
94 isNewTransaction = true;
96 if (!aDocRef->value().get()) { // create (or open) a document if it is not yet created
97 Handle(Model_Application) anApp = Model_Application::getApplication();
98 if (anApp->isLoadByDemand(data()->name(), aDocRef->docId())) {
99 anApp->loadDocument(data()->name(), aDocRef->docId()); // if it is just new part, load fails
101 anApp->createDocument(aDocRef->docId());
103 std::shared_ptr<ModelAPI_Document> aDoc = aDocRef->value();
105 aDoc->synchronizeTransactions();
106 aDocRef->setValue(aDoc);
109 if (aDocRef->value().get()) {
110 ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
112 if (isNewTransaction) {
113 aMgr->finishOperation();
117 std::shared_ptr<ModelAPI_ResultPart> Model_ResultPart::original()
119 if (myTrsf.get() && baseRef().get()) { // the second condition is due to #2035
120 return baseRef()->original();
122 return std::dynamic_pointer_cast<ModelAPI_ResultPart>(data()->owner());
125 bool Model_ResultPart::isActivated()
128 if (!baseRef().get()) // may be on close
130 return baseRef()->isActivated();
133 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
134 return aDocRef->value().get() != NULL;
137 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
140 if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
141 if (!myTrsf.get()) { // disable of base result part
142 DocumentPtr aDoc = Model_ResultPart::partDoc();
143 if (aDoc.get() && aDoc->isOpened()) {
144 // make the current feature the last in any case: to update shapes before deactivation too
145 int aSize = aDoc->size(ModelAPI_Feature::group());
146 FeaturePtr aLastFeature;
148 aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
149 ModelAPI_Feature::group(), aSize - 1));
151 aDoc->setCurrentFeature(aLastFeature, false);
152 if (theFlag) { // disable, so make all features disabled too
153 // update the shape just before the deactivation: it will be used outside of part
156 aDoc->setCurrentFeature(FeaturePtr(), false);
157 // in order to update OB sub-elements of document before the document closing
158 Events_Loop* aLoop = Events_Loop::loop();
159 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
160 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
161 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
170 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
172 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
173 if (myShape.IsNull()) { // shape is not produced yet, create it
174 SessionPtr aMgr = ModelAPI_Session::get();
175 bool aToSendUpdate = aMgr->isOperation(); // inside of operation may send an update event
176 if (myTrsf.get()) { // get shape of the base result and apply the transformation
177 ResultPtr anOrigResult = baseRef();
178 std::shared_ptr<GeomAPI_Shape> anOrigShape = anOrigResult->shape();
179 if (anOrigShape.get()) {
180 TopoDS_Shape aShape = anOrigShape->impl<TopoDS_Shape>();
181 if (!aShape.IsNull()) {
182 aShape.Move(*(myTrsf.get()));
184 aResult->setImpl(new TopoDS_Shape(aShape));
187 if (!myShape.IsNull() && aToSendUpdate) {
188 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
189 ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
193 DocumentPtr aDoc = Model_ResultPart::partDoc();
194 if (aDoc.get() && aDoc->isOpened()) {
195 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
196 TopoDS_Compound aResultComp;
197 BRep_Builder aBuilder;
198 aBuilder.MakeCompound(aResultComp);
200 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
201 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
202 // "object" method filters out disabled and concealed anyway, so don't check
203 if (aBody.get() && aBody->data()->isValid() && aBody->shape().get()) {
204 TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
205 if (!aShape.IsNull()) {
206 aBuilder.Add(aResultComp, aShape);
212 myShape = aResultComp;
216 if (!myShape.IsNull() && aToSendUpdate) {
217 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
218 ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
221 if (!myShape.IsNull()) {
222 aResult->setImpl(new TopoDS_Shape(myShape));
227 // Returns true is transformation matrices are equal
228 static bool IsEqualTrsf(gp_Trsf& theT1, gp_Trsf theT2) {
229 for(int aRow = 1; aRow < 4; aRow++) {
230 for(int aCol = 1; aCol < 5; aCol++) {
231 double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
232 if (Abs(aDiff) > 1.e-9)
239 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
242 theIndex = 0; // not initialized
244 if (myTrsf.get()) { // if this is moved copy of part => return the name of original shape
245 ResultPartPtr anOrigResult = baseRef();
246 // searching in the origin the shape equal to the given but with myTrsf
247 TopoDS_Shape aSelection = theShape->impl<TopoDS_Shape>();
248 gp_Trsf aSelTrsf = aSelection.Location().Transformation();
249 TopoDS_Shape anOrigMain = anOrigResult->shape()->impl<TopoDS_Shape>();
250 if (!aSelection.IsNull() && !anOrigMain.IsNull()) {
251 TopExp_Explorer anExp(anOrigMain, aSelection.ShapeType());
252 for(; anExp.More(); anExp.Next()) {
253 if (anExp.Current().IsPartner(aSelection)) {
254 TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()));
255 //if (anOrigMoved.IsSame(aSelection)) {
256 if (IsEqualTrsf(aSelTrsf, anOrigMoved.Location().Transformation())) {
257 std::shared_ptr<GeomAPI_Shape> anOrigSel(new GeomAPI_Shape);
258 anOrigSel->setImpl(new TopoDS_Shape(anExp.Current()));
259 return anOrigResult->nameInPart(anOrigSel, theIndex);
264 // something is not right
268 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
272 // getting an access to the document of part
273 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
274 if (!aDoc.get()) // the part document is not presented for the moment
276 TDF_Label anAccessLabel = aDoc->generalLabel();
277 // make the selection attribute anyway:
278 // otherwise just by name it is not stable to search the result
280 // for this the context result is needed
282 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
283 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
284 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
285 if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
286 TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
287 // check is body contain the selected sub-shape
288 for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
289 if (aShape.IsEqual(anExp.Current())) {
296 if (aContext.get()) {
297 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
298 aSelAttr->append(aContext, theShape);
299 theIndex = aSelAttr->size();
300 AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
301 return aNewAttr->namingName();
306 bool Model_ResultPart::updateInPart(const int theIndex)
308 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
310 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
311 AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
312 if (aThisAttr.get()) {
313 return aThisAttr->update();
316 return false; // something is wrong
319 gp_Trsf Model_ResultPart::sumTrsf() {
323 aResult = aResult * baseRef()->sumTrsf();
328 bool Model_ResultPart::combineGeometrical(const int theIndex, std::string& theNewName)
330 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
332 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
333 AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
334 if (aThisAttr.get()) {
335 aThisAttr->combineGeometrical();
336 if (aThisAttr->value().get()) {
338 theNewName = nameInPart(aThisAttr->value(), anIndex);
343 return false; // something is wrong
346 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(
347 const std::string& theName, const std::string& theType, int& theIndex)
349 theIndex = 0; // not found yet
350 std::shared_ptr<GeomAPI_Shape> aResult;
351 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
352 if (!aDoc.get()) // the part document is not presented for the moment
355 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
356 // check this selection is already there: reuse it
357 int aSize = aSelAttr->size();
358 for(int a = 0; a < aSize; a++) {
359 if (aSelAttr->value(a)->namingName() == theName) {
361 return aSelAttr->value(a)->value();
365 aSelAttr->append(theName, theType);
366 theIndex = aSelAttr->size();
367 aResult = aSelAttr->value(theIndex - 1)->value();
368 if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
369 gp_Trsf aSumTrsf = sumTrsf();
370 TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
371 aResult->setImpl(new TopoDS_Shape(anOrigMoved));
376 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::selectionValue(const int theIndex)
378 std::shared_ptr<GeomAPI_Shape> aResult;
379 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
380 if (!aDoc.get()) // the part document is not presented for the moment
383 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
384 aResult = aSelAttr->value(theIndex - 1)->value();
385 if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
386 gp_Trsf aSumTrsf = sumTrsf();
387 TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
388 aResult->setImpl(new TopoDS_Shape(anOrigMoved));
393 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
394 std::string& theDefault)
396 theSection = "Visualization";
397 theName = "result_part_color";
398 theDefault = DEFAULT_COLOR();
401 void Model_ResultPart::updateShape()
407 void Model_ResultPart::setTrsf(std::shared_ptr<ModelAPI_Result> theThis,
408 const std::shared_ptr<GeomAPI_Trsf>& theTransformation)
411 if (theTransformation.get()) {
412 myTrsf = std::shared_ptr<gp_Trsf>(new gp_Trsf(theTransformation->impl<gp_Trsf>()));
414 // the result must be explicitly updated
415 static Events_Loop* aLoop = Events_Loop::loop();
416 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
417 ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update