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 "ModuleBase_Tools.h"
23 #include <ModuleBase_ParamIntSpinBox.h>
24 #include <ModuleBase_ParamSpinBox.h>
25 #include <ModuleBase_WidgetFactory.h>
26 #include <ModuleBase_IWorkshop.h>
27 #include <ModuleBase_IModule.h>
28 #include <ModuleBase_IconFactory.h>
29 #include <ModuleBase_ResultPrs.h>
30 #include <ModuleBase_ViewerPrs.h>
32 #include <ModelAPI_Attribute.h>
33 #include <ModelAPI_AttributeRefAttr.h>
34 #include <ModelAPI_AttributeReference.h>
35 #include <ModelAPI_AttributeSelection.h>
36 #include <ModelAPI_AttributeSelectionList.h>
37 #include <ModelAPI_AttributeRefList.h>
38 #include <ModelAPI_AttributeRefAttrList.h>
39 #include <ModelAPI_ResultPart.h>
40 #include <ModelAPI_ResultConstruction.h>
41 #include <Events_Loop.h>
43 #include <ModelAPI_Data.h>
44 #include <ModelAPI_Result.h>
45 #include <ModelAPI_ResultCompSolid.h>
46 #include <ModelAPI_ResultParameter.h>
47 #include <ModelAPI_Tools.h>
48 #include <ModelAPI_Session.h>
49 #include <ModelAPI_Events.h>
50 #include <ModelAPI_Folder.h>
52 #include <ModelGeomAlgo_Point2D.h>
54 #include <StdSelect_BRepOwner.hxx>
55 #include <TopoDS_Iterator.hxx>
57 #include <GeomDataAPI_Point2D.h>
58 #include <Events_InfoMessage.h>
59 #include <GeomAPI_ShapeExplorer.h>
61 #include <Config_PropManager.h>
62 #include <Config_Translator.h>
64 #include <Prs3d_PointAspect.hxx>
65 #include <Graphic3d_AspectMarker3d.hxx>
67 #include <Image_AlienPixMap.hxx>
73 #include <QDoubleSpinBox>
74 #include <QGraphicsDropShadowEffect>
76 #include <QApplication>
77 #include <QMessageBox>
85 #pragma warning(disable : 4996) // for getenv
88 const double tolerance = 1e-7;
89 const double DEFAULT_DEVIATION_COEFFICIENT = 1.e-4;
91 //#define DEBUG_ACTIVATE_WINDOW
92 //#define DEBUG_SET_FOCUS
100 namespace ModuleBase_Tools {
102 //******************************************************************
104 //******************************************************************
106 void adjustMargins(QWidget* theWidget)
110 adjustMargins(theWidget->layout());
113 void adjustMargins(QLayout* theLayout)
117 theLayout->setContentsMargins(2, 5, 2, 5);
118 theLayout->setSpacing(4);
121 void zeroMargins(QWidget* theWidget)
125 zeroMargins(theWidget->layout());
128 void zeroMargins(QLayout* theLayout)
132 theLayout->setContentsMargins(0, 0, 0, 0);
133 theLayout->setSpacing(5);
136 void activateWindow(QWidget* theWidget, const QString& theInfo)
139 theWidget->activateWindow();
143 #ifdef DEBUG_ACTIVATE_WINDOW
144 qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
148 void setFocus(QWidget* theWidget, const QString& theInfo)
150 activateWindow(theWidget);
151 theWidget->setFocus();
152 // rectangle of focus is not visible on tool button widgets
153 theWidget->repaint();
154 #ifdef DEBUG_SET_FOCUS
155 qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
159 void setShadowEffect(QWidget* theWidget, const bool isSetEffect)
162 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
163 aGlowEffect->setOffset(.0);
164 aGlowEffect->setBlurRadius(10.0);
165 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
166 theWidget->setGraphicsEffect(aGlowEffect);
169 QGraphicsEffect* anEffect = theWidget->graphicsEffect();
171 anEffect->deleteLater();
172 theWidget->setGraphicsEffect(NULL);
176 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
178 QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
179 QImage anAditional(theAdditionalIcon);
184 int anAddWidth = anAditional.width();
185 int anAddHeight = anAditional.height();
187 int aWidth = anIcon.width();
188 int aHeight = anIcon.height();
190 int aStartWidthPos = aWidth - anAddWidth - 1;
191 int aStartHeightPos = aHeight - anAddHeight - 1;
193 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
195 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
197 if (qAlpha(anAditional.pixel(i, j)) > 0)
198 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
201 return QPixmap::fromImage(anIcon);
204 QPixmap lighter(const QString& theIcon, const int theLighterValue)
206 QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
210 QImage aResult = ModuleBase_IconFactory::loadImage(theIcon);
211 for (int i = 0; i < anIcon.width(); i++)
213 for (int j = 0; j < anIcon.height(); j++)
215 QRgb anRgb = anIcon.pixel(i, j);
216 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
217 qAlpha(aResult.pixel(i, j)));
219 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
220 aResult.setPixel(i, j, qRgba(aLighterColor.red(), aLighterColor.green(),
221 aLighterColor.blue(), aLighterColor.alpha()));
224 return QPixmap::fromImage(aResult);
227 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
229 if (theSpin->text() == theText)
231 // In order to avoid extra text setting because it will
232 // reset cursor position in control
233 bool isBlocked = theSpin->blockSignals(true);
234 theSpin->setText(theText);
235 theSpin->blockSignals(isBlocked);
238 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
240 if (fabs(theSpin->value() - theValue) < tolerance)
242 bool isBlocked = theSpin->blockSignals(true);
243 theSpin->setValue(theValue);
244 theSpin->blockSignals(isBlocked);
247 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
249 if (fabs(theSpin->value() - theValue) < tolerance)
251 bool isBlocked = theSpin->blockSignals(true);
252 theSpin->setValue(theValue);
253 theSpin->blockSignals(isBlocked);
256 void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText)
258 // In order to avoid extra text setting because it will
259 // reset cursor position in control
260 if (theSpin->text() == theText)
262 bool isBlocked = theSpin->blockSignals(true);
263 theSpin->setText(theText);
264 theSpin->blockSignals(isBlocked);
267 void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue)
269 if (theSpin->value() == theValue)
271 bool isBlocked = theSpin->blockSignals(true);
272 theSpin->setValue(theValue);
273 theSpin->blockSignals(isBlocked);
276 QAction* createAction(const QIcon& theIcon, const QString& theText,
277 QObject* theParent, const QObject* theReceiver,
278 const char* theMember, const QString& theToolTip,
279 const QString& theStatusTip)
281 QAction* anAction = new QAction(theIcon, theText, theParent);
282 anAction->setToolTip(theToolTip.isEmpty() ? theText : theToolTip);
283 anAction->setStatusTip(!theStatusTip.isEmpty() ? theStatusTip :
284 (!theToolTip.isEmpty() ? theToolTip : theText));
286 QObject::connect(anAction, SIGNAL(triggered(bool)), theReceiver, theMember);
292 QString objectName(const ObjectPtr& theObj)
297 return theObj->data()->name().c_str();
300 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
302 QString aFeatureStr = "feature";
306 std::ostringstream aPtrStr;
307 aPtrStr << "[" << theObj.get() << "]";
309 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
310 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
312 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
313 if (aRes->isDisabled())
314 aFeatureStr.append("[disabled]");
315 if (aRes->isConcealed())
316 aFeatureStr.append("[concealed]");
317 if (ModelAPI_Tools::hasSubResults(aRes))
318 aFeatureStr.append("[hasSubResults]");
320 aFeature = ModelAPI_Feature::feature(aRes);
323 aFeatureStr.append(aPtrStr.str().c_str());
325 if (aFeature.get()) {
326 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
327 if (aFeature->data()->isValid()) {
328 aFeatureStr.append(QString(", name=%1").arg(theObj->data()->name().c_str()).toStdString()
331 if (isUseAttributesInfo) {
332 std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes;
333 std::string aPointsInfo = ModelGeomAlgo_Point2D::getPontAttributesInfo(aFeature,
334 anAttributes).c_str();
335 if (!aPointsInfo.empty())
336 aFeatureStr.append(QString(", attributes: %1")
337 .arg(aPointsInfo.c_str()).toStdString().c_str());
345 typedef QMap<QString, int> ShapeTypes;
346 static ShapeTypes myShapeTypes;
348 int shapeType(const QString& theType)
350 if (myShapeTypes.count() == 0) {
351 myShapeTypes["compound"] = TopAbs_COMPOUND;
352 myShapeTypes["compounds"] = TopAbs_COMPOUND;
353 myShapeTypes["compsolid"] = TopAbs_COMPSOLID;
354 myShapeTypes["compsolids"] = TopAbs_COMPSOLID;
355 myShapeTypes["solid"] = TopAbs_SOLID;
356 myShapeTypes["solids"] = TopAbs_SOLID;
357 myShapeTypes["shell"] = TopAbs_SHELL;
358 myShapeTypes["shells"] = TopAbs_SHELL;
359 myShapeTypes["face"] = TopAbs_FACE;
360 myShapeTypes["faces"] = TopAbs_FACE;
361 myShapeTypes["wire"] = TopAbs_WIRE;
362 myShapeTypes["wires"] = TopAbs_WIRE;
363 myShapeTypes["edge"] = TopAbs_EDGE;
364 myShapeTypes["edges"] = TopAbs_EDGE;
365 myShapeTypes["vertex"] = TopAbs_VERTEX;
366 myShapeTypes["vertices"] = TopAbs_VERTEX;
367 myShapeTypes["object"] = ModuleBase_ResultPrs::Sel_Result;
368 myShapeTypes["objects"] = ModuleBase_ResultPrs::Sel_Result;
370 QString aType = theType.toLower();
371 if(myShapeTypes.contains(aType))
372 return myShapeTypes[aType];
373 Events_InfoMessage("ModuleBase_Tools", "Shape type defined in XML is not implemented!").send();
377 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature,
378 bool& hasParameter, bool& hasCompositeOwner, bool& hasResultInHistory,
383 hasParameter = false;
384 hasCompositeOwner = false;
385 hasResultInHistory = false;
387 foreach(ObjectPtr aObj, theObjects) {
388 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
389 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
390 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aObj);
391 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
393 hasResult |= (aResult.get() != NULL);
394 hasFeature |= (aFeature.get() != NULL);
395 hasFolder |= (aFolder.get() != NULL);
396 hasParameter |= (aConstruction.get() != NULL);
398 hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
400 if (!hasResultInHistory && aResult.get()) {
401 FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
402 hasResultInHistory = aFeature.get() && aFeature->isInHistory();
405 if (hasFeature && hasResult && hasParameter && hasCompositeOwner && hasFeature)
410 /*bool setDefaultDeviationCoefficient(std::shared_ptr<GeomAPI_Shape> theGeomShape)
412 if (!theGeomShape.get())
414 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
415 // correction of deviation for them should not influence to the application performance
416 GeomAPI_ShapeExplorer anExp(theGeomShape, GeomAPI_Shape::FACE);
417 bool anEmpty = anExp.empty();
418 return !anExp.more();
421 /*void setDefaultDeviationCoefficient(const std::shared_ptr<ModelAPI_Result>& theResult,
422 const Handle(Prs3d_Drawer)& theDrawer)
424 if (!theResult.get())
426 bool aUseDeviation = false;
428 std::string aResultGroup = theResult->groupName();
429 if (aResultGroup == ModelAPI_ResultConstruction::group())
430 aUseDeviation = true;
431 else if (aResultGroup == ModelAPI_ResultBody::group()) {
432 GeomShapePtr aGeomShape = theResult->shape();
433 if (aGeomShape.get())
434 aUseDeviation = setDefaultDeviationCoefficient(aGeomShape);
437 theDrawer->SetDeviationCoefficient(DEFAULT_DEVIATION_COEFFICIENT);
440 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
441 const Handle(Prs3d_Drawer)& theDrawer)
443 if (theShape.IsNull())
446 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape());
447 aGeomShape->setImpl(new TopoDS_Shape(theShape));
449 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
450 // correction of deviation for them should not influence to the application performance
451 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
452 bool isConstruction = !anExp.more();
456 aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
458 aDeflection = Config_PropManager::real("Visualization", "body_deflection");
460 theDrawer->SetDeviationCoefficient(aDeflection);
463 Quantity_Color color(const std::string& theSection,
464 const std::string& theName)
466 std::vector<int> aColor = Config_PropManager::color(theSection, theName);
467 return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
470 ObjectPtr getObject(const AttributePtr& theAttribute)
473 std::string anAttrType = theAttribute->attributeType();
474 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
475 AttributeRefAttrPtr anAttr =
476 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
477 if (anAttr != NULL && anAttr->isObject())
478 anObject = anAttr->object();
480 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
481 AttributeSelectionPtr anAttr =
482 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
484 anObject = anAttr->context();
486 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
487 AttributeReferencePtr anAttr =
488 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
489 if (anAttr.get() != NULL)
490 anObject = anAttr->value();
495 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
497 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
499 // for compounds check sub-shapes: it may be compound of needed type:
500 // Booleans may produce compounds of Solids
501 if (aShapeType == TopAbs_COMPOUND) {
502 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
503 if (!aSubs.Value().IsNull()) {
504 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
505 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
506 aShapeType = TopAbs_COMPOUND;
509 if (aShapeType == TopAbs_COMPOUND) {
510 aShapeType = aSubType;
511 } else if (aShapeType != aSubType) { // compound of shapes of different types
512 aShapeType = TopAbs_COMPOUND;
521 TopoDS_Shape getSelectedShape(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
523 if (thePrs->shape().get())
524 return thePrs->shape()->impl<TopoDS_Shape>();
526 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(thePrs->owner());
527 if (!anOwner.IsNull())
528 return anOwner->Shape();
530 return TopoDS_Shape();
533 void getParameters(QStringList& theParameters)
535 theParameters.clear();
537 SessionPtr aSession = ModelAPI_Session::get();
538 std::list<DocumentPtr> aDocList;
539 DocumentPtr anActiveDocument = aSession->activeDocument();
540 DocumentPtr aRootDocument = aSession->moduleDocument();
541 aDocList.push_back(anActiveDocument);
542 if (anActiveDocument != aRootDocument) {
543 aDocList.push_back(aRootDocument);
545 std::string aGroupId = ModelAPI_ResultParameter::group();
546 for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
547 DocumentPtr aDocument = *it;
548 int aSize = aDocument->size(aGroupId);
549 for (int i = 0; i < aSize; i++) {
550 ObjectPtr anObject = aDocument->object(aGroupId, i);
551 std::string aParameterName = anObject->data()->name();
552 theParameters.append(aParameterName.c_str());
557 std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop,
558 const FeaturePtr& theFeature)
560 std::string anAttributeId;
562 std::string aXmlCfg, aDescription;
563 theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
565 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
566 std::string anAttributeTitle;
567 aFactory.getGreedAttribute(anAttributeId);
569 return anAttributeId;
572 bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
573 const std::shared_ptr<GeomAPI_Shape>& theShape,
574 ModuleBase_IWorkshop* theWorkshop,
575 const bool theTemporarily)
577 bool aHasObject = false;
578 if (!theAttribute.get())
581 std::string aType = theAttribute->attributeType();
582 if (aType == ModelAPI_AttributeReference::typeId()) {
583 AttributeReferencePtr aRef =
584 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
585 ObjectPtr aObject = aRef->value();
586 aHasObject = aObject && aObject->isSame(theObject);
587 //if (!(aObject && aObject->isSame(theObject))) {
588 // aRef->setValue(theObject);
590 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
591 AttributeRefAttrPtr aRefAttr =
592 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
594 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
595 if (anAttribute.get()) {
596 //aRefAttr->setAttr(anAttribute);
599 ObjectPtr aObject = aRefAttr->object();
600 aHasObject = aObject && aObject->isSame(theObject);
601 //if (!(aObject && aObject->isSame(theObject))) {
602 // aRefAttr->setObject(theObject);
605 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
606 /*AttributeSelectionPtr aSelectAttr =
607 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
608 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
609 if (aSelectAttr.get() != NULL) {
610 aSelectAttr->setValue(aResult, theShape, theTemporarily);
613 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
614 AttributeSelectionListPtr aSelectionListAttr =
615 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
616 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
617 aHasObject = aSelectionListAttr->isInList(aResult, theShape, theTemporarily);
619 else if (aType == ModelAPI_AttributeRefList::typeId()) {
620 AttributeRefListPtr aRefListAttr =
621 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
622 aHasObject = aRefListAttr->isInList(theObject);
623 //if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject))
624 // aRefListAttr->append(theObject);
626 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
627 AttributeRefAttrListPtr aRefAttrListAttr =
628 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
629 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
631 if (anAttribute.get()) {
632 aHasObject = aRefAttrListAttr->isInList(anAttribute);
633 //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
634 // aRefAttrListAttr->append(anAttribute);
637 aHasObject = aRefAttrListAttr->isInList(theObject);
638 //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
639 // aRefAttrListAttr->append(theObject);
645 bool setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
646 const GeomShapePtr& theShape, ModuleBase_IWorkshop* theWorkshop,
647 const bool theTemporarily, const bool theCheckIfAttributeHasObject)
649 if (!theAttribute.get())
653 std::string aType = theAttribute->attributeType();
654 if (aType == ModelAPI_AttributeReference::typeId()) {
655 AttributeReferencePtr aRef =
656 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
657 ObjectPtr aObject = aRef->value();
658 if (!(aObject && aObject->isSame(theObject))) {
659 aRef->setValue(theObject);
661 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
662 AttributeRefAttrPtr aRefAttr =
663 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
665 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
666 if (anAttribute.get())
667 aRefAttr->setAttr(anAttribute);
669 ObjectPtr aObject = aRefAttr->object();
670 if (!(aObject && aObject->isSame(theObject))) {
671 aRefAttr->setObject(theObject);
674 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
675 AttributeSelectionPtr aSelectAttr =
676 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
677 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
678 if (aSelectAttr.get() != NULL) {
679 aSelectAttr->setValue(aResult, theShape, theTemporarily);
682 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
683 AttributeSelectionListPtr aSelectionListAttr =
684 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
685 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
686 if (!theCheckIfAttributeHasObject ||
687 !aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
688 aSelectionListAttr->append(aResult, theShape, theTemporarily);
690 else if (aType == ModelAPI_AttributeRefList::typeId()) {
691 AttributeRefListPtr aRefListAttr =
692 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
693 if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject)) {
695 aRefListAttr->append(theObject);
700 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
701 AttributeRefAttrListPtr aRefAttrListAttr =
702 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
703 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
705 if (anAttribute.get()) {
706 if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
707 aRefAttrListAttr->append(anAttribute);
710 if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject)) {
712 aRefAttrListAttr->append(theObject);
721 GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* theWorkshop)
724 if (!theAttribute.get())
727 std::string aType = theAttribute->attributeType();
728 if (aType == ModelAPI_AttributeReference::typeId()) {
729 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
730 AttributeRefAttrPtr aRefAttr =
731 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
732 if (aRefAttr.get() && !aRefAttr->isObject()) {
733 AttributePtr anAttribute = aRefAttr->attr();
734 aShape = theWorkshop->module()->findShape(anAttribute);
736 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
737 AttributeSelectionPtr aSelectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
739 aShape = aSelectAttr->value();
741 else // Geom2D point processing
742 aShape = theWorkshop->module()->findShape(theAttribute);
746 void flushUpdated(ObjectPtr theObject)
748 blockUpdateViewer(true);
750 // Fix the problem of not previewed results of constraints applied. Flush Create/Delete
751 // (for the sketch result) to start processing of the sketch in the solver.
752 // TODO: these flushes should be moved in a separate method provided by Model
753 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
754 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
755 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
757 blockUpdateViewer(false);
760 void blockUpdateViewer(const bool theValue)
762 // the viewer update should be blocked in order to avoid the temporary feature content
763 // when the solver processes the feature, the redisplay message can be flushed
764 // what caused the display in the viewer preliminary states of object
765 // e.g. fillet feature, angle value change
766 std::shared_ptr<Events_Message> aMsg;
768 aMsg = std::shared_ptr<Events_Message>(
769 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
772 // the viewer update should be unblocked
773 aMsg = std::shared_ptr<Events_Message>(
774 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
776 Events_Loop::loop()->send(aMsg);
779 QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
780 int theMaxLineInPixels)
782 static QFontMetrics tfm(theWidget ? theWidget->font() : QApplication::font());
783 static qreal phi = 2.618;
785 QRect aBounds = tfm.boundingRect(theValue);
786 if(aBounds.width() <= theMaxLineInPixels)
789 qreal s = aBounds.width() * aBounds.height();
790 qreal aGoldWidth = sqrt(s*phi);
792 QStringList aWords = theValue.split(" ", QString::SkipEmptyParts);
794 int n = aWords.count();
796 for (int i = 0; i < n; i++) {
797 QString aLineExt = aLine + " " + aWords[i];
798 qreal anWidthNonExt = tfm.boundingRect(aLine).width();
799 qreal anWidthExt = tfm.boundingRect(aLineExt).width();
800 qreal aDeltaNonExt = fabs(anWidthNonExt-aGoldWidth);
801 qreal aDeltaExt = fabs(anWidthExt-aGoldWidth);
802 if(aDeltaNonExt < aDeltaExt) {
804 aLines.append(aLine);
812 aLines.append(aLine);
814 QString aResult = aLines.join("\n");
818 //**************************************************************
819 QLocale doubleLocale()
821 // VSR 01/07/2010: Disable thousands separator for spin box
822 // (to avoid inconsistency of double-2-string and string-2-double conversion)
824 aLocale.setNumberOptions(aLocale.numberOptions() |
825 QLocale::OmitGroupSeparator |
826 QLocale::RejectGroupSeparator);
830 //**************************************************************
831 void refsToFeatureInFeatureDocument(const ObjectPtr& theObject,
832 std::set<FeaturePtr>& theRefFeatures)
834 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
835 if (aFeature.get()) {
836 DocumentPtr aFeatureDoc = aFeature->document();
837 // 1. find references in the current document
838 aFeatureDoc->refsToFeature(aFeature, theRefFeatures, false);
843 //**************************************************************
844 /*bool isSubOfComposite(const ObjectPtr& theObject)
847 std::set<FeaturePtr> aRefFeatures;
848 refsToFeatureInFeatureDocument(theObject, aRefFeatures);
849 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
850 aLast = aRefFeatures.end();
851 for (; anIt != aLast && !isSub; anIt++) {
852 isSub = isSubOfComposite(theObject, *anIt);
857 //**************************************************************
858 /*bool isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature)
861 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
862 if (aComposite.get()) {
863 isSub = aComposite->isSub(theObject);
864 // the recursive is possible, the parameters are sketch circle and extrusion cut. They are
865 // separated by composite sketch feature
867 int aNbSubs = aComposite->numberOfSubs();
868 for (int aSub = 0; aSub < aNbSubs && !isSub; aSub++) {
869 isSub = isSubOfComposite(theObject, aComposite->subFeature(aSub));
876 //**************************************************************
877 ResultPtr firstResult(const ObjectPtr& theObject)
879 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
880 if (!aResult.get()) {
881 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
883 aResult = aFeature->firstResult();
888 //**************************************************************
889 bool isFeatureOfResult(const FeaturePtr& theFeature, const std::string& theGroupOfResult)
891 bool isResult = false;
893 if (!theFeature->data()->isValid())
896 ResultPtr aFirstResult = theFeature->firstResult();
897 if (!aFirstResult.get())
900 return aFirstResult->groupName() == theGroupOfResult;
903 //**************************************************************
904 bool hasModuleDocumentFeature(const std::set<FeaturePtr>& theFeatures)
906 bool aFoundModuleDocumentObject = false;
907 DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
909 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
910 for (; anIt != aLast && !aFoundModuleDocumentObject; anIt++) {
911 FeaturePtr aFeature = *anIt;
912 ResultPtr aResult = ModuleBase_Tools::firstResult(aFeature);
913 if (aResult.get() && aResult->groupName() == ModelAPI_ResultPart::group())
915 aFoundModuleDocumentObject = aFeature->document() == aModuleDoc;
918 return aFoundModuleDocumentObject;
921 //**************************************************************
922 bool askToDelete(const std::set<FeaturePtr> theFeatures,
923 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
925 std::set<FeaturePtr>& theReferencesToDelete,
926 const std::string& thePrefixInfo)
928 QString aNotActivatedDocWrn;
929 std::string aNotActivatedNames;
930 if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
931 if (ModuleBase_Tools::hasModuleDocumentFeature(theFeatures))
932 aNotActivatedDocWrn =
933 QObject::tr("Selected objects can be used in Part documents which are not loaded:%1.\n")
934 .arg(aNotActivatedNames.c_str());
937 std::set<FeaturePtr> aFeaturesRefsTo;
938 std::set<FeaturePtr> aFeaturesRefsToParameter;
939 std::set<FeaturePtr> aParameterFeatures;
940 QStringList aPartFeatureNames;
941 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
942 aLast = theFeatures.end();
943 // separate features to references to parameter features and references to others
944 for (; anIt != aLast; anIt++) {
945 FeaturePtr aFeature = *anIt;
946 if (theReferences.find(aFeature) == theReferences.end())
949 if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
950 aPartFeatureNames.append(aFeature->name().c_str());
952 std::set<FeaturePtr> aRefFeatures;
953 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
954 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
955 for (; aRefIt != aRefLast; aRefIt++) {
956 FeaturePtr aRefFeature = *aRefIt;
957 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
958 aRefFeatures.find(aRefFeature) == aRefFeatures.end()) // it is not added
959 aRefFeatures.insert(aRefFeature);
962 if (isFeatureOfResult(aFeature, ModelAPI_ResultParameter::group())) {
963 aFeaturesRefsToParameter.insert(aRefFeatures.begin(), aRefFeatures.end());
964 aParameterFeatures.insert(aFeature);
967 theReferencesToDelete.insert(aRefFeatures.begin(), aRefFeatures.end());
971 std::set<FeaturePtr> aFeaturesRefsToParameterOnly;
972 anIt = aFeaturesRefsToParameter.begin();
973 aLast = aFeaturesRefsToParameter.end();
974 // separate features to references to parameter features and references to others
975 QStringList aParamFeatureNames;
976 for (; anIt != aLast; anIt++) {
977 FeaturePtr aFeature = *anIt;
978 if (theReferencesToDelete.find(aFeature) == theReferencesToDelete.end()) {
979 aFeaturesRefsToParameterOnly.insert(aFeature);
980 aParamFeatureNames.append(aFeature->name().c_str());
983 aParamFeatureNames.sort();
984 QStringList anOtherFeatureNames;
985 anIt = theReferencesToDelete.begin();
986 aLast = theReferencesToDelete.end();
987 for (; anIt != aLast; anIt++) {
988 FeaturePtr aFeature = *anIt;
989 if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
990 aPartFeatureNames.append(aFeature->name().c_str());
992 anOtherFeatureNames.append(aFeature->name().c_str());
994 aPartFeatureNames.sort();
995 anOtherFeatureNames.sort();
997 bool aCanReplaceParameters = !aFeaturesRefsToParameterOnly.empty();
999 QMessageBox aMessageBox(theParent);
1000 aMessageBox.setWindowTitle(QObject::tr("Delete features"));
1001 aMessageBox.setIcon(QMessageBox::Warning);
1002 aMessageBox.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
1003 aMessageBox.setDefaultButton(QMessageBox::No);
1006 if (!thePrefixInfo.empty())
1007 aText = thePrefixInfo.c_str();
1008 QString aSep = ", ";
1009 if (!aPartFeatureNames.empty()) {
1010 aText += QString(QObject::tr("The following parts will be deleted: %1.\n"))
1011 .arg(aPartFeatureNames.join(aSep));
1013 if (!aNotActivatedDocWrn.isEmpty())
1014 aText += aNotActivatedDocWrn;
1015 if (!anOtherFeatureNames.empty()) {
1016 const char* aMsg = "Features are used in the following features: %1.\nThese "
1017 "features will be deleted.\n";
1018 aText += QString(QObject::tr(aMsg))
1019 .arg(anOtherFeatureNames.join(aSep));
1021 if (!aParamFeatureNames.empty()) {
1022 const char* aMsg = "Parameters are used in the following features: %1.\nThese features will "
1023 "be deleted.\nOr parameters could be replaced by their values.\n";
1024 aText += QString(QObject::tr(aMsg))
1025 .arg(aParamFeatureNames.join(aSep));
1026 QPushButton *aReplaceButton =
1027 aMessageBox.addButton(QObject::tr("Replace"), QMessageBox::ActionRole);
1030 if (!aText.isEmpty()) {
1031 aText += "Would you like to continue?";
1032 aMessageBox.setText(aText);
1034 QMessageBox::ButtonRole aButtonRole = aMessageBox.buttonRole(aMessageBox.clickedButton());
1036 if (aButtonRole == QMessageBox::NoRole)
1039 if (aButtonRole == QMessageBox::ActionRole) {
1040 foreach (FeaturePtr aObj, aParameterFeatures)
1041 ModelAPI_ReplaceParameterMessage::send(aObj, 0);
1044 theReferencesToDelete.insert(aFeaturesRefsToParameterOnly.begin(),
1045 aFeaturesRefsToParameterOnly.end());
1050 //**************************************************************
1051 void convertToFeatures(const QObjectPtrList& theObjects, std::set<FeaturePtr>& theFeatures)
1053 QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
1054 for(; anIt != aLast; anIt++) {
1055 ObjectPtr anObject = *anIt;
1056 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
1057 // for parameter result, use the corresponded reature to be removed
1058 if (!aFeature.get() && anObject->groupName() == ModelAPI_ResultParameter::group()) {
1059 aFeature = ModelAPI_Feature::feature(anObject);
1062 theFeatures.insert(aFeature);
1066 //**************************************************************
1067 void convertToFolders(const QObjectPtrList& theObjects,
1068 std::set<FolderPtr>& theFolders)
1070 QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
1071 for(; anIt != aLast; anIt++) {
1072 ObjectPtr anObject = *anIt;
1073 FolderPtr aFeature = std::dynamic_pointer_cast<ModelAPI_Folder>(anObject);
1075 theFolders.insert(aFeature);
1080 //**************************************************************
1081 QString translate(const Events_InfoMessage& theMessage)
1085 if (!theMessage.empty()) {
1086 std::string aStr = Config_Translator::translate(theMessage);
1087 if (!aStr.empty()) {
1088 std::string aCodec = Config_Translator::codec(theMessage);
1089 aMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
1096 QString translate(const std::string& theContext, const std::string& theMessage)
1100 if (!theMessage.empty()) {
1101 std::string aStr = Config_Translator::translate(theContext, theMessage);
1102 if (!aStr.empty()) {
1103 std::string aCodec = Config_Translator::codec(theContext);
1104 aMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
1111 void setPointBallHighlighting(AIS_Shape* theAIS)
1113 static Handle(Image_AlienPixMap) aPixMap;
1114 if(aPixMap.IsNull()) {
1115 // Load icon for the presentation
1117 char* anEnv = getenv("SHAPER_ROOT_DIR");
1119 aFile = std::string(anEnv) +
1120 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
1122 anEnv = getenv("OPENPARTS_ROOT_DIR");
1124 aFile = std::string(anEnv) + FSEP + "resources";
1128 static const std::string aMarkerName = "marker_dot.png";
1129 aFile += aMarkerName;
1130 aPixMap = new Image_AlienPixMap();
1131 if(!aPixMap->Load(aFile.c_str())) {
1132 // The icon for constraint is not found
1133 static const std::string aMsg =
1134 "Error: Point market not found by path: \"" + aFile + "\". Falling back.";
1135 //Events_InfoMessage("ModuleBase_Tools::setPointBallHighlighting", aMsg).send();
1139 Handle(Graphic3d_AspectMarker3d) anAspect;
1140 Handle(Prs3d_Drawer) aDrawer = theAIS->HilightAttributes();
1142 // to do: implement ball highlighting, in 7.2.0 this drawer is NULL
1144 if(aDrawer->HasOwnPointAspect()) {
1145 Handle(Prs3d_PointAspect) aPntAspect = aDrawer->PointAspect();
1146 if(aPixMap->IsEmpty()) {
1147 anAspect = aPntAspect->Aspect();
1148 anAspect->SetType(Aspect_TOM_BALL);
1150 if(aPixMap->Format() == Image_PixMap::ImgGray) {
1151 aPixMap->SetFormat (Image_PixMap::ImgAlpha);
1152 } else if(aPixMap->Format() == Image_PixMap::ImgGrayF) {
1153 aPixMap->SetFormat (Image_PixMap::ImgAlphaF);
1155 anAspect = new Graphic3d_AspectMarker3d(aPixMap);
1156 aPntAspect->SetAspect(anAspect);
1158 aDrawer->SetPointAspect(aPntAspect);
1159 theAIS->SetHilightAttributes(aDrawer);
1164 } // namespace ModuleBase_Tools