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>
31 #include <ModelAPI_Attribute.h>
32 #include <ModelAPI_AttributeRefAttr.h>
33 #include <ModelAPI_AttributeReference.h>
34 #include <ModelAPI_AttributeSelection.h>
35 #include <ModelAPI_AttributeSelectionList.h>
36 #include <ModelAPI_AttributeRefList.h>
37 #include <ModelAPI_AttributeRefAttrList.h>
38 #include <ModelAPI_ResultPart.h>
39 #include <ModelAPI_ResultConstruction.h>
40 #include <Events_Loop.h>
42 #include <ModelAPI_Data.h>
43 #include <ModelAPI_Result.h>
44 #include <ModelAPI_ResultCompSolid.h>
45 #include <ModelAPI_ResultParameter.h>
46 #include <ModelAPI_Tools.h>
47 #include <ModelAPI_Session.h>
48 #include <ModelAPI_Events.h>
49 #include <ModelAPI_Folder.h>
51 #include <ModelGeomAlgo_Point2D.h>
53 #include <TopoDS_Iterator.hxx>
55 #include <GeomDataAPI_Point2D.h>
56 #include <Events_InfoMessage.h>
57 #include <GeomAPI_ShapeExplorer.h>
59 #include <Config_PropManager.h>
60 #include <Config_Translator.h>
62 #include <Prs3d_PointAspect.hxx>
63 #include <Graphic3d_AspectMarker3d.hxx>
65 #include <Image_AlienPixMap.hxx>
71 #include <QDoubleSpinBox>
72 #include <QGraphicsDropShadowEffect>
74 #include <QApplication>
75 #include <QMessageBox>
83 #pragma warning(disable : 4996) // for getenv
86 const double tolerance = 1e-7;
87 const double DEFAULT_DEVIATION_COEFFICIENT = 1.e-4;
89 //#define DEBUG_ACTIVATE_WINDOW
90 //#define DEBUG_SET_FOCUS
98 namespace ModuleBase_Tools {
100 //******************************************************************
102 //******************************************************************
104 void adjustMargins(QWidget* theWidget)
108 adjustMargins(theWidget->layout());
111 void adjustMargins(QLayout* theLayout)
115 theLayout->setContentsMargins(2, 5, 2, 5);
116 theLayout->setSpacing(4);
119 void zeroMargins(QWidget* theWidget)
123 zeroMargins(theWidget->layout());
126 void zeroMargins(QLayout* theLayout)
130 theLayout->setContentsMargins(0, 0, 0, 0);
131 theLayout->setSpacing(5);
134 void activateWindow(QWidget* theWidget, const QString& theInfo)
137 theWidget->activateWindow();
141 #ifdef DEBUG_ACTIVATE_WINDOW
142 qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
146 void setFocus(QWidget* theWidget, const QString& theInfo)
148 activateWindow(theWidget);
149 theWidget->setFocus();
150 // rectangle of focus is not visible on tool button widgets
151 theWidget->repaint();
152 #ifdef DEBUG_SET_FOCUS
153 qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
157 void setShadowEffect(QWidget* theWidget, const bool isSetEffect)
160 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
161 aGlowEffect->setOffset(.0);
162 aGlowEffect->setBlurRadius(10.0);
163 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
164 theWidget->setGraphicsEffect(aGlowEffect);
167 QGraphicsEffect* anEffect = theWidget->graphicsEffect();
169 anEffect->deleteLater();
170 theWidget->setGraphicsEffect(NULL);
174 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
176 QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
177 QImage anAditional(theAdditionalIcon);
182 int anAddWidth = anAditional.width();
183 int anAddHeight = anAditional.height();
185 int aWidth = anIcon.width();
186 int aHeight = anIcon.height();
188 int aStartWidthPos = aWidth - anAddWidth - 1;
189 int aStartHeightPos = aHeight - anAddHeight - 1;
191 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
193 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
195 if (qAlpha(anAditional.pixel(i, j)) > 0)
196 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
199 return QPixmap::fromImage(anIcon);
202 QPixmap lighter(const QString& theIcon, const int theLighterValue)
204 QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
208 QImage aResult = ModuleBase_IconFactory::loadImage(theIcon);
209 for (int i = 0; i < anIcon.width(); i++)
211 for (int j = 0; j < anIcon.height(); j++)
213 QRgb anRgb = anIcon.pixel(i, j);
214 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
215 qAlpha(aResult.pixel(i, j)));
217 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
218 aResult.setPixel(i, j, qRgba(aLighterColor.red(), aLighterColor.green(),
219 aLighterColor.blue(), aLighterColor.alpha()));
222 return QPixmap::fromImage(aResult);
225 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
227 if (theSpin->text() == theText)
229 // In order to avoid extra text setting because it will
230 // reset cursor position in control
231 bool isBlocked = theSpin->blockSignals(true);
232 theSpin->setText(theText);
233 theSpin->blockSignals(isBlocked);
236 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
238 if (fabs(theSpin->value() - theValue) < tolerance)
240 bool isBlocked = theSpin->blockSignals(true);
241 theSpin->setValue(theValue);
242 theSpin->blockSignals(isBlocked);
245 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
247 if (fabs(theSpin->value() - theValue) < tolerance)
249 bool isBlocked = theSpin->blockSignals(true);
250 theSpin->setValue(theValue);
251 theSpin->blockSignals(isBlocked);
254 void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText)
256 // In order to avoid extra text setting because it will
257 // reset cursor position in control
258 if (theSpin->text() == theText)
260 bool isBlocked = theSpin->blockSignals(true);
261 theSpin->setText(theText);
262 theSpin->blockSignals(isBlocked);
265 void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue)
267 if (theSpin->value() == theValue)
269 bool isBlocked = theSpin->blockSignals(true);
270 theSpin->setValue(theValue);
271 theSpin->blockSignals(isBlocked);
274 QAction* createAction(const QIcon& theIcon, const QString& theText,
275 QObject* theParent, const QObject* theReceiver,
276 const char* theMember, const QString& theToolTip,
277 const QString& theStatusTip)
279 QAction* anAction = new QAction(theIcon, theText, theParent);
280 anAction->setToolTip(theToolTip.isEmpty() ? theText : theToolTip);
281 anAction->setStatusTip(!theStatusTip.isEmpty() ? theStatusTip :
282 (!theToolTip.isEmpty() ? theToolTip : theText));
284 QObject::connect(anAction, SIGNAL(triggered(bool)), theReceiver, theMember);
290 QString objectName(const ObjectPtr& theObj)
295 return theObj->data()->name().c_str();
298 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
300 QString aFeatureStr = "feature";
304 std::ostringstream aPtrStr;
305 aPtrStr << "[" << theObj.get() << "]";
307 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
308 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
310 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
311 if (aRes->isDisabled())
312 aFeatureStr.append("[disabled]");
313 if (aRes->isConcealed())
314 aFeatureStr.append("[concealed]");
315 if (ModelAPI_Tools::hasSubResults(aRes))
316 aFeatureStr.append("[hasSubResults]");
318 aFeature = ModelAPI_Feature::feature(aRes);
321 aFeatureStr.append(aPtrStr.str().c_str());
323 if (aFeature.get()) {
324 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
325 if (aFeature->data()->isValid()) {
326 aFeatureStr.append(QString(", name=%1").arg(theObj->data()->name().c_str()).toStdString()
329 if (isUseAttributesInfo) {
330 std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes;
331 std::string aPointsInfo = ModelGeomAlgo_Point2D::getPontAttributesInfo(aFeature,
332 anAttributes).c_str();
333 if (!aPointsInfo.empty())
334 aFeatureStr.append(QString(", attributes: %1")
335 .arg(aPointsInfo.c_str()).toStdString().c_str());
343 typedef QMap<QString, int> ShapeTypes;
344 static ShapeTypes myShapeTypes;
346 int shapeType(const QString& theType)
348 if (myShapeTypes.count() == 0) {
349 myShapeTypes["compound"] = TopAbs_COMPOUND;
350 myShapeTypes["compounds"] = TopAbs_COMPOUND;
351 myShapeTypes["compsolid"] = TopAbs_COMPSOLID;
352 myShapeTypes["compsolids"] = TopAbs_COMPSOLID;
353 myShapeTypes["solid"] = TopAbs_SOLID;
354 myShapeTypes["solids"] = TopAbs_SOLID;
355 myShapeTypes["shell"] = TopAbs_SHELL;
356 myShapeTypes["shells"] = TopAbs_SHELL;
357 myShapeTypes["face"] = TopAbs_FACE;
358 myShapeTypes["faces"] = TopAbs_FACE;
359 myShapeTypes["wire"] = TopAbs_WIRE;
360 myShapeTypes["wires"] = TopAbs_WIRE;
361 myShapeTypes["edge"] = TopAbs_EDGE;
362 myShapeTypes["edges"] = TopAbs_EDGE;
363 myShapeTypes["vertex"] = TopAbs_VERTEX;
364 myShapeTypes["vertices"] = TopAbs_VERTEX;
365 myShapeTypes["object"] = ModuleBase_ResultPrs::Sel_Result;
366 myShapeTypes["objects"] = ModuleBase_ResultPrs::Sel_Result;
368 QString aType = theType.toLower();
369 if(myShapeTypes.contains(aType))
370 return myShapeTypes[aType];
371 Events_InfoMessage("ModuleBase_Tools", "Shape type defined in XML is not implemented!").send();
375 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature,
376 bool& hasParameter, bool& hasCompositeOwner, bool& hasResultInHistory,
381 hasParameter = false;
382 hasCompositeOwner = false;
383 hasResultInHistory = false;
385 foreach(ObjectPtr aObj, theObjects) {
386 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
387 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
388 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aObj);
389 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
391 hasResult |= (aResult.get() != NULL);
392 hasFeature |= (aFeature.get() != NULL);
393 hasFolder |= (aFolder.get() != NULL);
394 hasParameter |= (aConstruction.get() != NULL);
396 hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
398 if (!hasResultInHistory && aResult.get()) {
399 FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
400 hasResultInHistory = aFeature.get() && aFeature->isInHistory();
403 if (hasFeature && hasResult && hasParameter && hasCompositeOwner && hasFeature)
408 /*bool setDefaultDeviationCoefficient(std::shared_ptr<GeomAPI_Shape> theGeomShape)
410 if (!theGeomShape.get())
412 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
413 // correction of deviation for them should not influence to the application performance
414 GeomAPI_ShapeExplorer anExp(theGeomShape, GeomAPI_Shape::FACE);
415 bool anEmpty = anExp.empty();
416 return !anExp.more();
419 /*void setDefaultDeviationCoefficient(const std::shared_ptr<ModelAPI_Result>& theResult,
420 const Handle(Prs3d_Drawer)& theDrawer)
422 if (!theResult.get())
424 bool aUseDeviation = false;
426 std::string aResultGroup = theResult->groupName();
427 if (aResultGroup == ModelAPI_ResultConstruction::group())
428 aUseDeviation = true;
429 else if (aResultGroup == ModelAPI_ResultBody::group()) {
430 GeomShapePtr aGeomShape = theResult->shape();
431 if (aGeomShape.get())
432 aUseDeviation = setDefaultDeviationCoefficient(aGeomShape);
435 theDrawer->SetDeviationCoefficient(DEFAULT_DEVIATION_COEFFICIENT);
438 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
439 const Handle(Prs3d_Drawer)& theDrawer)
441 if (theShape.IsNull())
444 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape());
445 aGeomShape->setImpl(new TopoDS_Shape(theShape));
447 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
448 // correction of deviation for them should not influence to the application performance
449 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
450 bool isConstruction = !anExp.more();
454 aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
456 aDeflection = Config_PropManager::real("Visualization", "body_deflection");
458 theDrawer->SetDeviationCoefficient(aDeflection);
461 Quantity_Color color(const std::string& theSection,
462 const std::string& theName)
464 std::vector<int> aColor = Config_PropManager::color(theSection, theName);
465 return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
468 ObjectPtr getObject(const AttributePtr& theAttribute)
471 std::string anAttrType = theAttribute->attributeType();
472 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
473 AttributeRefAttrPtr anAttr =
474 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
475 if (anAttr != NULL && anAttr->isObject())
476 anObject = anAttr->object();
478 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
479 AttributeSelectionPtr anAttr =
480 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
482 anObject = anAttr->context();
484 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
485 AttributeReferencePtr anAttr =
486 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
487 if (anAttr.get() != NULL)
488 anObject = anAttr->value();
493 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
495 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
497 // for compounds check sub-shapes: it may be compound of needed type:
498 // Booleans may produce compounds of Solids
499 if (aShapeType == TopAbs_COMPOUND) {
500 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
501 if (!aSubs.Value().IsNull()) {
502 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
503 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
504 aShapeType = TopAbs_COMPOUND;
507 if (aShapeType == TopAbs_COMPOUND) {
508 aShapeType = aSubType;
509 } else if (aShapeType != aSubType) { // compound of shapes of different types
510 aShapeType = TopAbs_COMPOUND;
519 void getParameters(QStringList& theParameters)
521 theParameters.clear();
523 SessionPtr aSession = ModelAPI_Session::get();
524 std::list<DocumentPtr> aDocList;
525 DocumentPtr anActiveDocument = aSession->activeDocument();
526 DocumentPtr aRootDocument = aSession->moduleDocument();
527 aDocList.push_back(anActiveDocument);
528 if (anActiveDocument != aRootDocument) {
529 aDocList.push_back(aRootDocument);
531 std::string aGroupId = ModelAPI_ResultParameter::group();
532 for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
533 DocumentPtr aDocument = *it;
534 int aSize = aDocument->size(aGroupId);
535 for (int i = 0; i < aSize; i++) {
536 ObjectPtr anObject = aDocument->object(aGroupId, i);
537 std::string aParameterName = anObject->data()->name();
538 theParameters.append(aParameterName.c_str());
543 std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop,
544 const FeaturePtr& theFeature)
546 std::string anAttributeId;
548 std::string aXmlCfg, aDescription;
549 theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
551 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
552 std::string anAttributeTitle;
553 aFactory.getGreedAttribute(anAttributeId);
555 return anAttributeId;
558 bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
559 const std::shared_ptr<GeomAPI_Shape>& theShape,
560 ModuleBase_IWorkshop* theWorkshop,
561 const bool theTemporarily)
563 bool aHasObject = false;
564 if (!theAttribute.get())
567 std::string aType = theAttribute->attributeType();
568 if (aType == ModelAPI_AttributeReference::typeId()) {
569 AttributeReferencePtr aRef =
570 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
571 ObjectPtr aObject = aRef->value();
572 aHasObject = aObject && aObject->isSame(theObject);
573 //if (!(aObject && aObject->isSame(theObject))) {
574 // aRef->setValue(theObject);
576 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
577 AttributeRefAttrPtr aRefAttr =
578 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
580 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
581 if (anAttribute.get()) {
582 //aRefAttr->setAttr(anAttribute);
585 ObjectPtr aObject = aRefAttr->object();
586 aHasObject = aObject && aObject->isSame(theObject);
587 //if (!(aObject && aObject->isSame(theObject))) {
588 // aRefAttr->setObject(theObject);
591 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
592 /*AttributeSelectionPtr aSelectAttr =
593 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
594 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
595 if (aSelectAttr.get() != NULL) {
596 aSelectAttr->setValue(aResult, theShape, theTemporarily);
599 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
600 AttributeSelectionListPtr aSelectionListAttr =
601 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
602 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
603 aHasObject = aSelectionListAttr->isInList(aResult, theShape, theTemporarily);
605 else if (aType == ModelAPI_AttributeRefList::typeId()) {
606 AttributeRefListPtr aRefListAttr =
607 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
608 aHasObject = aRefListAttr->isInList(theObject);
609 //if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject))
610 // aRefListAttr->append(theObject);
612 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
613 AttributeRefAttrListPtr aRefAttrListAttr =
614 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
615 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
617 if (anAttribute.get()) {
618 aHasObject = aRefAttrListAttr->isInList(anAttribute);
619 //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
620 // aRefAttrListAttr->append(anAttribute);
623 aHasObject = aRefAttrListAttr->isInList(theObject);
624 //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
625 // aRefAttrListAttr->append(theObject);
631 bool setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
632 const GeomShapePtr& theShape, ModuleBase_IWorkshop* theWorkshop,
633 const bool theTemporarily, const bool theCheckIfAttributeHasObject)
635 if (!theAttribute.get())
639 std::string aType = theAttribute->attributeType();
640 if (aType == ModelAPI_AttributeReference::typeId()) {
641 AttributeReferencePtr aRef =
642 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
643 ObjectPtr aObject = aRef->value();
644 if (!(aObject && aObject->isSame(theObject))) {
645 aRef->setValue(theObject);
647 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
648 AttributeRefAttrPtr aRefAttr =
649 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
651 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
652 if (anAttribute.get())
653 aRefAttr->setAttr(anAttribute);
655 ObjectPtr aObject = aRefAttr->object();
656 if (!(aObject && aObject->isSame(theObject))) {
657 aRefAttr->setObject(theObject);
660 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
661 AttributeSelectionPtr aSelectAttr =
662 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
663 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
664 if (aSelectAttr.get() != NULL) {
665 aSelectAttr->setValue(aResult, theShape, theTemporarily);
668 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
669 AttributeSelectionListPtr aSelectionListAttr =
670 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
671 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
672 if (!theCheckIfAttributeHasObject ||
673 !aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
674 aSelectionListAttr->append(aResult, theShape, theTemporarily);
676 else if (aType == ModelAPI_AttributeRefList::typeId()) {
677 AttributeRefListPtr aRefListAttr =
678 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
679 if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject)) {
681 aRefListAttr->append(theObject);
686 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
687 AttributeRefAttrListPtr aRefAttrListAttr =
688 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
689 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
691 if (anAttribute.get()) {
692 if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
693 aRefAttrListAttr->append(anAttribute);
696 if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject)) {
698 aRefAttrListAttr->append(theObject);
707 GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* theWorkshop)
710 if (!theAttribute.get())
713 std::string aType = theAttribute->attributeType();
714 if (aType == ModelAPI_AttributeReference::typeId()) {
715 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
716 AttributeRefAttrPtr aRefAttr =
717 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
718 if (aRefAttr.get() && !aRefAttr->isObject()) {
719 AttributePtr anAttribute = aRefAttr->attr();
720 aShape = theWorkshop->module()->findShape(anAttribute);
722 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
723 AttributeSelectionPtr aSelectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
725 aShape = aSelectAttr->value();
727 else // Geom2D point processing
728 aShape = theWorkshop->module()->findShape(theAttribute);
732 void flushUpdated(ObjectPtr theObject)
734 blockUpdateViewer(true);
736 // Fix the problem of not previewed results of constraints applied. Flush Create/Delete
737 // (for the sketch result) to start processing of the sketch in the solver.
738 // TODO: these flushes should be moved in a separate method provided by Model
739 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
740 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
741 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
743 blockUpdateViewer(false);
746 void blockUpdateViewer(const bool theValue)
748 // the viewer update should be blocked in order to avoid the temporary feature content
749 // when the solver processes the feature, the redisplay message can be flushed
750 // what caused the display in the viewer preliminary states of object
751 // e.g. fillet feature, angle value change
752 std::shared_ptr<Events_Message> aMsg;
754 aMsg = std::shared_ptr<Events_Message>(
755 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
758 // the viewer update should be unblocked
759 aMsg = std::shared_ptr<Events_Message>(
760 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
762 Events_Loop::loop()->send(aMsg);
765 QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
766 int theMaxLineInPixels)
768 static QFontMetrics tfm(theWidget ? theWidget->font() : QApplication::font());
769 static qreal phi = 2.618;
771 QRect aBounds = tfm.boundingRect(theValue);
772 if(aBounds.width() <= theMaxLineInPixels)
775 qreal s = aBounds.width() * aBounds.height();
776 qreal aGoldWidth = sqrt(s*phi);
778 QStringList aWords = theValue.split(" ", QString::SkipEmptyParts);
780 int n = aWords.count();
782 for (int i = 0; i < n; i++) {
783 QString aLineExt = aLine + " " + aWords[i];
784 qreal anWidthNonExt = tfm.boundingRect(aLine).width();
785 qreal anWidthExt = tfm.boundingRect(aLineExt).width();
786 qreal aDeltaNonExt = fabs(anWidthNonExt-aGoldWidth);
787 qreal aDeltaExt = fabs(anWidthExt-aGoldWidth);
788 if(aDeltaNonExt < aDeltaExt) {
790 aLines.append(aLine);
798 aLines.append(aLine);
800 QString aResult = aLines.join("\n");
804 //**************************************************************
805 QLocale doubleLocale()
807 // VSR 01/07/2010: Disable thousands separator for spin box
808 // (to avoid inconsistency of double-2-string and string-2-double conversion)
810 aLocale.setNumberOptions(aLocale.numberOptions() |
811 QLocale::OmitGroupSeparator |
812 QLocale::RejectGroupSeparator);
816 //**************************************************************
817 void refsToFeatureInFeatureDocument(const ObjectPtr& theObject,
818 std::set<FeaturePtr>& theRefFeatures)
820 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
821 if (aFeature.get()) {
822 DocumentPtr aFeatureDoc = aFeature->document();
823 // 1. find references in the current document
824 aFeatureDoc->refsToFeature(aFeature, theRefFeatures, false);
829 //**************************************************************
830 /*bool isSubOfComposite(const ObjectPtr& theObject)
833 std::set<FeaturePtr> aRefFeatures;
834 refsToFeatureInFeatureDocument(theObject, aRefFeatures);
835 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
836 aLast = aRefFeatures.end();
837 for (; anIt != aLast && !isSub; anIt++) {
838 isSub = isSubOfComposite(theObject, *anIt);
843 //**************************************************************
844 /*bool isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature)
847 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
848 if (aComposite.get()) {
849 isSub = aComposite->isSub(theObject);
850 // the recursive is possible, the parameters are sketch circle and extrusion cut. They are
851 // separated by composite sketch feature
853 int aNbSubs = aComposite->numberOfSubs();
854 for (int aSub = 0; aSub < aNbSubs && !isSub; aSub++) {
855 isSub = isSubOfComposite(theObject, aComposite->subFeature(aSub));
862 //**************************************************************
863 ResultPtr firstResult(const ObjectPtr& theObject)
865 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
866 if (!aResult.get()) {
867 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
869 aResult = aFeature->firstResult();
874 //**************************************************************
875 bool isFeatureOfResult(const FeaturePtr& theFeature, const std::string& theGroupOfResult)
877 bool isResult = false;
879 if (!theFeature->data()->isValid())
882 ResultPtr aFirstResult = theFeature->firstResult();
883 if (!aFirstResult.get())
886 return aFirstResult->groupName() == theGroupOfResult;
889 //**************************************************************
890 bool hasModuleDocumentFeature(const std::set<FeaturePtr>& theFeatures)
892 bool aFoundModuleDocumentObject = false;
893 DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
895 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
896 for (; anIt != aLast && !aFoundModuleDocumentObject; anIt++) {
897 FeaturePtr aFeature = *anIt;
898 ResultPtr aResult = ModuleBase_Tools::firstResult(aFeature);
899 if (aResult.get() && aResult->groupName() == ModelAPI_ResultPart::group())
901 aFoundModuleDocumentObject = aFeature->document() == aModuleDoc;
904 return aFoundModuleDocumentObject;
907 //**************************************************************
908 bool askToDelete(const std::set<FeaturePtr> theFeatures,
909 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
911 std::set<FeaturePtr>& theReferencesToDelete,
912 const std::string& thePrefixInfo)
914 QString aNotActivatedDocWrn;
915 std::string aNotActivatedNames;
916 if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
917 if (ModuleBase_Tools::hasModuleDocumentFeature(theFeatures))
918 aNotActivatedDocWrn =
919 QObject::tr("Selected objects can be used in Part documents which are not loaded:%1.\n")
920 .arg(aNotActivatedNames.c_str());
923 std::set<FeaturePtr> aFeaturesRefsTo;
924 std::set<FeaturePtr> aFeaturesRefsToParameter;
925 std::set<FeaturePtr> aParameterFeatures;
926 QStringList aPartFeatureNames;
927 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
928 aLast = theFeatures.end();
929 // separate features to references to parameter features and references to others
930 for (; anIt != aLast; anIt++) {
931 FeaturePtr aFeature = *anIt;
932 if (theReferences.find(aFeature) == theReferences.end())
935 if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
936 aPartFeatureNames.append(aFeature->name().c_str());
938 std::set<FeaturePtr> aRefFeatures;
939 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
940 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
941 for (; aRefIt != aRefLast; aRefIt++) {
942 FeaturePtr aRefFeature = *aRefIt;
943 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
944 aRefFeatures.find(aRefFeature) == aRefFeatures.end()) // it is not added
945 aRefFeatures.insert(aRefFeature);
948 if (isFeatureOfResult(aFeature, ModelAPI_ResultParameter::group())) {
949 aFeaturesRefsToParameter.insert(aRefFeatures.begin(), aRefFeatures.end());
950 aParameterFeatures.insert(aFeature);
953 theReferencesToDelete.insert(aRefFeatures.begin(), aRefFeatures.end());
957 std::set<FeaturePtr> aFeaturesRefsToParameterOnly;
958 anIt = aFeaturesRefsToParameter.begin();
959 aLast = aFeaturesRefsToParameter.end();
960 // separate features to references to parameter features and references to others
961 QStringList aParamFeatureNames;
962 for (; anIt != aLast; anIt++) {
963 FeaturePtr aFeature = *anIt;
964 if (theReferencesToDelete.find(aFeature) == theReferencesToDelete.end()) {
965 aFeaturesRefsToParameterOnly.insert(aFeature);
966 aParamFeatureNames.append(aFeature->name().c_str());
969 aParamFeatureNames.sort();
970 QStringList anOtherFeatureNames;
971 anIt = theReferencesToDelete.begin();
972 aLast = theReferencesToDelete.end();
973 for (; anIt != aLast; anIt++) {
974 FeaturePtr aFeature = *anIt;
975 if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
976 aPartFeatureNames.append(aFeature->name().c_str());
978 anOtherFeatureNames.append(aFeature->name().c_str());
980 aPartFeatureNames.sort();
981 anOtherFeatureNames.sort();
983 bool aCanReplaceParameters = !aFeaturesRefsToParameterOnly.empty();
985 QMessageBox aMessageBox(theParent);
986 aMessageBox.setWindowTitle(QObject::tr("Delete features"));
987 aMessageBox.setIcon(QMessageBox::Warning);
988 aMessageBox.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
989 aMessageBox.setDefaultButton(QMessageBox::No);
992 if (!thePrefixInfo.empty())
993 aText = thePrefixInfo.c_str();
995 if (!aPartFeatureNames.empty()) {
996 aText += QString(QObject::tr("The following parts will be deleted: %1.\n"))
997 .arg(aPartFeatureNames.join(aSep));
999 if (!aNotActivatedDocWrn.isEmpty())
1000 aText += aNotActivatedDocWrn;
1001 if (!anOtherFeatureNames.empty()) {
1002 const char* aMsg = "Features are used in the following features: %1.\nThese "
1003 "features will be deleted.\n";
1004 aText += QString(QObject::tr(aMsg))
1005 .arg(anOtherFeatureNames.join(aSep));
1007 if (!aParamFeatureNames.empty()) {
1008 const char* aMsg = "Parameters are used in the following features: %1.\nThese features will "
1009 "be deleted.\nOr parameters could be replaced by their values.\n";
1010 aText += QString(QObject::tr(aMsg))
1011 .arg(aParamFeatureNames.join(aSep));
1012 QPushButton *aReplaceButton =
1013 aMessageBox.addButton(QObject::tr("Replace"), QMessageBox::ActionRole);
1016 if (!aText.isEmpty()) {
1017 aText += "Would you like to continue?";
1018 aMessageBox.setText(aText);
1020 QMessageBox::ButtonRole aButtonRole = aMessageBox.buttonRole(aMessageBox.clickedButton());
1022 if (aButtonRole == QMessageBox::NoRole)
1025 if (aButtonRole == QMessageBox::ActionRole) {
1026 foreach (FeaturePtr aObj, aParameterFeatures)
1027 ModelAPI_ReplaceParameterMessage::send(aObj, 0);
1030 theReferencesToDelete.insert(aFeaturesRefsToParameterOnly.begin(),
1031 aFeaturesRefsToParameterOnly.end());
1036 //**************************************************************
1037 void convertToFeatures(const QObjectPtrList& theObjects, std::set<FeaturePtr>& theFeatures)
1039 QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
1040 for(; anIt != aLast; anIt++) {
1041 ObjectPtr anObject = *anIt;
1042 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
1043 // for parameter result, use the corresponded reature to be removed
1044 if (!aFeature.get() && anObject->groupName() == ModelAPI_ResultParameter::group()) {
1045 aFeature = ModelAPI_Feature::feature(anObject);
1047 theFeatures.insert(aFeature);
1051 QString translate(const Events_InfoMessage& theMessage)
1055 if (!theMessage.empty()) {
1056 std::string aStr = Config_Translator::translate(theMessage);
1057 if (!aStr.empty()) {
1058 std::string aCodec = Config_Translator::codec(theMessage);
1059 aMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
1066 QString translate(const std::string& theContext, const std::string& theMessage)
1070 if (!theMessage.empty()) {
1071 std::string aStr = Config_Translator::translate(theContext, theMessage);
1072 if (!aStr.empty()) {
1073 std::string aCodec = Config_Translator::codec(theContext);
1074 aMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
1081 void setPointBallHighlighting(AIS_Shape* theAIS)
1083 static Handle(Image_AlienPixMap) aPixMap;
1084 if(aPixMap.IsNull()) {
1085 // Load icon for the presentation
1087 char* anEnv = getenv("SHAPER_ROOT_DIR");
1089 aFile = std::string(anEnv) +
1090 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
1092 anEnv = getenv("OPENPARTS_ROOT_DIR");
1094 aFile = std::string(anEnv) + FSEP + "resources";
1098 static const std::string aMarkerName = "marker_dot.png";
1099 aFile += aMarkerName;
1100 aPixMap = new Image_AlienPixMap();
1101 if(!aPixMap->Load(aFile.c_str())) {
1102 // The icon for constraint is not found
1103 static const std::string aMsg =
1104 "Error: Point market not found by path: \"" + aFile + "\". Falling back.";
1105 //Events_InfoMessage("ModuleBase_Tools::setPointBallHighlighting", aMsg).send();
1109 Handle(Graphic3d_AspectMarker3d) anAspect;
1110 Handle(Prs3d_Drawer) aDrawer = theAIS->HilightAttributes();
1112 // to do: implement ball highlighting, in 7.2.0 this drawer is NULL
1114 if(aDrawer->HasOwnPointAspect()) {
1115 Handle(Prs3d_PointAspect) aPntAspect = aDrawer->PointAspect();
1116 if(aPixMap->IsEmpty()) {
1117 anAspect = aPntAspect->Aspect();
1118 anAspect->SetType(Aspect_TOM_BALL);
1120 if(aPixMap->Format() == Image_PixMap::ImgGray) {
1121 aPixMap->SetFormat (Image_PixMap::ImgAlpha);
1122 } else if(aPixMap->Format() == Image_PixMap::ImgGrayF) {
1123 aPixMap->SetFormat (Image_PixMap::ImgAlphaF);
1125 anAspect = new Graphic3d_AspectMarker3d(aPixMap);
1126 aPntAspect->SetAspect(anAspect);
1128 aDrawer->SetPointAspect(aPntAspect);
1129 theAIS->SetHilightAttributes(aDrawer);
1134 } // namespace ModuleBase_Tools