1 // Copyright (C) 2014-2022 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 email : webmaster.salome@opencascade.com
20 #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>
36 #include <ModelAPI_Tools.h>
38 #include <GeomAPI_Trsf.h>
40 #include <GeomAlgoAPI_Transform.h>
42 #include <Locale_Convert.h>
44 #include <TNaming_Tool.hxx>
45 #include <TNaming_NamedShape.hxx>
46 #include <TDataStd_Name.hxx>
47 #include <TopoDS_Compound.hxx>
48 #include <BRep_Builder.hxx>
49 #include <TopExp_Explorer.hxx>
51 #include <Basics_OCCTVersion.hxx>
54 std::dynamic_pointer_cast<Model_ResultPart>(data()->reference(BASE_REF_ID())->value())
56 void Model_ResultPart::initAttributes()
58 // append the color attribute. It is empty, the attribute will be filled by a request
59 AttributeDocRefPtr aDocRef = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(
60 data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId()));
61 data()->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
62 data()->addAttribute(BASE_REF_ID(), ModelAPI_AttributeReference::typeId());
63 data()->addAttribute(DEFLECTION_ID(), ModelAPI_AttributeDouble::typeId());
64 data()->addAttribute(TRANSPARENCY_ID(), ModelAPI_AttributeDouble::typeId());
65 data()->addAttribute(ISO_LINES_ID(), ModelAPI_AttributeIntArray::typeId());
67 if (aDocRef->isInitialized() && // initialized immediately means already exist and will be loaded
68 !Model_Application::getApplication()->hasDocument(aDocRef->docId()))
69 Model_Application::getApplication()->setLoadByDemand(data()->name(), aDocRef->docId());
72 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
74 if (myTrsf.get() && baseRef().get()) { // the second condition is due to #2035
75 return baseRef()->partDoc();
77 if (!data()->isValid())
79 DocumentPtr aRes = data()->document(DOC_REF())->value();
83 Model_ResultPart::Model_ResultPart()
87 void Model_ResultPart::activate()
90 baseRef()->activate();
94 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
96 // activation may cause changes in current features in document, so it must be in transaction
97 bool isNewTransaction = false;
98 SessionPtr aMgr = ModelAPI_Session::get();
99 if (!aMgr->isOperation()) {
100 // open transaction even document is not created to set current docs in setActiveDocument
101 std::string aMsg = "Activation " + Locale::Convert::toString(data()->name());
102 aMgr->startOperation(aMsg);
103 isNewTransaction = true;
105 if (!aDocRef->value().get()) { // create (or open) a document if it is not yet created
106 Handle(Model_Application) anApp = Model_Application::getApplication();
107 if (anApp->isLoadByDemand(data()->name(), aDocRef->docId())) {
108 anApp->loadDocument(data()->name(), aDocRef->docId()); // if it is just new part, load fails
110 anApp->createDocument(aDocRef->docId());
112 std::shared_ptr<ModelAPI_Document> aDoc = aDocRef->value();
114 aDoc->synchronizeTransactions();
115 aDocRef->setValue(aDoc);
118 if (aDocRef->value().get()) {
119 ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
121 if (isNewTransaction) {
122 aMgr->finishOperation();
127 void Model_ResultPart::loadPart()
129 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
130 if (!aDocRef->value().get()) { // create (or open) a document if it is not yet created
131 Handle(Model_Application) anApp = Model_Application::getApplication();
132 if (anApp->isLoadByDemand(data()->name(), aDocRef->docId())) {
133 anApp->loadDocument(data()->name(), aDocRef->docId()); // if it is just new part, load fails
136 anApp->createDocument(aDocRef->docId());
142 std::shared_ptr<ModelAPI_ResultPart> Model_ResultPart::original()
144 if (myTrsf.get() && baseRef().get()) { // the second condition is due to #2035
145 return baseRef()->original();
147 return std::dynamic_pointer_cast<ModelAPI_ResultPart>(data()->owner());
150 bool Model_ResultPart::isActivated()
153 if (!baseRef().get()) // may be on close
155 return baseRef()->isActivated();
158 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
159 return aDocRef->value().get() != NULL;
162 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
165 if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
166 if (!myTrsf.get()) { // disable of base result part
167 DocumentPtr aDoc = Model_ResultPart::partDoc();
168 if (aDoc.get() && aDoc->isOpened()) {
169 // make the current feature the last in any case: to update shapes before deactivation too
170 int aSize = aDoc->size(ModelAPI_Feature::group());
171 FeaturePtr aLastFeature;
173 aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
174 ModelAPI_Feature::group(), aSize - 1));
176 aDoc->setCurrentFeature(aLastFeature, false);
177 if (theFlag) { // disable, so make all features disabled too
178 // update the shape just before the deactivation: it will be used outside of part
181 aDoc->setCurrentFeature(FeaturePtr(), false);
182 // in order to update OB sub-elements of document before the document closing
183 Events_Loop* aLoop = Events_Loop::loop();
184 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
185 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
186 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
195 static GeomShapePtr transformShape(const GeomShapePtr theShape, const gp_Trsf& theTrsf)
197 GeomShapePtr aResult(new GeomAPI_Shape);
198 if (theTrsf.ScaleFactor() > 0) {
199 // just update the location of the shape in case of affine transformation
200 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
201 if (!aShape.IsNull()) {
202 #if OCC_VERSION_LARGE < 0x07070000
203 aShape.Move(theTrsf);
205 aShape.Move(theTrsf, Standard_False);
207 aResult->setImpl(new TopoDS_Shape(aShape));
211 // all other transformations will modify the shape
212 GeomTrsfPtr aTrsf = std::make_shared<GeomAPI_Trsf>(new gp_Trsf(theTrsf));
213 GeomAlgoAPI_Transform aTransform(theShape, aTrsf);
214 aResult = aTransform.shape();
219 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
221 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
222 if (myShape.IsNull()) { // shape is not produced yet, create it
223 SessionPtr aMgr = ModelAPI_Session::get();
224 bool aToSendUpdate = aMgr->isOperation(); // inside of operation may send an update event
225 if (myTrsf.get()) { // get shape of the base result and apply the transformation
226 ResultPtr anOrigResult = baseRef();
227 std::shared_ptr<GeomAPI_Shape> anOrigShape = anOrigResult->shape();
228 if (anOrigShape.get()) {
229 aResult = transformShape(anOrigShape, *myTrsf);
230 myShape = aResult->impl<TopoDS_Shape>();
232 if (!myShape.IsNull() && aToSendUpdate) {
233 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
234 ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
238 DocumentPtr aDoc = Model_ResultPart::partDoc();
239 if (aDoc.get() && aDoc->isOpened()) {
240 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
241 TopoDS_Compound aResultComp;
242 BRep_Builder aBuilder;
243 aBuilder.MakeCompound(aResultComp);
245 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
246 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
247 // "object" method filters out disabled and concealed anyway, so don't check
248 if (aBody.get() && aBody->data()->isValid() && aBody->shape().get()) {
249 TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
250 if (!aShape.IsNull()) {
251 aBuilder.Add(aResultComp, aShape);
257 myShape = aResultComp;
261 if (!myShape.IsNull() && aToSendUpdate) {
262 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
263 ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
266 if (!myShape.IsNull()) {
267 aResult->setImpl(new TopoDS_Shape(myShape));
272 // Returns true is transformation matrices are equal
273 static bool IsEqualTrsf(gp_Trsf& theT1, gp_Trsf theT2) {
274 for(int aRow = 1; aRow < 4; aRow++) {
275 for(int aCol = 1; aCol < 5; aCol++) {
276 double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
277 if (Abs(aDiff) > 1.e-9)
284 std::wstring Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
287 theIndex = 0; // not initialized
289 if (myTrsf.get()) { // if this is moved copy of part => return the name of original shape
290 ResultPartPtr anOrigResult = baseRef();
291 // searching in the origin the shape equal to the given but with myTrsf
292 TopoDS_Shape aSelection = theShape->impl<TopoDS_Shape>();
293 gp_Trsf aSelTrsf = aSelection.Location().Transformation();
294 TopoDS_Shape anOrigMain = anOrigResult->shape()->impl<TopoDS_Shape>();
295 if (!aSelection.IsNull() && !anOrigMain.IsNull()) {
296 TopExp_Explorer anExp(anOrigMain, aSelection.ShapeType());
297 for(; anExp.More(); anExp.Next()) {
298 if (anExp.Current().IsPartner(aSelection)) {
299 #if OCC_VERSION_LARGE < 0x07070000
300 TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()));
302 TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()), Standard_False);
304 //if (anOrigMoved.IsSame(aSelection)) {
305 if (IsEqualTrsf(aSelTrsf, anOrigMoved.Location().Transformation())) {
306 std::shared_ptr<GeomAPI_Shape> anOrigSel(new GeomAPI_Shape);
307 anOrigSel->setImpl(new TopoDS_Shape(anExp.Current()));
308 return anOrigResult->nameInPart(anOrigSel, theIndex);
313 // something is not right
317 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
321 // getting an access to the document of part
322 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
323 if (!aDoc.get()) // the part document is not presented for the moment
325 MAYBE_UNUSED TDF_Label anAccessLabel = aDoc->generalLabel();
326 // make the selection attribute anyway:
327 // otherwise just by name it is not stable to search the result
329 // for this the context result is needed
331 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
332 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
333 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
334 if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
335 TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
336 // check is body contain the selected sub-shape
337 for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
338 if (aShape.IsSame(anExp.Current())) {
345 if (aContext.get()) {
346 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
347 aSelAttr->append(aContext, theShape);
348 theIndex = aSelAttr->size();
349 AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
350 return aNewAttr->namingName();
355 bool Model_ResultPart::updateInPart(const int theIndex)
357 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
359 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
360 AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
361 if (aThisAttr.get()) {
362 if (aThisAttr->update()) {
363 bool aRemove = false;
364 aThisAttr->updateInHistory(aRemove);
365 return true; // it was updated
369 return false; // something is wrong
372 gp_Trsf Model_ResultPart::sumTrsf() {
376 aResult = aResult * baseRef()->sumTrsf();
381 bool Model_ResultPart::combineGeometrical(const int theIndex, std::wstring& theNewName)
383 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
385 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
386 AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
387 if (aThisAttr.get()) {
388 aThisAttr->combineGeometrical();
389 if (aThisAttr->value().get()) {
391 theNewName = nameInPart(aThisAttr->value(), anIndex);
396 return false; // something is wrong
399 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(
400 const std::wstring& theName, const std::string& theType, int& theIndex)
402 theIndex = 0; // not found yet
403 std::shared_ptr<GeomAPI_Shape> aResult;
404 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
405 if (!aDoc.get()) // the part document is not presented for the moment
408 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
409 // check this selection is already there: reuse it
410 int aSize = aSelAttr->size();
411 for(int a = 0; a < aSize; a++) {
412 if (aSelAttr->value(a)->namingName() == theName) {
414 return aSelAttr->value(a)->value();
418 aSelAttr->append(theName, theType);
419 theIndex = aSelAttr->size();
420 aResult = aSelAttr->value(theIndex - 1)->value();
421 if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
422 gp_Trsf aSumTrsf = sumTrsf();
423 #if OCC_VERSION_LARGE < 0x07070000
424 TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
426 TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf, Standard_False);
428 aResult->setImpl(new TopoDS_Shape(anOrigMoved));
433 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::selectionValue(const int theIndex)
435 std::shared_ptr<GeomAPI_Shape> aResult;
436 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
437 if (!aDoc.get()) // the part document is not presented for the moment
440 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
441 aResult = aSelAttr->value(theIndex - 1)->value();
442 if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
443 gp_Trsf aSumTrsf = sumTrsf();
444 #if OCC_VERSION_LARGE < 0x07070000
445 TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
447 TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf, Standard_False);
449 aResult->setImpl(new TopoDS_Shape(anOrigMoved));
454 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
455 std::string& theDefault)
457 theSection = "Visualization";
458 theName = "result_part_color";
459 theDefault = DEFAULT_COLOR();
462 void Model_ResultPart::updateShape()
468 void Model_ResultPart::setTrsf(std::shared_ptr<ModelAPI_Result> theThis,
469 const std::shared_ptr<GeomAPI_Trsf>& theTransformation)
472 if (theTransformation.get()) {
473 myTrsf = std::shared_ptr<gp_Trsf>(new gp_Trsf(theTransformation->impl<gp_Trsf>()));
475 // the result must be explicitly updated
476 static Events_Loop* aLoop = Events_Loop::loop();
477 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
478 ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update
481 std::shared_ptr<GeomAPI_Trsf> Model_ResultPart::summaryTrsf()
483 GeomTrsfPtr aResult(new GeomAPI_Trsf);
484 aResult->setImpl<gp_Trsf>(new gp_Trsf(sumTrsf()));