1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_Tools.cpp
4 // Created: 11 July 2014
5 // Author: Vitaly Smetannikov
7 #include "ModuleBase_Tools.h"
9 #include <ModuleBase_ParamIntSpinBox.h>
10 #include <ModuleBase_ParamSpinBox.h>
11 #include <ModuleBase_WidgetFactory.h>
12 #include <ModuleBase_IWorkshop.h>
13 #include <ModuleBase_IModule.h>
14 #include <ModuleBase_IconFactory.h>
15 #include <ModuleBase_ResultPrs.h>
17 #include <ModelAPI_Attribute.h>
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_AttributeReference.h>
20 #include <ModelAPI_AttributeSelection.h>
21 #include <ModelAPI_AttributeSelectionList.h>
22 #include <ModelAPI_AttributeRefList.h>
23 #include <ModelAPI_AttributeRefAttrList.h>
24 #include <ModelAPI_ResultPart.h>
25 #include <ModelAPI_ResultConstruction.h>
26 #include <Events_Loop.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_Result.h>
30 #include <ModelAPI_ResultCompSolid.h>
31 #include <ModelAPI_ResultParameter.h>
32 #include <ModelAPI_Tools.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Events.h>
36 #include <ModelGeomAlgo_Point2D.h>
38 #include <TopoDS_Iterator.hxx>
40 #include <GeomDataAPI_Point2D.h>
41 #include <Events_InfoMessage.h>
42 #include <GeomAPI_ShapeExplorer.h>
44 #include <Config_PropManager.h>
45 #include <Config_Translator.h>
47 #include <Prs3d_PointAspect.hxx>
48 #include <Graphic3d_AspectMarker3d.hxx>
50 #include <Image_AlienPixMap.hxx>
56 #include <QDoubleSpinBox>
57 #include <QGraphicsDropShadowEffect>
59 #include <QApplication>
60 #include <QMessageBox>
68 #pragma warning(disable : 4996) // for getenv
71 const double tolerance = 1e-7;
72 const double DEFAULT_DEVIATION_COEFFICIENT = 1.e-4;
74 //#define DEBUG_ACTIVATE_WINDOW
75 //#define DEBUG_SET_FOCUS
83 namespace ModuleBase_Tools {
85 //******************************************************************
87 //******************************************************************
89 void adjustMargins(QWidget* theWidget)
93 adjustMargins(theWidget->layout());
96 void adjustMargins(QLayout* theLayout)
100 theLayout->setContentsMargins(2, 5, 2, 5);
101 theLayout->setSpacing(4);
104 void zeroMargins(QWidget* theWidget)
108 zeroMargins(theWidget->layout());
111 void zeroMargins(QLayout* theLayout)
115 theLayout->setContentsMargins(0, 0, 0, 0);
116 theLayout->setSpacing(5);
119 void activateWindow(QWidget* theWidget, const QString& theInfo)
121 theWidget->activateWindow();
123 #ifdef DEBUG_ACTIVATE_WINDOW
124 qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
128 void setFocus(QWidget* theWidget, const QString& theInfo)
130 theWidget->setFocus();
132 #ifdef DEBUG_SET_FOCUS
133 qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
137 void setShadowEffect(QWidget* theWidget, const bool isSetEffect)
140 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
141 aGlowEffect->setOffset(.0);
142 aGlowEffect->setBlurRadius(10.0);
143 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
144 theWidget->setGraphicsEffect(aGlowEffect);
147 QGraphicsEffect* anEffect = theWidget->graphicsEffect();
149 anEffect->deleteLater();
150 theWidget->setGraphicsEffect(NULL);
154 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
156 QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
157 QImage anAditional(theAdditionalIcon);
162 int anAddWidth = anAditional.width();
163 int anAddHeight = anAditional.height();
165 int aWidth = anIcon.width();
166 int aHeight = anIcon.height();
168 int aStartWidthPos = aWidth - anAddWidth - 1;
169 int aStartHeightPos = aHeight - anAddHeight - 1;
171 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
173 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
175 if (qAlpha(anAditional.pixel(i, j)) > 0)
176 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
179 return QPixmap::fromImage(anIcon);
182 QPixmap lighter(const QString& theIcon, const int theLighterValue)
184 QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
188 QImage aResult = ModuleBase_IconFactory::loadImage(theIcon);
189 for (int i = 0; i < anIcon.width(); i++)
191 for (int j = 0; j < anIcon.height(); j++)
193 QRgb anRgb = anIcon.pixel(i, j);
194 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
195 qAlpha(aResult.pixel(i, j)));
197 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
198 aResult.setPixel(i, j, qRgba(aLighterColor.red(), aLighterColor.green(),
199 aLighterColor.blue(), aLighterColor.alpha()));
202 return QPixmap::fromImage(aResult);
205 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
207 if (theSpin->text() == theText)
209 // In order to avoid extra text setting because it will
210 // reset cursor position in control
211 bool isBlocked = theSpin->blockSignals(true);
212 theSpin->setText(theText);
213 theSpin->blockSignals(isBlocked);
216 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
218 if (fabs(theSpin->value() - theValue) < tolerance)
220 bool isBlocked = theSpin->blockSignals(true);
221 theSpin->setValue(theValue);
222 theSpin->blockSignals(isBlocked);
225 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
227 if (fabs(theSpin->value() - theValue) < tolerance)
229 bool isBlocked = theSpin->blockSignals(true);
230 theSpin->setValue(theValue);
231 theSpin->blockSignals(isBlocked);
234 void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText)
236 // In order to avoid extra text setting because it will
237 // reset cursor position in control
238 if (theSpin->text() == theText)
240 bool isBlocked = theSpin->blockSignals(true);
241 theSpin->setText(theText);
242 theSpin->blockSignals(isBlocked);
245 void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue)
247 if (theSpin->value() == theValue)
249 bool isBlocked = theSpin->blockSignals(true);
250 theSpin->setValue(theValue);
251 theSpin->blockSignals(isBlocked);
254 QAction* createAction(const QIcon& theIcon, const QString& theText,
255 QObject* theParent, const QObject* theReceiver,
256 const char* theMember, const QString& theToolTip,
257 const QString& theStatusTip)
259 QAction* anAction = new QAction(theIcon, theText, theParent);
260 anAction->setToolTip(theToolTip.isEmpty() ? theText : theToolTip);
261 anAction->setStatusTip(!theStatusTip.isEmpty() ? theStatusTip :
262 (!theToolTip.isEmpty() ? theToolTip : theText));
264 QObject::connect(anAction, SIGNAL(triggered(bool)), theReceiver, theMember);
270 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
272 QString aFeatureStr = "feature";
276 std::ostringstream aPtrStr;
277 aPtrStr << "[" << theObj.get() << "]";
279 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
280 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
282 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
283 if (aRes->isDisabled())
284 aFeatureStr.append("[disabled]");
285 if (aRes->isConcealed())
286 aFeatureStr.append("[concealed]");
287 if (ModelAPI_Tools::hasSubResults(aRes))
288 aFeatureStr.append("[hasSubResults]");
290 aFeature = ModelAPI_Feature::feature(aRes);
293 aFeatureStr.append(aPtrStr.str().c_str());
295 if (aFeature.get()) {
296 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
297 if (aFeature->data()->isValid()) {
298 aFeatureStr.append(QString(", name=%1").arg(theObj->data()->name().c_str()).toStdString()
301 if (isUseAttributesInfo) {
302 std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes;
303 std::string aPointsInfo = ModelGeomAlgo_Point2D::getPontAttributesInfo(aFeature,
304 anAttributes).c_str();
305 if (!aPointsInfo.empty())
306 aFeatureStr.append(QString(", attributes: %1")
307 .arg(aPointsInfo.c_str()).toStdString().c_str());
315 typedef QMap<QString, int> ShapeTypes;
316 static ShapeTypes myShapeTypes;
318 int shapeType(const QString& theType)
320 if (myShapeTypes.count() == 0) {
321 myShapeTypes["compound"] = TopAbs_COMPOUND;
322 myShapeTypes["compounds"] = TopAbs_COMPOUND;
323 myShapeTypes["compsolid"] = TopAbs_COMPSOLID;
324 myShapeTypes["compsolids"] = TopAbs_COMPSOLID;
325 myShapeTypes["solid"] = TopAbs_SOLID;
326 myShapeTypes["solids"] = TopAbs_SOLID;
327 myShapeTypes["shell"] = TopAbs_SHELL;
328 myShapeTypes["shells"] = TopAbs_SHELL;
329 myShapeTypes["face"] = TopAbs_FACE;
330 myShapeTypes["faces"] = TopAbs_FACE;
331 myShapeTypes["wire"] = TopAbs_WIRE;
332 myShapeTypes["wires"] = TopAbs_WIRE;
333 myShapeTypes["edge"] = TopAbs_EDGE;
334 myShapeTypes["edges"] = TopAbs_EDGE;
335 myShapeTypes["vertex"] = TopAbs_VERTEX;
336 myShapeTypes["vertices"] = TopAbs_VERTEX;
337 myShapeTypes["object"] = ModuleBase_ResultPrs::Sel_Result;
338 myShapeTypes["objects"] = ModuleBase_ResultPrs::Sel_Result;
340 QString aType = theType.toLower();
341 if(myShapeTypes.contains(aType))
342 return myShapeTypes[aType];
343 Events_InfoMessage("ModuleBase_Tools", "Shape type defined in XML is not implemented!").send();
347 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature,
348 bool& hasParameter, bool& hasCompositeOwner)
352 hasParameter = false;
353 hasCompositeOwner = false;
354 foreach(ObjectPtr aObj, theObjects) {
355 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
356 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
357 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
359 hasResult |= (aResult.get() != NULL);
360 hasFeature |= (aFeature.get() != NULL);
361 hasParameter |= (aConstruction.get() != NULL);
363 hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
364 if (hasFeature && hasResult && hasParameter && hasCompositeOwner)
369 /*bool setDefaultDeviationCoefficient(std::shared_ptr<GeomAPI_Shape> theGeomShape)
371 if (!theGeomShape.get())
373 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
374 // correction of deviation for them should not influence to the application performance
375 GeomAPI_ShapeExplorer anExp(theGeomShape, GeomAPI_Shape::FACE);
376 bool anEmpty = anExp.empty();
377 return !anExp.more();
380 /*void setDefaultDeviationCoefficient(const std::shared_ptr<ModelAPI_Result>& theResult,
381 const Handle(Prs3d_Drawer)& theDrawer)
383 if (!theResult.get())
385 bool aUseDeviation = false;
387 std::string aResultGroup = theResult->groupName();
388 if (aResultGroup == ModelAPI_ResultConstruction::group())
389 aUseDeviation = true;
390 else if (aResultGroup == ModelAPI_ResultBody::group()) {
391 GeomShapePtr aGeomShape = theResult->shape();
392 if (aGeomShape.get())
393 aUseDeviation = setDefaultDeviationCoefficient(aGeomShape);
396 theDrawer->SetDeviationCoefficient(DEFAULT_DEVIATION_COEFFICIENT);
399 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
400 const Handle(Prs3d_Drawer)& theDrawer)
402 if (theShape.IsNull())
405 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape());
406 aGeomShape->setImpl(new TopoDS_Shape(theShape));
408 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
409 // correction of deviation for them should not influence to the application performance
410 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
411 bool isConstruction = !anExp.more();
415 aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
417 aDeflection = Config_PropManager::real("Visualization", "body_deflection");
419 theDrawer->SetDeviationCoefficient(aDeflection);
422 Quantity_Color color(const std::string& theSection,
423 const std::string& theName)
425 std::vector<int> aColor = Config_PropManager::color(theSection, theName);
426 return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
429 ObjectPtr getObject(const AttributePtr& theAttribute)
432 std::string anAttrType = theAttribute->attributeType();
433 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
434 AttributeRefAttrPtr anAttr =
435 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
436 if (anAttr != NULL && anAttr->isObject())
437 anObject = anAttr->object();
439 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
440 AttributeSelectionPtr anAttr =
441 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
443 anObject = anAttr->context();
445 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
446 AttributeReferencePtr anAttr =
447 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
448 if (anAttr.get() != NULL)
449 anObject = anAttr->value();
454 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
456 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
458 // for compounds check sub-shapes: it may be compound of needed type:
459 // Booleans may produce compounds of Solids
460 if (aShapeType == TopAbs_COMPOUND) {
461 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
462 if (!aSubs.Value().IsNull()) {
463 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
464 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
465 aShapeType = TopAbs_COMPOUND;
468 if (aShapeType == TopAbs_COMPOUND) {
469 aShapeType = aSubType;
470 } else if (aShapeType != aSubType) { // compound of shapes of different types
471 aShapeType = TopAbs_COMPOUND;
480 void getParameters(QStringList& theParameters)
482 theParameters.clear();
484 SessionPtr aSession = ModelAPI_Session::get();
485 std::list<DocumentPtr> aDocList;
486 DocumentPtr anActiveDocument = aSession->activeDocument();
487 DocumentPtr aRootDocument = aSession->moduleDocument();
488 aDocList.push_back(anActiveDocument);
489 if (anActiveDocument != aRootDocument) {
490 aDocList.push_back(aRootDocument);
492 std::string aGroupId = ModelAPI_ResultParameter::group();
493 for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
494 DocumentPtr aDocument = *it;
495 int aSize = aDocument->size(aGroupId);
496 for (int i = 0; i < aSize; i++) {
497 ObjectPtr anObject = aDocument->object(aGroupId, i);
498 std::string aParameterName = anObject->data()->name();
499 theParameters.append(aParameterName.c_str());
504 std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop,
505 const FeaturePtr& theFeature)
507 std::string anAttributeId;
509 std::string aXmlCfg, aDescription;
510 theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
512 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
513 std::string anAttributeTitle;
514 aFactory.getGreedAttribute(anAttributeId);
516 return anAttributeId;
519 bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
520 const std::shared_ptr<GeomAPI_Shape>& theShape,
521 ModuleBase_IWorkshop* theWorkshop,
522 const bool theTemporarily)
524 bool aHasObject = false;
525 if (!theAttribute.get())
528 std::string aType = theAttribute->attributeType();
529 if (aType == ModelAPI_AttributeReference::typeId()) {
530 AttributeReferencePtr aRef =
531 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
532 ObjectPtr aObject = aRef->value();
533 aHasObject = aObject && aObject->isSame(theObject);
534 //if (!(aObject && aObject->isSame(theObject))) {
535 // aRef->setValue(theObject);
537 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
538 AttributeRefAttrPtr aRefAttr =
539 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
541 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
542 if (anAttribute.get()) {
543 //aRefAttr->setAttr(anAttribute);
546 ObjectPtr aObject = aRefAttr->object();
547 aHasObject = aObject && aObject->isSame(theObject);
548 //if (!(aObject && aObject->isSame(theObject))) {
549 // aRefAttr->setObject(theObject);
552 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
553 /*AttributeSelectionPtr aSelectAttr =
554 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
555 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
556 if (aSelectAttr.get() != NULL) {
557 aSelectAttr->setValue(aResult, theShape, theTemporarily);
560 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
561 AttributeSelectionListPtr aSelectionListAttr =
562 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
563 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
564 aHasObject = aSelectionListAttr->isInList(aResult, theShape, theTemporarily);
566 else if (aType == ModelAPI_AttributeRefList::typeId()) {
567 AttributeRefListPtr aRefListAttr =
568 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
569 aHasObject = aRefListAttr->isInList(theObject);
570 //if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject))
571 // aRefListAttr->append(theObject);
573 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
574 AttributeRefAttrListPtr aRefAttrListAttr =
575 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
576 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
578 if (anAttribute.get()) {
579 aHasObject = aRefAttrListAttr->isInList(anAttribute);
580 //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
581 // aRefAttrListAttr->append(anAttribute);
584 aHasObject = aRefAttrListAttr->isInList(theObject);
585 //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
586 // aRefAttrListAttr->append(theObject);
592 bool setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
593 const GeomShapePtr& theShape, ModuleBase_IWorkshop* theWorkshop,
594 const bool theTemporarily, const bool theCheckIfAttributeHasObject)
596 if (!theAttribute.get())
600 std::string aType = theAttribute->attributeType();
601 if (aType == ModelAPI_AttributeReference::typeId()) {
602 AttributeReferencePtr aRef =
603 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
604 ObjectPtr aObject = aRef->value();
605 if (!(aObject && aObject->isSame(theObject))) {
606 aRef->setValue(theObject);
608 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
609 AttributeRefAttrPtr aRefAttr =
610 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
612 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
613 if (anAttribute.get())
614 aRefAttr->setAttr(anAttribute);
616 ObjectPtr aObject = aRefAttr->object();
617 if (!(aObject && aObject->isSame(theObject))) {
618 aRefAttr->setObject(theObject);
621 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
622 AttributeSelectionPtr aSelectAttr =
623 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
624 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
625 if (aSelectAttr.get() != NULL) {
626 aSelectAttr->setValue(aResult, theShape, theTemporarily);
629 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
630 AttributeSelectionListPtr aSelectionListAttr =
631 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
632 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
633 if (!theCheckIfAttributeHasObject ||
634 !aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
635 aSelectionListAttr->append(aResult, theShape, theTemporarily);
637 else if (aType == ModelAPI_AttributeRefList::typeId()) {
638 AttributeRefListPtr aRefListAttr =
639 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
640 if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject)) {
642 aRefListAttr->append(theObject);
647 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
648 AttributeRefAttrListPtr aRefAttrListAttr =
649 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
650 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
652 if (anAttribute.get()) {
653 if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
654 aRefAttrListAttr->append(anAttribute);
657 if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject)) {
659 aRefAttrListAttr->append(theObject);
668 GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* theWorkshop)
671 if (!theAttribute.get())
674 std::string aType = theAttribute->attributeType();
675 if (aType == ModelAPI_AttributeReference::typeId()) {
676 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
677 AttributeRefAttrPtr aRefAttr =
678 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
679 if (aRefAttr.get() && !aRefAttr->isObject()) {
680 AttributePtr anAttribute = aRefAttr->attr();
681 aShape = theWorkshop->module()->findShape(anAttribute);
683 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
684 AttributeSelectionPtr aSelectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
686 aShape = aSelectAttr->value();
688 else // Geom2D point processing
689 aShape = theWorkshop->module()->findShape(theAttribute);
693 void flushUpdated(ObjectPtr theObject)
695 blockUpdateViewer(true);
697 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
699 blockUpdateViewer(false);
702 void blockUpdateViewer(const bool theValue)
704 // the viewer update should be blocked in order to avoid the temporary feature content
705 // when the solver processes the feature, the redisplay message can be flushed
706 // what caused the display in the viewer preliminary states of object
707 // e.g. fillet feature, angle value change
708 std::shared_ptr<Events_Message> aMsg;
710 aMsg = std::shared_ptr<Events_Message>(
711 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
714 // the viewer update should be unblocked
715 aMsg = std::shared_ptr<Events_Message>(
716 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
718 Events_Loop::loop()->send(aMsg);
721 QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
722 int theMaxLineInPixels)
724 static QFontMetrics tfm(theWidget ? theWidget->font() : QApplication::font());
725 static qreal phi = 2.618;
727 QRect aBounds = tfm.boundingRect(theValue);
728 if(aBounds.width() <= theMaxLineInPixels)
731 qreal s = aBounds.width() * aBounds.height();
732 qreal aGoldWidth = sqrt(s*phi);
734 QStringList aWords = theValue.split(" ", QString::SkipEmptyParts);
736 int n = aWords.count();
738 for (int i = 0; i < n; i++) {
739 QString aLineExt = aLine + " " + aWords[i];
740 qreal anWidthNonExt = tfm.boundingRect(aLine).width();
741 qreal anWidthExt = tfm.boundingRect(aLineExt).width();
742 qreal aDeltaNonExt = fabs(anWidthNonExt-aGoldWidth);
743 qreal aDeltaExt = fabs(anWidthExt-aGoldWidth);
744 if(aDeltaNonExt < aDeltaExt) {
746 aLines.append(aLine);
754 aLines.append(aLine);
756 QString aResult = aLines.join("\n");
760 //**************************************************************
761 QLocale doubleLocale()
763 // VSR 01/07/2010: Disable thousands separator for spin box
764 // (to avoid inconsistency of double-2-string and string-2-double conversion)
766 aLocale.setNumberOptions(aLocale.numberOptions() |
767 QLocale::OmitGroupSeparator |
768 QLocale::RejectGroupSeparator);
772 //**************************************************************
773 void refsToFeatureInFeatureDocument(const ObjectPtr& theObject,
774 std::set<FeaturePtr>& theRefFeatures)
776 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
777 if (aFeature.get()) {
778 DocumentPtr aFeatureDoc = aFeature->document();
779 // 1. find references in the current document
780 aFeatureDoc->refsToFeature(aFeature, theRefFeatures, false);
785 //**************************************************************
786 /*bool isSubOfComposite(const ObjectPtr& theObject)
789 std::set<FeaturePtr> aRefFeatures;
790 refsToFeatureInFeatureDocument(theObject, aRefFeatures);
791 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
792 aLast = aRefFeatures.end();
793 for (; anIt != aLast && !isSub; anIt++) {
794 isSub = isSubOfComposite(theObject, *anIt);
799 //**************************************************************
800 /*bool isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature)
803 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
804 if (aComposite.get()) {
805 isSub = aComposite->isSub(theObject);
806 // the recursive is possible, the parameters are sketch circle and extrusion cut. They are
807 // separated by composite sketch feature
809 int aNbSubs = aComposite->numberOfSubs();
810 for (int aSub = 0; aSub < aNbSubs && !isSub; aSub++) {
811 isSub = isSubOfComposite(theObject, aComposite->subFeature(aSub));
818 //**************************************************************
819 ResultPtr firstResult(const ObjectPtr& theObject)
821 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
822 if (!aResult.get()) {
823 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
825 aResult = aFeature->firstResult();
830 //**************************************************************
831 bool isFeatureOfResult(const FeaturePtr& theFeature, const std::string& theGroupOfResult)
833 bool isResult = false;
835 if (!theFeature->data()->isValid())
838 ResultPtr aFirstResult = theFeature->firstResult();
839 if (!aFirstResult.get())
842 return aFirstResult->groupName() == theGroupOfResult;
845 //**************************************************************
846 bool hasModuleDocumentFeature(const std::set<FeaturePtr>& theFeatures)
848 bool aFoundModuleDocumentObject = false;
849 DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
851 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
852 for (; anIt != aLast && !aFoundModuleDocumentObject; anIt++) {
853 FeaturePtr aFeature = *anIt;
854 ResultPtr aResult = ModuleBase_Tools::firstResult(aFeature);
855 if (aResult.get() && aResult->groupName() == ModelAPI_ResultPart::group())
857 aFoundModuleDocumentObject = aFeature->document() == aModuleDoc;
860 return aFoundModuleDocumentObject;
863 //**************************************************************
864 bool askToDelete(const std::set<FeaturePtr> theFeatures,
865 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
867 std::set<FeaturePtr>& theReferencesToDelete,
868 const std::string& thePrefixInfo)
870 QString aNotActivatedDocWrn;
871 std::string aNotActivatedNames;
872 if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
873 if (ModuleBase_Tools::hasModuleDocumentFeature(theFeatures))
874 aNotActivatedDocWrn =
875 QObject::tr("Selected objects can be used in Part documents which are not loaded:%1.\n")
876 .arg(aNotActivatedNames.c_str());
879 std::set<FeaturePtr> aFeaturesRefsTo;
880 std::set<FeaturePtr> aFeaturesRefsToParameter;
881 std::set<FeaturePtr> aParameterFeatures;
882 QStringList aPartFeatureNames;
883 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
884 aLast = theFeatures.end();
885 // separate features to references to parameter features and references to others
886 for (; anIt != aLast; anIt++) {
887 FeaturePtr aFeature = *anIt;
888 if (theReferences.find(aFeature) == theReferences.end())
891 if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
892 aPartFeatureNames.append(aFeature->name().c_str());
894 std::set<FeaturePtr> aRefFeatures;
895 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
896 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
897 for (; aRefIt != aRefLast; aRefIt++) {
898 FeaturePtr aRefFeature = *aRefIt;
899 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
900 aRefFeatures.find(aRefFeature) == aRefFeatures.end()) // it is not added
901 aRefFeatures.insert(aRefFeature);
904 if (isFeatureOfResult(aFeature, ModelAPI_ResultParameter::group())) {
905 aFeaturesRefsToParameter.insert(aRefFeatures.begin(), aRefFeatures.end());
906 aParameterFeatures.insert(aFeature);
909 theReferencesToDelete.insert(aRefFeatures.begin(), aRefFeatures.end());
913 std::set<FeaturePtr> aFeaturesRefsToParameterOnly;
914 anIt = aFeaturesRefsToParameter.begin();
915 aLast = aFeaturesRefsToParameter.end();
916 // separate features to references to parameter features and references to others
917 QStringList aParamFeatureNames;
918 for (; anIt != aLast; anIt++) {
919 FeaturePtr aFeature = *anIt;
920 if (theReferencesToDelete.find(aFeature) == theReferencesToDelete.end()) {
921 aFeaturesRefsToParameterOnly.insert(aFeature);
922 aParamFeatureNames.append(aFeature->name().c_str());
925 aParamFeatureNames.sort();
926 QStringList anOtherFeatureNames;
927 anIt = theReferencesToDelete.begin();
928 aLast = theReferencesToDelete.end();
929 for (; anIt != aLast; anIt++) {
930 FeaturePtr aFeature = *anIt;
931 if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
932 aPartFeatureNames.append(aFeature->name().c_str());
934 anOtherFeatureNames.append(aFeature->name().c_str());
936 aPartFeatureNames.sort();
937 anOtherFeatureNames.sort();
939 bool aCanReplaceParameters = !aFeaturesRefsToParameterOnly.empty();
941 QMessageBox aMessageBox(theParent);
942 aMessageBox.setWindowTitle(QObject::tr("Delete features"));
943 aMessageBox.setIcon(QMessageBox::Warning);
944 aMessageBox.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
945 aMessageBox.setDefaultButton(QMessageBox::No);
948 if (!thePrefixInfo.empty())
949 aText = thePrefixInfo.c_str();
951 if (!aPartFeatureNames.empty()) {
952 aText += QString(QObject::tr("The following parts will be deleted: %1.\n"))
953 .arg(aPartFeatureNames.join(aSep));
955 if (!aNotActivatedDocWrn.isEmpty())
956 aText += aNotActivatedDocWrn;
957 if (!anOtherFeatureNames.empty()) {
958 const char* aMsg = "Features are used in the following features: %1.\nThese "
959 "features will be deleted.\n";
960 aText += QString(QObject::tr(aMsg))
961 .arg(anOtherFeatureNames.join(aSep));
963 if (!aParamFeatureNames.empty()) {
964 const char* aMsg = "Parameters are used in the following features: %1.\nThese features will "
965 "be deleted.\nOr parameters could be replaced by their values.\n";
966 aText += QString(QObject::tr(aMsg))
967 .arg(aParamFeatureNames.join(aSep));
968 QPushButton *aReplaceButton =
969 aMessageBox.addButton(QObject::tr("Replace"), QMessageBox::ActionRole);
972 if (!aText.isEmpty()) {
973 aText += "Would you like to continue?";
974 aMessageBox.setText(aText);
976 QMessageBox::ButtonRole aButtonRole = aMessageBox.buttonRole(aMessageBox.clickedButton());
978 if (aButtonRole == QMessageBox::NoRole)
981 if (aButtonRole == QMessageBox::ActionRole) {
982 foreach (FeaturePtr aObj, aParameterFeatures)
983 ModelAPI_ReplaceParameterMessage::send(aObj, 0);
986 theReferencesToDelete.insert(aFeaturesRefsToParameterOnly.begin(),
987 aFeaturesRefsToParameterOnly.end());
992 //**************************************************************
993 void convertToFeatures(const QObjectPtrList& theObjects, std::set<FeaturePtr>& theFeatures)
995 QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
996 for(; anIt != aLast; anIt++) {
997 ObjectPtr anObject = *anIt;
998 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
999 // for parameter result, use the corresponded reature to be removed
1000 if (!aFeature.get() && anObject->groupName() == ModelAPI_ResultParameter::group()) {
1001 aFeature = ModelAPI_Feature::feature(anObject);
1003 theFeatures.insert(aFeature);
1007 QString translate(const Events_InfoMessage& theMessage)
1011 if (!theMessage.empty()) {
1012 std::string aStr = Config_Translator::translate(theMessage);
1013 if (!aStr.empty()) {
1014 std::string aCodec = Config_Translator::codec(theMessage);
1015 aMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
1022 QString translate(const std::string& theContext, const std::string& theMessage)
1026 if (!theMessage.empty()) {
1027 std::string aStr = Config_Translator::translate(theContext, theMessage);
1028 if (!aStr.empty()) {
1029 std::string aCodec = Config_Translator::codec(theContext);
1030 aMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
1037 void setPointBallHighlighting(AIS_Shape* theAIS)
1039 static Handle(Image_AlienPixMap) aPixMap;
1040 if(aPixMap.IsNull()) {
1041 // Load icon for the presentation
1043 char* anEnv = getenv("SHAPER_ROOT_DIR");
1045 aFile = std::string(anEnv) +
1046 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
1048 anEnv = getenv("OPENPARTS_ROOT_DIR");
1050 aFile = std::string(anEnv) + FSEP + "resources";
1054 static const std::string aMarkerName = "marker_dot.png";
1055 aFile += aMarkerName;
1056 aPixMap = new Image_AlienPixMap();
1057 if(!aPixMap->Load(aFile.c_str())) {
1058 // The icon for constraint is not found
1059 static const std::string aMsg =
1060 "Error: Point market not found by path: \"" + aFile + "\". Falling back.";
1061 //Events_InfoMessage("ModuleBase_Tools::setPointBallHighlighting", aMsg).send();
1065 Handle(Graphic3d_AspectMarker3d) anAspect;
1066 Handle(Prs3d_Drawer) aDrawer = theAIS->HilightAttributes();
1067 if(aDrawer->HasOwnPointAspect()) {
1068 Handle(Prs3d_PointAspect) aPntAspect = aDrawer->PointAspect();
1069 if(aPixMap->IsEmpty()) {
1070 anAspect = aPntAspect->Aspect();
1071 anAspect->SetType(Aspect_TOM_BALL);
1073 if(aPixMap->Format() == Image_PixMap::ImgGray) {
1074 aPixMap->SetFormat (Image_PixMap::ImgAlpha);
1075 } else if(aPixMap->Format() == Image_PixMap::ImgGrayF) {
1076 aPixMap->SetFormat (Image_PixMap::ImgAlphaF);
1078 anAspect = new Graphic3d_AspectMarker3d(aPixMap);
1079 aPntAspect->SetAspect(anAspect);
1081 aDrawer->SetPointAspect(aPntAspect);
1082 theAIS->SetHilightAttributes(aDrawer);
1086 } // namespace ModuleBase_Tools