X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2FExchangePlugin%2FExchangePlugin_ImportPart.cpp;h=c7f198bc948c0c40c5e3467a354661563baf1ea4;hb=c564630a9aa1a9197b5faa884b42ad777319e6a6;hp=14f2c19511c78055003704b03e867956885461a7;hpb=69192b1ac7fbfedef36d95068a9407581fd87ea2;p=modules%2Fshaper.git diff --git a/src/ExchangePlugin/ExchangePlugin_ImportPart.cpp b/src/ExchangePlugin/ExchangePlugin_ImportPart.cpp index 14f2c1951..c7f198bc9 100644 --- a/src/ExchangePlugin/ExchangePlugin_ImportPart.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ImportPart.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2019 CEA/DEN, EDF R&D +// Copyright (C) 2014-2022 CEA/DEN, EDF R&D // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -19,17 +19,28 @@ #include +#include + +#include #include +#include #include #include +#include #include #include #include +#include + +static const std::string THE_NEW_PART_STR("New Part"); +static const std::string THE_PART_SET_STR("PartSet"); // Update names of imported features/results concurent with existing objects. static void correntNonUniqueNames(DocumentPtr theDocument, std::list& theImported); +// Find the document according to its name or create the new one. +static DocumentPtr findDocument(DocumentPtr thePartSetDoc, const std::string& thePartName); ExchangePlugin_ImportPart::ExchangePlugin_ImportPart() { @@ -38,8 +49,11 @@ ExchangePlugin_ImportPart::ExchangePlugin_ImportPart() void ExchangePlugin_ImportPart::initAttributes() { data()->addAttribute(FILE_PATH_ID(), ModelAPI_AttributeString::typeId()); + data()->addAttribute(TARGET_PART_ID(), ModelAPI_AttributeInteger::typeId()); + data()->addAttribute(TARGET_PARTS_LIST_ID(), ModelAPI_AttributeStringArray::typeId()); } + void ExchangePlugin_ImportPart::execute() { AttributeStringPtr aFilePathAttr = string(FILE_PATH_ID()); @@ -49,45 +63,121 @@ void ExchangePlugin_ImportPart::execute() return; } - // load the file into the active document + // get the document where to import + AttributeStringArrayPtr aPartsAttr = stringArray(TARGET_PARTS_LIST_ID()); + AttributeIntegerPtr aTargetAttr = integer(TARGET_PART_ID()); SessionPtr aSession = ModelAPI_Session::get(); - DocumentPtr aDoc = document(); - bool isPartSet = aDoc == aSession->moduleDocument(); + DocumentPtr aDoc = + findDocument(aSession->moduleDocument(), aPartsAttr->value(aTargetAttr->value())); + + // load the file into the document std::list anImportedFeatures; - bool isOk = aDoc->import(aFilename.c_str(), anImportedFeatures, isPartSet); - if (!isOk && isPartSet) { - // there are features not appropriate for PartSet, - // create new part and load there - FeaturePtr aPartFeature = aDoc->addFeature(PartSetPlugin_Part::ID()); - ResultPartPtr aPartResult; - if (aPartFeature) { - aPartFeature->execute(); - aPartResult = std::dynamic_pointer_cast(aPartFeature->lastResult()); - } - if (aPartResult) { - aDoc = aPartResult->partDoc(); - isOk = aDoc->import(aFilename.c_str(), anImportedFeatures); - } - } - if (isOk) + if (aDoc && aDoc->importPart(aFilename.c_str(), anImportedFeatures)) correntNonUniqueNames(aDoc, anImportedFeatures); else setError("Cannot import the document."); } +void ExchangePlugin_ImportPart::attributeChanged(const std::string& theID) +{ + if (theID == FILE_PATH_ID()) { + AttributeStringPtr aFilePathAttr = string(FILE_PATH_ID()); + if (aFilePathAttr->value().empty()) + return; + + AttributeStringArrayPtr aPartsAttr = stringArray(TARGET_PARTS_LIST_ID()); + AttributeIntegerPtr aTargetAttr = integer(TARGET_PART_ID()); + + // update the list of target parts + SessionPtr aSession = ModelAPI_Session::get(); + DocumentPtr aDoc = document(); + bool isPartSet = aDoc == aSession->moduleDocument(); + if (isPartSet) { + std::list anAcceptedValues; + anAcceptedValues.push_back(THE_NEW_PART_STR); + + std::list anImportedFeatures; + if (aDoc->importPart(aFilePathAttr->value().c_str(), anImportedFeatures, isPartSet)) + anAcceptedValues.push_back(THE_PART_SET_STR); + + // append names of all parts + std::list aSubFeatures = aDoc->allFeatures(); + for (std::list::iterator aFIt = aSubFeatures.begin(); + aFIt != aSubFeatures.end(); ++aFIt) { + if ((*aFIt)->getKind() == PartSetPlugin_Part::ID()) + anAcceptedValues.push_back(Locale::Convert::toString((*aFIt)->name())); + } + + if ((size_t)aPartsAttr->size() != anAcceptedValues.size()) + aTargetAttr->setValue(0); + + aPartsAttr->setSize((int)anAcceptedValues.size()); + std::list::iterator anIt = anAcceptedValues.begin(); + for (int anInd = 0; anIt != anAcceptedValues.end(); ++anIt, ++anInd) + aPartsAttr->setValue(anInd, *anIt); + } + else { + // keep only the name of the current part + if (aPartsAttr->size() == 0) { + FeaturePtr aPartFeature = ModelAPI_Tools::findPartFeature(aSession->moduleDocument(), aDoc); + + aPartsAttr->setSize(1); + aPartsAttr->setValue(0, Locale::Convert::toString(aPartFeature->name())); + aTargetAttr->setValue(0); + } + } + } +} + // ================================ Auxiliary functions =================================== -typedef std::map > > ObjectNameMap; +DocumentPtr findDocument(DocumentPtr thePartSetDoc, const std::string& thePartName) +{ + DocumentPtr aDoc; + if (thePartName == THE_PART_SET_STR) + aDoc = thePartSetDoc; + else { + FeaturePtr aPartFeature; + if (thePartName == THE_NEW_PART_STR) { + // create new part + aPartFeature = thePartSetDoc->addFeature(PartSetPlugin_Part::ID()); + if (aPartFeature) + aPartFeature->execute(); + } + else { + // find existing part by its name + std::list aSubFeatures = thePartSetDoc->allFeatures(); + for (std::list::iterator aFIt = aSubFeatures.begin(); + aFIt != aSubFeatures.end(); ++aFIt) { + if ((*aFIt)->getKind() == PartSetPlugin_Part::ID() && + Locale::Convert::toString((*aFIt)->name()) == thePartName) { + aPartFeature = *aFIt; + break; + } + } + } + + if (aPartFeature) { + ResultPartPtr aPartResult = + std::dynamic_pointer_cast(aPartFeature->lastResult()); + if (aPartResult) + aDoc = aPartResult->partDoc(); + } + } + return aDoc; +} + +typedef std::map > > ObjectNameMap; -bool splitName(std::string& theName, int& theIndex) +bool splitName(std::wstring& theName, int& theIndex) { size_t aLastUndercore = theName.find_last_of('_'); - bool isOk = aLastUndercore != std::string::npos; + bool isOk = aLastUndercore != std::wstring::npos; if (isOk) { - char* isNumber; - std::string anIndexStr = theName.substr(aLastUndercore + 1); - theIndex = std::strtol(anIndexStr.c_str(), &isNumber, 10); + size_t isNumber; + std::wstring anIndexStr = theName.substr(aLastUndercore + 1); + theIndex = std::stol(anIndexStr, &isNumber); isOk = isNumber != 0; if (isOk) theName.erase(aLastUndercore); @@ -97,7 +187,7 @@ bool splitName(std::string& theName, int& theIndex) void addIndexedName(const ObjectPtr& theObject, ObjectNameMap& theIndexedNames) { - std::string aName = theObject->data()->name(); + std::wstring aName =theObject->data()->name(); std::string aGroup = theObject->groupName(); int anIndex = 0; bool isIndexed = splitName(aName, anIndex); @@ -134,9 +224,9 @@ static void collectOldNames(DocumentPtr theDocument, std::list& theA } } -static std::string uniqueName(const ObjectPtr& theObject, ObjectNameMap& theExistingNames) +static std::wstring uniqueName(const ObjectPtr& theObject, ObjectNameMap& theExistingNames) { - std::string aName = theObject->data()->name(); + std::wstring aName = theObject->data()->name(); std::string aGroup = theObject->groupName(); int anIndex = 1; splitName(aName, anIndex); @@ -144,7 +234,7 @@ static std::string uniqueName(const ObjectPtr& theObject, ObjectNameMap& theExis ObjectNameMap::iterator aFoundGroup = theExistingNames.find(aGroup); bool isUnique = aFoundGroup == theExistingNames.end(); - std::map >::iterator aFound; + std::map >::iterator aFound; if (!isUnique) { aFound = aFoundGroup->second.find(aName); isUnique = aFound == aFoundGroup->second.end(); @@ -162,7 +252,7 @@ static std::string uniqueName(const ObjectPtr& theObject, ObjectNameMap& theExis if (anIndex != *aFoundIndex) break; // compose the new name - std::ostringstream aNewName; + std::wostringstream aNewName; aNewName << aName << "_" << anIndex; aName = aNewName.str(); // add new index @@ -180,7 +270,7 @@ void correntNonUniqueNames(DocumentPtr theDocument, std::list& theIm for (std::list::iterator anIt = theImported.begin(); anIt != theImported.end(); ++anIt) { // update name of feature - std::string aNewName = uniqueName(*anIt, aNames); + std::wstring aNewName = uniqueName(*anIt, aNames); (*anIt)->data()->setName(aNewName); // update names of results const std::list& aResults = (*anIt)->results();