+void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature,
+ bool& hasParameter, bool& hasCompositeOwner)
+{
+ hasResult = false;
+ hasFeature = false;
+ hasParameter = false;
+ hasCompositeOwner = false;
+ foreach(ObjectPtr aObj, theObjects) {
+ FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
+ ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
+ ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
+
+ hasResult |= (aResult.get() != NULL);
+ hasFeature |= (aFeature.get() != NULL);
+ hasParameter |= (aConstruction.get() != NULL);
+ if (hasFeature)
+ hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
+ if (hasFeature && hasResult && hasParameter && hasCompositeOwner)
+ break;
+ }
+}
+
+void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
+ const Handle(Prs3d_Drawer)& theDrawer)
+{
+ if (theShape.IsNull())
+ return;
+ TopAbs_ShapeEnum aType = theShape.ShapeType();
+ if ((aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
+ theDrawer->SetDeviationCoefficient(1.e-4);
+}
+
+Quantity_Color color(const std::string& theSection,
+ const std::string& theName,
+ const std::string& theDefault)
+{
+ std::vector<int> aColor = Config_PropManager::color(theSection, theName, theDefault);
+ return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
+}
+
+ObjectPtr getObject(const AttributePtr& theAttribute)
+{
+ ObjectPtr anObject;
+ std::string anAttrType = theAttribute->attributeType();
+ if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
+ AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
+ if (anAttr != NULL && anAttr->isObject())
+ anObject = anAttr->object();
+ }
+ if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
+ AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
+ if (anAttr != NULL)
+ anObject = anAttr->context();
+ }
+ if (anAttrType == ModelAPI_AttributeReference::typeId()) {
+ AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
+ if (anAttr.get() != NULL)
+ anObject = anAttr->value();
+ }
+ return anObject;
+}
+
+TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
+{
+ TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
+
+ // for compounds check sub-shapes: it may be compound of needed type:
+ // Booleans may produce compounds of Solids
+ if (aShapeType == TopAbs_COMPOUND) {
+ for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
+ if (!aSubs.Value().IsNull()) {
+ TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
+ if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
+ aShapeType = TopAbs_COMPOUND;
+ break;
+ }
+ if (aShapeType == TopAbs_COMPOUND) {
+ aShapeType = aSubType;
+ } else if (aShapeType != aSubType) { // compound of shapes of different types
+ aShapeType = TopAbs_COMPOUND;
+ break;
+ }
+ }
+ }
+ }
+ return aShapeType;
+}
+
+void getParameters(QStringList& theParameters)
+{
+ theParameters.clear();
+
+ SessionPtr aSession = ModelAPI_Session::get();
+ std::list<DocumentPtr> aDocList;
+ DocumentPtr anActiveDocument = aSession->activeDocument();
+ DocumentPtr aRootDocument = aSession->moduleDocument();
+ aDocList.push_back(anActiveDocument);
+ if (anActiveDocument != aRootDocument) {
+ aDocList.push_back(aRootDocument);
+ }
+ std::string aGroupId = ModelAPI_ResultParameter::group();
+ for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
+ DocumentPtr aDocument = *it;
+ int aSize = aDocument->size(aGroupId);
+ for (int i = 0; i < aSize; i++) {
+ ObjectPtr anObject = aDocument->object(aGroupId, i);
+ std::string aParameterName = anObject->data()->name();
+ theParameters.append(aParameterName.c_str());
+ }
+ }
+}
+
+std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop, const FeaturePtr& theFeature)
+{
+ std::string anAttributeId;
+
+ std::string aXmlCfg, aDescription;
+ theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
+
+ ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
+ std::string anAttributeTitle;
+ aFactory.getGreedAttribute(anAttributeId);
+
+ return anAttributeId;
+}
+
+bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
+ const std::shared_ptr<GeomAPI_Shape>& theShape,
+ ModuleBase_IWorkshop* theWorkshop,
+ const bool theTemporarily)
+{
+ bool aHasObject = false;
+ if (!theAttribute.get())
+ return aHasObject;
+
+ std::string aType = theAttribute->attributeType();
+ if (aType == ModelAPI_AttributeReference::typeId()) {
+ AttributeReferencePtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
+ ObjectPtr aObject = aRef->value();
+ aHasObject = aObject && aObject->isSame(theObject);
+ //if (!(aObject && aObject->isSame(theObject))) {
+ // aRef->setValue(theObject);
+ //}
+ } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
+ AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
+
+ AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
+ if (anAttribute.get()) {
+ //aRefAttr->setAttr(anAttribute);
+ }
+ else {
+ ObjectPtr aObject = aRefAttr->object();
+ aHasObject = aObject && aObject->isSame(theObject);
+ //if (!(aObject && aObject->isSame(theObject))) {
+ // aRefAttr->setObject(theObject);
+ //}
+ }
+ } else if (aType == ModelAPI_AttributeSelection::typeId()) {
+ /*AttributeSelectionPtr aSelectAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
+ ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+ if (aSelectAttr.get() != NULL) {
+ aSelectAttr->setValue(aResult, theShape, theTemporarily);
+ }*/
+ }
+ if (aType == ModelAPI_AttributeSelectionList::typeId()) {
+ AttributeSelectionListPtr aSelectionListAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
+ ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+ aHasObject = aSelectionListAttr->isInList(aResult, theShape, theTemporarily);
+ //if (!theCheckIfAttributeHasObject || !aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
+ // aSelectionListAttr->append(aResult, theShape, theTemporarily);
+ }
+ else if (aType == ModelAPI_AttributeRefList::typeId()) {
+ AttributeRefListPtr aRefListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
+ aHasObject = aRefListAttr->isInList(theObject);
+ //if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject))
+ // aRefListAttr->append(theObject);
+ }
+ else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
+ AttributeRefAttrListPtr aRefAttrListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
+ AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
+
+ if (anAttribute.get()) {
+ aHasObject = aRefAttrListAttr->isInList(anAttribute);
+ //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
+ // aRefAttrListAttr->append(anAttribute);
+ }
+ else {
+ aHasObject = aRefAttrListAttr->isInList(theObject);
+ //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
+ // aRefAttrListAttr->append(theObject);
+ }
+ }
+ return aHasObject;
+}
+
+void setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
+ const GeomShapePtr& theShape, ModuleBase_IWorkshop* theWorkshop,
+ const bool theTemporarily, const bool theCheckIfAttributeHasObject)
+{
+ if (!theAttribute.get())
+ return;
+
+ std::string aType = theAttribute->attributeType();
+ if (aType == ModelAPI_AttributeReference::typeId()) {
+ AttributeReferencePtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
+ ObjectPtr aObject = aRef->value();
+ if (!(aObject && aObject->isSame(theObject))) {
+ aRef->setValue(theObject);
+ }
+ } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
+ AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
+
+ AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
+ if (anAttribute.get())
+ aRefAttr->setAttr(anAttribute);
+ else {
+ ObjectPtr aObject = aRefAttr->object();
+ if (!(aObject && aObject->isSame(theObject))) {
+ aRefAttr->setObject(theObject);
+ }
+ }
+ } else if (aType == ModelAPI_AttributeSelection::typeId()) {
+ AttributeSelectionPtr aSelectAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
+ ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+ if (aSelectAttr.get() != NULL) {
+ aSelectAttr->setValue(aResult, theShape, theTemporarily);
+ }
+ }
+ if (aType == ModelAPI_AttributeSelectionList::typeId()) {
+ AttributeSelectionListPtr aSelectionListAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
+ ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+ if (!theCheckIfAttributeHasObject || !aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
+ aSelectionListAttr->append(aResult, theShape, theTemporarily);
+ }
+ else if (aType == ModelAPI_AttributeRefList::typeId()) {
+ AttributeRefListPtr aRefListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
+ if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject))
+ aRefListAttr->append(theObject);
+ }
+ else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
+ AttributeRefAttrListPtr aRefAttrListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
+ AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
+
+ if (anAttribute.get()) {
+ if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
+ aRefAttrListAttr->append(anAttribute);
+ }
+ else {
+ if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
+ aRefAttrListAttr->append(theObject);
+ }
+ }
+}
+
+GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* theWorkshop)
+{
+ GeomShapePtr aShape;
+ if (!theAttribute.get())
+ return aShape;
+
+ std::string aType = theAttribute->attributeType();
+ if (aType == ModelAPI_AttributeReference::typeId()) {
+ } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
+ AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
+ if (aRefAttr.get() && !aRefAttr->isObject()) {
+ AttributePtr anAttribute = aRefAttr->attr();
+ aShape = theWorkshop->module()->findShape(anAttribute);
+ }
+ } else if (aType == ModelAPI_AttributeSelection::typeId()) {
+ AttributeSelectionPtr aSelectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
+ (theAttribute);
+ aShape = aSelectAttr->value();
+ }
+ return aShape;
+}
+
+void flushUpdated(ObjectPtr theObject)
+{
+ blockUpdateViewer(true);
+
+ Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
+
+ blockUpdateViewer(false);
+}
+
+void blockUpdateViewer(const bool theValue)
+{
+ // the viewer update should be blocked in order to avoid the temporary feature content
+ // when the solver processes the feature, the redisplay message can be flushed
+ // what caused the display in the viewer preliminary states of object
+ // e.g. fillet feature, angle value change
+ std::shared_ptr<Events_Message> aMsg;
+ if (theValue) {
+ aMsg = std::shared_ptr<Events_Message>(
+ new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
+ }
+ else {
+ // the viewer update should be unblocked
+ aMsg = std::shared_ptr<Events_Message>(
+ new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
+ }
+ Events_Loop::loop()->send(aMsg);
+}
+
+QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
+ int theMaxLineInPixels)
+{
+ static QFontMetrics tfm(theWidget ? theWidget->font() : QApplication::font());
+ static qreal phi = 2.618;
+
+ QRect aBounds = tfm.boundingRect(theValue);
+ if(aBounds.width() <= theMaxLineInPixels)
+ return theValue;
+
+ qreal s = aBounds.width() * aBounds.height();
+ qreal aGoldWidth = sqrt(s*phi);
+
+ QStringList aWords = theValue.split(" ", QString::SkipEmptyParts);
+ QStringList aLines;
+ int n = aWords.count();
+ QString aLine;
+ for (int i = 0; i < n; i++) {
+ QString aLineExt = aLine + " " + aWords[i];
+ qreal anWidthNonExt = tfm.boundingRect(aLine).width();
+ qreal anWidthExt = tfm.boundingRect(aLineExt).width();
+ qreal aDeltaNonExt = fabs(anWidthNonExt-aGoldWidth);
+ qreal aDeltaExt = fabs(anWidthExt-aGoldWidth);
+ if(aDeltaNonExt < aDeltaExt) {
+ // new line
+ aLines.append(aLine);
+ aLine = aWords[i];
+ }
+ else
+ aLine = aLineExt;
+ }
+
+ if(!aLine.isEmpty())
+ aLines.append(aLine);
+
+ QString aResult = aLines.join("\n");
+ return aResult;
+}
+
+void findReferences(const QObjectPtrList& theList,
+ std::set<FeaturePtr>& aDirectRefFeatures,
+ std::set<FeaturePtr>& aIndirectRefFeatures)
+{
+ foreach (ObjectPtr aDeletedObj, theList) {
+ std::set<FeaturePtr> alreadyProcessed;
+ refsToFeatureInAllDocuments(aDeletedObj, aDeletedObj, theList, aDirectRefFeatures,
+ aIndirectRefFeatures, alreadyProcessed);
+ std::set<FeaturePtr> aDifference;
+ std::set_difference(aIndirectRefFeatures.begin(), aIndirectRefFeatures.end(),
+ aDirectRefFeatures.begin(), aDirectRefFeatures.end(),
+ std::inserter(aDifference, aDifference.begin()));
+ aIndirectRefFeatures = aDifference;
+ }
+}
+
+//**************************************************************
+void refsToFeatureInAllDocuments(const ObjectPtr& theSourceObject, const ObjectPtr& theObject,
+ const QObjectPtrList& theIgnoreList,
+ std::set<FeaturePtr>& theDirectRefFeatures,
+ std::set<FeaturePtr>& theIndirectRefFeatures,
+ std::set<FeaturePtr>& theAlreadyProcessed)
+{
+ refsDirectToFeatureInAllDocuments(theSourceObject, theObject, theIgnoreList, theDirectRefFeatures,
+ theAlreadyProcessed);
+
+ // Run recursion. It is possible recursive dependency, like the following: plane, extrusion uses plane,
+ // axis is built on extrusion. Delete of a plane should check the dependency from the axis also.
+ std::set<FeaturePtr>::const_iterator aFeatureIt = theDirectRefFeatures.begin();
+ for (; aFeatureIt != theDirectRefFeatures.end(); ++aFeatureIt) {
+ std::set<FeaturePtr> aRecursiveRefFeatures;
+ refsToFeatureInAllDocuments(theSourceObject, *aFeatureIt, theIgnoreList,
+ aRecursiveRefFeatures, aRecursiveRefFeatures, theAlreadyProcessed);
+ theIndirectRefFeatures.insert(aRecursiveRefFeatures.begin(), aRecursiveRefFeatures.end());
+ }
+
+}
+