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>
16 #include <ModelAPI_Attribute.h>
17 #include <ModelAPI_AttributeRefAttr.h>
18 #include <ModelAPI_AttributeReference.h>
19 #include <ModelAPI_AttributeSelection.h>
20 #include <ModelAPI_AttributeSelectionList.h>
21 #include <ModelAPI_AttributeRefList.h>
22 #include <ModelAPI_AttributeRefAttrList.h>
23 #include <ModelAPI_ResultPart.h>
24 #include <ModelAPI_ResultConstruction.h>
25 #include <Events_Loop.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Result.h>
29 #include <ModelAPI_ResultCompSolid.h>
30 #include <ModelAPI_ResultParameter.h>
31 #include <ModelAPI_Tools.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_Events.h>
35 #include <TopoDS_Iterator.hxx>
37 #include <GeomDataAPI_Point2D.h>
38 #include <Events_InfoMessage.h>
39 #include <GeomAPI_ShapeExplorer.h>
41 #include <Config_PropManager.h>
42 #include <Config_Translator.h>
44 #include <Prs3d_PointAspect.hxx>
45 #include <Graphic3d_AspectMarker3d.hxx>
51 #include <QDoubleSpinBox>
52 #include <QGraphicsDropShadowEffect>
54 #include <QApplication>
55 #include <QMessageBox>
62 const double tolerance = 1e-7;
63 const double DEFAULT_DEVIATION_COEFFICIENT = 1.e-4;
65 //#define DEBUG_ACTIVATE_WINDOW
66 //#define DEBUG_SET_FOCUS
67 //#define DEBUG_SELECTION_INFO
69 #ifdef DEBUG_SELECTION_INFO
70 #include <ModuleBase_ISelection.h>
71 #include <ModuleBase_ViewerPrs.h>
73 #include <AIS_InteractiveContext.hxx>
74 #include <AIS_ListOfInteractive.hxx>
75 #include <AIS_ListIteratorOfListOfInteractive.hxx>
76 #include <AIS_Shape.hxx>
77 #include <TopoDS_Shape.hxx>
78 #include <AIS_LocalContext.hxx>
79 #include <NCollection_List.hxx>
80 #include <StdSelect_BRepOwner.hxx>
81 #include <TColStd_ListOfInteger.hxx>
82 #include <TColStd_ListIteratorOfListOfInteger.hxx>
85 namespace ModuleBase_Tools {
87 //******************************************************************
89 //******************************************************************
91 void adjustMargins(QWidget* theWidget)
95 adjustMargins(theWidget->layout());
98 void adjustMargins(QLayout* theLayout)
102 theLayout->setContentsMargins(2, 5, 2, 5);
103 theLayout->setSpacing(4);
106 void zeroMargins(QWidget* theWidget)
110 zeroMargins(theWidget->layout());
113 void zeroMargins(QLayout* theLayout)
117 theLayout->setContentsMargins(0, 0, 0, 0);
118 theLayout->setSpacing(5);
121 void activateWindow(QWidget* theWidget, const QString& theInfo)
123 theWidget->activateWindow();
125 #ifdef DEBUG_ACTIVATE_WINDOW
126 qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
130 void setFocus(QWidget* theWidget, const QString& theInfo)
132 theWidget->setFocus();
134 #ifdef DEBUG_SET_FOCUS
135 qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
139 void setShadowEffect(QWidget* theWidget, const bool isSetEffect)
142 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
143 aGlowEffect->setOffset(.0);
144 aGlowEffect->setBlurRadius(10.0);
145 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
146 theWidget->setGraphicsEffect(aGlowEffect);
149 QGraphicsEffect* anEffect = theWidget->graphicsEffect();
151 anEffect->deleteLater();
152 theWidget->setGraphicsEffect(NULL);
156 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
158 QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
159 QImage anAditional(theAdditionalIcon);
164 int anAddWidth = anAditional.width();
165 int anAddHeight = anAditional.height();
167 int aWidth = anIcon.width();
168 int aHeight = anIcon.height();
170 int aStartWidthPos = aWidth - anAddWidth - 1;
171 int aStartHeightPos = aHeight - anAddHeight - 1;
173 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
175 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
177 if (qAlpha(anAditional.pixel(i, j)) > 0)
178 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
181 return QPixmap::fromImage(anIcon);
184 QPixmap lighter(const QString& theIcon, const int theLighterValue)
186 QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
190 QImage aResult = ModuleBase_IconFactory::loadImage(theIcon);
191 for (int i = 0; i < anIcon.width(); i++)
193 for (int j = 0; j < anIcon.height(); j++)
195 QRgb anRgb = anIcon.pixel(i, j);
196 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
197 qAlpha(aResult.pixel(i, j)));
199 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
200 aResult.setPixel(i, j, qRgba(aLighterColor.red(), aLighterColor.green(),
201 aLighterColor.blue(), aLighterColor.alpha()));
204 return QPixmap::fromImage(aResult);
207 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
209 if (theSpin->text() == theText)
211 // In order to avoid extra text setting because it will
212 // reset cursor position in control
213 bool isBlocked = theSpin->blockSignals(true);
214 theSpin->setText(theText);
215 theSpin->blockSignals(isBlocked);
218 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
220 if (fabs(theSpin->value() - theValue) < tolerance)
222 bool isBlocked = theSpin->blockSignals(true);
223 theSpin->setValue(theValue);
224 theSpin->blockSignals(isBlocked);
227 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
229 if (fabs(theSpin->value() - theValue) < tolerance)
231 bool isBlocked = theSpin->blockSignals(true);
232 theSpin->setValue(theValue);
233 theSpin->blockSignals(isBlocked);
236 void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText)
238 // In order to avoid extra text setting because it will
239 // reset cursor position in control
240 if (theSpin->text() == theText)
242 bool isBlocked = theSpin->blockSignals(true);
243 theSpin->setText(theText);
244 theSpin->blockSignals(isBlocked);
247 void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue)
249 if (theSpin->value() == theValue)
251 bool isBlocked = theSpin->blockSignals(true);
252 theSpin->setValue(theValue);
253 theSpin->blockSignals(isBlocked);
256 QAction* createAction(const QIcon& theIcon, const QString& theText,
257 QObject* theParent, const QObject* theReceiver,
258 const char* theMember, const QString& theToolTip,
259 const QString& theStatusTip)
261 QAction* anAction = new QAction(theIcon, theText, theParent);
262 anAction->setToolTip(theToolTip.isEmpty() ? theText : theToolTip);
263 anAction->setStatusTip(!theStatusTip.isEmpty() ? theStatusTip :
264 (!theToolTip.isEmpty() ? theToolTip : theText));
266 QObject::connect(anAction, SIGNAL(triggered(bool)), theReceiver, theMember);
271 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
273 QString aFeatureStr = "feature";
277 std::ostringstream aPtrStr;
278 aPtrStr << "[" << theObj.get() << "]";
280 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
281 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
283 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
284 if (aRes->isDisabled())
285 aFeatureStr.append("[disabled]");
286 if (aRes->isConcealed())
287 aFeatureStr.append("[concealed]");
288 if (ModelAPI_Tools::hasSubResults(aRes))
289 aFeatureStr.append("[hasSubResults]");
291 aFeature = ModelAPI_Feature::feature(aRes);
294 aFeatureStr.append(aPtrStr.str().c_str());
296 if (aFeature.get()) {
297 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
298 if (aFeature->data()->isValid()) {
299 aFeatureStr.append(QString(", name=%1").arg(theObj->data()->name().c_str()).toStdString()
302 if (isUseAttributesInfo) {
303 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
304 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
306 for(; anIt != aLast; anIt++) {
307 AttributePtr anAttr = *anIt;
308 QString aValue = "not defined";
309 std::string aType = anAttr->attributeType();
310 if (aType == GeomDataAPI_Point2D::typeId()) {
311 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
314 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
316 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
319 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
321 if (!aValues.empty())
322 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
329 #ifdef DEBUG_SELECTION_INFO
330 QString getShapeTypeInfo(const int theType)
332 QString anInfo = "Undefined";
334 case TopAbs_COMPOUND: anInfo = "compound"; break;
335 case TopAbs_COMPSOLID: anInfo = "compsolid"; break;
336 case TopAbs_SOLID: anInfo = "solid"; break;
337 case TopAbs_SHELL: anInfo = "shell"; break;
338 case TopAbs_FACE: anInfo = "face"; break;
339 case TopAbs_WIRE: anInfo = "wire"; break;
340 case TopAbs_EDGE: anInfo = "edge"; break;
341 case TopAbs_VERTEX: anInfo = "vertex"; break;
342 case TopAbs_SHAPE: anInfo = "shape"; break;
348 QString getModeInfo(const int theMode)
350 QString anInfo = "Undefined";
352 case 0: anInfo = "SHAPE(0)"; break;
353 case 1: anInfo = "VERTEX(1)"; break;
354 case 2: anInfo = "EDGE(2)"; break;
355 case 3: anInfo = "WIRE(3)"; break;
356 case 4: anInfo = "FACE(4)"; break;
357 case 5: anInfo = "SHELL(5)"; break;
358 case 6: anInfo = "SOLID(6)"; break;
359 case 7: anInfo = "COMPSOLID(7)"; break;
360 case 8: anInfo = "COMPOUND(8)"; break;
361 case 100: anInfo = "Sel_Mode_First(100)"; break; //SketcherPrs_Tools
362 case 101: anInfo = "Sel_Constraint(101)"; break;
363 case 102: anInfo = "Sel_Dimension_All(102)"; break;
364 case 103: anInfo = "Sel_Dimension_Line(103)"; break;
365 case 104: anInfo = "Sel_Dimension_Text(104)"; break;
371 QString displayedInteractiveObjects(Handle(AIS_InteractiveContext)& theContext,
372 const bool theShapeInfoOnly = true)
374 if (theContext.IsNull())
376 AIS_ListOfInteractive aListOfIO;
377 theContext->DisplayedObjects(aListOfIO, false);
378 QStringList anObjects;
379 AIS_ListIteratorOfListOfInteractive aIt;
380 for (aIt.Initialize(aListOfIO); aIt.More(); aIt.Next()) {
381 Handle(AIS_InteractiveObject) anAISIO = aIt.Value();
382 Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISIO);
383 if (theShapeInfoOnly && aShapePrs.IsNull())
386 QString anInfo = "IO";
387 std::ostringstream aPtrStr;
388 aPtrStr << "[" << anAISIO.Access() << "]";
389 anInfo += aPtrStr.str().c_str();
391 if (!aShapePrs.IsNull()) {
392 const TopoDS_Shape& aShape = aShapePrs->Shape();
393 if (aShape.IsNull()) {
394 //Events_Error::throwException("An empty AIS presentation");
397 anInfo += QString(", shape type: %1").arg(getShapeTypeInfo(aShape.ShapeType()));
399 TColStd_ListOfInteger aTColModes;
400 theContext->ActivatedModes(anAISIO, aTColModes);
401 TColStd_ListIteratorOfListOfInteger itr( aTColModes );
402 QIntList aModesActivatedForIO;
403 bool isDeactivated = false;
405 for (; itr.More(); itr.Next() ) {
406 Standard_Integer aMode = itr.Value();
407 aModes.append(getModeInfo(aMode));
408 //int aShapeMode = (aMode > 8)? aMode : AIS_Shape::SelectionType(aMode);
410 if (aModes.size() > 0)
411 anInfo += QString(", activated modes: %1").arg(aModes.join(", "));
415 QString aValue = QString("displayedIO[%1]").arg(anObjects.size());
416 if (anObjects.size() > 0)
417 aValue += QString(":\n %1").arg(anObjects.join("\n "));
421 QString activeOwners(Handle(AIS_InteractiveContext)& theContext, const bool theShapeInfoOnly = true)
423 if (theContext.IsNull())
425 Handle(AIS_LocalContext) aLContext = theContext->LocalContext();
426 TCollection_AsciiString aSelectionName = aLContext->SelectionName();
427 aLContext->UnhilightPicked(Standard_False);
429 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
430 aLContext->MainSelector()->ActiveOwners(anActiveOwners);
431 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
432 Handle(SelectMgr_EntityOwner) anOwner;
433 QStringList anObjects;
434 for (; anOwnersIt.More(); anOwnersIt.Next()) {
435 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
436 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
437 if (theShapeInfoOnly && BROwnr.IsNull())
440 QString anInfo = "Owner";
441 std::ostringstream aPtrStr;
442 aPtrStr << "[" << anOwner.Access() << "]";
443 anInfo += aPtrStr.str().c_str();
445 Handle(AIS_InteractiveObject) aAISObj =
446 Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
447 if (!aAISObj.IsNull()) {
448 std::ostringstream aPtrStr;
449 aPtrStr << "[" << aAISObj.Access() << "]";
450 anInfo += QString(", selectable(IO): %1").arg(aPtrStr.str().c_str());
453 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
454 const TopoDS_Shape& aShape = BROwnr->Shape();
455 if (aShape.IsNull()) {
456 //Events_Error::throwException("An empty AIS presentation");
459 anInfo += QString(", shape type: %1").arg(getShapeTypeInfo(aShape.ShapeType()));
463 QString aValue = QString("activeOwners[%1]").arg(anObjects.size());
464 if (anObjects.size() > 0)
465 aValue += QString(":\n %1").arg(anObjects.join("\n "));
469 QString selectedOwners(Handle(AIS_InteractiveContext)& theContext, const bool theShapeInfoOnly = true)
471 QStringList anObjects;
472 if (theContext.IsNull())
475 QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
476 for (theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected()) {
477 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
478 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
479 if (theShapeInfoOnly && BROwnr.IsNull())
482 if (aSelectedIds.contains((long)anOwner.Access()))
484 aSelectedIds.append((long)anOwner.Access());
486 QString anInfo = "Owner";
487 std::ostringstream aPtrStr;
488 aPtrStr << "[" << anOwner.Access() << "]";
489 anInfo += aPtrStr.str().c_str();
491 if (!BROwnr.IsNull() && BROwnr->HasShape()) {
492 const TopoDS_Shape& aShape = BROwnr->Shape();
493 anInfo += QString(", shape type = %1").arg(getShapeTypeInfo(aShape.ShapeType()));
497 QString aValue = QString("selectedOwners[%1]").arg(anObjects.size());
498 if (anObjects.size() > 0)
499 aValue += QString(":\n %1").arg(anObjects.join("\n "));
504 void selectionInfo(Handle(AIS_InteractiveContext)& theContext, const std::string& thePrefix)
506 #ifdef DEBUG_SELECTION_INFO
507 QString aValue = QString("\n\n\nDEBUG_SELECTION_INFO for '%1'\n%2\n%3\n%4")
508 .arg(thePrefix.c_str())
509 .arg(displayedInteractiveObjects(theContext))
510 .arg(activeOwners(theContext))
511 .arg(selectedOwners(theContext));
512 qDebug(aValue.toStdString().c_str());
516 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
517 static ShapeTypes myShapeTypes;
519 TopAbs_ShapeEnum shapeType(const QString& theType)
521 if (myShapeTypes.count() == 0) {
522 myShapeTypes["compound"] = TopAbs_COMPOUND;
523 myShapeTypes["compounds"] = TopAbs_COMPOUND;
524 myShapeTypes["compsolid"] = TopAbs_COMPSOLID;
525 myShapeTypes["compsolids"] = TopAbs_COMPSOLID;
526 myShapeTypes["solid"] = TopAbs_SOLID;
527 myShapeTypes["solids"] = TopAbs_SOLID;
528 myShapeTypes["shell"] = TopAbs_SHELL;
529 myShapeTypes["shells"] = TopAbs_SHELL;
530 myShapeTypes["face"] = TopAbs_FACE;
531 myShapeTypes["faces"] = TopAbs_FACE;
532 myShapeTypes["wire"] = TopAbs_WIRE;
533 myShapeTypes["wires"] = TopAbs_WIRE;
534 myShapeTypes["edge"] = TopAbs_EDGE;
535 myShapeTypes["edges"] = TopAbs_EDGE;
536 myShapeTypes["vertex"] = TopAbs_VERTEX;
537 myShapeTypes["vertices"] = TopAbs_VERTEX;
538 myShapeTypes["objects"] = TopAbs_SHAPE;
540 QString aType = theType.toLower();
541 if(myShapeTypes.contains(aType))
542 return myShapeTypes[aType];
543 Events_InfoMessage("ModuleBase_Tools", "Shape type defined in XML is not implemented!").send();
547 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature,
548 bool& hasParameter, bool& hasCompositeOwner)
552 hasParameter = false;
553 hasCompositeOwner = false;
554 foreach(ObjectPtr aObj, theObjects) {
555 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
556 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
557 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
559 hasResult |= (aResult.get() != NULL);
560 hasFeature |= (aFeature.get() != NULL);
561 hasParameter |= (aConstruction.get() != NULL);
563 hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
564 if (hasFeature && hasResult && hasParameter && hasCompositeOwner)
569 bool setDefaultDeviationCoefficient(std::shared_ptr<GeomAPI_Shape> theGeomShape)
571 if (!theGeomShape.get())
573 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
574 // correction of deviation for them should not influence to the application performance
575 GeomAPI_ShapeExplorer anExp(theGeomShape, GeomAPI_Shape::FACE);
576 bool anEmpty = anExp.empty();
577 return !anExp.more();
580 void setDefaultDeviationCoefficient(const std::shared_ptr<ModelAPI_Result>& theResult,
581 const Handle(Prs3d_Drawer)& theDrawer)
583 if (!theResult.get())
585 bool aUseDeviation = false;
587 std::string aResultGroup = theResult->groupName();
588 if (aResultGroup == ModelAPI_ResultConstruction::group())
589 aUseDeviation = true;
590 else if (aResultGroup == ModelAPI_ResultBody::group()) {
591 GeomShapePtr aGeomShape = theResult->shape();
592 if (aGeomShape.get())
593 aUseDeviation = setDefaultDeviationCoefficient(aGeomShape);
596 theDrawer->SetDeviationCoefficient(DEFAULT_DEVIATION_COEFFICIENT);
599 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
600 const Handle(Prs3d_Drawer)& theDrawer)
602 if (theShape.IsNull())
605 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape());
606 aGeomShape->setImpl(new TopoDS_Shape(theShape));
607 if (setDefaultDeviationCoefficient(aGeomShape))
608 theDrawer->SetDeviationCoefficient(DEFAULT_DEVIATION_COEFFICIENT);
611 Quantity_Color color(const std::string& theSection,
612 const std::string& theName,
613 const std::string& theDefault)
615 std::vector<int> aColor = Config_PropManager::color(theSection, theName, theDefault);
616 return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
619 ObjectPtr getObject(const AttributePtr& theAttribute)
622 std::string anAttrType = theAttribute->attributeType();
623 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
624 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
625 if (anAttr != NULL && anAttr->isObject())
626 anObject = anAttr->object();
628 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
629 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
631 anObject = anAttr->context();
633 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
634 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
635 if (anAttr.get() != NULL)
636 anObject = anAttr->value();
641 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
643 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
645 // for compounds check sub-shapes: it may be compound of needed type:
646 // Booleans may produce compounds of Solids
647 if (aShapeType == TopAbs_COMPOUND) {
648 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
649 if (!aSubs.Value().IsNull()) {
650 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
651 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
652 aShapeType = TopAbs_COMPOUND;
655 if (aShapeType == TopAbs_COMPOUND) {
656 aShapeType = aSubType;
657 } else if (aShapeType != aSubType) { // compound of shapes of different types
658 aShapeType = TopAbs_COMPOUND;
667 void getParameters(QStringList& theParameters)
669 theParameters.clear();
671 SessionPtr aSession = ModelAPI_Session::get();
672 std::list<DocumentPtr> aDocList;
673 DocumentPtr anActiveDocument = aSession->activeDocument();
674 DocumentPtr aRootDocument = aSession->moduleDocument();
675 aDocList.push_back(anActiveDocument);
676 if (anActiveDocument != aRootDocument) {
677 aDocList.push_back(aRootDocument);
679 std::string aGroupId = ModelAPI_ResultParameter::group();
680 for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
681 DocumentPtr aDocument = *it;
682 int aSize = aDocument->size(aGroupId);
683 for (int i = 0; i < aSize; i++) {
684 ObjectPtr anObject = aDocument->object(aGroupId, i);
685 std::string aParameterName = anObject->data()->name();
686 theParameters.append(aParameterName.c_str());
691 std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop, const FeaturePtr& theFeature)
693 std::string anAttributeId;
695 std::string aXmlCfg, aDescription;
696 theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
698 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
699 std::string anAttributeTitle;
700 aFactory.getGreedAttribute(anAttributeId);
702 return anAttributeId;
705 bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
706 const std::shared_ptr<GeomAPI_Shape>& theShape,
707 ModuleBase_IWorkshop* theWorkshop,
708 const bool theTemporarily)
710 bool aHasObject = false;
711 if (!theAttribute.get())
714 std::string aType = theAttribute->attributeType();
715 if (aType == ModelAPI_AttributeReference::typeId()) {
716 AttributeReferencePtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
717 ObjectPtr aObject = aRef->value();
718 aHasObject = aObject && aObject->isSame(theObject);
719 //if (!(aObject && aObject->isSame(theObject))) {
720 // aRef->setValue(theObject);
722 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
723 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
725 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
726 if (anAttribute.get()) {
727 //aRefAttr->setAttr(anAttribute);
730 ObjectPtr aObject = aRefAttr->object();
731 aHasObject = aObject && aObject->isSame(theObject);
732 //if (!(aObject && aObject->isSame(theObject))) {
733 // aRefAttr->setObject(theObject);
736 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
737 /*AttributeSelectionPtr aSelectAttr =
738 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
739 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
740 if (aSelectAttr.get() != NULL) {
741 aSelectAttr->setValue(aResult, theShape, theTemporarily);
744 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
745 AttributeSelectionListPtr aSelectionListAttr =
746 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
747 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
748 aHasObject = aSelectionListAttr->isInList(aResult, theShape, theTemporarily);
749 //if (!theCheckIfAttributeHasObject || !aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
750 // aSelectionListAttr->append(aResult, theShape, theTemporarily);
752 else if (aType == ModelAPI_AttributeRefList::typeId()) {
753 AttributeRefListPtr aRefListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
754 aHasObject = aRefListAttr->isInList(theObject);
755 //if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject))
756 // aRefListAttr->append(theObject);
758 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
759 AttributeRefAttrListPtr aRefAttrListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
760 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
762 if (anAttribute.get()) {
763 aHasObject = aRefAttrListAttr->isInList(anAttribute);
764 //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
765 // aRefAttrListAttr->append(anAttribute);
768 aHasObject = aRefAttrListAttr->isInList(theObject);
769 //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
770 // aRefAttrListAttr->append(theObject);
776 void setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
777 const GeomShapePtr& theShape, ModuleBase_IWorkshop* theWorkshop,
778 const bool theTemporarily, const bool theCheckIfAttributeHasObject)
780 if (!theAttribute.get())
783 std::string aType = theAttribute->attributeType();
784 if (aType == ModelAPI_AttributeReference::typeId()) {
785 AttributeReferencePtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
786 ObjectPtr aObject = aRef->value();
787 if (!(aObject && aObject->isSame(theObject))) {
788 aRef->setValue(theObject);
790 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
791 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
793 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
794 if (anAttribute.get())
795 aRefAttr->setAttr(anAttribute);
797 ObjectPtr aObject = aRefAttr->object();
798 if (!(aObject && aObject->isSame(theObject))) {
799 aRefAttr->setObject(theObject);
802 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
803 AttributeSelectionPtr aSelectAttr =
804 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
805 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
806 if (aSelectAttr.get() != NULL) {
807 aSelectAttr->setValue(aResult, theShape, theTemporarily);
810 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
811 AttributeSelectionListPtr aSelectionListAttr =
812 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
813 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
814 if (!theCheckIfAttributeHasObject || !aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
815 aSelectionListAttr->append(aResult, theShape, theTemporarily);
817 else if (aType == ModelAPI_AttributeRefList::typeId()) {
818 AttributeRefListPtr aRefListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
819 if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject))
820 aRefListAttr->append(theObject);
822 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
823 AttributeRefAttrListPtr aRefAttrListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
824 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
826 if (anAttribute.get()) {
827 if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
828 aRefAttrListAttr->append(anAttribute);
831 if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
832 aRefAttrListAttr->append(theObject);
837 GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* theWorkshop)
840 if (!theAttribute.get())
843 std::string aType = theAttribute->attributeType();
844 if (aType == ModelAPI_AttributeReference::typeId()) {
845 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
846 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
847 if (aRefAttr.get() && !aRefAttr->isObject()) {
848 AttributePtr anAttribute = aRefAttr->attr();
849 aShape = theWorkshop->module()->findShape(anAttribute);
851 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
852 AttributeSelectionPtr aSelectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
854 aShape = aSelectAttr->value();
856 else // Geom2D point processing
857 aShape = theWorkshop->module()->findShape(theAttribute);
861 void flushUpdated(ObjectPtr theObject)
863 blockUpdateViewer(true);
865 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
867 blockUpdateViewer(false);
870 void blockUpdateViewer(const bool theValue)
872 // the viewer update should be blocked in order to avoid the temporary feature content
873 // when the solver processes the feature, the redisplay message can be flushed
874 // what caused the display in the viewer preliminary states of object
875 // e.g. fillet feature, angle value change
876 std::shared_ptr<Events_Message> aMsg;
878 aMsg = std::shared_ptr<Events_Message>(
879 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
882 // the viewer update should be unblocked
883 aMsg = std::shared_ptr<Events_Message>(
884 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
886 Events_Loop::loop()->send(aMsg);
889 QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
890 int theMaxLineInPixels)
892 static QFontMetrics tfm(theWidget ? theWidget->font() : QApplication::font());
893 static qreal phi = 2.618;
895 QRect aBounds = tfm.boundingRect(theValue);
896 if(aBounds.width() <= theMaxLineInPixels)
899 qreal s = aBounds.width() * aBounds.height();
900 qreal aGoldWidth = sqrt(s*phi);
902 QStringList aWords = theValue.split(" ", QString::SkipEmptyParts);
904 int n = aWords.count();
906 for (int i = 0; i < n; i++) {
907 QString aLineExt = aLine + " " + aWords[i];
908 qreal anWidthNonExt = tfm.boundingRect(aLine).width();
909 qreal anWidthExt = tfm.boundingRect(aLineExt).width();
910 qreal aDeltaNonExt = fabs(anWidthNonExt-aGoldWidth);
911 qreal aDeltaExt = fabs(anWidthExt-aGoldWidth);
912 if(aDeltaNonExt < aDeltaExt) {
914 aLines.append(aLine);
922 aLines.append(aLine);
924 QString aResult = aLines.join("\n");
928 //**************************************************************
929 void refsToFeatureInFeatureDocument(const ObjectPtr& theObject, std::set<FeaturePtr>& theRefFeatures)
931 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
932 if (aFeature.get()) {
933 DocumentPtr aFeatureDoc = aFeature->document();
934 // 1. find references in the current document
935 aFeatureDoc->refsToFeature(aFeature, theRefFeatures, false);
940 //**************************************************************
941 /*bool isSubOfComposite(const ObjectPtr& theObject)
944 std::set<FeaturePtr> aRefFeatures;
945 refsToFeatureInFeatureDocument(theObject, aRefFeatures);
946 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
947 aLast = aRefFeatures.end();
948 for (; anIt != aLast && !isSub; anIt++) {
949 isSub = isSubOfComposite(theObject, *anIt);
954 //**************************************************************
955 /*bool isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature)
958 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
959 if (aComposite.get()) {
960 isSub = aComposite->isSub(theObject);
961 // the recursive is possible, the parameters are sketch circle and extrusion cut. They are
962 // separated by composite sketch feature
964 int aNbSubs = aComposite->numberOfSubs();
965 for (int aSub = 0; aSub < aNbSubs && !isSub; aSub++) {
966 isSub = isSubOfComposite(theObject, aComposite->subFeature(aSub));
973 //**************************************************************
974 ResultPtr firstResult(const ObjectPtr& theObject)
976 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
977 if (!aResult.get()) {
978 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
980 aResult = aFeature->firstResult();
985 //**************************************************************
986 bool isFeatureOfResult(const FeaturePtr& theFeature, const std::string& theGroupOfResult)
988 bool isResult = false;
990 if (!theFeature->data()->isValid())
993 ResultPtr aFirstResult = theFeature->firstResult();
994 if (!aFirstResult.get())
997 return aFirstResult->groupName() == theGroupOfResult;
1000 //**************************************************************
1001 bool hasModuleDocumentFeature(const std::set<FeaturePtr>& theFeatures)
1003 bool aFoundModuleDocumentObject = false;
1004 DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
1006 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
1007 for (; anIt != aLast && !aFoundModuleDocumentObject; anIt++) {
1008 FeaturePtr aFeature = *anIt;
1009 ResultPtr aResult = ModuleBase_Tools::firstResult(aFeature);
1010 if (aResult.get() && aResult->groupName() == ModelAPI_ResultPart::group())
1012 aFoundModuleDocumentObject = aFeature->document() == aModuleDoc;
1015 return aFoundModuleDocumentObject;
1018 //**************************************************************
1019 bool askToDelete(const std::set<FeaturePtr> theFeatures,
1020 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
1022 std::set<FeaturePtr>& theReferencesToDelete)
1024 QString aNotActivatedDocWrn;
1025 std::string aNotActivatedNames;
1026 if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
1027 if (ModuleBase_Tools::hasModuleDocumentFeature(theFeatures))
1028 aNotActivatedDocWrn = QObject::tr("Selected objects can be used in Part documents which are not loaded:%1.\n")
1029 .arg(aNotActivatedNames.c_str());
1032 std::set<FeaturePtr> aFeaturesRefsTo;
1033 std::set<FeaturePtr> aFeaturesRefsToParameter;
1034 std::set<FeaturePtr> aParameterFeatures;
1035 QStringList aPartFeatureNames;
1036 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
1037 aLast = theFeatures.end();
1038 // separate features to references to parameter features and references to others
1039 for (; anIt != aLast; anIt++) {
1040 FeaturePtr aFeature = *anIt;
1041 if (theReferences.find(aFeature) == theReferences.end())
1044 if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
1045 aPartFeatureNames.append(aFeature->name().c_str());
1047 std::set<FeaturePtr> aRefFeatures;
1048 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
1049 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
1050 for (; aRefIt != aRefLast; aRefIt++) {
1051 FeaturePtr aRefFeature = *aRefIt;
1052 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
1053 aRefFeatures.find(aRefFeature) == aRefFeatures.end()) // it is not added
1054 aRefFeatures.insert(aRefFeature);
1057 if (isFeatureOfResult(aFeature, ModelAPI_ResultParameter::group())) {
1058 aFeaturesRefsToParameter.insert(aRefFeatures.begin(), aRefFeatures.end());
1059 aParameterFeatures.insert(aFeature);
1062 theReferencesToDelete.insert(aRefFeatures.begin(), aRefFeatures.end());
1066 std::set<FeaturePtr> aFeaturesRefsToParameterOnly;
1067 anIt = aFeaturesRefsToParameter.begin();
1068 aLast = aFeaturesRefsToParameter.end();
1069 // separate features to references to parameter features and references to others
1070 QStringList aParamFeatureNames;
1071 for (; anIt != aLast; anIt++) {
1072 FeaturePtr aFeature = *anIt;
1073 if (theReferencesToDelete.find(aFeature) == theReferencesToDelete.end()) {
1074 aFeaturesRefsToParameterOnly.insert(aFeature);
1075 aParamFeatureNames.append(aFeature->name().c_str());
1078 aParamFeatureNames.sort();
1079 QStringList anOtherFeatureNames;
1080 anIt = theReferencesToDelete.begin();
1081 aLast = theReferencesToDelete.end();
1082 for (; anIt != aLast; anIt++) {
1083 FeaturePtr aFeature = *anIt;
1084 if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
1085 aPartFeatureNames.append(aFeature->name().c_str());
1087 anOtherFeatureNames.append(aFeature->name().c_str());
1089 aPartFeatureNames.sort();
1090 anOtherFeatureNames.sort();
1092 bool aCanReplaceParameters = !aFeaturesRefsToParameterOnly.empty();
1094 QMessageBox aMessageBox(theParent);
1095 aMessageBox.setWindowTitle(QObject::tr("Delete features"));
1096 aMessageBox.setIcon(QMessageBox::Warning);
1097 aMessageBox.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
1098 aMessageBox.setDefaultButton(QMessageBox::No);
1101 QString aSep = ", ";
1102 if (!aPartFeatureNames.empty())
1103 aText += QString(QObject::tr("The following parts will be deleted: %1.\n")).arg(aPartFeatureNames.join(aSep));
1104 if (!aNotActivatedDocWrn.isEmpty())
1105 aText += aNotActivatedDocWrn;
1106 if (!anOtherFeatureNames.empty())
1107 aText += QString(QObject::tr("Selected features are used in the following features: %1.\nThese features will be deleted.\n"))
1108 .arg(anOtherFeatureNames.join(aSep));
1109 if (!aParamFeatureNames.empty()) {
1110 aText += QString(QObject::tr("Selected parameters are used in the following features: %1.\nThese features will be deleted.\nOr parameters could be replaced by their values.\n"))
1111 .arg(aParamFeatureNames.join(aSep));
1112 QPushButton *aReplaceButton = aMessageBox.addButton(QObject::tr("Replace"), QMessageBox::ActionRole);
1115 if (!aText.isEmpty()) {
1116 aText += "Would you like to continue?";
1117 aMessageBox.setText(aText);
1119 QMessageBox::ButtonRole aButtonRole = aMessageBox.buttonRole(aMessageBox.clickedButton());
1121 if (aButtonRole == QMessageBox::NoRole)
1124 if (aButtonRole == QMessageBox::ActionRole) {
1125 foreach (FeaturePtr aObj, aParameterFeatures)
1126 ModelAPI_ReplaceParameterMessage::send(aObj, 0);
1129 theReferencesToDelete.insert(aFeaturesRefsToParameterOnly.begin(), aFeaturesRefsToParameterOnly.end());
1134 //**************************************************************
1135 void convertToFeatures(const QObjectPtrList& theObjects, std::set<FeaturePtr>& theFeatures)
1137 QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
1138 for(; anIt != aLast; anIt++) {
1139 ObjectPtr anObject = *anIt;
1140 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
1141 // for parameter result, use the corresponded reature to be removed
1142 if (!aFeature.get() && anObject->groupName() == ModelAPI_ResultParameter::group()) {
1143 aFeature = ModelAPI_Feature::feature(anObject);
1145 theFeatures.insert(aFeature);
1149 void translate(const std::string& theContext, std::string& theMessage)
1151 if (!theMessage.empty()) {
1152 std::string aStr = Config_Translator::translate(theContext, theMessage);
1153 if (!aStr.empty()) {
1154 std::string aCodec = Config_Translator::codec(theContext);
1155 theMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str()).toStdString();
1160 void setPointBallHighlighting(AIS_Shape* theAIS)
1162 Handle(Prs3d_Drawer) aDrawer = theAIS->HilightAttributes();
1163 if (aDrawer->HasOwnPointAspect()) {
1164 Handle(Prs3d_PointAspect) aPntAspect = aDrawer->PointAspect();
1165 aPntAspect->Aspect()->SetType(Aspect_TOM_BALL);
1166 aDrawer->SetPointAspect(aPntAspect);
1167 theAIS->SetHilightAttributes(aDrawer);
1171 } // namespace ModuleBase_Tools