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_IViewer.h>
29 #include <ModuleBase_IconFactory.h>
30 #include <ModuleBase_ResultPrs.h>
31 #include <ModuleBase_ViewerPrs.h>
33 #include <ModelAPI_Attribute.h>
34 #include <ModelAPI_AttributeRefAttr.h>
35 #include <ModelAPI_AttributeReference.h>
36 #include <ModelAPI_AttributeSelection.h>
37 #include <ModelAPI_AttributeSelectionList.h>
38 #include <ModelAPI_AttributeRefList.h>
39 #include <ModelAPI_AttributeRefAttrList.h>
40 #include <ModelAPI_ResultPart.h>
41 #include <ModelAPI_ResultConstruction.h>
42 #include <ModelAPI_AttributeString.h>
43 #include <ModelAPI_Expression.h>
44 #include <Events_Loop.h>
46 #include <ModelAPI_Data.h>
47 #include <ModelAPI_Result.h>
48 #include <ModelAPI_ResultParameter.h>
49 #include <ModelAPI_Tools.h>
50 #include <ModelAPI_Session.h>
51 #include <ModelAPI_Events.h>
52 #include <ModelAPI_Folder.h>
54 #include <ModelGeomAlgo_Point2D.h>
56 #include <StdSelect_BRepOwner.hxx>
57 #include <TopoDS_Iterator.hxx>
58 #include <AIS_InteractiveContext.hxx>
59 #include <Prs3d_LineAspect.hxx>
60 #include <Prs3d_PlaneAspect.hxx>
62 #include <GeomDataAPI_Point2D.h>
63 #include <Events_InfoMessage.h>
64 #include <GeomAPI_ShapeExplorer.h>
66 #include <Config_PropManager.h>
67 #include <Config_Translator.h>
69 #include <Prs3d_PointAspect.hxx>
70 #include <Graphic3d_AspectMarker3d.hxx>
72 #include <Image_AlienPixMap.hxx>
78 #include <QDoubleSpinBox>
79 #include <QGraphicsDropShadowEffect>
81 #include <QApplication>
82 #include <QMessageBox>
90 #pragma warning(disable : 4996) // for getenv
93 const double tolerance = 1e-7;
94 const double DEFAULT_DEVIATION_COEFFICIENT = 1.e-4;
96 //#define DEBUG_ACTIVATE_WINDOW
97 //#define DEBUG_SET_FOCUS
105 namespace ModuleBase_Tools {
107 //******************************************************************
109 //******************************************************************
111 void adjustMargins(QWidget* theWidget)
115 adjustMargins(theWidget->layout());
118 void adjustMargins(QLayout* theLayout)
122 theLayout->setContentsMargins(2, 5, 2, 5);
123 theLayout->setSpacing(4);
126 void zeroMargins(QWidget* theWidget)
130 zeroMargins(theWidget->layout());
133 void zeroMargins(QLayout* theLayout)
137 theLayout->setContentsMargins(0, 0, 0, 0);
138 theLayout->setSpacing(5);
141 void activateWindow(QWidget* theWidget, const QString& theInfo)
144 theWidget->activateWindow();
148 #ifdef DEBUG_ACTIVATE_WINDOW
149 qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
153 void setFocus(QWidget* theWidget, const QString& theInfo)
155 activateWindow(theWidget);
156 theWidget->setFocus();
157 // rectangle of focus is not visible on tool button widgets
158 theWidget->repaint();
159 #ifdef DEBUG_SET_FOCUS
160 qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
164 void setShadowEffect(QWidget* theWidget, const bool isSetEffect)
167 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
168 aGlowEffect->setOffset(.0);
169 aGlowEffect->setBlurRadius(10.0);
170 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
171 theWidget->setGraphicsEffect(aGlowEffect);
174 QGraphicsEffect* anEffect = theWidget->graphicsEffect();
176 anEffect->deleteLater();
177 theWidget->setGraphicsEffect(NULL);
181 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
183 QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
184 QImage anAditional(theAdditionalIcon);
189 int anAddWidth = anAditional.width();
190 int anAddHeight = anAditional.height();
192 int aWidth = anIcon.width();
193 int aHeight = anIcon.height();
195 int aStartWidthPos = aWidth - anAddWidth - 1;
196 int aStartHeightPos = aHeight - anAddHeight - 1;
198 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
200 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
202 if (qAlpha(anAditional.pixel(i, j)) > 0)
203 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
206 return QPixmap::fromImage(anIcon);
209 QPixmap lighter(const QString& theIcon, const int theLighterValue)
211 QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
215 QImage aResult = ModuleBase_IconFactory::loadImage(theIcon);
216 for (int i = 0; i < anIcon.width(); i++)
218 for (int j = 0; j < anIcon.height(); j++)
220 QRgb anRgb = anIcon.pixel(i, j);
221 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
222 qAlpha(aResult.pixel(i, j)));
224 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
225 aResult.setPixel(i, j, qRgba(aLighterColor.red(), aLighterColor.green(),
226 aLighterColor.blue(), aLighterColor.alpha()));
229 return QPixmap::fromImage(aResult);
232 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
234 if (theSpin->text() == theText)
236 // In order to avoid extra text setting because it will
237 // reset cursor position in control
238 bool isBlocked = theSpin->blockSignals(true);
239 theSpin->setText(theText);
240 theSpin->blockSignals(isBlocked);
243 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
245 if (fabs(theSpin->value() - theValue) < tolerance)
247 bool isBlocked = theSpin->blockSignals(true);
248 theSpin->setValue(theValue);
249 theSpin->blockSignals(isBlocked);
252 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
254 if (fabs(theSpin->value() - theValue) < tolerance)
256 bool isBlocked = theSpin->blockSignals(true);
257 theSpin->setValue(theValue);
258 theSpin->blockSignals(isBlocked);
261 void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText)
263 // In order to avoid extra text setting because it will
264 // reset cursor position in control
265 if (theSpin->text() == theText)
267 bool isBlocked = theSpin->blockSignals(true);
268 theSpin->setText(theText);
269 theSpin->blockSignals(isBlocked);
272 void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue)
274 if (theSpin->value() == theValue)
276 bool isBlocked = theSpin->blockSignals(true);
277 theSpin->setValue(theValue);
278 theSpin->blockSignals(isBlocked);
281 QAction* createAction(const QIcon& theIcon, const QString& theText,
282 QObject* theParent, const QObject* theReceiver,
283 const char* theMember, const QString& theToolTip,
284 const QString& theStatusTip)
286 QAction* anAction = new QAction(theIcon, theText, theParent);
287 anAction->setToolTip(theToolTip.isEmpty() ? theText : theToolTip);
288 anAction->setStatusTip(!theStatusTip.isEmpty() ? theStatusTip :
289 (!theToolTip.isEmpty() ? theToolTip : theText));
291 QObject::connect(anAction, SIGNAL(triggered(bool)), theReceiver, theMember);
297 QString objectName(const ObjectPtr& theObj)
302 return theObj->data()->name().c_str();
305 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
307 QString aFeatureStr = "feature";
311 std::ostringstream aPtrStr;
312 aPtrStr << "[" << theObj.get() << "]";
314 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
315 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
317 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
318 if (aRes->isDisabled())
319 aFeatureStr.append("[disabled]");
320 if (aRes->isConcealed())
321 aFeatureStr.append("[concealed]");
322 if (ModelAPI_Tools::hasSubResults(aRes))
323 aFeatureStr.append("[hasSubResults]");
325 aFeature = ModelAPI_Feature::feature(aRes);
328 aFeatureStr.append(aPtrStr.str().c_str());
330 if (aFeature.get()) {
331 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
332 if (aFeature->data()->isValid()) {
333 aFeatureStr.append(QString(", name=%1").arg(theObj->data()->name().c_str()).toStdString()
336 if (isUseAttributesInfo) {
337 std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes;
338 std::string aPointsInfo = ModelGeomAlgo_Point2D::getPontAttributesInfo(aFeature,
339 anAttributes).c_str();
340 if (!aPointsInfo.empty())
341 aFeatureStr.append(QString(", attributes: %1")
342 .arg(aPointsInfo.c_str()).toStdString().c_str());
350 typedef QMap<QString, int> ShapeTypes;
351 static ShapeTypes myShapeTypes;
353 int shapeType(const QString& theType)
355 if (myShapeTypes.count() == 0) {
356 myShapeTypes["compound"] = TopAbs_COMPOUND;
357 myShapeTypes["compounds"] = TopAbs_COMPOUND;
358 myShapeTypes["compsolid"] = TopAbs_COMPSOLID;
359 myShapeTypes["compsolids"] = TopAbs_COMPSOLID;
360 myShapeTypes["solid"] = TopAbs_SOLID;
361 myShapeTypes["solids"] = TopAbs_SOLID;
362 myShapeTypes["shell"] = TopAbs_SHELL;
363 myShapeTypes["shells"] = TopAbs_SHELL;
364 myShapeTypes["face"] = TopAbs_FACE;
365 myShapeTypes["faces"] = TopAbs_FACE;
366 myShapeTypes["wire"] = TopAbs_WIRE;
367 myShapeTypes["wires"] = TopAbs_WIRE;
368 myShapeTypes["edge"] = TopAbs_EDGE;
369 myShapeTypes["edges"] = TopAbs_EDGE;
370 myShapeTypes["vertex"] = TopAbs_VERTEX;
371 myShapeTypes["vertices"] = TopAbs_VERTEX;
372 myShapeTypes["object"] = ModuleBase_ResultPrs::Sel_Result;
373 myShapeTypes["objects"] = ModuleBase_ResultPrs::Sel_Result;
375 QString aType = theType.toLower();
376 if(myShapeTypes.contains(aType))
377 return myShapeTypes[aType];
378 Events_InfoMessage("ModuleBase_Tools", "Shape type defined in XML is not implemented!").send();
382 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature,
383 bool& hasParameter, bool& hasCompositeOwner, bool& hasResultInHistory,
388 hasParameter = false;
389 hasCompositeOwner = false;
390 hasResultInHistory = false;
392 foreach(ObjectPtr aObj, theObjects) {
393 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
394 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
395 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aObj);
396 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
398 hasResult |= (aResult.get() != NULL);
399 hasFeature |= (aFeature.get() != NULL);
400 hasFolder |= (aFolder.get() != NULL);
401 hasParameter |= (aConstruction.get() != NULL);
403 hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
405 if (!hasResultInHistory && aResult.get()) {
406 FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
407 hasResultInHistory = aFeature.get() && aFeature->isInHistory();
410 if (hasFeature && hasResult && hasParameter && hasCompositeOwner && hasFeature)
415 /*bool setDefaultDeviationCoefficient(std::shared_ptr<GeomAPI_Shape> theGeomShape)
417 if (!theGeomShape.get())
419 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
420 // correction of deviation for them should not influence to the application performance
421 GeomAPI_ShapeExplorer anExp(theGeomShape, GeomAPI_Shape::FACE);
422 bool anEmpty = anExp.empty();
423 return !anExp.more();
426 /*void setDefaultDeviationCoefficient(const std::shared_ptr<ModelAPI_Result>& theResult,
427 const Handle(Prs3d_Drawer)& theDrawer)
429 if (!theResult.get())
431 bool aUseDeviation = false;
433 std::string aResultGroup = theResult->groupName();
434 if (aResultGroup == ModelAPI_ResultConstruction::group())
435 aUseDeviation = true;
436 else if (aResultGroup == ModelAPI_ResultBody::group()) {
437 GeomShapePtr aGeomShape = theResult->shape();
438 if (aGeomShape.get())
439 aUseDeviation = setDefaultDeviationCoefficient(aGeomShape);
442 theDrawer->SetDeviationCoefficient(DEFAULT_DEVIATION_COEFFICIENT);
445 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
446 const Handle(Prs3d_Drawer)& theDrawer)
448 if (theShape.IsNull())
451 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape());
452 aGeomShape->setImpl(new TopoDS_Shape(theShape));
454 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
455 // correction of deviation for them should not influence to the application performance
456 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
457 bool isConstruction = !anExp.more();
461 aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
463 aDeflection = Config_PropManager::real("Visualization", "body_deflection");
465 theDrawer->SetDeviationCoefficient(aDeflection);
468 Quantity_Color color(const std::string& theSection,
469 const std::string& theName)
471 std::vector<int> aColor = Config_PropManager::color(theSection, theName);
472 return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
475 ObjectPtr getObject(const AttributePtr& theAttribute)
478 std::string anAttrType = theAttribute->attributeType();
479 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
480 AttributeRefAttrPtr anAttr =
481 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
482 if (anAttr != NULL && anAttr->isObject())
483 anObject = anAttr->object();
485 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
486 AttributeSelectionPtr anAttr =
487 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
489 anObject = anAttr->context();
491 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
492 AttributeReferencePtr anAttr =
493 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
494 if (anAttr.get() != NULL)
495 anObject = anAttr->value();
500 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
502 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
504 // for compounds check sub-shapes: it may be compound of needed type:
505 // Booleans may produce compounds of Solids
506 if (aShapeType == TopAbs_COMPOUND) {
507 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
508 if (!aSubs.Value().IsNull()) {
509 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
510 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
511 aShapeType = TopAbs_COMPOUND;
514 if (aShapeType == TopAbs_COMPOUND) {
515 aShapeType = aSubType;
516 } else if (aShapeType != aSubType) { // compound of shapes of different types
517 aShapeType = TopAbs_COMPOUND;
526 TopoDS_Shape getSelectedShape(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
528 if (thePrs->shape().get())
529 return thePrs->shape()->impl<TopoDS_Shape>();
531 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(thePrs->owner());
532 if (!anOwner.IsNull())
533 return anOwner->Shape();
535 return TopoDS_Shape();
538 void getParameters(QStringList& theParameters)
540 theParameters.clear();
542 SessionPtr aSession = ModelAPI_Session::get();
543 std::list<DocumentPtr> aDocList;
544 DocumentPtr anActiveDocument = aSession->activeDocument();
545 DocumentPtr aRootDocument = aSession->moduleDocument();
546 aDocList.push_back(anActiveDocument);
547 if (anActiveDocument != aRootDocument) {
548 aDocList.push_back(aRootDocument);
550 std::string aGroupId = ModelAPI_ResultParameter::group();
551 for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
552 DocumentPtr aDocument = *it;
553 int aSize = aDocument->size(aGroupId);
554 for (int i = 0; i < aSize; i++) {
555 ObjectPtr anObject = aDocument->object(aGroupId, i);
556 std::string aParameterName = anObject->data()->name();
557 theParameters.append(aParameterName.c_str());
562 std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop,
563 const FeaturePtr& theFeature)
565 std::string anAttributeId;
567 std::string aXmlCfg, aDescription;
568 theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
570 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
571 std::string anAttributeTitle;
572 aFactory.getGreedAttribute(anAttributeId);
574 return anAttributeId;
577 bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
578 const std::shared_ptr<GeomAPI_Shape>& theShape,
579 ModuleBase_IWorkshop* theWorkshop,
580 const bool theTemporarily)
582 bool aHasObject = false;
583 if (!theAttribute.get())
586 std::string aType = theAttribute->attributeType();
587 if (aType == ModelAPI_AttributeReference::typeId()) {
588 AttributeReferencePtr aRef =
589 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
590 ObjectPtr aObject = aRef->value();
591 aHasObject = aObject && aObject->isSame(theObject);
592 //if (!(aObject && aObject->isSame(theObject))) {
593 // aRef->setValue(theObject);
595 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
596 AttributeRefAttrPtr aRefAttr =
597 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
599 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
600 if (anAttribute.get()) {
601 //aRefAttr->setAttr(anAttribute);
604 ObjectPtr aObject = aRefAttr->object();
605 aHasObject = aObject && aObject->isSame(theObject);
606 //if (!(aObject && aObject->isSame(theObject))) {
607 // aRefAttr->setObject(theObject);
610 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
611 /*AttributeSelectionPtr aSelectAttr =
612 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
613 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
614 if (aSelectAttr.get() != NULL) {
615 aSelectAttr->setValue(aResult, theShape, theTemporarily);
618 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
619 AttributeSelectionListPtr aSelectionListAttr =
620 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
621 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
622 aHasObject = aSelectionListAttr->isInList(aResult, theShape, theTemporarily);
624 else if (aType == ModelAPI_AttributeRefList::typeId()) {
625 AttributeRefListPtr aRefListAttr =
626 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
627 aHasObject = aRefListAttr->isInList(theObject);
628 //if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject))
629 // aRefListAttr->append(theObject);
631 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
632 AttributeRefAttrListPtr aRefAttrListAttr =
633 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
634 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
636 if (anAttribute.get()) {
637 aHasObject = aRefAttrListAttr->isInList(anAttribute);
638 //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
639 // aRefAttrListAttr->append(anAttribute);
642 aHasObject = aRefAttrListAttr->isInList(theObject);
643 //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
644 // aRefAttrListAttr->append(theObject);
650 bool setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
651 const GeomShapePtr& theShape, ModuleBase_IWorkshop* theWorkshop,
652 const bool theTemporarily, const bool theCheckIfAttributeHasObject)
654 if (!theAttribute.get())
658 std::string aType = theAttribute->attributeType();
659 if (aType == ModelAPI_AttributeReference::typeId()) {
660 AttributeReferencePtr aRef =
661 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
662 ObjectPtr aObject = aRef->value();
663 if (!(aObject && aObject->isSame(theObject))) {
664 aRef->setValue(theObject);
666 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
667 AttributeRefAttrPtr aRefAttr =
668 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
670 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
671 if (anAttribute.get())
672 aRefAttr->setAttr(anAttribute);
674 ObjectPtr aObject = aRefAttr->object();
675 if (!(aObject && aObject->isSame(theObject))) {
676 aRefAttr->setObject(theObject);
679 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
680 AttributeSelectionPtr aSelectAttr =
681 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
682 if (aSelectAttr.get() != NULL) {
683 aSelectAttr->setValue(theObject, theShape, theTemporarily);
686 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
687 AttributeSelectionListPtr aSelectionListAttr =
688 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
689 if (!theCheckIfAttributeHasObject ||
690 !aSelectionListAttr->isInList(theObject, theShape, theTemporarily))
691 aSelectionListAttr->append(theObject, theShape, theTemporarily);
693 else if (aType == ModelAPI_AttributeRefList::typeId()) {
694 AttributeRefListPtr aRefListAttr =
695 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
696 if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject)) {
698 aRefListAttr->append(theObject);
703 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
704 AttributeRefAttrListPtr aRefAttrListAttr =
705 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
706 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
708 if (anAttribute.get()) {
709 if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
710 aRefAttrListAttr->append(anAttribute);
713 if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject)) {
715 aRefAttrListAttr->append(theObject);
724 GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* theWorkshop)
727 if (!theAttribute.get())
730 std::string aType = theAttribute->attributeType();
731 if (aType == ModelAPI_AttributeReference::typeId()) {
732 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
733 AttributeRefAttrPtr aRefAttr =
734 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
735 if (aRefAttr.get() && !aRefAttr->isObject()) {
736 AttributePtr anAttribute = aRefAttr->attr();
737 aShape = theWorkshop->module()->findShape(anAttribute);
739 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
740 AttributeSelectionPtr aSelectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
742 aShape = aSelectAttr->value();
744 else // Geom2D point processing
745 aShape = theWorkshop->module()->findShape(theAttribute);
749 void flushUpdated(ObjectPtr theObject)
751 blockUpdateViewer(true);
753 // Fix the problem of not previewed results of constraints applied. Flush Create/Delete
754 // (for the sketch result) to start processing of the sketch in the solver.
755 // TODO: these flushes should be moved in a separate method provided by Model
756 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
757 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
758 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
760 blockUpdateViewer(false);
763 void blockUpdateViewer(const bool theValue)
765 // the viewer update should be blocked in order to avoid the temporary feature content
766 // when the solver processes the feature, the redisplay message can be flushed
767 // what caused the display in the viewer preliminary states of object
768 // e.g. fillet feature, angle value change
769 std::shared_ptr<Events_Message> aMsg;
771 aMsg = std::shared_ptr<Events_Message>(
772 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
775 // the viewer update should be unblocked
776 aMsg = std::shared_ptr<Events_Message>(
777 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
779 Events_Loop::loop()->send(aMsg);
782 QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
783 int theMaxLineInPixels)
785 static QFontMetrics tfm(theWidget ? theWidget->font() : QApplication::font());
786 static qreal phi = 2.618;
788 QRect aBounds = tfm.boundingRect(theValue);
789 if(aBounds.width() <= theMaxLineInPixels)
792 qreal s = aBounds.width() * aBounds.height();
793 qreal aGoldWidth = sqrt(s*phi);
795 QStringList aWords = theValue.split(" ", QString::SkipEmptyParts);
797 int n = aWords.count();
799 for (int i = 0; i < n; i++) {
800 QString aLineExt = aLine + " " + aWords[i];
801 qreal anWidthNonExt = tfm.boundingRect(aLine).width();
802 qreal anWidthExt = tfm.boundingRect(aLineExt).width();
803 qreal aDeltaNonExt = fabs(anWidthNonExt-aGoldWidth);
804 qreal aDeltaExt = fabs(anWidthExt-aGoldWidth);
805 if(aDeltaNonExt < aDeltaExt) {
807 aLines.append(aLine);
815 aLines.append(aLine);
817 QString aResult = aLines.join("\n");
821 //**************************************************************
822 QLocale doubleLocale()
824 // VSR 01/07/2010: Disable thousands separator for spin box
825 // (to avoid inconsistency of double-2-string and string-2-double conversion)
827 aLocale.setNumberOptions(aLocale.numberOptions() |
828 QLocale::OmitGroupSeparator |
829 QLocale::RejectGroupSeparator);
833 //**************************************************************
834 void refsToFeatureInFeatureDocument(const ObjectPtr& theObject,
835 std::set<FeaturePtr>& theRefFeatures)
837 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
838 if (aFeature.get()) {
839 DocumentPtr aFeatureDoc = aFeature->document();
840 // 1. find references in the current document
841 aFeatureDoc->refsToFeature(aFeature, theRefFeatures, false);
846 //**************************************************************
847 /*bool isSubOfComposite(const ObjectPtr& theObject)
850 std::set<FeaturePtr> aRefFeatures;
851 refsToFeatureInFeatureDocument(theObject, aRefFeatures);
852 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
853 aLast = aRefFeatures.end();
854 for (; anIt != aLast && !isSub; anIt++) {
855 isSub = isSubOfComposite(theObject, *anIt);
860 //**************************************************************
861 /*bool isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature)
864 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
865 if (aComposite.get()) {
866 isSub = aComposite->isSub(theObject);
867 // the recursive is possible, the parameters are sketch circle and extrusion cut. They are
868 // separated by composite sketch feature
870 int aNbSubs = aComposite->numberOfSubs();
871 for (int aSub = 0; aSub < aNbSubs && !isSub; aSub++) {
872 isSub = isSubOfComposite(theObject, aComposite->subFeature(aSub));
879 //**************************************************************
880 ResultPtr firstResult(const ObjectPtr& theObject)
882 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
883 if (!aResult.get()) {
884 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
886 aResult = aFeature->firstResult();
891 //**************************************************************
892 bool isFeatureOfResult(const FeaturePtr& theFeature, const std::string& theGroupOfResult)
894 bool isResult = false;
896 if (!theFeature->data()->isValid())
899 ResultPtr aFirstResult = theFeature->firstResult();
900 if (!aFirstResult.get())
903 return aFirstResult->groupName() == theGroupOfResult;
906 //**************************************************************
907 bool hasModuleDocumentFeature(const std::set<FeaturePtr>& theFeatures)
909 bool aFoundModuleDocumentObject = false;
910 DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
912 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
913 for (; anIt != aLast && !aFoundModuleDocumentObject; anIt++) {
914 FeaturePtr aFeature = *anIt;
915 ResultPtr aResult = ModuleBase_Tools::firstResult(aFeature);
916 if (aResult.get() && aResult->groupName() == ModelAPI_ResultPart::group())
918 aFoundModuleDocumentObject = aFeature->document() == aModuleDoc;
921 return aFoundModuleDocumentObject;
924 //**************************************************************
925 bool askToDelete(const std::set<FeaturePtr> theFeatures,
926 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
928 std::set<FeaturePtr>& theReferencesToDelete,
929 const std::string& thePrefixInfo)
931 QString aNotActivatedDocWrn;
932 std::string aNotActivatedNames;
933 if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
934 if (ModuleBase_Tools::hasModuleDocumentFeature(theFeatures))
935 aNotActivatedDocWrn =
936 QObject::tr("Selected objects can be used in Part documents which are not loaded:%1.\n")
937 .arg(aNotActivatedNames.c_str());
940 std::set<FeaturePtr> aFeaturesRefsTo;
941 std::set<FeaturePtr> aFeaturesRefsToParameter;
942 std::set<FeaturePtr> aParameterFeatures;
943 QStringList aPartFeatureNames;
944 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
945 aLast = theFeatures.end();
946 // separate features to references to parameter features and references to others
947 for (; anIt != aLast; anIt++) {
948 FeaturePtr aFeature = *anIt;
949 if (theReferences.find(aFeature) == theReferences.end())
952 if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
953 aPartFeatureNames.append(aFeature->name().c_str());
955 std::set<FeaturePtr> aRefFeatures;
956 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
957 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
958 for (; aRefIt != aRefLast; aRefIt++) {
959 FeaturePtr aRefFeature = *aRefIt;
960 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
961 aRefFeatures.find(aRefFeature) == aRefFeatures.end()) // it is not added
962 aRefFeatures.insert(aRefFeature);
965 if (isFeatureOfResult(aFeature, ModelAPI_ResultParameter::group())) {
966 aFeaturesRefsToParameter.insert(aRefFeatures.begin(), aRefFeatures.end());
967 aParameterFeatures.insert(aFeature);
970 theReferencesToDelete.insert(aRefFeatures.begin(), aRefFeatures.end());
974 std::set<FeaturePtr> aFeaturesRefsToParameterOnly;
975 anIt = aFeaturesRefsToParameter.begin();
976 aLast = aFeaturesRefsToParameter.end();
977 // separate features to references to parameter features and references to others
978 QStringList aParamFeatureNames;
979 for (; anIt != aLast; anIt++) {
980 FeaturePtr aFeature = *anIt;
981 if (theReferencesToDelete.find(aFeature) == theReferencesToDelete.end()) {
982 aFeaturesRefsToParameterOnly.insert(aFeature);
983 aParamFeatureNames.append(aFeature->name().c_str());
986 aParamFeatureNames.sort();
987 QStringList anOtherFeatureNames;
988 anIt = theReferencesToDelete.begin();
989 aLast = theReferencesToDelete.end();
990 for (; anIt != aLast; anIt++) {
991 FeaturePtr aFeature = *anIt;
992 if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
993 aPartFeatureNames.append(aFeature->name().c_str());
995 anOtherFeatureNames.append(aFeature->name().c_str());
997 aPartFeatureNames.sort();
998 anOtherFeatureNames.sort();
1000 bool aCanReplaceParameters = !aFeaturesRefsToParameterOnly.empty();
1002 QMessageBox aMessageBox(theParent);
1003 aMessageBox.setWindowTitle(QObject::tr("Delete features"));
1004 aMessageBox.setIcon(QMessageBox::Warning);
1005 aMessageBox.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
1006 aMessageBox.setDefaultButton(QMessageBox::No);
1009 if (!thePrefixInfo.empty())
1010 aText = thePrefixInfo.c_str();
1011 QString aSep = ", ";
1012 if (!aPartFeatureNames.empty()) {
1013 aText += QString(QObject::tr("The following parts will be deleted: %1.\n"))
1014 .arg(aPartFeatureNames.join(aSep));
1016 if (!aNotActivatedDocWrn.isEmpty())
1017 aText += aNotActivatedDocWrn;
1018 if (!anOtherFeatureNames.empty()) {
1019 const char* aMsg = "Features are used in the following features: %1.\nThese "
1020 "features will be deleted.\n";
1021 aText += QString(QObject::tr(aMsg))
1022 .arg(anOtherFeatureNames.join(aSep));
1024 if (!aParamFeatureNames.empty()) {
1025 const char* aMsg = "Parameters are used directly and through a sequence "
1026 "of dependencies in the following features: %1.\nThese features will "
1027 "be deleted.\nOr parameters could be replaced by their values.\n";
1028 aText += QString(QObject::tr(aMsg))
1029 .arg(aParamFeatureNames.join(aSep));
1030 QPushButton *aReplaceButton =
1031 aMessageBox.addButton(QObject::tr("Replace"), QMessageBox::ActionRole);
1034 if (!aText.isEmpty()) {
1035 aText += "Would you like to continue?";
1036 aMessageBox.setText(aText);
1038 QMessageBox::ButtonRole aButtonRole = aMessageBox.buttonRole(aMessageBox.clickedButton());
1040 if (aButtonRole == QMessageBox::NoRole)
1043 if (aButtonRole == QMessageBox::ActionRole) {
1044 foreach (FeaturePtr aObj, aParameterFeatures)
1045 ModelAPI_ReplaceParameterMessage::send(aObj, 0);
1048 theReferencesToDelete.insert(aFeaturesRefsToParameterOnly.begin(),
1049 aFeaturesRefsToParameterOnly.end());
1054 //**************************************************************
1055 void convertToFeatures(const QObjectPtrList& theObjects, std::set<FeaturePtr>& theFeatures)
1057 QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
1058 for(; anIt != aLast; anIt++) {
1059 ObjectPtr anObject = *anIt;
1060 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
1061 // for parameter result, use the corresponded reature to be removed
1062 if (!aFeature.get() && anObject->groupName() == ModelAPI_ResultParameter::group()) {
1063 aFeature = ModelAPI_Feature::feature(anObject);
1066 theFeatures.insert(aFeature);
1070 //**************************************************************
1071 void convertToFolders(const QObjectPtrList& theObjects,
1072 std::set<FolderPtr>& theFolders)
1074 QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
1075 for(; anIt != aLast; anIt++) {
1076 ObjectPtr anObject = *anIt;
1077 FolderPtr aFeature = std::dynamic_pointer_cast<ModelAPI_Folder>(anObject);
1079 theFolders.insert(aFeature);
1084 //**************************************************************
1085 QString translate(const Events_InfoMessage& theMessage)
1089 if (!theMessage.empty()) {
1090 std::string aStr = Config_Translator::translate(theMessage);
1091 if (!aStr.empty()) {
1092 std::string aCodec = Config_Translator::codec(theMessage);
1093 aMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
1100 QString translate(const std::string& theContext, const std::string& theMessage)
1104 if (!theMessage.empty()) {
1105 std::string aStr = Config_Translator::translate(theContext, theMessage);
1106 if (!aStr.empty()) {
1107 std::string aCodec = Config_Translator::codec(theContext);
1108 aMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
1115 void setPointBallHighlighting(AIS_Shape* theAIS)
1117 static Handle(Image_AlienPixMap) aPixMap;
1118 if(aPixMap.IsNull()) {
1119 // Load icon for the presentation
1121 char* anEnv = getenv("SHAPER_ROOT_DIR");
1123 aFile = std::string(anEnv) +
1124 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
1126 anEnv = getenv("OPENPARTS_ROOT_DIR");
1128 aFile = std::string(anEnv) + FSEP + "resources";
1132 static const std::string aMarkerName = "marker_dot.png";
1133 aFile += aMarkerName;
1134 aPixMap = new Image_AlienPixMap();
1135 if(!aPixMap->Load(aFile.c_str())) {
1136 // The icon for constraint is not found
1137 static const std::string aMsg =
1138 "Error: Point market not found by path: \"" + aFile + "\". Falling back.";
1139 //Events_InfoMessage("ModuleBase_Tools::setPointBallHighlighting", aMsg).send();
1143 Handle(Graphic3d_AspectMarker3d) anAspect;
1144 Handle(Prs3d_Drawer) aDrawer = theAIS->DynamicHilightAttributes();
1145 if (aDrawer.IsNull()) {
1146 if (ModuleBase_IViewer::DefaultHighlightDrawer.IsNull())
1148 aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
1149 if (!aDrawer->HasOwnPointAspect()) {
1150 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_BALL, Quantity_NOC_BLACK, 2.0));
1153 if(aDrawer->HasOwnPointAspect()) {
1154 Handle(Prs3d_PointAspect) aPntAspect = aDrawer->PointAspect();
1155 if(aPixMap->IsEmpty()) {
1156 anAspect = aPntAspect->Aspect();
1157 anAspect->SetType(Aspect_TOM_BALL);
1159 if(aPixMap->Format() == Image_PixMap::ImgGray) {
1160 aPixMap->SetFormat (Image_PixMap::ImgAlpha);
1161 } else if(aPixMap->Format() == Image_PixMap::ImgGrayF) {
1162 aPixMap->SetFormat (Image_PixMap::ImgAlphaF);
1164 anAspect = new Graphic3d_AspectMarker3d(aPixMap);
1165 aPntAspect->SetAspect(anAspect);
1167 aDrawer->SetPointAspect(aPntAspect);
1168 theAIS->SetDynamicHilightAttributes(aDrawer);
1172 FeaturePtr createParameter(const QString& theText)
1174 FeaturePtr aParameter;
1175 QStringList aList = theText.split("=");
1176 if (aList.count() != 2) {
1179 QString aParamName = aList.at(0).trimmed();
1181 if (isNameExist(aParamName, FeaturePtr())) {
1185 if (!ModelAPI_Expression::isVariable(aParamName.toStdString())) {
1189 QString aExpression = aList.at(1).trimmed();
1190 if (aExpression.isEmpty()) {
1194 SessionPtr aMgr = ModelAPI_Session::get();
1195 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
1197 aParameter = aDoc->addFeature("Parameter");
1198 if (aParameter.get()) {
1199 AttributeStringPtr aNameAttr = aParameter->string("variable");
1200 aNameAttr->setValue(aParamName.toStdString());
1202 AttributeStringPtr aExprAttr = aParameter->string("expression");
1203 aExprAttr->setValue(aExpression.toStdString());
1204 aParameter->execute();
1206 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
1207 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
1212 void editParameter(FeaturePtr theParam, const QString& theText)
1214 QStringList aList = theText.split("=");
1215 QString aParamName = aList.at(0).trimmed();
1217 QString aExpression = aList.at(1).trimmed();
1218 if (aExpression.isEmpty()) {
1222 if (isNameExist(aParamName, theParam)) {
1225 AttributeStringPtr aNameAttr = theParam->string("variable");
1226 aNameAttr->setValue(aParamName.toStdString());
1228 AttributeStringPtr aExprAttr = theParam->string("expression");
1229 aExprAttr->setValue(aExpression.toStdString());
1230 theParam->execute();
1232 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
1235 bool isNameExist(const QString& theName, FeaturePtr theIgnoreParameter)
1237 SessionPtr aMgr = ModelAPI_Session::get();
1238 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
1239 FeaturePtr aParamFeature;
1240 int aNbFeatures = aDoc->numInternalFeatures();
1241 std::string aName = theName.toStdString();
1242 for (int i = 0; i < aNbFeatures; i++) {
1243 aParamFeature = aDoc->internalFeature(i);
1244 if (aParamFeature && aParamFeature->getKind() == "Parameter") {
1245 if ((theIgnoreParameter != aParamFeature) && (aParamFeature->name() == aName))
1252 FeaturePtr findParameter(const QString& theName)
1254 SessionPtr aMgr = ModelAPI_Session::get();
1255 std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
1256 FeaturePtr aParamFeature;
1257 int aNbFeatures = aDoc->numInternalFeatures();
1258 std::string aName = theName.toStdString();
1259 for (int i = 0; i < aNbFeatures; i++) {
1260 aParamFeature = aDoc->internalFeature(i);
1261 if (aParamFeature && aParamFeature->getKind() == "Parameter") {
1262 if (aParamFeature->name() == aName)
1263 return aParamFeature;
1266 return FeaturePtr();
1270 //********************************************************************
1271 std::string generateName(const AttributePtr& theAttribute,
1272 ModuleBase_IWorkshop* theWorkshop)
1275 if (theAttribute.get() != NULL) {
1276 ModuleBase_Operation* anOperation = theWorkshop->currentOperation();
1278 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1279 if (aFeature.get()) {
1280 std::string aXmlCfg, aDescription;
1281 theWorkshop->module()->getXMLRepresentation(aFeature->getKind(), aXmlCfg, aDescription);
1283 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
1284 std::string anAttributeTitle;
1285 aFactory.getAttributeTitle(theAttribute->id(), anAttributeTitle);
1287 std::stringstream aStreamName;
1288 aStreamName << theAttribute->owner()->data()->name() << "/" << anAttributeTitle.c_str();
1289 aName = aStreamName.str();
1295 } // namespace ModuleBase_Tools