+TopoDS_Shape getSelectedShape(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
+{
+ if (thePrs->shape().get())
+ return thePrs->shape()->impl<TopoDS_Shape>();
+
+ 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();
+
+ SessionPtr aSession = ModelAPI_Session::get();
+ std::list<DocumentPtr> aDocList;
+ DocumentPtr anActiveDocument = aSession->activeDocument();
+ DocumentPtr aRootDocument = aSession->moduleDocument();
+ aDocList.push_back(anActiveDocument);
+ if (anActiveDocument != aRootDocument) {
+ aDocList.push_back(aRootDocument);
+ }
+ std::string aGroupId = ModelAPI_ResultParameter::group();
+ for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
+ DocumentPtr aDocument = *it;
+ int aSize = aDocument->size(aGroupId);
+ for (int i = 0; i < aSize; i++) {
+ ObjectPtr anObject = aDocument->object(aGroupId, i);
+ std::string aParameterName = anObject->data()->name();
+ theParameters.append(aParameterName.c_str());
+ }
+ }
+}
+
+std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop,
+ const FeaturePtr& theFeature)
+{
+ std::string anAttributeId;
+
+ std::string aXmlCfg, aDescription;
+ theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
+
+ ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
+ std::string anAttributeTitle;
+ aFactory.getGreedAttribute(anAttributeId);
+
+ return anAttributeId;
+}
+
+bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
+ const std::shared_ptr<GeomAPI_Shape>& theShape,
+ ModuleBase_IWorkshop* theWorkshop,
+ const bool theTemporarily)
+{
+ bool aHasObject = false;
+ if (!theAttribute.get())
+ return aHasObject;
+
+ std::string aType = theAttribute->attributeType();
+ if (aType == ModelAPI_AttributeReference::typeId()) {
+ AttributeReferencePtr aRef =
+ std::dynamic_pointer_cast<ModelAPI_AttributeReference>(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<ModelAPI_AttributeRefAttr>(theAttribute);
+
+ AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
+ if (anAttribute.get()) {
+ //aRefAttr->setAttr(anAttribute);
+ }
+ else {
+ ObjectPtr aObject = aRefAttr->object();
+ aHasObject = aObject && aObject->isSame(theObject);
+ //if (!(aObject && aObject->isSame(theObject))) {
+ // aRefAttr->setObject(theObject);
+ //}
+ }
+ } else if (aType == ModelAPI_AttributeSelection::typeId()) {
+ /*AttributeSelectionPtr aSelectAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
+ ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+ if (aSelectAttr.get() != NULL) {
+ aSelectAttr->setValue(aResult, theShape, theTemporarily);
+ }*/
+ }
+ if (aType == ModelAPI_AttributeSelectionList::typeId()) {
+ AttributeSelectionListPtr aSelectionListAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
+ ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+ aHasObject = aSelectionListAttr->isInList(aResult, theShape, theTemporarily);
+ }
+ else if (aType == ModelAPI_AttributeRefList::typeId()) {
+ AttributeRefListPtr aRefListAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(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<ModelAPI_AttributeRefAttrList>(theAttribute);
+ AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
+
+ if (anAttribute.get()) {
+ aHasObject = aRefAttrListAttr->isInList(anAttribute);
+ //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
+ // aRefAttrListAttr->append(anAttribute);
+ }
+ else {
+ aHasObject = aRefAttrListAttr->isInList(theObject);
+ //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
+ // aRefAttrListAttr->append(theObject);
+ }
+ }
+ return aHasObject;
+}
+
+bool setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
+ const GeomShapePtr& theShape, ModuleBase_IWorkshop* theWorkshop,
+ const bool theTemporarily, const bool theCheckIfAttributeHasObject)
+{
+ if (!theAttribute.get())
+ return false;
+
+ bool isDone = true;
+ std::string aType = theAttribute->attributeType();
+ if (aType == ModelAPI_AttributeReference::typeId()) {
+ AttributeReferencePtr aRef =
+ std::dynamic_pointer_cast<ModelAPI_AttributeReference>(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<ModelAPI_AttributeRefAttr>(theAttribute);
+
+ AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
+ if (anAttribute.get())
+ aRefAttr->setAttr(anAttribute);
+ else {
+ ObjectPtr aObject = aRefAttr->object();
+ if (!(aObject && aObject->isSame(theObject))) {
+ aRefAttr->setObject(theObject);
+ }
+ }
+ } else if (aType == ModelAPI_AttributeSelection::typeId()) {
+ AttributeSelectionPtr aSelectAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
+ if (aSelectAttr.get() != NULL) {
+ aSelectAttr->setValue(theObject, theShape, theTemporarily);
+ }
+ }
+ if (aType == ModelAPI_AttributeSelectionList::typeId()) {
+ AttributeSelectionListPtr aSelectionListAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
+ if (!theCheckIfAttributeHasObject ||
+ !aSelectionListAttr->isInList(theObject, theShape, theTemporarily))
+ aSelectionListAttr->append(theObject, theShape, theTemporarily);
+ }
+ else if (aType == ModelAPI_AttributeRefList::typeId()) {
+ AttributeRefListPtr aRefListAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(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<ModelAPI_AttributeRefAttrList>(theAttribute);
+ AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
+
+ if (anAttribute.get()) {
+ if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
+ aRefAttrListAttr->append(anAttribute);
+ }
+ else {
+ if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject)) {
+ if (theObject.get())
+ aRefAttrListAttr->append(theObject);
+ else
+ isDone = false;
+ }
+ }
+ }
+ return isDone;
+}
+
+GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* theWorkshop)
+{
+ GeomShapePtr aShape;
+ if (!theAttribute.get())
+ return aShape;
+
+ std::string aType = theAttribute->attributeType();
+ if (aType == ModelAPI_AttributeReference::typeId()) {
+ } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
+ AttributeRefAttrPtr aRefAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
+ if (aRefAttr.get() && !aRefAttr->isObject()) {
+ AttributePtr anAttribute = aRefAttr->attr();
+ aShape = theWorkshop->module()->findShape(anAttribute);
+ }
+ } else if (aType == ModelAPI_AttributeSelection::typeId()) {
+ AttributeSelectionPtr aSelectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
+ (theAttribute);
+ aShape = aSelectAttr->value();
+ }
+ else // Geom2D point processing
+ aShape = theWorkshop->module()->findShape(theAttribute);
+ return aShape;
+}
+
+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);
+}
+
+void blockUpdateViewer(const bool theValue)
+{
+ // the viewer update should be blocked in order to avoid the temporary feature content
+ // when the solver processes the feature, the redisplay message can be flushed
+ // what caused the display in the viewer preliminary states of object
+ // e.g. fillet feature, angle value change
+ std::shared_ptr<Events_Message> aMsg;
+ if (theValue) {
+ aMsg = std::shared_ptr<Events_Message>(
+ new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
+ }
+ else {
+ // the viewer update should be unblocked
+ aMsg = std::shared_ptr<Events_Message>(
+ new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
+ }
+ Events_Loop::loop()->send(aMsg);
+}
+
+QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
+ int theMaxLineInPixels)
+{
+ static QFontMetrics tfm(theWidget ? theWidget->font() : QApplication::font());
+ static qreal phi = 2.618;
+
+ QRect aBounds = tfm.boundingRect(theValue);
+ if(aBounds.width() <= theMaxLineInPixels)
+ return theValue;
+
+ qreal s = aBounds.width() * aBounds.height();
+ qreal aGoldWidth = sqrt(s*phi);
+
+ QStringList aWords = theValue.split(" ", QString::SkipEmptyParts);
+ QStringList aLines;
+ int n = aWords.count();
+ QString aLine;
+ for (int i = 0; i < n; i++) {
+ QString aLineExt = aLine + " " + aWords[i];
+ qreal anWidthNonExt = tfm.boundingRect(aLine).width();
+ qreal anWidthExt = tfm.boundingRect(aLineExt).width();
+ qreal aDeltaNonExt = fabs(anWidthNonExt-aGoldWidth);
+ qreal aDeltaExt = fabs(anWidthExt-aGoldWidth);
+ if(aDeltaNonExt < aDeltaExt) {
+ // new line
+ aLines.append(aLine);
+ aLine = aWords[i];
+ }
+ else
+ aLine = aLineExt;
+ }
+
+ if(!aLine.isEmpty())
+ aLines.append(aLine);
+
+ QString aResult = aLines.join("\n");
+ return aResult;
+}
+
+//**************************************************************
+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<FeaturePtr>& theRefFeatures)
+{
+ FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
+ if (aFeature.get()) {
+ DocumentPtr aFeatureDoc = aFeature->document();
+ // 1. find references in the current document
+ aFeatureDoc->refsToFeature(aFeature, theRefFeatures, false);
+ }
+}
+
+
+//**************************************************************
+/*bool isSubOfComposite(const ObjectPtr& theObject)
+{
+ bool isSub = false;
+ std::set<FeaturePtr> aRefFeatures;
+ refsToFeatureInFeatureDocument(theObject, aRefFeatures);
+ std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
+ aLast = aRefFeatures.end();
+ for (; anIt != aLast && !isSub; anIt++) {
+ isSub = isSubOfComposite(theObject, *anIt);
+ }
+ return isSub;
+}*/
+
+//**************************************************************
+/*bool isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature)
+{
+ bool isSub = false;
+ CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
+ if (aComposite.get()) {
+ isSub = aComposite->isSub(theObject);
+ // the recursive is possible, the parameters are sketch circle and extrusion cut. They are
+ // separated by composite sketch feature
+ if (!isSub) {
+ int aNbSubs = aComposite->numberOfSubs();
+ for (int aSub = 0; aSub < aNbSubs && !isSub; aSub++) {
+ isSub = isSubOfComposite(theObject, aComposite->subFeature(aSub));
+ }
+ }
+ }
+ return isSub;
+}*/
+
+//**************************************************************
+ResultPtr firstResult(const ObjectPtr& theObject)
+{
+ ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+ if (!aResult.get()) {
+ FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
+ if (aFeature.get())
+ aResult = aFeature->firstResult();
+ }
+ return aResult;
+}
+
+//**************************************************************
+bool isFeatureOfResult(const FeaturePtr& theFeature, const std::string& theGroupOfResult)
+{
+ bool isResult = false;
+
+ if (!theFeature->data()->isValid())
+ return isResult;
+
+ ResultPtr aFirstResult = theFeature->firstResult();
+ if (!aFirstResult.get())
+ return isResult;
+
+ return aFirstResult->groupName() == theGroupOfResult;
+}
+
+//**************************************************************
+bool hasModuleDocumentFeature(const std::set<FeaturePtr>& theFeatures)
+{
+ bool aFoundModuleDocumentObject = false;
+ DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
+
+ std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
+ for (; anIt != aLast && !aFoundModuleDocumentObject; anIt++) {
+ FeaturePtr aFeature = *anIt;
+ ResultPtr aResult = ModuleBase_Tools::firstResult(aFeature);
+ if (aResult.get() && aResult->groupName() == ModelAPI_ResultPart::group())
+ continue;
+ aFoundModuleDocumentObject = aFeature->document() == aModuleDoc;
+ }
+
+ return aFoundModuleDocumentObject;
+}
+
+//**************************************************************
+bool askToDelete(const std::set<FeaturePtr> theFeatures,
+ const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
+ QWidget* theParent,
+ std::set<FeaturePtr>& 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")
+ .arg(aNotActivatedNames.c_str());
+ }
+
+ std::set<FeaturePtr> aFeaturesRefsTo;
+ std::set<FeaturePtr> aFeaturesRefsToParameter;
+ std::set<FeaturePtr> aParameterFeatures;
+ QStringList aPartFeatureNames;
+ std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
+ aLast = theFeatures.end();
+ // separate features to references to parameter features and references to others
+ for (; anIt != aLast; anIt++) {
+ FeaturePtr aFeature = *anIt;
+ if (theReferences.find(aFeature) == theReferences.end())
+ continue;
+
+ if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
+ aPartFeatureNames.append(aFeature->name().c_str());
+
+ std::set<FeaturePtr> aRefFeatures;
+ std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
+ std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
+ for (; aRefIt != aRefLast; aRefIt++) {
+ FeaturePtr aRefFeature = *aRefIt;
+ if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
+ aRefFeatures.find(aRefFeature) == aRefFeatures.end()) // it is not added
+ aRefFeatures.insert(aRefFeature);
+ }
+
+ if (isFeatureOfResult(aFeature, ModelAPI_ResultParameter::group())) {
+ aFeaturesRefsToParameter.insert(aRefFeatures.begin(), aRefFeatures.end());
+ aParameterFeatures.insert(aFeature);
+ }
+ else {
+ theReferencesToDelete.insert(aRefFeatures.begin(), aRefFeatures.end());
+ }
+ }
+
+ std::set<FeaturePtr> aFeaturesRefsToParameterOnly;
+ anIt = aFeaturesRefsToParameter.begin();
+ aLast = aFeaturesRefsToParameter.end();
+ // separate features to references to parameter features and references to others
+ QStringList aParamFeatureNames;
+ for (; anIt != aLast; anIt++) {
+ FeaturePtr aFeature = *anIt;
+ if (theReferencesToDelete.find(aFeature) == theReferencesToDelete.end()) {
+ aFeaturesRefsToParameterOnly.insert(aFeature);
+ aParamFeatureNames.append(aFeature->name().c_str());
+ }
+ }
+ aParamFeatureNames.sort();
+ QStringList anOtherFeatureNames;
+ anIt = theReferencesToDelete.begin();
+ aLast = theReferencesToDelete.end();
+ for (; anIt != aLast; anIt++) {
+ FeaturePtr aFeature = *anIt;
+ if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
+ aPartFeatureNames.append(aFeature->name().c_str());
+ else
+ anOtherFeatureNames.append(aFeature->name().c_str());
+ }
+ aPartFeatureNames.sort();
+ anOtherFeatureNames.sort();
+
+ bool aCanReplaceParameters = !aFeaturesRefsToParameterOnly.empty();
+
+ QMessageBox aMessageBox(theParent);
+ aMessageBox.setWindowTitle(QObject::tr("Delete features"));
+ aMessageBox.setIcon(QMessageBox::Warning);
+ aMessageBox.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
+ 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 (!aNotActivatedDocWrn.isEmpty())
+ aText += aNotActivatedDocWrn;
+ 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()) {
+ 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);
+ }
+
+ if (!aText.isEmpty()) {
+ aText += "Would you like to continue?";
+ aMessageBox.setText(aText);
+ aMessageBox.exec();
+ QMessageBox::ButtonRole aButtonRole = aMessageBox.buttonRole(aMessageBox.clickedButton());
+
+ if (aButtonRole == QMessageBox::NoRole)
+ return false;
+
+ if (aButtonRole == QMessageBox::ActionRole) {
+ foreach (FeaturePtr aObj, aParameterFeatures)
+ ModelAPI_ReplaceParameterMessage::send(aObj, 0);
+ }
+ else
+ theReferencesToDelete.insert(aFeaturesRefsToParameterOnly.begin(),
+ aFeaturesRefsToParameterOnly.end());
+ }
+ return true;
+}
+
+//**************************************************************
+void convertToFeatures(const QObjectPtrList& theObjects, std::set<FeaturePtr>& theFeatures)
+{
+ QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
+ for(; anIt != aLast; anIt++) {
+ ObjectPtr anObject = *anIt;
+ FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
+ // for parameter result, use the corresponded reature to be removed
+ if (!aFeature.get() && anObject->groupName() == ModelAPI_ResultParameter::group()) {
+ aFeature = ModelAPI_Feature::feature(anObject);
+ }
+ if (aFeature.get())
+ theFeatures.insert(aFeature);
+ }
+}
+
+//**************************************************************
+void convertToFolders(const QObjectPtrList& theObjects,
+ std::set<FolderPtr>& theFolders)
+{
+ QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
+ for(; anIt != aLast; anIt++) {
+ ObjectPtr anObject = *anIt;
+ FolderPtr aFeature = std::dynamic_pointer_cast<ModelAPI_Folder>(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);
+ aMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
+ }
+ }
+
+ return aMessage;
+}
+
+void setPointBallHighlighting(AIS_Shape* theAIS)
+{
+ static Handle(Image_AlienPixMap) aPixMap;
+ if(aPixMap.IsNull()) {
+ // Load icon for the presentation
+ std::string aFile;
+ char* anEnv = getenv("SHAPER_ROOT_DIR");
+ if(anEnv) {
+ aFile = std::string(anEnv) +
+ FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
+ } else {
+ anEnv = getenv("OPENPARTS_ROOT_DIR");
+ if (anEnv)
+ aFile = std::string(anEnv) + FSEP + "resources";
+ }
+
+ aFile += FSEP;
+ static const std::string aMarkerName = "marker_dot.png";
+ aFile += aMarkerName;
+ 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.";
+ //Events_InfoMessage("ModuleBase_Tools::setPointBallHighlighting", aMsg).send();
+ }
+ }
+
+ Handle(Graphic3d_AspectMarker3d) anAspect;
+ 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()) {
+ anAspect = aPntAspect->Aspect();
+ anAspect->SetType(Aspect_TOM_BALL);
+ } else {
+ if(aPixMap->Format() == Image_PixMap::ImgGray) {
+ aPixMap->SetFormat (Image_PixMap::ImgAlpha);
+ } else if(aPixMap->Format() == Image_PixMap::ImgGrayF) {
+ aPixMap->SetFormat (Image_PixMap::ImgAlphaF);
+ }
+ anAspect = new Graphic3d_AspectMarker3d(aPixMap);
+ aPntAspect->SetAspect(anAspect);
+ }
+ aDrawer->SetPointAspect(aPntAspect);
+ 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<ModelAPI_Document> 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<ModelAPI_Document> 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<ModelAPI_Document> 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;
+}
+