]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_Tools.cpp
Salome HOME
Issue #2924: Disable Delete command for sub-shapes of comp-solids
[modules/shaper.git] / src / ModuleBase / ModuleBase_Tools.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ModuleBase_Tools.h"
21
22 #include <ModuleBase_ParamIntSpinBox.h>
23 #include <ModuleBase_ParamSpinBox.h>
24 #include <ModuleBase_WidgetFactory.h>
25 #include <ModuleBase_IWorkshop.h>
26 #include <ModuleBase_IModule.h>
27 #include <ModuleBase_IViewer.h>
28 #include <ModuleBase_IconFactory.h>
29 #include <ModuleBase_ResultPrs.h>
30 #include <ModuleBase_ViewerPrs.h>
31
32 #include <ModelAPI_Attribute.h>
33 #include <ModelAPI_AttributeRefAttr.h>
34 #include <ModelAPI_AttributeReference.h>
35 #include <ModelAPI_AttributeSelection.h>
36 #include <ModelAPI_AttributeSelectionList.h>
37 #include <ModelAPI_AttributeRefList.h>
38 #include <ModelAPI_AttributeRefAttrList.h>
39 #include <ModelAPI_ResultPart.h>
40 #include <ModelAPI_ResultConstruction.h>
41 #include <ModelAPI_AttributeString.h>
42 #include <ModelAPI_Expression.h>
43 #include <ModelAPI_ResultField.h>
44 #include <Events_Loop.h>
45
46 #include <ModelAPI_Data.h>
47 #include <ModelAPI_Result.h>
48 #include <ModelAPI_ResultParameter.h>
49 #include <ModelAPI_Tools.h>
50 #include <ModelAPI_Session.h>
51 #include <ModelAPI_Events.h>
52 #include <ModelAPI_Folder.h>
53
54 #include <ModelGeomAlgo_Point2D.h>
55
56 #include <StdSelect_BRepOwner.hxx>
57 #include <TopoDS_Iterator.hxx>
58 #include <AIS_InteractiveContext.hxx>
59 #include <Prs3d_LineAspect.hxx>
60 #include <Prs3d_PlaneAspect.hxx>
61
62 #include <GeomDataAPI_Point2D.h>
63 #include <Events_InfoMessage.h>
64 #include <GeomAPI_ShapeExplorer.h>
65
66 #include <Config_PropManager.h>
67 #include <Config_Translator.h>
68
69 #include <Prs3d_PointAspect.hxx>
70 #include <Graphic3d_AspectMarker3d.hxx>
71
72 #include <Image_AlienPixMap.hxx>
73
74 #include <QWidget>
75 #include <QLayout>
76 #include <QPainter>
77 #include <QBitmap>
78 #include <QDoubleSpinBox>
79 #include <QGraphicsDropShadowEffect>
80 #include <QColor>
81 #include <QApplication>
82 #include <QMessageBox>
83 #include <QAction>
84 #include <QTextCodec>
85 #include <QWindow>
86 #include <QScreen>
87
88 #include <sstream>
89 #include <string>
90
91 #ifdef WIN32
92 #pragma warning(disable : 4996) // for getenv
93 #endif
94
95 const double tolerance = 1e-7;
96 const double DEFAULT_DEVIATION_COEFFICIENT = 1.e-4;
97
98 //#define DEBUG_ACTIVATE_WINDOW
99 //#define DEBUG_SET_FOCUS
100
101 #ifdef WIN32
102 # define FSEP "\\"
103 #else
104 # define FSEP "/"
105 #endif
106
107 namespace ModuleBase_Tools {
108
109 //******************************************************************
110
111 //******************************************************************
112
113 void adjustMargins(QWidget* theWidget)
114 {
115   if(!theWidget)
116     return;
117   adjustMargins(theWidget->layout());
118 }
119
120 void adjustMargins(QLayout* theLayout)
121 {
122   if(!theLayout)
123     return;
124   theLayout->setContentsMargins(2, 5, 2, 5);
125   theLayout->setSpacing(4);
126 }
127
128 void zeroMargins(QWidget* theWidget)
129 {
130   if(!theWidget)
131     return;
132   zeroMargins(theWidget->layout());
133 }
134
135 void zeroMargins(QLayout* theLayout)
136 {
137   if(!theLayout)
138     return;
139   theLayout->setContentsMargins(0, 0, 0, 0);
140   theLayout->setSpacing(5);
141 }
142
143 void activateWindow(QWidget* theWidget, const QString& theInfo)
144 {
145   if (theWidget) {
146     theWidget->activateWindow();
147     theWidget->raise();
148   }
149
150 #ifdef DEBUG_ACTIVATE_WINDOW
151   qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
152 #endif
153 }
154
155 void setFocus(QWidget* theWidget, const QString& theInfo)
156 {
157   theWidget->setFocus();
158   // rectangle of focus is not visible on tool button widgets
159   theWidget->update();
160 #ifdef DEBUG_SET_FOCUS
161   qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
162 #endif
163 }
164
165 void setShadowEffect(QWidget* theWidget, const bool isSetEffect)
166 {
167   if (isSetEffect) {
168     QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
169     aGlowEffect->setOffset(.0);
170     aGlowEffect->setBlurRadius(10.0);
171     aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
172     theWidget->setGraphicsEffect(aGlowEffect);
173   }
174   else {
175     QGraphicsEffect* anEffect = theWidget->graphicsEffect();
176     if(anEffect)
177       anEffect->deleteLater();
178     theWidget->setGraphicsEffect(NULL);
179   }
180 }
181
182 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
183 {
184   QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
185   QImage anAditional(theAdditionalIcon);
186   return composite(anAditional, anIcon);
187 }
188
189 QPixmap composite(const QImage& theAdditionalIcon, QImage& theIcon)
190 {
191   if (theIcon.isNull())
192     return QPixmap();
193
194   int anAddWidth = theAdditionalIcon.width();
195   int anAddHeight = theAdditionalIcon.height();
196
197   int aWidth = theIcon.width();
198   int aHeight = theIcon.height();
199
200   int aStartWidthPos = aWidth - anAddWidth;
201   int aStartHeightPos = aHeight - anAddHeight;
202
203   for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
204   {
205     for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
206     {
207       if (qAlpha(theAdditionalIcon.pixel(i, j)) > 0)
208         theIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, theAdditionalIcon.pixel(i, j));
209     }
210   }
211   return QPixmap::fromImage(theIcon);
212 }
213
214 QPixmap lighter(const QString& theIcon, const int theLighterValue)
215 {
216   QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
217   if (anIcon.isNull())
218     return QPixmap();
219
220   QImage aResult = ModuleBase_IconFactory::loadImage(theIcon);
221   for (int i = 0; i < anIcon.width(); i++)
222   {
223     for (int j = 0; j < anIcon.height(); j++)
224     {
225       QRgb anRgb = anIcon.pixel(i, j);
226       QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
227                          qAlpha(aResult.pixel(i, j)));
228
229       QColor aLighterColor = aPixelColor.lighter(theLighterValue);
230       aResult.setPixel(i, j, qRgba(aLighterColor.red(), aLighterColor.green(),
231                                     aLighterColor.blue(), aLighterColor.alpha()));
232     }
233   }
234   return QPixmap::fromImage(aResult);
235 }
236
237 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
238 {
239   if (theSpin->text() == theText)
240     return;
241   // In order to avoid extra text setting because it will
242   // reset cursor position in control
243   bool isBlocked = theSpin->blockSignals(true);
244   theSpin->setText(theText);
245   theSpin->blockSignals(isBlocked);
246 }
247
248 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
249 {
250   if (fabs(theSpin->value() - theValue) < tolerance)
251     return;
252   bool isBlocked = theSpin->blockSignals(true);
253   theSpin->setValue(theValue);
254   theSpin->blockSignals(isBlocked);
255 }
256
257 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
258 {
259   if (fabs(theSpin->value() - theValue) < tolerance)
260     return;
261   bool isBlocked = theSpin->blockSignals(true);
262   theSpin->setValue(theValue);
263   theSpin->blockSignals(isBlocked);
264 }
265
266 void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText)
267 {
268   // In order to avoid extra text setting because it will
269   // reset cursor position in control
270   if (theSpin->text() == theText)
271     return;
272   bool isBlocked = theSpin->blockSignals(true);
273   theSpin->setText(theText);
274   theSpin->blockSignals(isBlocked);
275 }
276
277 void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue)
278 {
279   if (theSpin->value() == theValue)
280     return;
281   bool isBlocked = theSpin->blockSignals(true);
282   theSpin->setValue(theValue);
283   theSpin->blockSignals(isBlocked);
284 }
285
286 QAction* createAction(const QIcon& theIcon, const QString& theText,
287                       QObject* theParent, const QObject* theReceiver,
288                       const char* theMember, const QString& theToolTip,
289                       const QString& theStatusTip)
290 {
291   QAction* anAction = new QAction(theIcon, theText, theParent);
292   anAction->setToolTip(theToolTip.isEmpty() ? theText : theToolTip);
293   anAction->setStatusTip(!theStatusTip.isEmpty() ? theStatusTip :
294                                                    (!theToolTip.isEmpty() ? theToolTip : theText));
295   if (theReceiver)
296     QObject::connect(anAction, SIGNAL(triggered(bool)), theReceiver, theMember);
297
298   return anAction;
299 }
300
301 #ifdef _DEBUG
302 QString objectName(const ObjectPtr& theObj)
303 {
304   if (!theObj.get())
305     return "";
306
307   return theObj->data()->name().c_str();
308 }
309
310 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
311 {
312   QString aFeatureStr = "feature";
313   if (!theObj.get())
314     return aFeatureStr;
315
316   std::ostringstream aPtrStr;
317   aPtrStr << "[" << theObj.get() << "]";
318
319   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
320   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
321   if(aRes.get()) {
322     aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
323     if (aRes->isDisabled())
324       aFeatureStr.append("[disabled]");
325     if (aRes->isConcealed())
326       aFeatureStr.append("[concealed]");
327     if (ModelAPI_Tools::hasSubResults(aRes))
328       aFeatureStr.append("[hasSubResults]");
329
330     aFeature = ModelAPI_Feature::feature(aRes);
331   }
332   else
333     aFeatureStr.append(aPtrStr.str().c_str());
334
335   if (aFeature.get()) {
336     aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
337     if (aFeature->data()->isValid()) {
338       aFeatureStr.append(QString(", name=%1").arg(theObj->data()->name().c_str()).toStdString()
339                                                                                        .c_str());
340     }
341     if (isUseAttributesInfo) {
342       std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes;
343       std::string aPointsInfo = ModelGeomAlgo_Point2D::getPontAttributesInfo(aFeature,
344                                                                           anAttributes).c_str();
345       if (!aPointsInfo.empty())
346         aFeatureStr.append(QString(", attributes: %1")
347           .arg(aPointsInfo.c_str()).toStdString().c_str());
348     }
349   }
350
351   return aFeatureStr;
352 }
353 #endif
354
355 typedef QMap<QString, int> ShapeTypes;
356 static ShapeTypes myShapeTypes;
357
358 int shapeType(const QString& theType)
359 {
360   if (myShapeTypes.count() == 0) {
361     myShapeTypes["compound"]   = TopAbs_COMPOUND;
362     myShapeTypes["compounds"]  = TopAbs_COMPOUND;
363     myShapeTypes["compsolid"]  = TopAbs_COMPSOLID;
364     myShapeTypes["compsolids"] = TopAbs_COMPSOLID;
365     myShapeTypes["solid"]      = TopAbs_SOLID;
366     myShapeTypes["solids"]     = TopAbs_SOLID;
367     myShapeTypes["shell"]      = TopAbs_SHELL;
368     myShapeTypes["shells"]     = TopAbs_SHELL;
369     myShapeTypes["face"]       = TopAbs_FACE;
370     myShapeTypes["faces"]      = TopAbs_FACE;
371     myShapeTypes["wire"]       = TopAbs_WIRE;
372     myShapeTypes["wires"]      = TopAbs_WIRE;
373     myShapeTypes["edge"]       = TopAbs_EDGE;
374     myShapeTypes["edges"]      = TopAbs_EDGE;
375     myShapeTypes["vertex"]     = TopAbs_VERTEX;
376     myShapeTypes["vertices"]   = TopAbs_VERTEX;
377     myShapeTypes["object"]     = ModuleBase_ResultPrs::Sel_Result;
378     myShapeTypes["objects"]    = ModuleBase_ResultPrs::Sel_Result;
379   }
380   QString aType = theType.toLower();
381   if(myShapeTypes.contains(aType))
382     return myShapeTypes[aType];
383   Events_InfoMessage("ModuleBase_Tools", "Shape type defined in XML is not implemented!").send();
384   return TopAbs_SHAPE;
385 }
386
387 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature,
388                   bool& hasParameter, bool& hasCompositeOwner, bool& hasResultInHistory,
389                   bool& hasFolder)
390 {
391   hasResult = false;
392   hasFeature = false;
393   hasParameter = false;
394   hasCompositeOwner = false;
395   hasResultInHistory = false;
396   hasFolder = false;
397   foreach(ObjectPtr aObj, theObjects) {
398     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
399     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
400     FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aObj);
401     ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
402     FieldStepPtr aStep = std::dynamic_pointer_cast<ModelAPI_ResultField::ModelAPI_FieldStep>(aObj);
403
404     hasResult |= ((aResult.get() != NULL) || (aStep.get() != NULL));
405     hasFeature |= (aFeature.get() != NULL);
406     hasFolder |= (aFolder.get() != NULL);
407     hasParameter |= (aConstruction.get() != NULL);
408     if (hasFeature)
409       hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
410     else if (aResult.get())
411       hasCompositeOwner |= (ModelAPI_Tools::bodyOwner(aResult) != NULL);
412
413     if (!hasResultInHistory && aResult.get()) {
414       FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
415       hasResultInHistory = aFeature.get() && aFeature->isInHistory();
416     }
417
418     if (hasFeature && hasResult  && hasParameter && hasCompositeOwner)
419       break;
420   }
421 }
422
423 /*bool setDefaultDeviationCoefficient(std::shared_ptr<GeomAPI_Shape> theGeomShape)
424 {
425   if (!theGeomShape.get())
426     return false;
427   // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
428   // correction of deviation for them should not influence to the application performance
429   GeomAPI_ShapeExplorer anExp(theGeomShape, GeomAPI_Shape::FACE);
430   bool anEmpty = anExp.empty();
431   return !anExp.more();
432 }*/
433
434 /*void setDefaultDeviationCoefficient(const std::shared_ptr<ModelAPI_Result>& theResult,
435                                     const Handle(Prs3d_Drawer)& theDrawer)
436 {
437   if (!theResult.get())
438     return;
439   bool aUseDeviation = false;
440
441   std::string aResultGroup = theResult->groupName();
442   if (aResultGroup == ModelAPI_ResultConstruction::group())
443     aUseDeviation = true;
444   else if (aResultGroup == ModelAPI_ResultBody::group()) {
445     GeomShapePtr aGeomShape = theResult->shape();
446     if (aGeomShape.get())
447       aUseDeviation = setDefaultDeviationCoefficient(aGeomShape);
448   }
449   if (aUseDeviation)
450     theDrawer->SetDeviationCoefficient(DEFAULT_DEVIATION_COEFFICIENT);
451 }
452 */
453 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
454                                     const Handle(Prs3d_Drawer)& theDrawer)
455 {
456   if (theShape.IsNull())
457     return;
458
459   std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape());
460   aGeomShape->setImpl(new TopoDS_Shape(theShape));
461
462   // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
463   // correction of deviation for them should not influence to the application performance
464   GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
465   bool isConstruction = !anExp.more();
466
467   double aDeflection;
468   if (isConstruction)
469     aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
470   else
471     aDeflection = Config_PropManager::real("Visualization", "body_deflection");
472
473   theDrawer->SetDeviationCoefficient(aDeflection);
474 }
475
476 Quantity_Color color(const std::string& theSection,
477                      const std::string& theName)
478 {
479   std::vector<int> aColor = Config_PropManager::color(theSection, theName);
480   return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
481 }
482
483 ObjectPtr getObject(const AttributePtr& theAttribute)
484 {
485   ObjectPtr anObject;
486   std::string anAttrType = theAttribute->attributeType();
487   if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
488     AttributeRefAttrPtr anAttr =
489       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
490     if (anAttr != NULL && anAttr->isObject())
491       anObject = anAttr->object();
492   }
493   if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
494     AttributeSelectionPtr anAttr =
495       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
496     if (anAttr != NULL)
497       anObject = anAttr->context();
498   }
499   if (anAttrType == ModelAPI_AttributeReference::typeId()) {
500     AttributeReferencePtr anAttr =
501       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
502     if (anAttr.get() != NULL)
503       anObject = anAttr->value();
504   }
505   return anObject;
506 }
507
508 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
509 {
510   TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
511
512   // for compounds check sub-shapes: it may be compound of needed type:
513   // Booleans may produce compounds of Solids
514   if (aShapeType == TopAbs_COMPOUND) {
515     for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
516       if (!aSubs.Value().IsNull()) {
517         TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
518         if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
519           aShapeType = TopAbs_COMPOUND;
520           break;
521         }
522         if (aShapeType == TopAbs_COMPOUND) {
523           aShapeType = aSubType;
524         } else if (aShapeType != aSubType) { // compound of shapes of different types
525           aShapeType = TopAbs_COMPOUND;
526           break;
527         }
528       }
529     }
530   }
531   return aShapeType;
532 }
533
534 TopoDS_Shape getSelectedShape(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
535 {
536   if (thePrs->shape().get())
537     return thePrs->shape()->impl<TopoDS_Shape>();
538
539   Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(thePrs->owner());
540   if (!anOwner.IsNull())
541     return anOwner->Shape();
542
543   return TopoDS_Shape();
544 }
545
546 void getParameters(QStringList& theParameters)
547 {
548   theParameters.clear();
549
550   SessionPtr aSession = ModelAPI_Session::get();
551   std::list<DocumentPtr> aDocList;
552   DocumentPtr anActiveDocument = aSession->activeDocument();
553   DocumentPtr aRootDocument = aSession->moduleDocument();
554   aDocList.push_back(anActiveDocument);
555   if (anActiveDocument != aRootDocument) {
556     aDocList.push_back(aRootDocument);
557   }
558   std::string aGroupId = ModelAPI_ResultParameter::group();
559   for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
560     DocumentPtr aDocument = *it;
561     int aSize = aDocument->size(aGroupId);
562     for (int i = 0; i < aSize; i++) {
563       ObjectPtr anObject = aDocument->object(aGroupId, i);
564       std::string aParameterName = anObject->data()->name();
565       theParameters.append(aParameterName.c_str());
566     }
567   }
568 }
569
570 std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop,
571                                const FeaturePtr& theFeature)
572 {
573   std::string anAttributeId;
574
575   std::string aXmlCfg, aDescription;
576   theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
577
578   ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
579   std::string anAttributeTitle;
580   aFactory.getGreedAttribute(anAttributeId);
581
582   return anAttributeId;
583 }
584
585 bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
586                const std::shared_ptr<GeomAPI_Shape>& theShape,
587                ModuleBase_IWorkshop* theWorkshop,
588                const bool theTemporarily)
589 {
590   bool aHasObject = false;
591   if (!theAttribute.get())
592     return aHasObject;
593
594   std::string aType = theAttribute->attributeType();
595   if (aType == ModelAPI_AttributeReference::typeId()) {
596     AttributeReferencePtr aRef =
597       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
598     ObjectPtr aObject = aRef->value();
599     aHasObject = aObject && aObject->isSame(theObject);
600     //if (!(aObject && aObject->isSame(theObject))) {
601     //  aRef->setValue(theObject);
602     //}
603   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
604     AttributeRefAttrPtr aRefAttr =
605       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
606
607     AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
608     if (anAttribute.get()) {
609       //aRefAttr->setAttr(anAttribute);
610     }
611     else {
612       ObjectPtr aObject = aRefAttr->object();
613       aHasObject = aObject && aObject->isSame(theObject);
614       //if (!(aObject && aObject->isSame(theObject))) {
615       //  aRefAttr->setObject(theObject);
616       //}
617     }
618   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
619     /*AttributeSelectionPtr aSelectAttr =
620                              std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
621     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
622     if (aSelectAttr.get() != NULL) {
623       aSelectAttr->setValue(aResult, theShape, theTemporarily);
624     }*/
625   }
626   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
627     AttributeSelectionListPtr aSelectionListAttr =
628                          std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
629     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
630     aHasObject = aSelectionListAttr->isInList(aResult, theShape, theTemporarily);
631   }
632   else if (aType == ModelAPI_AttributeRefList::typeId()) {
633     AttributeRefListPtr aRefListAttr =
634       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
635     aHasObject = aRefListAttr->isInList(theObject);
636     //if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject))
637     //  aRefListAttr->append(theObject);
638   }
639   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
640     AttributeRefAttrListPtr aRefAttrListAttr =
641       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
642     AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
643
644     if (anAttribute.get()) {
645       aHasObject = aRefAttrListAttr->isInList(anAttribute);
646       //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
647       //  aRefAttrListAttr->append(anAttribute);
648     }
649     else {
650       aHasObject = aRefAttrListAttr->isInList(theObject);
651       //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
652       //  aRefAttrListAttr->append(theObject);
653     }
654   }
655   return aHasObject;
656 }
657
658 bool setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
659                const GeomShapePtr& theShape, ModuleBase_IWorkshop* theWorkshop,
660                const bool theTemporarily, const bool theCheckIfAttributeHasObject)
661 {
662   if (!theAttribute.get())
663     return false;
664
665   bool isDone = true;
666   std::string aType = theAttribute->attributeType();
667   if (aType == ModelAPI_AttributeReference::typeId()) {
668     AttributeReferencePtr aRef =
669       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
670     ObjectPtr aObject = aRef->value();
671     if (!(aObject && aObject->isSame(theObject))) {
672       aRef->setValue(theObject);
673     }
674   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
675     AttributeRefAttrPtr aRefAttr =
676       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
677
678     AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
679     if (anAttribute.get())
680       aRefAttr->setAttr(anAttribute);
681     else {
682       ObjectPtr aObject = aRefAttr->object();
683       if (!(aObject && aObject->isSame(theObject))) {
684         aRefAttr->setObject(theObject);
685       }
686     }
687   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
688     AttributeSelectionPtr aSelectAttr =
689                              std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
690     if (aSelectAttr.get() != NULL) {
691       aSelectAttr->setValue(theObject, theShape, theTemporarily);
692     }
693   }
694   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
695     AttributeSelectionListPtr aSelectionListAttr =
696                          std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
697     if (!theCheckIfAttributeHasObject ||
698       !aSelectionListAttr->isInList(theObject, theShape, theTemporarily))
699       aSelectionListAttr->append(theObject, theShape, theTemporarily);
700   }
701   else if (aType == ModelAPI_AttributeRefList::typeId()) {
702     AttributeRefListPtr aRefListAttr =
703       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
704     if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject)) {
705       if (theObject.get())
706         aRefListAttr->append(theObject);
707       else
708         isDone = false;
709     }
710   }
711   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
712     AttributeRefAttrListPtr aRefAttrListAttr =
713       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
714     AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
715
716     if (anAttribute.get()) {
717       if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
718         aRefAttrListAttr->append(anAttribute);
719     }
720     else {
721       if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject)) {
722         if (theObject.get())
723           aRefAttrListAttr->append(theObject);
724         else
725           isDone = false;
726       }
727     }
728   }
729   return isDone;
730 }
731
732 GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* theWorkshop)
733 {
734   GeomShapePtr aShape;
735   if (!theAttribute.get())
736     return aShape;
737
738   std::string aType = theAttribute->attributeType();
739   if (aType == ModelAPI_AttributeReference::typeId()) {
740   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
741     AttributeRefAttrPtr aRefAttr =
742       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
743     if (aRefAttr.get() && !aRefAttr->isObject()) {
744       AttributePtr anAttribute = aRefAttr->attr();
745       aShape = theWorkshop->module()->findShape(anAttribute);
746     }
747   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
748     AttributeSelectionPtr aSelectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
749                                                                                  (theAttribute);
750     aShape = aSelectAttr->value();
751   }
752   else // Geom2D point processing
753     aShape = theWorkshop->module()->findShape(theAttribute);
754   return aShape;
755 }
756
757 void flushUpdated(ObjectPtr theObject)
758 {
759   blockUpdateViewer(true);
760
761   // Fix the problem of not previewed results of constraints applied. Flush Create/Delete
762   // (for the sketch result) to start processing of the sketch in the solver.
763   // TODO: these flushes should be moved in a separate method provided by Model
764   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
765   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
766   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
767
768   blockUpdateViewer(false);
769 }
770
771 void blockUpdateViewer(const bool theValue)
772 {
773   // the viewer update should be blocked in order to avoid the temporary feature content
774   // when the solver processes the feature, the redisplay message can be flushed
775   // what caused the display in the viewer preliminary states of object
776   // e.g. fillet feature, angle value change
777   std::shared_ptr<Events_Message> aMsg;
778   if (theValue) {
779     aMsg = std::shared_ptr<Events_Message>(
780         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
781   }
782   else {
783     // the viewer update should be unblocked
784     aMsg = std::shared_ptr<Events_Message>(
785         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
786   }
787   Events_Loop::loop()->send(aMsg);
788 }
789
790 QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
791                                           int theMaxLineInPixels)
792 {
793   static QFontMetrics tfm(theWidget ? theWidget->font() : QApplication::font());
794   static qreal phi = 2.618;
795
796   QRect aBounds = tfm.boundingRect(theValue);
797   if(aBounds.width() <= theMaxLineInPixels)
798     return theValue;
799
800   qreal s = aBounds.width() * aBounds.height();
801   qreal aGoldWidth = sqrt(s*phi);
802
803   QStringList aWords = theValue.split(" ", QString::SkipEmptyParts);
804   QStringList aLines;
805   int n = aWords.count();
806   QString aLine;
807   for (int i = 0; i < n; i++) {
808     QString aLineExt =  i == 0 ? aWords[i] : aLine + " " + aWords[i];
809     qreal anWidthNonExt = tfm.boundingRect(aLine).width();
810     qreal anWidthExt = tfm.boundingRect(aLineExt).width();
811     qreal aDeltaNonExt = fabs(anWidthNonExt-aGoldWidth);
812     qreal aDeltaExt    = fabs(anWidthExt-aGoldWidth);
813     if(aDeltaNonExt < aDeltaExt) {
814       // new line
815       aLines.append(aLine);
816       aLine = aWords[i];
817     }
818     else
819       aLine = aLineExt;
820   }
821
822   if(!aLine.isEmpty())
823     aLines.append(aLine);
824
825   QString aResult = aLines.join("\n");
826   return aResult;
827 }
828
829 //**************************************************************
830 QLocale doubleLocale()
831 {
832   // VSR 01/07/2010: Disable thousands separator for spin box
833   // (to avoid inconsistency of double-2-string and string-2-double conversion)
834   QLocale aLocale;
835   aLocale.setNumberOptions(aLocale.numberOptions() |
836                            QLocale::OmitGroupSeparator |
837                            QLocale::RejectGroupSeparator);
838   return aLocale;
839 }
840
841 //**************************************************************
842 void refsToFeatureInFeatureDocument(const ObjectPtr& theObject,
843                                     std::set<FeaturePtr>& theRefFeatures)
844 {
845   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
846   if (aFeature.get()) {
847     DocumentPtr aFeatureDoc = aFeature->document();
848     // 1. find references in the current document
849     aFeatureDoc->refsToFeature(aFeature, theRefFeatures, false);
850   }
851 }
852
853
854 //**************************************************************
855 /*bool isSubOfComposite(const ObjectPtr& theObject)
856 {
857   bool isSub = false;
858   std::set<FeaturePtr> aRefFeatures;
859   refsToFeatureInFeatureDocument(theObject, aRefFeatures);
860   std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
861                                        aLast = aRefFeatures.end();
862   for (; anIt != aLast && !isSub; anIt++) {
863     isSub = isSubOfComposite(theObject, *anIt);
864   }
865   return isSub;
866 }*/
867
868 //**************************************************************
869 /*bool isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature)
870 {
871   bool isSub = false;
872   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
873   if (aComposite.get()) {
874     isSub = aComposite->isSub(theObject);
875     // the recursive is possible, the parameters are sketch circle and extrusion cut. They are
876     // separated by composite sketch feature
877     if (!isSub) {
878       int aNbSubs = aComposite->numberOfSubs();
879       for (int aSub = 0; aSub < aNbSubs && !isSub; aSub++) {
880         isSub = isSubOfComposite(theObject, aComposite->subFeature(aSub));
881       }
882     }
883   }
884   return isSub;
885 }*/
886
887 //**************************************************************
888 ResultPtr firstResult(const ObjectPtr& theObject)
889 {
890   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
891   if (!aResult.get()) {
892     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
893     if (aFeature.get())
894       aResult = aFeature->firstResult();
895   }
896   return aResult;
897 }
898
899 //**************************************************************
900 bool isFeatureOfResult(const FeaturePtr& theFeature, const std::string& theGroupOfResult)
901 {
902   bool isResult = false;
903
904   if (!theFeature->data()->isValid())
905     return isResult;
906
907   ResultPtr aFirstResult = theFeature->firstResult();
908   if (!aFirstResult.get())
909     return isResult;
910
911   return aFirstResult->groupName() == theGroupOfResult;
912 }
913
914 //**************************************************************
915 bool hasModuleDocumentFeature(const std::set<FeaturePtr>& theFeatures)
916 {
917   bool aFoundModuleDocumentObject = false;
918   DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
919
920   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
921   for (; anIt != aLast && !aFoundModuleDocumentObject; anIt++) {
922     FeaturePtr aFeature = *anIt;
923     ResultPtr aResult = ModuleBase_Tools::firstResult(aFeature);
924     if (aResult.get() && aResult->groupName() == ModelAPI_ResultPart::group())
925       continue;
926     aFoundModuleDocumentObject = aFeature->document() == aModuleDoc;
927   }
928
929   return aFoundModuleDocumentObject;
930 }
931
932 //**************************************************************
933 bool askToDelete(const std::set<FeaturePtr> theFeatures,
934                  const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
935                  QWidget* theParent,
936                  std::set<FeaturePtr>& theReferencesToDelete,
937                  const std::string& thePrefixInfo)
938 {
939   QString aNotActivatedDocWrn;
940   std::string aNotActivatedNames;
941   if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
942     if (ModuleBase_Tools::hasModuleDocumentFeature(theFeatures))
943       aNotActivatedDocWrn =
944         QObject::tr("Selected objects can be used in Part documents which are not loaded: %1.\n")
945                             .arg(aNotActivatedNames.c_str());
946   }
947
948   std::set<FeaturePtr> aFeaturesRefsTo;
949   std::set<FeaturePtr> aFeaturesRefsToParameter;
950   std::set<FeaturePtr> aParameterFeatures;
951   QStringList aPartFeatureNames;
952   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
953                                        aLast = theFeatures.end();
954   // separate features to references to parameter features and references to others
955   for (; anIt != aLast; anIt++) {
956     FeaturePtr aFeature = *anIt;
957     if (theReferences.find(aFeature) == theReferences.end())
958       continue;
959
960     if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
961       aPartFeatureNames.append(aFeature->name().c_str());
962
963     std::set<FeaturePtr> aRefFeatures;
964     std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
965     std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
966     for (; aRefIt != aRefLast; aRefIt++) {
967       FeaturePtr aRefFeature = *aRefIt;
968       if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
969           aRefFeatures.find(aRefFeature) == aRefFeatures.end()) // it is not added
970         aRefFeatures.insert(aRefFeature);
971     }
972
973     if (isFeatureOfResult(aFeature, ModelAPI_ResultParameter::group())) {
974       aFeaturesRefsToParameter.insert(aRefFeatures.begin(), aRefFeatures.end());
975       aParameterFeatures.insert(aFeature);
976     }
977     else {
978       theReferencesToDelete.insert(aRefFeatures.begin(), aRefFeatures.end());
979     }
980   }
981
982   std::set<FeaturePtr> aFeaturesRefsToParameterOnly;
983   anIt = aFeaturesRefsToParameter.begin();
984   aLast = aFeaturesRefsToParameter.end();
985   // separate features to references to parameter features and references to others
986   QStringList aParamFeatureNames;
987   for (; anIt != aLast; anIt++) {
988     FeaturePtr aFeature = *anIt;
989     if (theReferencesToDelete.find(aFeature) == theReferencesToDelete.end()) {
990       aFeaturesRefsToParameterOnly.insert(aFeature);
991       aParamFeatureNames.append(aFeature->name().c_str());
992     }
993   }
994   aParamFeatureNames.sort();
995   QStringList anOtherFeatureNames;
996   anIt = theReferencesToDelete.begin();
997   aLast = theReferencesToDelete.end();
998   for (; anIt != aLast; anIt++) {
999     FeaturePtr aFeature = *anIt;
1000     if (aFeature->getKind() == "RemoveResults")
1001       continue; // skip the remove results feature mentioning: result will be removed anyway
1002     if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
1003       aPartFeatureNames.append(aFeature->name().c_str());
1004     else
1005       anOtherFeatureNames.append(aFeature->name().c_str());
1006   }
1007   aPartFeatureNames.sort();
1008   anOtherFeatureNames.sort();
1009
1010   bool aCanReplaceParameters = !aFeaturesRefsToParameterOnly.empty();
1011
1012   QMessageBox aMessageBox(theParent);
1013   aMessageBox.setWindowTitle(QObject::tr("Delete features"));
1014   aMessageBox.setIcon(QMessageBox::Warning);
1015   aMessageBox.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
1016   aMessageBox.setDefaultButton(QMessageBox::No);
1017
1018   QString aText;
1019   if (!thePrefixInfo.empty())
1020     aText = thePrefixInfo.c_str();
1021   QString aSep = ", ";
1022   if (!aPartFeatureNames.empty()) {
1023     aText += QString(QObject::tr("The following parts will be deleted: %1.\n"))
1024              .arg(aPartFeatureNames.join(aSep));
1025   }
1026   if (!aNotActivatedDocWrn.isEmpty())
1027     aText += aNotActivatedDocWrn;
1028   if (!anOtherFeatureNames.empty()) {
1029     const char* aMsg = "Features are used in the following features: %1.\nThese "
1030                        "features will be deleted.\n";
1031     aText += QString(QObject::tr(aMsg))
1032                      .arg(anOtherFeatureNames.join(aSep));
1033   }
1034   if (!aParamFeatureNames.empty()) {
1035     const char* aMsg = "Parameters are used directly and through a sequence "
1036                        "of dependencies in the following features: %1.\nThese features will "
1037                        "be deleted.\nOr parameters could be replaced by their values.\n";
1038     aText += QString(QObject::tr(aMsg))
1039                      .arg(aParamFeatureNames.join(aSep));
1040     QPushButton *aReplaceButton =
1041       aMessageBox.addButton(QObject::tr("Replace"), QMessageBox::ActionRole);
1042   }
1043
1044   if (!aText.isEmpty()) {
1045     aText += "Would you like to continue?";
1046     aMessageBox.setText(aText);
1047     aMessageBox.exec();
1048     QMessageBox::ButtonRole aButtonRole = aMessageBox.buttonRole(aMessageBox.clickedButton());
1049
1050     if (aButtonRole == QMessageBox::NoRole)
1051       return false;
1052
1053     if (aButtonRole == QMessageBox::ActionRole) {
1054       foreach (FeaturePtr aObj, aParameterFeatures)
1055         ModelAPI_ReplaceParameterMessage::send(aObj, 0);
1056     }
1057     else
1058       theReferencesToDelete.insert(aFeaturesRefsToParameterOnly.begin(),
1059                                    aFeaturesRefsToParameterOnly.end());
1060   }
1061   return true;
1062 }
1063
1064 //**************************************************************
1065 void convertToFeatures(const QObjectPtrList& theObjects, std::set<FeaturePtr>& theFeatures)
1066 {
1067   QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
1068   for(; anIt != aLast; anIt++) {
1069     ObjectPtr anObject = *anIt;
1070     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
1071     // for parameter result, use the corresponded reature to be removed
1072     if (!aFeature.get() && anObject->groupName() == ModelAPI_ResultParameter::group()) {
1073       aFeature = ModelAPI_Feature::feature(anObject);
1074     }
1075     if (aFeature.get())
1076       theFeatures.insert(aFeature);
1077   }
1078 }
1079
1080 //**************************************************************
1081 void convertToFolders(const QObjectPtrList& theObjects,
1082                                          std::set<FolderPtr>& theFolders)
1083 {
1084   QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
1085   for(; anIt != aLast; anIt++) {
1086     ObjectPtr anObject = *anIt;
1087     FolderPtr aFeature = std::dynamic_pointer_cast<ModelAPI_Folder>(anObject);
1088     if (aFeature.get())
1089       theFolders.insert(aFeature);
1090   }
1091 }
1092
1093
1094 //**************************************************************
1095 QString translate(const Events_InfoMessage& theMessage)
1096 {
1097   QString aMessage;
1098
1099   if (!theMessage.empty()) {
1100     std::string aStr = Config_Translator::translate(theMessage);
1101     if (!aStr.empty()) {
1102       std::string aCodec = Config_Translator::codec(theMessage);
1103       aMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
1104     }
1105   }
1106
1107   return aMessage;
1108 }
1109
1110 QString translate(const std::string& theContext, const std::string& theMessage)
1111 {
1112   QString aMessage;
1113
1114   if (!theMessage.empty()) {
1115     std::string aStr = Config_Translator::translate(theContext, theMessage);
1116     if (!aStr.empty()) {
1117       std::string aCodec = Config_Translator::codec(theContext);
1118       aMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
1119     }
1120   }
1121
1122   return aMessage;
1123 }
1124
1125 void setPointBallHighlighting(AIS_Shape* theAIS)
1126 {
1127   static Handle(Image_AlienPixMap) aPixMap;
1128   if(aPixMap.IsNull()) {
1129     // Load icon for the presentation
1130     std::string aFile;
1131     char* anEnv = getenv("SHAPER_ROOT_DIR");
1132     if(anEnv) {
1133       aFile = std::string(anEnv) +
1134         FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
1135     } else {
1136       anEnv = getenv("CADBUILDER_ROOT_DIR");
1137       if (anEnv)
1138         aFile = std::string(anEnv) + FSEP + "resources";
1139     }
1140
1141     aFile += FSEP;
1142     static const std::string aMarkerName = "marker_dot.png";
1143     aFile += aMarkerName;
1144     aPixMap = new Image_AlienPixMap();
1145     if(!aPixMap->Load(aFile.c_str())) {
1146       // The icon for constraint is not found
1147       static const std::string aMsg =
1148         "Error: Point market not found by path: \"" + aFile + "\". Falling back.";
1149       //Events_InfoMessage("ModuleBase_Tools::setPointBallHighlighting", aMsg).send();
1150     }
1151   }
1152
1153   Handle(Graphic3d_AspectMarker3d) anAspect;
1154   Handle(Prs3d_Drawer) aDrawer = theAIS->DynamicHilightAttributes();
1155   if (aDrawer.IsNull()) {
1156     if (ModuleBase_IViewer::DefaultHighlightDrawer.IsNull())
1157       return;
1158     aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
1159     if (!aDrawer->HasOwnPointAspect()) {
1160       aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_BALL, Quantity_NOC_BLACK, 2.0));
1161     }
1162   }
1163   if(aDrawer->HasOwnPointAspect()) {
1164     Handle(Prs3d_PointAspect) aPntAspect = aDrawer->PointAspect();
1165     if(aPixMap->IsEmpty()) {
1166       anAspect = aPntAspect->Aspect();
1167       anAspect->SetType(Aspect_TOM_BALL);
1168     } else {
1169       if(aPixMap->Format() == Image_PixMap::ImgGray) {
1170         aPixMap->SetFormat (Image_PixMap::ImgAlpha);
1171       } else if(aPixMap->Format() == Image_PixMap::ImgGrayF) {
1172         aPixMap->SetFormat (Image_PixMap::ImgAlphaF);
1173       }
1174       anAspect = new Graphic3d_AspectMarker3d(aPixMap);
1175       aPntAspect->SetAspect(anAspect);
1176     }
1177     aDrawer->SetPointAspect(aPntAspect);
1178           theAIS->SetDynamicHilightAttributes(aDrawer);
1179   }
1180 }
1181
1182 FeaturePtr createParameter(const QString& theText)
1183 {
1184   FeaturePtr aParameter;
1185   QStringList aList = theText.split("=");
1186   if (aList.count() != 2) {
1187     return aParameter;
1188   }
1189   QString aParamName = aList.at(0).trimmed();
1190
1191   if (isNameExist(aParamName, FeaturePtr())) {
1192     return aParameter;
1193   }
1194
1195   if (!ModelAPI_Expression::isVariable(aParamName.toStdString())) {
1196     return aParameter;
1197   }
1198
1199   QString aExpression = aList.at(1).trimmed();
1200   if (aExpression.isEmpty()) {
1201     return aParameter;
1202   }
1203
1204   SessionPtr aMgr = ModelAPI_Session::get();
1205   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
1206
1207   aParameter = aDoc->addFeature("Parameter");
1208   if (aParameter.get()) {
1209     AttributeStringPtr aNameAttr = aParameter->string("variable");
1210     aNameAttr->setValue(aParamName.toStdString());
1211
1212     AttributeStringPtr aExprAttr = aParameter->string("expression");
1213     aExprAttr->setValue(aExpression.toStdString());
1214     aParameter->execute();
1215
1216     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
1217     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
1218   }
1219   return aParameter;
1220 }
1221
1222 void editParameter(FeaturePtr theParam, const QString& theText)
1223 {
1224   QStringList aList = theText.split("=");
1225   QString aParamName = aList.at(0).trimmed();
1226
1227   QString aExpression = aList.at(1).trimmed();
1228   if (aExpression.isEmpty()) {
1229     return;
1230   }
1231
1232   if (isNameExist(aParamName, theParam)) {
1233     return;
1234   }
1235   AttributeStringPtr aNameAttr = theParam->string("variable");
1236   aNameAttr->setValue(aParamName.toStdString());
1237
1238   AttributeStringPtr aExprAttr = theParam->string("expression");
1239   aExprAttr->setValue(aExpression.toStdString());
1240   theParam->execute();
1241
1242   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
1243 }
1244
1245 bool isNameExist(const QString& theName, FeaturePtr theIgnoreParameter)
1246 {
1247   SessionPtr aMgr = ModelAPI_Session::get();
1248   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
1249   FeaturePtr aParamFeature;
1250   int aNbFeatures = aDoc->numInternalFeatures();
1251   std::string aName = theName.toStdString();
1252   for (int i = 0; i < aNbFeatures; i++) {
1253     aParamFeature = aDoc->internalFeature(i);
1254     if (aParamFeature && aParamFeature->getKind() == "Parameter") {
1255       if ((theIgnoreParameter != aParamFeature) && (aParamFeature->name() == aName))
1256         return true;
1257     }
1258   }
1259   return false;
1260 }
1261
1262 FeaturePtr findParameter(const QString& theName)
1263 {
1264   SessionPtr aMgr = ModelAPI_Session::get();
1265   std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
1266   FeaturePtr aParamFeature;
1267   int aNbFeatures = aDoc->numInternalFeatures();
1268   std::string aName = theName.toStdString();
1269   for (int i = 0; i < aNbFeatures; i++) {
1270     aParamFeature = aDoc->internalFeature(i);
1271     if (aParamFeature && aParamFeature->getKind() == "Parameter") {
1272       if (aParamFeature->name() == aName)
1273         return aParamFeature;
1274     }
1275   }
1276   return FeaturePtr();
1277 }
1278
1279
1280 //********************************************************************
1281 std::string generateName(const AttributePtr& theAttribute,
1282   ModuleBase_IWorkshop* theWorkshop)
1283 {
1284   std::string aName;
1285   if (theAttribute.get() != NULL) {
1286     ModuleBase_Operation* anOperation = theWorkshop->currentOperation();
1287
1288     FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
1289     if (aFeature.get()) {
1290       std::string aXmlCfg, aDescription;
1291       theWorkshop->module()->getXMLRepresentation(aFeature->getKind(), aXmlCfg, aDescription);
1292
1293       ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
1294       std::string anAttributeTitle;
1295       aFactory.getAttributeTitle(theAttribute->id(), anAttributeTitle);
1296
1297       std::stringstream aStreamName;
1298       aStreamName << theAttribute->owner()->data()->name() << "/" << anAttributeTitle.c_str();
1299       aName = aStreamName.str();
1300     }
1301   }
1302   return aName;
1303 }
1304
1305 bool isSameShape(const TopoDS_Shape& theShape1, const TopoDS_Shape& theShape2)
1306 {
1307   // In case of compound we cannot rely on simple comparison method.
1308   // If the compound is generated by Group feature then this compound is alwais new.
1309   // So, we have to compare content of these compounds
1310   if (theShape1.ShapeType() != theShape2.ShapeType())
1311     return false;
1312
1313   if (theShape1.ShapeType() != TopAbs_COMPOUND)
1314     return theShape1.IsSame(theShape2);
1315
1316   TopoDS_Iterator aIt1(theShape1);
1317   TopoDS_Iterator aIt2(theShape2);
1318
1319   for (; aIt1.More() && aIt2.More(); aIt1.Next(), aIt2.Next()) {
1320     if (!(aIt1.Value()).IsSame(aIt2.Value()))
1321       return false;
1322   }
1323   return true;
1324 }
1325
1326 qreal currentPixelRatio()
1327 {
1328   QWindowList aWnds = qApp->topLevelWindows();
1329   if (aWnds.size() > 0)
1330     return aWnds.first()->devicePixelRatio();
1331   return qApp->primaryScreen()->devicePixelRatio();
1332 }
1333
1334
1335 } // namespace ModuleBase_Tools
1336
1337