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