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 immideately 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 DocumentPtr aRes = data()->document(DOC_REF())->value();
73 Model_ResultPart::Model_ResultPart()
77 void Model_ResultPart::activate()
80 baseRef()->activate();
84 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
86 // activation may cause changes in current features in document, so it must be in transaction
87 bool isNewTransaction = false;
88 SessionPtr aMgr = ModelAPI_Session::get();
89 if (!aMgr->isOperation()) {
90 // open transaction even document is not created to set current docs in setActiveDocument
91 aMgr->startOperation("Activation");
92 isNewTransaction = true;
94 if (!aDocRef->value().get()) { // create (or open) a document if it is not yet created
95 Handle(Model_Application) anApp = Model_Application::getApplication();
96 if (anApp->isLoadByDemand(data()->name(), aDocRef->docId())) {
97 anApp->loadDocument(data()->name(), aDocRef->docId()); // if it is just new part, load fails
99 anApp->createDocument(aDocRef->docId());
101 std::shared_ptr<ModelAPI_Document> aDoc = aDocRef->value();
103 aDoc->synchronizeTransactions();
104 aDocRef->setValue(aDoc);
107 if (aDocRef->value().get()) {
108 ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
110 if (isNewTransaction) {
111 aMgr->finishOperation();
115 std::shared_ptr<ModelAPI_ResultPart> Model_ResultPart::original()
117 if (myTrsf.get() && baseRef().get()) { // the second condition is due to #2035
118 return baseRef()->original();
120 return std::dynamic_pointer_cast<ModelAPI_ResultPart>(data()->owner());
123 bool Model_ResultPart::isActivated()
126 if (!baseRef().get()) // may be on close
128 return baseRef()->isActivated();
131 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
132 return aDocRef->value().get() != NULL;
135 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
138 if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
139 if (!myTrsf.get()) { // disable of base result part
140 DocumentPtr aDoc = Model_ResultPart::partDoc();
141 if (aDoc.get() && aDoc->isOpened()) {
142 // make the current feature the last in any case: to update shapes defore deactivation too
143 int aSize = aDoc->size(ModelAPI_Feature::group());
144 FeaturePtr aLastFeature;
146 aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
147 ModelAPI_Feature::group(), aSize - 1));
149 aDoc->setCurrentFeature(aLastFeature, false);
150 if (theFlag) { // disable, so make all features disabled too
151 // update the shape just before the deactivation: it will be used outside of part
154 aDoc->setCurrentFeature(FeaturePtr(), false);
155 // in order to update OB sub-elements of document before the document closing
156 Events_Loop* aLoop = Events_Loop::loop();
157 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
158 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
159 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
168 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
170 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
171 if (myShape.IsNull()) { // shape is not produced yet, create it
172 SessionPtr aMgr = ModelAPI_Session::get();
173 bool aToSendUpdate = aMgr->isOperation(); // inside of operation may send an update evnet
174 if (myTrsf.get()) { // get shape of the base result and apply the transformation
175 ResultPtr anOrigResult = baseRef();
176 std::shared_ptr<GeomAPI_Shape> anOrigShape = anOrigResult->shape();
177 if (anOrigShape.get()) {
178 TopoDS_Shape aShape = anOrigShape->impl<TopoDS_Shape>();
179 if (!aShape.IsNull()) {
180 aShape.Move(*(myTrsf.get()));
182 aResult->setImpl(new TopoDS_Shape(aShape));
185 if (!myShape.IsNull() && aToSendUpdate) {
186 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
187 ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
191 DocumentPtr aDoc = Model_ResultPart::partDoc();
192 if (aDoc.get() && aDoc->isOpened()) {
193 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
194 TopoDS_Compound aResultComp;
195 BRep_Builder aBuilder;
196 aBuilder.MakeCompound(aResultComp);
198 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
199 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
200 // "object" method filters out disabled and concealed anyway, so don't check
201 if (aBody.get() && aBody->data()->isValid() && aBody->shape().get()) {
202 TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
203 if (!aShape.IsNull()) {
204 aBuilder.Add(aResultComp, aShape);
210 myShape = aResultComp;
214 if (!myShape.IsNull() && aToSendUpdate) {
215 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
216 ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
219 if (!myShape.IsNull()) {
220 aResult->setImpl(new TopoDS_Shape(myShape));
225 // Returns true is transformation matrices are equal
226 static bool IsEqualTrsf(gp_Trsf& theT1, gp_Trsf theT2) {
227 for(int aRow = 1; aRow < 4; aRow++) {
228 for(int aCol = 1; aCol < 5; aCol++) {
229 double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
230 if (Abs(aDiff) > 1.e-9)
237 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
240 theIndex = 0; // not initialized
242 if (myTrsf.get()) { // if this is moved copy of part => return the name of original shape
243 ResultPartPtr anOrigResult = baseRef();
244 // searching in the origin the shape equal to the given but with myTrsf
245 TopoDS_Shape aSelection = theShape->impl<TopoDS_Shape>();
246 gp_Trsf aSelTrsf = aSelection.Location().Transformation();
247 TopoDS_Shape anOrigMain = anOrigResult->shape()->impl<TopoDS_Shape>();
248 if (!aSelection.IsNull() && !anOrigMain.IsNull()) {
249 TopExp_Explorer anExp(anOrigMain, aSelection.ShapeType());
250 for(; anExp.More(); anExp.Next()) {
251 if (anExp.Current().IsPartner(aSelection)) {
252 TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()));
253 //if (anOrigMoved.IsSame(aSelection)) {
254 if (IsEqualTrsf(aSelTrsf, anOrigMoved.Location().Transformation())) {
255 std::shared_ptr<GeomAPI_Shape> anOrigSel(new GeomAPI_Shape);
256 anOrigSel->setImpl(new TopoDS_Shape(anExp.Current()));
257 return anOrigResult->nameInPart(anOrigSel, theIndex);
262 // something is not right
266 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
270 // getting an access to the document of part
271 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
272 if (!aDoc.get()) // the part document is not presented for the moment
274 TDF_Label anAccessLabel = aDoc->generalLabel();
275 // make the selection attribute anyway:
276 // otherwise just by name it is not stable to search the result
278 // for this the context result is needed
280 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
281 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
282 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
283 if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
284 TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
285 // check is body contain the selected sub-shape
286 for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
287 if (aShape.IsEqual(anExp.Current())) {
294 if (aContext.get()) {
295 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
296 aSelAttr->append(aContext, theShape);
297 theIndex = aSelAttr->size();
298 AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
299 return aNewAttr->namingName();
304 bool Model_ResultPart::updateInPart(const int theIndex)
306 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
308 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
309 AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
310 if (aThisAttr.get()) {
311 return aThisAttr->update();
314 return false; // something is wrong
317 gp_Trsf Model_ResultPart::sumTrsf() {
321 aResult = aResult * baseRef()->sumTrsf();
326 bool Model_ResultPart::combineGeometrical(const int theIndex, std::string& theNewName)
328 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
330 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
331 AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
332 if (aThisAttr.get()) {
333 aThisAttr->combineGeometrical();
334 if (aThisAttr->value().get()) {
336 theNewName = nameInPart(aThisAttr->value(), anIndex);
341 return false; // something is wrong
344 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(
345 const std::string& theName, const std::string& theType, int& theIndex)
347 theIndex = 0; // not found yet
348 std::shared_ptr<GeomAPI_Shape> aResult;
349 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
350 if (!aDoc.get()) // the part document is not presented for the moment
353 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
354 // check this selection is already there: reuse it
355 int aSize = aSelAttr->size();
356 for(int a = 0; a < aSize; a++) {
357 if (aSelAttr->value(a)->namingName() == theName) {
359 return aSelAttr->value(a)->value();
363 aSelAttr->append(theName, theType);
364 theIndex = aSelAttr->size();
365 aResult = aSelAttr->value(theIndex - 1)->value();
366 if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
367 gp_Trsf aSumTrsf = sumTrsf();
368 TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
369 aResult->setImpl(new TopoDS_Shape(anOrigMoved));
374 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::selectionValue(const int theIndex)
376 std::shared_ptr<GeomAPI_Shape> aResult;
377 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
378 if (!aDoc.get()) // the part document is not presented for the moment
381 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
382 aResult = aSelAttr->value(theIndex - 1)->value();
383 if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
384 gp_Trsf aSumTrsf = sumTrsf();
385 TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
386 aResult->setImpl(new TopoDS_Shape(anOrigMoved));
391 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
392 std::string& theDefault)
394 theSection = "Visualization";
395 theName = "result_part_color";
396 theDefault = DEFAULT_COLOR();
399 void Model_ResultPart::updateShape()
405 void Model_ResultPart::setTrsf(std::shared_ptr<ModelAPI_Result> theThis,
406 const std::shared_ptr<GeomAPI_Trsf>& theTransformation)
409 if (theTransformation.get()) {
410 myTrsf = std::shared_ptr<gp_Trsf>(new gp_Trsf(theTransformation->impl<gp_Trsf>()));
412 // the result must be explicitly updated
413 static Events_Loop* aLoop = Events_Loop::loop();
414 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
415 ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update