From 45b03d86c0d3014a3cc4dd428e214a5cc8d0e7e1 Mon Sep 17 00:00:00 2001 From: mpv Date: Fri, 25 Nov 2016 15:31:47 +0300 Subject: [PATCH] Issue #1865 : implementation of import/export fields in XAO format --- .../ExchangePlugin_ExportFeature.cpp | 90 ++++++++++++++++++- .../ExchangePlugin_ImportFeature.cpp | 76 +++++++++++++++- src/ExchangePlugin/ExchangePlugin_Tools.cpp | 31 +++++++ src/ExchangePlugin/ExchangePlugin_Tools.h | 5 ++ src/XAO/XAO_XaoUtils.cxx | 2 +- 5 files changed, 197 insertions(+), 7 deletions(-) diff --git a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp index ddc8e1431..e2bbfa0e5 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp @@ -28,15 +28,20 @@ #include #include +#include +#include +#include #include #include #include #include #include +#include #include #include #include +#include #include #include @@ -224,7 +229,8 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName) // conversion of dimension std::string aSelectionType = aSelectionList->selectionType(); - std::string aDimensionString = ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType); + std::string aDimensionString = + ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType); XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString); XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension, @@ -243,6 +249,86 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName) } } + // fields + int aFieldCount = document()->size(ModelAPI_ResultField::group()); + for (int aFieldIndex = 0; aFieldIndex < aFieldCount; ++aFieldIndex) { + ResultFieldPtr aResultField = + std::dynamic_pointer_cast( + document()->object(ModelAPI_ResultField::group(), aFieldIndex)); + + FeaturePtr aFieldFeature = document()->feature(aResultField); + + AttributeSelectionListPtr aSelectionList = + aFieldFeature->selectionList("selected"); + + // conversion of dimension + std::string aSelectionType = aSelectionList->selectionType(); + std::string aDimensionString = + ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType); + XAO::Dimension aFieldDimension = XAO::XaoUtils::stringToDimension(aDimensionString); + bool isWholePart = aSelectionType == "part"; + // get tables and their type + std::shared_ptr aTables = aFieldFeature->tables("values"); + std::string aTypeString = ExchangePlugin_Tools::valuesType2xaoType(aTables->type()); + XAO::Type aFieldType = XAO::XaoUtils::stringToFieldType(aTypeString); + + XAO::Field* aXaoField = aXao.addField(aFieldType, aFieldDimension, aTables->columns(), + aResultField->data()->name()); + // set components names + AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names"); + for(int aComp = 0; aComp < aComponents->size(); aComp++) { + std::string aName = aComponents->value(aComp); + aXaoField->setComponentName(aComp, aName); + } + + AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps"); + for (int aStepIndex = 0; aStepIndex < aTables->tables(); aStepIndex++) { + XAO::Step* aStep = aXaoField->addNewStep(aStepIndex); + aStep->setStep(aStepIndex); + int aStampIndex = aStamps->value(aStepIndex); + aStep->setStamp(aStampIndex); + int aNumElements = isWholePart ? aXaoField->countElements() : aTables->rows(); + int aNumComps = aTables->columns(); + // omit default values first row + for(int aRow = isWholePart ? 0 : 1; aRow < aNumElements; aRow++) { + for(int aCol = 0; aCol < aNumComps; aCol++) { + int anElementID = 0; + if (!isWholePart) { + // element index actually is the ID of the selection + AttributeSelectionPtr aSelection = aSelectionList->value(aRow - 1); + + // complex conversion of reference id to element index + int aReferenceID = aSelection->Id(); + std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID); + int anElementID = + aXao.getGeometry()->getElementIndexByReference(aFieldDimension, aReferenceString); + } + + ModelAPI_AttributeTables::Value aVal = aTables->value( + isWholePart ? 0 : aRow, aCol, aStepIndex); + std::ostringstream aStr; // string value + switch(aTables->type()) { + case ModelAPI_AttributeTables::BOOLEAN: + aStr<<(aVal.myBool ? "True" : "False"); + break; + case ModelAPI_AttributeTables::INTEGER: + aStr<setStringValue(isWholePart ? aRow : anElementID, aCol, aStrVal); + } + } + } + } + + // exporting XAOExport(theFileName, &aXao, anError); @@ -254,7 +340,7 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName) } catch (XAO::XAO_Exception& e) { std::string anError = e.what(); - setError("An error occurred while importing " + theFileName + ": " + anError); + setError("An error occurred while exporting " + theFileName + ": " + anError); return; } } diff --git a/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp index e55c4efd9..2d8c81bb2 100644 --- a/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp @@ -23,6 +23,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -35,6 +38,8 @@ #include #include +#include +#include #include @@ -147,11 +152,11 @@ void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName) ResultBodyPtr aResultBody = createResultBody(aGeomShape); setResult(aResultBody); - // Process groups + // Process groups/fields std::shared_ptr aRefListOfGroups = std::dynamic_pointer_cast(data()->attribute(FEATURES_ID())); - // Remove previous groups stored in RefList + // Remove previous groups/fields stored in RefList std::list anGroupList = aRefListOfGroups->list(); std::list::iterator anGroupIt = anGroupList.begin(); for (; anGroupIt != anGroupList.end(); ++anGroupIt) { @@ -174,9 +179,9 @@ void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName) AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list"); // conversion of dimension - XAO::Dimension aGroupDimension = aXaoGroup->getDimension(); std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension()); - std::string aSelectionType = ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString); + std::string aSelectionType = + ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString); aSelectionList->setSelectionType(aSelectionType); for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) { @@ -190,6 +195,69 @@ void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName) aSelectionList->value(anElementIndex)->setId(aReferenceID); } } + // Create new fields + for (int aFieldIndex = 0; aFieldIndex < aXao.countFields(); ++aFieldIndex) { + XAO::Field* aXaoField = aXao.getField(aFieldIndex); + + std::shared_ptr aFieldFeature = addFeature("Field"); + + // group name + if (!aXaoField->getName().empty()) + aFieldFeature->data()->setName(aXaoField->getName()); + + // fill selection + AttributeSelectionListPtr aSelectionList = aFieldFeature->selectionList("selected"); + + // conversion of dimension + std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoField->getDimension()); + std::string aSelectionType = + ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString); + aSelectionList->setSelectionType(aSelectionType); + // conversion of type + XAO::Type aFieldType = aXaoField->getType(); + std::string aTypeString = XAO::XaoUtils::fieldTypeToString(aFieldType); + ModelAPI_AttributeTables::ValueType aType = + ExchangePlugin_Tools::xaoType2valuesType(aTypeString); + // set components names + AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names"); + aComponents->setSize(aXaoField->countComponents()); + for(int aComp = 0; aComp < aXaoField->countComponents(); aComp++) { + aComponents->setValue(aComp, aXaoField->getComponentName(aComp)); + } + + AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps"); + aStamps->setSize(aXaoField->countSteps()); + std::shared_ptr aTables = aFieldFeature->tables("values"); + aTables->setSize( + aXaoField->countElements() + 1, aXaoField->countComponents(), aXaoField->countSteps()); + aTables->setType(aType); + // iterate steps + XAO::stepIterator aStepIter = aXaoField->begin(); + for(int aStepIndex = 0; aStepIter != aXaoField->end(); aStepIter++, aStepIndex++) { + aStamps->setValue(aStepIndex, (*aStepIter)->getStamp()); + for(int aRow = 1; aRow <= aXaoField->countElements(); aRow++) { + for(int aCol = 0; aCol < aXaoField->countComponents(); aCol++) { + ModelAPI_AttributeTables::Value aVal; + std::string aValStr = (*aStepIter)->getStringValue(aRow - 1, aCol); + switch(aType) { + case ModelAPI_AttributeTables::BOOLEAN: + aVal.myBool = aValStr == "True"; + break; + case ModelAPI_AttributeTables::INTEGER: + aVal.myInt = atoi(aValStr.c_str()); + break; + case ModelAPI_AttributeTables::DOUBLE: + aVal.myDouble = atof(aValStr.c_str()); + break; + case ModelAPI_AttributeTables::STRING: + aVal.myStr = aValStr; + break; + } + aTables->setValue(aVal, aRow, aCol, aStepIndex); + } + } + } + } // Top avoid problems in Object Browser update: issue #1647. ModelAPI_EventCreator::get()->sendReordered( std::dynamic_pointer_cast(aRefListOfGroups->owner())); diff --git a/src/ExchangePlugin/ExchangePlugin_Tools.cpp b/src/ExchangePlugin/ExchangePlugin_Tools.cpp index a7ec0d9bf..c4299c30e 100644 --- a/src/ExchangePlugin/ExchangePlugin_Tools.cpp +++ b/src/ExchangePlugin/ExchangePlugin_Tools.cpp @@ -28,6 +28,8 @@ std::string ExchangePlugin_Tools::selectionType2xaoDimension(const std::string& return "face"; else if (theType == "Solids" || theType == "solid") return "solid"; + else if (theType == "Part" || theType == "part") + return "part"; return std::string(); } @@ -46,3 +48,32 @@ std::string ExchangePlugin_Tools::xaoDimension2selectionType(const std::string& return std::string(); } + +std::string ExchangePlugin_Tools::valuesType2xaoType( + const ModelAPI_AttributeTables::ValueType& theType) +{ + switch(theType) { + case ModelAPI_AttributeTables::BOOLEAN: + return "boolean"; + case ModelAPI_AttributeTables::INTEGER: + return "integer"; + case ModelAPI_AttributeTables::DOUBLE: + return "double"; + case ModelAPI_AttributeTables::STRING: + return "string"; + } + return ""; +} + +ModelAPI_AttributeTables::ValueType ExchangePlugin_Tools::xaoType2valuesType(std::string theType) +{ + if (theType == "boolean") + return ModelAPI_AttributeTables::BOOLEAN; + if (theType == "integer") + return ModelAPI_AttributeTables::INTEGER; + if (theType == "double") + return ModelAPI_AttributeTables::DOUBLE; + if (theType == "string") + return ModelAPI_AttributeTables::STRING; + return ModelAPI_AttributeTables::DOUBLE; +} diff --git a/src/ExchangePlugin/ExchangePlugin_Tools.h b/src/ExchangePlugin/ExchangePlugin_Tools.h index c481d8ddc..2ff2507dc 100644 --- a/src/ExchangePlugin/ExchangePlugin_Tools.h +++ b/src/ExchangePlugin/ExchangePlugin_Tools.h @@ -8,6 +8,7 @@ #define EXCHANGEPLUGIN_TOOLS_H_ #include +#include #include #include @@ -28,6 +29,10 @@ public: /// Converts string representation of XAO dimension to selection type. static std::string xaoDimension2selectionType(const std::string& theDimension); + /// Converts representation of values type to XAO type. + static std::string valuesType2xaoType(const ModelAPI_AttributeTables::ValueType& theType); + /// Converts representation of values type to XAO type. + static ModelAPI_AttributeTables::ValueType xaoType2valuesType(std::string theType); }; #endif /* EXCHANGEPLUGIN_TOOLS_H_ */ diff --git a/src/XAO/XAO_XaoUtils.cxx b/src/XAO/XAO_XaoUtils.cxx index 8d08cea84..5d63f1748 100644 --- a/src/XAO/XAO_XaoUtils.cxx +++ b/src/XAO/XAO_XaoUtils.cxx @@ -107,7 +107,7 @@ throw(XAO_Exception) return XAO::FACE; if (dimension == "solid") return XAO::SOLID; - if (dimension == "whole") + if (dimension == "part") return XAO::WHOLE; throw XAO_Exception(MsgBuilder() << "Bad dimension: " << dimension); -- 2.39.2