X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_Tools.cpp;h=3d47d3cde272b10d0441cdf0de12d8978a46c3a1;hb=ac53df4563f345f48d3a7274dbbb3f2618bacbda;hp=fa47856fa7d23b918cebf5092e1402ded7935f4f;hpb=b0434962195bc456f5bbea9698761d774a14ba87;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_Tools.cpp b/src/ModuleBase/ModuleBase_Tools.cpp old mode 100755 new mode 100644 index fa47856fa..3d47d3cde --- a/src/ModuleBase/ModuleBase_Tools.cpp +++ b/src/ModuleBase/ModuleBase_Tools.cpp @@ -1,8 +1,21 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D - -// File: ModuleBase_Tools.cpp -// Created: 11 July 2014 -// Author: Vitaly Smetannikov +// Copyright (C) 2014-2019 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// #include "ModuleBase_Tools.h" @@ -11,7 +24,10 @@ #include #include #include +#include #include +#include +#include #include #include @@ -22,17 +38,26 @@ #include #include #include +#include +#include +#include #include #include #include -#include #include #include #include #include +#include + +#include +#include #include +#include +#include +#include #include #include @@ -61,28 +86,15 @@ #include #include +#ifdef WIN32 +#pragma warning(disable : 4996) // for getenv +#endif + const double tolerance = 1e-7; const double DEFAULT_DEVIATION_COEFFICIENT = 1.e-4; //#define DEBUG_ACTIVATE_WINDOW //#define DEBUG_SET_FOCUS -//#define DEBUG_SELECTION_INFO - -#ifdef DEBUG_SELECTION_INFO -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#endif #ifdef WIN32 # define FSEP "\\" @@ -128,7 +140,10 @@ void zeroMargins(QLayout* theLayout) void activateWindow(QWidget* theWidget, const QString& theInfo) { - theWidget->activateWindow(); + if (theWidget) { + theWidget->activateWindow(); + theWidget->raise(); + } #ifdef DEBUG_ACTIVATE_WINDOW qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str()); @@ -137,8 +152,10 @@ void activateWindow(QWidget* theWidget, const QString& theInfo) void setFocus(QWidget* theWidget, const QString& theInfo) { + activateWindow(theWidget); theWidget->setFocus(); - + // rectangle of focus is not visible on tool button widgets + theWidget->repaint(); #ifdef DEBUG_SET_FOCUS qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str()); #endif @@ -165,28 +182,32 @@ QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon) { QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon); QImage anAditional(theAdditionalIcon); + return composite(anAditional, anIcon); +} - if (anIcon.isNull()) +QPixmap composite(const QImage& theAdditionalIcon, QImage& theIcon) +{ + if (theIcon.isNull()) return QPixmap(); - int anAddWidth = anAditional.width(); - int anAddHeight = anAditional.height(); + int anAddWidth = theAdditionalIcon.width(); + int anAddHeight = theAdditionalIcon.height(); - int aWidth = anIcon.width(); - int aHeight = anIcon.height(); + int aWidth = theIcon.width(); + int aHeight = theIcon.height(); - int aStartWidthPos = aWidth - anAddWidth - 1; - int aStartHeightPos = aHeight - anAddHeight - 1; + int aStartWidthPos = aWidth - anAddWidth; + int aStartHeightPos = aHeight - anAddHeight; for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++) { for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++) { - if (qAlpha(anAditional.pixel(i, j)) > 0) - anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j)); + if (qAlpha(theAdditionalIcon.pixel(i, j)) > 0) + theIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, theAdditionalIcon.pixel(i, j)); } } - return QPixmap::fromImage(anIcon); + return QPixmap::fromImage(theIcon); } QPixmap lighter(const QString& theIcon, const int theLighterValue) @@ -214,7 +235,7 @@ QPixmap lighter(const QString& theIcon, const int theLighterValue) void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText) { - if (theSpin->text() == theText) + if (theSpin->text() == theText) return; // In order to avoid extra text setting because it will // reset cursor position in control @@ -276,6 +297,15 @@ QAction* createAction(const QIcon& theIcon, const QString& theText, return anAction; } +#ifdef _DEBUG +QString objectName(const ObjectPtr& theObj) +{ + if (!theObj.get()) + return ""; + + return theObj->data()->name().c_str(); +} + QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo) { QString aFeatureStr = "feature"; @@ -308,223 +338,23 @@ QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo) .c_str()); } if (isUseAttributesInfo) { - std::list anAttrs = aFeature->data()->attributes(""); - std::list::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end(); - QStringList aValues; - for(; anIt != aLast; anIt++) { - AttributePtr anAttr = *anIt; - QString aValue = "not defined"; - std::string aType = anAttr->attributeType(); - if (aType == GeomDataAPI_Point2D::typeId()) { - std::shared_ptr aPoint = std::dynamic_pointer_cast( - anAttr); - if (aPoint.get()) - aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y()); - } - else if (aType == ModelAPI_AttributeRefAttr::typeId()) { - } - - aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str()); - } - if (!aValues.empty()) - aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str())); + std::set > anAttributes; + std::string aPointsInfo = ModelGeomAlgo_Point2D::getPontAttributesInfo(aFeature, + anAttributes).c_str(); + if (!aPointsInfo.empty()) + aFeatureStr.append(QString(", attributes: %1") + .arg(aPointsInfo.c_str()).toStdString().c_str()); } } return aFeatureStr; } - -#ifdef DEBUG_SELECTION_INFO -QString getShapeTypeInfo(const int theType) -{ - QString anInfo = "Undefined"; - switch(theType) { - case TopAbs_COMPOUND: anInfo = "compound"; break; - case TopAbs_COMPSOLID: anInfo = "compsolid"; break; - case TopAbs_SOLID: anInfo = "solid"; break; - case TopAbs_SHELL: anInfo = "shell"; break; - case TopAbs_FACE: anInfo = "face"; break; - case TopAbs_WIRE: anInfo = "wire"; break; - case TopAbs_EDGE: anInfo = "edge"; break; - case TopAbs_VERTEX: anInfo = "vertex"; break; - case TopAbs_SHAPE: anInfo = "shape"; break; - default: break; - } - return anInfo; -} - -QString getModeInfo(const int theMode) -{ - QString anInfo = "Undefined"; - switch(theMode) { - case 0: anInfo = "SHAPE(0)"; break; - case 1: anInfo = "VERTEX(1)"; break; - case 2: anInfo = "EDGE(2)"; break; - case 3: anInfo = "WIRE(3)"; break; - case 4: anInfo = "FACE(4)"; break; - case 5: anInfo = "SHELL(5)"; break; - case 6: anInfo = "SOLID(6)"; break; - case 7: anInfo = "COMPSOLID(7)"; break; - case 8: anInfo = "COMPOUND(8)"; break; - case 100: anInfo = "Sel_Mode_First(100)"; break; //SketcherPrs_Tools - case 101: anInfo = "Sel_Constraint(101)"; break; - case 102: anInfo = "Sel_Dimension_All(102)"; break; - case 103: anInfo = "Sel_Dimension_Line(103)"; break; - case 104: anInfo = "Sel_Dimension_Text(104)"; break; - default: break; - } - return anInfo; -} - -QString displayedInteractiveObjects(Handle(AIS_InteractiveContext)& theContext, - const bool theShapeInfoOnly = true) -{ - if (theContext.IsNull()) - return ""; - AIS_ListOfInteractive aListOfIO; - theContext->DisplayedObjects(aListOfIO, false); - QStringList anObjects; - AIS_ListIteratorOfListOfInteractive aIt; - for (aIt.Initialize(aListOfIO); aIt.More(); aIt.Next()) { - Handle(AIS_InteractiveObject) anAISIO = aIt.Value(); - Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISIO); - if (theShapeInfoOnly && aShapePrs.IsNull()) - continue; - - QString anInfo = "IO"; - std::ostringstream aPtrStr; - aPtrStr << "[" << anAISIO.Access() << "]"; - anInfo += aPtrStr.str().c_str(); - - if (!aShapePrs.IsNull()) { - const TopoDS_Shape& aShape = aShapePrs->Shape(); - if (aShape.IsNull()) { - //Events_Error::throwException("An empty AIS presentation"); - } - else - anInfo += QString(", shape type: %1").arg(getShapeTypeInfo(aShape.ShapeType())); - } - TColStd_ListOfInteger aTColModes; - theContext->ActivatedModes(anAISIO, aTColModes); - TColStd_ListIteratorOfListOfInteger itr( aTColModes ); - QIntList aModesActivatedForIO; - bool isDeactivated = false; - QStringList aModes; - for (; itr.More(); itr.Next() ) { - Standard_Integer aMode = itr.Value(); - aModes.append(getModeInfo(aMode)); - //int aShapeMode = (aMode > 8)? aMode : AIS_Shape::SelectionType(aMode); - } - if (aModes.size() > 0) - anInfo += QString(", activated modes: %1").arg(aModes.join(", ")); - - anObjects += anInfo; - } - QString aValue = QString("displayedIO[%1]").arg(anObjects.size()); - if (anObjects.size() > 0) - aValue += QString(":\n %1").arg(anObjects.join("\n ")); - return aValue; -} - -QString activeOwners(Handle(AIS_InteractiveContext)& theContext, const bool theShapeInfoOnly = true) -{ - if (theContext.IsNull()) - return ""; - Handle(AIS_LocalContext) aLContext = theContext->LocalContext(); - TCollection_AsciiString aSelectionName = aLContext->SelectionName(); - aLContext->UnhilightPicked(Standard_False); - - NCollection_List anActiveOwners; - aLContext->MainSelector()->ActiveOwners(anActiveOwners); - NCollection_List::Iterator anOwnersIt (anActiveOwners); - Handle(SelectMgr_EntityOwner) anOwner; - QStringList anObjects; - for (; anOwnersIt.More(); anOwnersIt.Next()) { - anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value()); - Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner); - if (theShapeInfoOnly && BROwnr.IsNull()) - continue; - - QString anInfo = "Owner"; - std::ostringstream aPtrStr; - aPtrStr << "[" << anOwner.Access() << "]"; - anInfo += aPtrStr.str().c_str(); - - Handle(AIS_InteractiveObject) aAISObj = - Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable()); - if (!aAISObj.IsNull()) { - std::ostringstream aPtrStr; - aPtrStr << "[" << aAISObj.Access() << "]"; - anInfo += QString(", selectable(IO): %1").arg(aPtrStr.str().c_str()); - } - - if (!BROwnr.IsNull() && BROwnr->HasShape()) { - const TopoDS_Shape& aShape = BROwnr->Shape(); - if (aShape.IsNull()) { - //Events_Error::throwException("An empty AIS presentation"); - } - else - anInfo += QString(", shape type: %1").arg(getShapeTypeInfo(aShape.ShapeType())); - } - anObjects += anInfo; - } - QString aValue = QString("activeOwners[%1]").arg(anObjects.size()); - if (anObjects.size() > 0) - aValue += QString(":\n %1").arg(anObjects.join("\n ")); - return aValue; -} - -QString selectedOwners(Handle(AIS_InteractiveContext)& theContext, const bool theShapeInfoOnly = true) -{ - QStringList anObjects; - if (theContext.IsNull()) - return ""; - - QList aSelectedIds; // Remember of selected address in order to avoid duplicates - for (theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected()) { - Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner(); - Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner); - if (theShapeInfoOnly && BROwnr.IsNull()) - continue; - - if (aSelectedIds.contains((long)anOwner.Access())) - continue; - aSelectedIds.append((long)anOwner.Access()); - - QString anInfo = "Owner"; - std::ostringstream aPtrStr; - aPtrStr << "[" << anOwner.Access() << "]"; - anInfo += aPtrStr.str().c_str(); - - if (!BROwnr.IsNull() && BROwnr->HasShape()) { - const TopoDS_Shape& aShape = BROwnr->Shape(); - anInfo += QString(", shape type = %1").arg(getShapeTypeInfo(aShape.ShapeType())); - } - anObjects += anInfo; - } - QString aValue = QString("selectedOwners[%1]").arg(anObjects.size()); - if (anObjects.size() > 0) - aValue += QString(":\n %1").arg(anObjects.join("\n ")); - return aValue; -} #endif -void selectionInfo(Handle(AIS_InteractiveContext)& theContext, const std::string& thePrefix) -{ -#ifdef DEBUG_SELECTION_INFO - QString aValue = QString("\n\n\nDEBUG_SELECTION_INFO for '%1'\n%2\n%3\n%4") - .arg(thePrefix.c_str()) - .arg(displayedInteractiveObjects(theContext)) - .arg(activeOwners(theContext)) - .arg(selectedOwners(theContext)); - qDebug(aValue.toStdString().c_str()); -#endif -} - -typedef QMap ShapeTypes; +typedef QMap ShapeTypes; static ShapeTypes myShapeTypes; -TopAbs_ShapeEnum shapeType(const QString& theType) +int shapeType(const QString& theType) { if (myShapeTypes.count() == 0) { myShapeTypes["compound"] = TopAbs_COMPOUND; @@ -543,7 +373,8 @@ TopAbs_ShapeEnum shapeType(const QString& theType) myShapeTypes["edges"] = TopAbs_EDGE; myShapeTypes["vertex"] = TopAbs_VERTEX; myShapeTypes["vertices"] = TopAbs_VERTEX; - myShapeTypes["objects"] = TopAbs_SHAPE; + myShapeTypes["object"] = ModuleBase_ResultPrs::Sel_Result; + myShapeTypes["objects"] = ModuleBase_ResultPrs::Sel_Result; } QString aType = theType.toLower(); if(myShapeTypes.contains(aType)) @@ -553,28 +384,40 @@ TopAbs_ShapeEnum shapeType(const QString& theType) } void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, - bool& hasParameter, bool& hasCompositeOwner) + bool& hasParameter, bool& hasCompositeOwner, bool& hasResultInHistory, + bool& hasFolder) { hasResult = false; hasFeature = false; hasParameter = false; hasCompositeOwner = false; + hasResultInHistory = false; + hasFolder = false; foreach(ObjectPtr aObj, theObjects) { FeaturePtr aFeature = std::dynamic_pointer_cast(aObj); ResultPtr aResult = std::dynamic_pointer_cast(aObj); + FolderPtr aFolder = std::dynamic_pointer_cast(aObj); ResultParameterPtr aConstruction = std::dynamic_pointer_cast(aResult); + FieldStepPtr aStep = std::dynamic_pointer_cast(aObj); - hasResult |= (aResult.get() != NULL); + hasResult |= ((aResult.get() != NULL) || (aStep.get() != NULL)); hasFeature |= (aFeature.get() != NULL); + hasFolder |= (aFolder.get() != NULL); hasParameter |= (aConstruction.get() != NULL); - if (hasFeature) + if (hasFeature) hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL); - if (hasFeature && hasResult && hasParameter && hasCompositeOwner) + + if (!hasResultInHistory && aResult.get()) { + FeaturePtr aFeature = ModelAPI_Feature::feature(aResult); + hasResultInHistory = aFeature.get() && aFeature->isInHistory(); + } + + if (hasFeature && hasResult && hasParameter && hasCompositeOwner && hasFeature) break; } } -bool setDefaultDeviationCoefficient(std::shared_ptr theGeomShape) +/*bool setDefaultDeviationCoefficient(std::shared_ptr theGeomShape) { if (!theGeomShape.get()) return false; @@ -583,9 +426,9 @@ bool setDefaultDeviationCoefficient(std::shared_ptr theGeomShape) GeomAPI_ShapeExplorer anExp(theGeomShape, GeomAPI_Shape::FACE); bool anEmpty = anExp.empty(); return !anExp.more(); -} +}*/ -void setDefaultDeviationCoefficient(const std::shared_ptr& theResult, +/*void setDefaultDeviationCoefficient(const std::shared_ptr& theResult, const Handle(Prs3d_Drawer)& theDrawer) { if (!theResult.get()) @@ -603,7 +446,7 @@ void setDefaultDeviationCoefficient(const std::shared_ptr& theR if (aUseDeviation) theDrawer->SetDeviationCoefficient(DEFAULT_DEVIATION_COEFFICIENT); } - +*/ void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape, const Handle(Prs3d_Drawer)& theDrawer) { @@ -612,15 +455,25 @@ void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape, std::shared_ptr aGeomShape(new GeomAPI_Shape()); aGeomShape->setImpl(new TopoDS_Shape(theShape)); - if (setDefaultDeviationCoefficient(aGeomShape)) - theDrawer->SetDeviationCoefficient(DEFAULT_DEVIATION_COEFFICIENT); + + // if the shape could not be exploded on faces, it contains only wires, edges, and vertices + // correction of deviation for them should not influence to the application performance + GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE); + bool isConstruction = !anExp.more(); + + double aDeflection; + if (isConstruction) + aDeflection = Config_PropManager::real("Visualization", "construction_deflection"); + else + aDeflection = Config_PropManager::real("Visualization", "body_deflection"); + + theDrawer->SetDeviationCoefficient(aDeflection); } Quantity_Color color(const std::string& theSection, - const std::string& theName, - const std::string& theDefault) + const std::string& theName) { - std::vector aColor = Config_PropManager::color(theSection, theName, theDefault); + std::vector aColor = Config_PropManager::color(theSection, theName); return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB); } @@ -629,17 +482,20 @@ ObjectPtr getObject(const AttributePtr& theAttribute) ObjectPtr anObject; std::string anAttrType = theAttribute->attributeType(); if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) { - AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast(theAttribute); + AttributeRefAttrPtr anAttr = + std::dynamic_pointer_cast(theAttribute); if (anAttr != NULL && anAttr->isObject()) anObject = anAttr->object(); } if (anAttrType == ModelAPI_AttributeSelection::typeId()) { - AttributeSelectionPtr anAttr = std::dynamic_pointer_cast(theAttribute); + AttributeSelectionPtr anAttr = + std::dynamic_pointer_cast(theAttribute); if (anAttr != NULL) anObject = anAttr->context(); } if (anAttrType == ModelAPI_AttributeReference::typeId()) { - AttributeReferencePtr anAttr = std::dynamic_pointer_cast(theAttribute); + AttributeReferencePtr anAttr = + std::dynamic_pointer_cast(theAttribute); if (anAttr.get() != NULL) anObject = anAttr->value(); } @@ -672,6 +528,18 @@ TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape) return aShapeType; } +TopoDS_Shape getSelectedShape(const std::shared_ptr& thePrs) +{ + if (thePrs->shape().get()) + return thePrs->shape()->impl(); + + Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(thePrs->owner()); + if (!anOwner.IsNull()) + return anOwner->Shape(); + + return TopoDS_Shape(); +} + void getParameters(QStringList& theParameters) { theParameters.clear(); @@ -696,7 +564,8 @@ void getParameters(QStringList& theParameters) } } -std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop, const FeaturePtr& theFeature) +std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop, + const FeaturePtr& theFeature) { std::string anAttributeId; @@ -721,14 +590,16 @@ bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject, std::string aType = theAttribute->attributeType(); if (aType == ModelAPI_AttributeReference::typeId()) { - AttributeReferencePtr aRef = std::dynamic_pointer_cast(theAttribute); + AttributeReferencePtr aRef = + std::dynamic_pointer_cast(theAttribute); ObjectPtr aObject = aRef->value(); aHasObject = aObject && aObject->isSame(theObject); //if (!(aObject && aObject->isSame(theObject))) { // aRef->setValue(theObject); //} } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { - AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast(theAttribute); + AttributeRefAttrPtr aRefAttr = + std::dynamic_pointer_cast(theAttribute); AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape); if (anAttribute.get()) { @@ -754,17 +625,17 @@ bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject, std::dynamic_pointer_cast(theAttribute); ResultPtr aResult = std::dynamic_pointer_cast(theObject); aHasObject = aSelectionListAttr->isInList(aResult, theShape, theTemporarily); - //if (!theCheckIfAttributeHasObject || !aSelectionListAttr->isInList(aResult, theShape, theTemporarily)) - // aSelectionListAttr->append(aResult, theShape, theTemporarily); } else if (aType == ModelAPI_AttributeRefList::typeId()) { - AttributeRefListPtr aRefListAttr = std::dynamic_pointer_cast(theAttribute); + AttributeRefListPtr aRefListAttr = + std::dynamic_pointer_cast(theAttribute); aHasObject = aRefListAttr->isInList(theObject); //if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject)) // aRefListAttr->append(theObject); } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) { - AttributeRefAttrListPtr aRefAttrListAttr = std::dynamic_pointer_cast(theAttribute); + AttributeRefAttrListPtr aRefAttrListAttr = + std::dynamic_pointer_cast(theAttribute); AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape); if (anAttribute.get()) { @@ -781,22 +652,25 @@ bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject, return aHasObject; } -void setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject, +bool setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject, const GeomShapePtr& theShape, ModuleBase_IWorkshop* theWorkshop, const bool theTemporarily, const bool theCheckIfAttributeHasObject) { if (!theAttribute.get()) - return; + return false; + bool isDone = true; std::string aType = theAttribute->attributeType(); if (aType == ModelAPI_AttributeReference::typeId()) { - AttributeReferencePtr aRef = std::dynamic_pointer_cast(theAttribute); + AttributeReferencePtr aRef = + std::dynamic_pointer_cast(theAttribute); ObjectPtr aObject = aRef->value(); if (!(aObject && aObject->isSame(theObject))) { aRef->setValue(theObject); } } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { - AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast(theAttribute); + AttributeRefAttrPtr aRefAttr = + std::dynamic_pointer_cast(theAttribute); AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape); if (anAttribute.get()) @@ -810,25 +684,30 @@ void setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject, } else if (aType == ModelAPI_AttributeSelection::typeId()) { AttributeSelectionPtr aSelectAttr = std::dynamic_pointer_cast(theAttribute); - ResultPtr aResult = std::dynamic_pointer_cast(theObject); if (aSelectAttr.get() != NULL) { - aSelectAttr->setValue(aResult, theShape, theTemporarily); + aSelectAttr->setValue(theObject, theShape, theTemporarily); } } if (aType == ModelAPI_AttributeSelectionList::typeId()) { AttributeSelectionListPtr aSelectionListAttr = std::dynamic_pointer_cast(theAttribute); - ResultPtr aResult = std::dynamic_pointer_cast(theObject); - if (!theCheckIfAttributeHasObject || !aSelectionListAttr->isInList(aResult, theShape, theTemporarily)) - aSelectionListAttr->append(aResult, theShape, theTemporarily); + if (!theCheckIfAttributeHasObject || + !aSelectionListAttr->isInList(theObject, theShape, theTemporarily)) + aSelectionListAttr->append(theObject, theShape, theTemporarily); } else if (aType == ModelAPI_AttributeRefList::typeId()) { - AttributeRefListPtr aRefListAttr = std::dynamic_pointer_cast(theAttribute); - if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject)) - aRefListAttr->append(theObject); + AttributeRefListPtr aRefListAttr = + std::dynamic_pointer_cast(theAttribute); + if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject)) { + if (theObject.get()) + aRefListAttr->append(theObject); + else + isDone = false; + } } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) { - AttributeRefAttrListPtr aRefAttrListAttr = std::dynamic_pointer_cast(theAttribute); + AttributeRefAttrListPtr aRefAttrListAttr = + std::dynamic_pointer_cast(theAttribute); AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape); if (anAttribute.get()) { @@ -836,10 +715,15 @@ void setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject, aRefAttrListAttr->append(anAttribute); } else { - if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject)) - aRefAttrListAttr->append(theObject); + if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject)) { + if (theObject.get()) + aRefAttrListAttr->append(theObject); + else + isDone = false; + } } } + return isDone; } GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* theWorkshop) @@ -851,7 +735,8 @@ GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* th std::string aType = theAttribute->attributeType(); if (aType == ModelAPI_AttributeReference::typeId()) { } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { - AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast(theAttribute); + AttributeRefAttrPtr aRefAttr = + std::dynamic_pointer_cast(theAttribute); if (aRefAttr.get() && !aRefAttr->isObject()) { AttributePtr anAttribute = aRefAttr->attr(); aShape = theWorkshop->module()->findShape(anAttribute); @@ -870,7 +755,12 @@ void flushUpdated(ObjectPtr theObject) { blockUpdateViewer(true); + // Fix the problem of not previewed results of constraints applied. Flush Create/Delete + // (for the sketch result) to start processing of the sketch in the solver. + // TODO: these flushes should be moved in a separate method provided by Model + Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED)); Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED)); + Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED)); blockUpdateViewer(false); } @@ -912,7 +802,7 @@ QString wrapTextByWords(const QString& theValue, QWidget* theWidget, int n = aWords.count(); QString aLine; for (int i = 0; i < n; i++) { - QString aLineExt = aLine + " " + aWords[i]; + QString aLineExt = i == 0 ? aWords[i] : aLine + " " + aWords[i]; qreal anWidthNonExt = tfm.boundingRect(aLine).width(); qreal anWidthExt = tfm.boundingRect(aLineExt).width(); qreal aDeltaNonExt = fabs(anWidthNonExt-aGoldWidth); @@ -934,7 +824,20 @@ QString wrapTextByWords(const QString& theValue, QWidget* theWidget, } //************************************************************** -void refsToFeatureInFeatureDocument(const ObjectPtr& theObject, std::set& theRefFeatures) +QLocale doubleLocale() +{ + // VSR 01/07/2010: Disable thousands separator for spin box + // (to avoid inconsistency of double-2-string and string-2-double conversion) + QLocale aLocale; + aLocale.setNumberOptions(aLocale.numberOptions() | + QLocale::OmitGroupSeparator | + QLocale::RejectGroupSeparator); + return aLocale; +} + +//************************************************************** +void refsToFeatureInFeatureDocument(const ObjectPtr& theObject, + std::set& theRefFeatures) { FeaturePtr aFeature = ModelAPI_Feature::feature(theObject); if (aFeature.get()) { @@ -1027,16 +930,18 @@ bool hasModuleDocumentFeature(const std::set& theFeatures) bool askToDelete(const std::set theFeatures, const std::map >& theReferences, QWidget* theParent, - std::set& theReferencesToDelete) + std::set& theReferencesToDelete, + const std::string& thePrefixInfo) { QString aNotActivatedDocWrn; std::string aNotActivatedNames; if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) { if (ModuleBase_Tools::hasModuleDocumentFeature(theFeatures)) - aNotActivatedDocWrn = QObject::tr("Selected objects can be used in Part documents which are not loaded:%1.\n") + aNotActivatedDocWrn = + QObject::tr("Selected objects can be used in Part documents which are not loaded:%1.\n") .arg(aNotActivatedNames.c_str()); } - + std::set aFeaturesRefsTo; std::set aFeaturesRefsToParameter; std::set aParameterFeatures; @@ -1089,6 +994,8 @@ bool askToDelete(const std::set theFeatures, aLast = theReferencesToDelete.end(); for (; anIt != aLast; anIt++) { FeaturePtr aFeature = *anIt; + if (aFeature->getKind() == "RemoveResults") + continue; // skip the remove results feature mentioning: result will be removed anyway if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group())) aPartFeatureNames.append(aFeature->name().c_str()); else @@ -1106,18 +1013,29 @@ bool askToDelete(const std::set theFeatures, aMessageBox.setDefaultButton(QMessageBox::No); QString aText; + if (!thePrefixInfo.empty()) + aText = thePrefixInfo.c_str(); QString aSep = ", "; - if (!aPartFeatureNames.empty()) - aText += QString(QObject::tr("The following parts will be deleted: %1.\n")).arg(aPartFeatureNames.join(aSep)); + if (!aPartFeatureNames.empty()) { + aText += QString(QObject::tr("The following parts will be deleted: %1.\n")) + .arg(aPartFeatureNames.join(aSep)); + } if (!aNotActivatedDocWrn.isEmpty()) aText += aNotActivatedDocWrn; - if (!anOtherFeatureNames.empty()) - aText += QString(QObject::tr("Selected features are used in the following features: %1.\nThese features will be deleted.\n")) + if (!anOtherFeatureNames.empty()) { + const char* aMsg = "Features are used in the following features: %1.\nThese " + "features will be deleted.\n"; + aText += QString(QObject::tr(aMsg)) .arg(anOtherFeatureNames.join(aSep)); + } if (!aParamFeatureNames.empty()) { - 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")) + const char* aMsg = "Parameters are used directly and through a sequence " + "of dependencies in the following features: %1.\nThese features will " + "be deleted.\nOr parameters could be replaced by their values.\n"; + aText += QString(QObject::tr(aMsg)) .arg(aParamFeatureNames.join(aSep)); - QPushButton *aReplaceButton = aMessageBox.addButton(QObject::tr("Replace"), QMessageBox::ActionRole); + QPushButton *aReplaceButton = + aMessageBox.addButton(QObject::tr("Replace"), QMessageBox::ActionRole); } if (!aText.isEmpty()) { @@ -1134,7 +1052,8 @@ bool askToDelete(const std::set theFeatures, ModelAPI_ReplaceParameterMessage::send(aObj, 0); } else - theReferencesToDelete.insert(aFeaturesRefsToParameterOnly.begin(), aFeaturesRefsToParameterOnly.end()); + theReferencesToDelete.insert(aFeaturesRefsToParameterOnly.begin(), + aFeaturesRefsToParameterOnly.end()); } return true; } @@ -1150,19 +1069,54 @@ void convertToFeatures(const QObjectPtrList& theObjects, std::set& t if (!aFeature.get() && anObject->groupName() == ModelAPI_ResultParameter::group()) { aFeature = ModelAPI_Feature::feature(anObject); } - theFeatures.insert(aFeature); + if (aFeature.get()) + theFeatures.insert(aFeature); } } -void translate(const std::string& theContext, std::string& theMessage) +//************************************************************** +void convertToFolders(const QObjectPtrList& theObjects, + std::set& theFolders) +{ + QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end(); + for(; anIt != aLast; anIt++) { + ObjectPtr anObject = *anIt; + FolderPtr aFeature = std::dynamic_pointer_cast(anObject); + if (aFeature.get()) + theFolders.insert(aFeature); + } +} + + +//************************************************************** +QString translate(const Events_InfoMessage& theMessage) { + QString aMessage; + + if (!theMessage.empty()) { + std::string aStr = Config_Translator::translate(theMessage); + if (!aStr.empty()) { + std::string aCodec = Config_Translator::codec(theMessage); + aMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str()); + } + } + + return aMessage; +} + +QString translate(const std::string& theContext, const std::string& theMessage) +{ + QString aMessage; + if (!theMessage.empty()) { std::string aStr = Config_Translator::translate(theContext, theMessage); if (!aStr.empty()) { std::string aCodec = Config_Translator::codec(theContext); - theMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str()).toStdString(); + aMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str()); } } + + return aMessage; } void setPointBallHighlighting(AIS_Shape* theAIS) @@ -1187,13 +1141,22 @@ void setPointBallHighlighting(AIS_Shape* theAIS) aPixMap = new Image_AlienPixMap(); if(!aPixMap->Load(aFile.c_str())) { // The icon for constraint is not found - static const std::string aMsg = "Error: Point market not found by path: \"" + aFile + "\". Falling back."; + static const std::string aMsg = + "Error: Point market not found by path: \"" + aFile + "\". Falling back."; //Events_InfoMessage("ModuleBase_Tools::setPointBallHighlighting", aMsg).send(); } } Handle(Graphic3d_AspectMarker3d) anAspect; - Handle(Prs3d_Drawer) aDrawer = theAIS->HilightAttributes(); + Handle(Prs3d_Drawer) aDrawer = theAIS->DynamicHilightAttributes(); + if (aDrawer.IsNull()) { + if (ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) + return; + aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer); + if (!aDrawer->HasOwnPointAspect()) { + aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_BALL, Quantity_NOC_BLACK, 2.0)); + } + } if(aDrawer->HasOwnPointAspect()) { Handle(Prs3d_PointAspect) aPntAspect = aDrawer->PointAspect(); if(aPixMap->IsEmpty()) { @@ -1209,8 +1172,131 @@ void setPointBallHighlighting(AIS_Shape* theAIS) aPntAspect->SetAspect(anAspect); } aDrawer->SetPointAspect(aPntAspect); - theAIS->SetHilightAttributes(aDrawer); + theAIS->SetDynamicHilightAttributes(aDrawer); + } +} + +FeaturePtr createParameter(const QString& theText) +{ + FeaturePtr aParameter; + QStringList aList = theText.split("="); + if (aList.count() != 2) { + return aParameter; + } + QString aParamName = aList.at(0).trimmed(); + + if (isNameExist(aParamName, FeaturePtr())) { + return aParameter; + } + + if (!ModelAPI_Expression::isVariable(aParamName.toStdString())) { + return aParameter; + } + + QString aExpression = aList.at(1).trimmed(); + if (aExpression.isEmpty()) { + return aParameter; + } + + SessionPtr aMgr = ModelAPI_Session::get(); + std::shared_ptr aDoc = aMgr->activeDocument(); + + aParameter = aDoc->addFeature("Parameter"); + if (aParameter.get()) { + AttributeStringPtr aNameAttr = aParameter->string("variable"); + aNameAttr->setValue(aParamName.toStdString()); + + AttributeStringPtr aExprAttr = aParameter->string("expression"); + aExprAttr->setValue(aExpression.toStdString()); + aParameter->execute(); + + Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED)); + Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED)); + } + return aParameter; +} + +void editParameter(FeaturePtr theParam, const QString& theText) +{ + QStringList aList = theText.split("="); + QString aParamName = aList.at(0).trimmed(); + + QString aExpression = aList.at(1).trimmed(); + if (aExpression.isEmpty()) { + return; + } + + if (isNameExist(aParamName, theParam)) { + return; + } + AttributeStringPtr aNameAttr = theParam->string("variable"); + aNameAttr->setValue(aParamName.toStdString()); + + AttributeStringPtr aExprAttr = theParam->string("expression"); + aExprAttr->setValue(aExpression.toStdString()); + theParam->execute(); + + Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED)); +} + +bool isNameExist(const QString& theName, FeaturePtr theIgnoreParameter) +{ + SessionPtr aMgr = ModelAPI_Session::get(); + std::shared_ptr aDoc = aMgr->activeDocument(); + FeaturePtr aParamFeature; + int aNbFeatures = aDoc->numInternalFeatures(); + std::string aName = theName.toStdString(); + for (int i = 0; i < aNbFeatures; i++) { + aParamFeature = aDoc->internalFeature(i); + if (aParamFeature && aParamFeature->getKind() == "Parameter") { + if ((theIgnoreParameter != aParamFeature) && (aParamFeature->name() == aName)) + return true; + } + } + return false; +} + +FeaturePtr findParameter(const QString& theName) +{ + SessionPtr aMgr = ModelAPI_Session::get(); + std::shared_ptr aDoc = aMgr->activeDocument(); + FeaturePtr aParamFeature; + int aNbFeatures = aDoc->numInternalFeatures(); + std::string aName = theName.toStdString(); + for (int i = 0; i < aNbFeatures; i++) { + aParamFeature = aDoc->internalFeature(i); + if (aParamFeature && aParamFeature->getKind() == "Parameter") { + if (aParamFeature->name() == aName) + return aParamFeature; + } + } + return FeaturePtr(); +} + + +//******************************************************************** +std::string generateName(const AttributePtr& theAttribute, + ModuleBase_IWorkshop* theWorkshop) +{ + std::string aName; + if (theAttribute.get() != NULL) { + ModuleBase_Operation* anOperation = theWorkshop->currentOperation(); + + FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner()); + if (aFeature.get()) { + std::string aXmlCfg, aDescription; + theWorkshop->module()->getXMLRepresentation(aFeature->getKind(), aXmlCfg, aDescription); + + ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop); + std::string anAttributeTitle; + aFactory.getAttributeTitle(theAttribute->id(), anAttributeTitle); + + std::stringstream aStreamName; + aStreamName << theAttribute->owner()->data()->name() << "/" << anAttributeTitle.c_str(); + aName = aStreamName.str(); + } } + return aName; } } // namespace ModuleBase_Tools