From e760b1e4c2f524a9a4b543374572fe60fe4fac50 Mon Sep 17 00:00:00 2001 From: mpv Date: Fri, 7 Feb 2020 11:30:24 +0300 Subject: [PATCH] Implementation of python dump of SHAPER STUDY for dead fields using XAO file format. --- idl/SHAPERSTUDY_Gen.idl | 4 + src/PY/SHAPERSTUDY.py | 80 ++++++++++++------- src/PY/SHAPERSTUDY_Object.py | 31 +++++++- src/StudyData/StudyData_XAO.cpp | 134 +++++++++++++++++++++++++++----- src/StudyData/StudyData_XAO.h | 37 +++++++++ 5 files changed, 239 insertions(+), 47 deletions(-) diff --git a/idl/SHAPERSTUDY_Gen.idl b/idl/SHAPERSTUDY_Gen.idl index eb0a6d9..fd9e2cc 100644 --- a/idl/SHAPERSTUDY_Gen.idl +++ b/idl/SHAPERSTUDY_Gen.idl @@ -121,6 +121,10 @@ interface SHAPER_Field : GEOM::GEOM_Field * \brief Sets the type of values in the field */ void SetValuesType(in short theType); + /*! + * \brief Returns the type of values in the field + */ + short GetValuesType(); /*! * \brief Sets the list of time step IDs in the field diff --git a/src/PY/SHAPERSTUDY.py b/src/PY/SHAPERSTUDY.py index 93789f5..9b9c5cd 100644 --- a/src/PY/SHAPERSTUDY.py +++ b/src/PY/SHAPERSTUDY.py @@ -218,39 +218,39 @@ class SHAPERSTUDY(SHAPERSTUDY_ORB__POA.Gen, anObj = salome.orb.string_to_object(anIOR) if type(anObj) == SHAPERSTUDY_ORB._objref_SHAPER_Group: if len(aResult): - aResult = aResult + '|' + aResult += '|' # store internal entry, type and list of indices of the group selection (separated by spaces) - aResult = aResult + anObj.GetEntry() + "|" + str(anObj.GetSelectionType()) + aResult += anObj.GetEntry() + "|" + str(anObj.GetSelectionType()) aSelList = anObj.GetSelection() - aResult = aResult + "|" + str(' '.join(str(anI) for anI in aSelList)) + aResult += "|" + str(' '.join(str(anI) for anI in aSelList)) elif type(anObj) == SHAPERSTUDY_ORB._objref_SHAPER_Field: if len(aResult): - aResult = aResult + '|' + aResult += '|' # same as for group, but in addition to the second string part - field specifics - aResult = aResult + anObj.GetEntry() + "|" + str(anObj.GetSelectionType()) - aResult = aResult + " " + str(anObj.GetDataType()) # values type + aResult += anObj.GetEntry() + "|" + str(anObj.GetSelectionType()) + aResult += " " + str(anObj.GetDataType()) # values type aSteps = anObj.GetSteps() - aResult = aResult + " " + str(len(aSteps)) # number of steps + aResult += " " + str(len(aSteps)) # number of steps aComps = anObj.GetComponents() - aResult = aResult + " " + str(len(aComps)) # number of components + aResult += " " + str(len(aComps)) # number of components for aComp in aComps: # components strings: but before remove spaces and '|' aCoded = aComp.replace(" ", "__space__").replace("|", "__vertical_bar__") - aResult = aResult + " " + aCoded + aResult += " " + aCoded for aStepNum in range(len(aSteps)): aVals = anObj.GetStep(aStepNum + 1).GetValues() if aStepNum == 0: - aResult = aResult + " " + str(len(aVals)) # first the number of values in the step - aResult = aResult + " " + str(anObj.GetStep(aStepNum + 1).GetStamp()) # ID of stamp in step + aResult += " " + str(len(aVals)) # first the number of values in the step + aResult += " " + str(anObj.GetStep(aStepNum + 1).GetStamp()) # ID of stamp in step for aVal in aVals: - aResult = aResult + " " + str(aVal) # all values of step + aResult += " " + str(aVal) # all values of step aSelList = anObj.GetSelection() - aResult = aResult + "|" + str(' '.join(str(anI) for anI in aSelList)) + aResult += "|" + str(' '.join(str(anI) for anI in aSelList)) elif isinstance(anObj, SHAPERSTUDY_ORB._objref_SHAPER_Object): if len(aResult): - aResult = aResult + '|' + aResult += '|' # store internal entry, current and old shapes in BRep format - aResult = aResult + anObj.GetEntry() + "|" + anObj.GetShapeStream().decode() - aResult = aResult + "|" + anObj.GetOldShapeStream().decode() + aResult += anObj.GetEntry() + "|" + anObj.GetShapeStream().decode() + aResult += "|" + anObj.GetOldShapeStream().decode() return aResult.encode() @@ -355,7 +355,7 @@ class SHAPERSTUDY(SHAPERSTUDY_ORB__POA.Gen, # to avoid spaces and parenthesis in the variable name aBaseName = theBaseName.replace(" ", "_").replace("(", "").replace(")", "") aName = aBaseName - while aName in __entry2DumpName__: + while aName in __entry2DumpName__.values(): aName = aBaseName + "_" + str(aPrefix) aPrefix = aPrefix + 1 __entry2DumpName__[theID] = aName @@ -403,8 +403,8 @@ class SHAPERSTUDY(SHAPERSTUDY_ORB__POA.Gen, aShapeVar = self.UniqueDumpName(anObj.GetName(), anObj.GetSO().GetID()) aShapeStr = aShapeVar + ", " for aGName in aGroupVarNames: - aShapeStr = aShapeStr + aGName + ", " - aShapeStr = aShapeStr + "= SHAPERSTUDY.shape(\"" + anObj.GetEntry() +"\")" + aShapeStr += aGName + ", " + aShapeStr += "= SHAPERSTUDY.shape(\"" + anObj.GetEntry() +"\")" script.append(aShapeStr) # dump also dead-shapes with groups and fields in the XAO format aRes, aHistSO = aShapeObj.GetSO().FindSubObject(2) # the History folder @@ -420,21 +420,38 @@ class SHAPERSTUDY(SHAPERSTUDY_ORB__POA.Gen, aXAO = StudyData_Swig.StudyData_XAO() aXAO.SetShape(aDeadShape.getShape()) anArchiveName = "archive_" + str(anArchiveNum) + ".xao" - anArchiveNum = anArchiveNum + 1 + anArchiveNum += 1 aDeadVarName = self.UniqueDumpName(aDeadShape.GetName(), aDSO.GetID()) - aDeadString = aDeadString + aDeadVarName + ", " + aDeadString += aDeadVarName + ", " aDGroupIter = aStudy.NewChildIterator(aDSO) + while aDGroupIter.More(): aDeadGroup = aDGroupIter.Value().GetObject() if isinstance(aDeadGroup, SHAPERSTUDY_ORB._objref_SHAPER_Group): aDGroupVarName = self.UniqueDumpName(aDeadGroup.GetName(), aDGroupIter.Value().GetID()) - aDeadString = aDeadString + aDGroupVarName + ", " + aDeadString += aDGroupVarName + ", " aGroupID = aXAO.AddGroup(aDeadGroup.GetSelectionType(), aDGroupVarName) for aSel in aDeadGroup.GetSelection(): aXAO.AddGroupSelection(aGroupID, aSel) + elif isinstance(aDeadGroup, SHAPERSTUDY_ORB._objref_SHAPER_Field): + aDeadField = aDeadGroup + aDFieldVarName = self.UniqueDumpName(aDeadField.GetName(), aDGroupIter.Value().GetID()) + aDeadString += aDFieldVarName + ", " + aComponents = aDeadField.GetComponents() + aFieldID = aXAO.AddField(aDeadField.GetValuesType(), aDeadField.GetSelectionType(), \ + len(aComponents), aDFieldVarName) + for aCompIndex in range(len(aComponents)): + aXAO.SetFieldComponent(aFieldID, aCompIndex, aComponents[aCompIndex]) + aSteps = aDeadField.GetSteps() + for aStep in aSteps: + aFieldStep = aDeadField.GetStep(aStep) + aXAO.AddStep(aFieldID, aStep, aFieldStep.GetStamp()) + aStepVals = aFieldStep.GetValues() + for aValue in aStepVals: + aXAO.AddStepValue(aFieldID, aStep, str(aValue)) aDGroupIter.Next() aXAO.Export(anArchiveName) - aDeadString = aDeadString + " = SHAPERSTUDY.archive(" + aShapeVar + ", \"" + anArchiveName + "\")" + aDeadString += " = SHAPERSTUDY.archive(" + aShapeVar + ", \"" + anArchiveName + "\")" script.append(aDeadString) aDeads.Next() pass @@ -606,6 +623,7 @@ def archive(theShape, theXAOFile): aRes = (aDShape,) # add groups and fields to the result aGroupIndex = 0 + aFieldIndex = 0 aSOIter = aStudy.NewChildIterator(aLastDeadSO) while aSOIter.More(): aGroupSO = aSOIter.Value() @@ -613,15 +631,25 @@ def archive(theShape, theXAOFile): if len(anIOR): aGroup = salome.orb.string_to_object(anIOR) if isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Group): - aRes = aRes + (aGroup,) + aRes += (aGroup,) aGroup.SetSelectionType(aXAO.GetGroupDimension(aGroupIndex)) aSelection = [] for aSel in aXAO.GetGroupSelection(aGroupIndex): aSelection.append(aSel) aGroup.SetSelection(aSelection) - aGroupIndex = aGroupIndex + 1 + aGroupIndex += 1 elif isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Field): - aRes = aRes + (aGroup,) + aField = aGroup + aRes += (aField,) + aField.SetValuesType(aXAO.GetValuesType(aFieldIndex)) + aField.SetSelectionType(aXAO.GetSelectionType(aFieldIndex)) + aField.SetComponents(aXAO.GetComponents(aFieldIndex)) + aField.ClearFieldSteps() + for aStepIndex in range(aXAO.GetStepsNum(aFieldIndex)): + aField.AddFieldStep(aXAO.GetStamp(aFieldIndex, aStepIndex), \ + aXAO.GetStepIndex(aFieldIndex, aStepIndex), \ + aXAO.GetValues(aFieldIndex, aStepIndex)) + aFieldIndex += 1 aSOIter.Next() return aRes return None # not found diff --git a/src/PY/SHAPERSTUDY_Object.py b/src/PY/SHAPERSTUDY_Object.py index 17ee361..dd75caa 100755 --- a/src/PY/SHAPERSTUDY_Object.py +++ b/src/PY/SHAPERSTUDY_Object.py @@ -264,6 +264,10 @@ class SHAPERSTUDY_Object(SHAPERSTUDY_ORB__POA.SHAPER_Object, aDeadSO = aBuilder.NewObject(aHistSO) anIndex = aDeadSO.Tag() aDeadSO.SetAttrString("AttributeName", self.SO.GetName() + " (" + str(anIndex) + ")") + aRes, aPixMap = aBuilder.FindAttribute(self.SO, "AttributePixMap") + if aRes: + aDeadPixMap = aBuilder.FindOrCreateAttribute(aDeadSO, "AttributePixMap") + aDeadPixMap.SetPixMap(aPixMap.GetPixMap()) aDead = SHAPERSTUDY_Object() aDeadEntry = "dead" + str(anIndex) + "_" + self.GetEntry() aDead.SetEntry(aDeadEntry) @@ -281,18 +285,32 @@ class SHAPERSTUDY_Object(SHAPERSTUDY_ORB__POA.SHAPER_Object, anIOR = aGroupSO.GetIOR() if len(anIOR): aGroup = salome.orb.string_to_object(anIOR) - if isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Group): - aDeadGroup = SHAPERSTUDY_Group() + if isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Group) or \ + isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Field): + if isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Group): + aDeadGroup = SHAPERSTUDY_Group() + else: + aDeadGroup = SHAPERSTUDY_Field() aDeadGroupEntry = "dead" + str(anIndex) + "_" + aGroup.GetEntry() aDeadGroup.SetEntry(aDeadGroupEntry) - aDeadGroup.SetShapeByPointer(aGroup.getShape()) aDeadGroup.SetSelectionType(aGroup.GetSelectionType()) aDeadGroup.SetSelection(aGroup.GetSelection()) + if isinstance(aGroup, SHAPERSTUDY_ORB._objref_SHAPER_Field): # additional field data + aDeadGroup.SetValuesType(aGroup.GetValuesType()) + aDeadGroup.SetSteps(aGroup.GetSteps()) + aDeadGroup.SetComponents(aGroup.GetComponents()) + for aStep in aGroup.GetSteps(): + aStepObj = aGroup.GetStep(aStep) + aDeadGroup.AddFieldStep(aStepObj.GetStamp(), aStep, aStepObj.GetValues()) aDeadGroupSO = aBuilder.NewObject(aDeadSO) aDeadGroup.SetSO(aDeadGroupSO) # 15.01.20 groups and fields names stays the same #aDeadGroupSO.SetAttrString("AttributeName", aGroupSO.GetName() + " (" + str(anIndex) + ")") aDeadGroupSO.SetAttrString("AttributeName", aGroupSO.GetName()) + aRes, aPixMap = aBuilder.FindAttribute(aGroupSO, "AttributePixMap") + if aRes: + aDeadPixMap = aBuilder.FindOrCreateAttribute(aDeadGroupSO, "AttributePixMap") + aDeadPixMap.SetPixMap(aPixMap.GetPixMap()) aDeadGroupObj = aDeadGroup._this() anIOR = salome.orb.object_to_string(aDeadGroupObj) aDeadGroupSO.SetAttrString("AttributeIOR", anIOR) @@ -407,6 +425,13 @@ class SHAPERSTUDY_Field(SHAPERSTUDY_ORB__POA.SHAPER_Field, SHAPERSTUDY_Group): """ self.valtype = theType + def GetValuesType( self ): + """ + Returns the type of values in the field + """ + return self.valtype + + def GetDataType( self ): """ Returns the type of values in the field in terms of GEOM enumeration diff --git a/src/StudyData/StudyData_XAO.cpp b/src/StudyData/StudyData_XAO.cpp index c48da3b..75e1e9a 100644 --- a/src/StudyData/StudyData_XAO.cpp +++ b/src/StudyData/StudyData_XAO.cpp @@ -29,7 +29,7 @@ #include -StudyData_XAO::StudyData_XAO() : myExport(NULL), myImport(NULL) +StudyData_XAO::StudyData_XAO() : myExport(NULL), myImport(NULL), myCurrentElementID(0) {} void StudyData_XAO::SetShape(const long long theShapePtr) @@ -42,19 +42,20 @@ void StudyData_XAO::SetShape(const long long theShapePtr) aGeometry->setTopoDS_Shape(*myShape); } -int StudyData_XAO::AddGroup(const int theSelType, const std::string theGroupName) -{ - if (!myExport) - myExport = new XAO::Xao(); - XAO::Dimension aDimension; +static XAO::Dimension GetDimension(const int theSelType) { switch(theSelType) { - case 7: aDimension = XAO::VERTEX; break; - case 6: aDimension = XAO::EDGE; break; - case 4: aDimension = XAO::FACE; break; - case 2: aDimension = XAO::SOLID; break; - default: aDimension = XAO::WHOLE; + case 7: return XAO::VERTEX; + case 6: return XAO::EDGE; + case 4: return XAO::FACE; + case 2: return XAO::SOLID; + default: return XAO::WHOLE; }; - XAO::Group* aNewGroup = myExport->addGroup(aDimension, theGroupName); + return XAO::WHOLE; +} + +int StudyData_XAO::AddGroup(const int theSelType, const std::string theGroupName) +{ + XAO::Group* aNewGroup = myExport->addGroup(GetDimension(theSelType), theGroupName); int anID = (int)myGroups.size(); myGroups[anID] = aNewGroup; return anID; @@ -68,13 +69,46 @@ void StudyData_XAO::AddGroupSelection(const int theGroupID, const int theSelecti void StudyData_XAO::Export(const std::string theFileName) { - if (!myExport) - myExport = new XAO::Xao(); myExport->setAuthor("ShaperStudy"); XAO::XaoExporter::saveToFile(myExport, theFileName, ""); } +int StudyData_XAO::AddField(const int theValType, const int theSelType, + const int theCompsNum, const std::string theFieldName) +{ + XAO::Field* aNewField = myExport->addField( + XAO::Type(theValType), GetDimension(theSelType), theCompsNum, theFieldName); + int anID = (int)myFields.size(); + myFields[anID] = aNewField; + return anID; +} + +void StudyData_XAO::SetFieldComponent(const int theFieldID, const int theCompIndex, + const std::string theCompName) +{ + myFields[theFieldID]->setComponentName(theCompIndex, theCompName); +} + +void StudyData_XAO::AddStep(const int theFieldID, const int theStepID, const int theStampID) +{ + XAO::Step* aNewStep = myFields[theFieldID]->addNewStep(theStepID); + aNewStep->setStamp(theStampID); + if (mySteps.find(theFieldID) == mySteps.end()) + mySteps[theFieldID] = std::map(); + mySteps[theFieldID][theStepID] = aNewStep; + myCurrentElementID = 0; +} + +void StudyData_XAO::AddStepValue( + const int theFieldID, const int theStepID, std::string theValue) +{ + int aColumns = myFields[theFieldID]->countComponents(); + mySteps[theFieldID][theStepID]->setStringValue( + myCurrentElementID / aColumns, myCurrentElementID % aColumns, theValue); + myCurrentElementID++; +} + std::string StudyData_XAO::Import(const std::string theFileName) { std::string anError; @@ -104,10 +138,8 @@ long long StudyData_XAO::GetShape() return (long long)(myShape); } -int StudyData_XAO::GetGroupDimension(const int theGroupID) -{ - XAO::Group* aXaoGroup = myImport->getGroup(theGroupID); - switch(aXaoGroup->getDimension()) { +static int GetSelectionType(const XAO::Dimension theDimension) { + switch(theDimension) { case XAO::VERTEX: return 7; case XAO::EDGE: return 6; case XAO::FACE: return 4; @@ -117,6 +149,12 @@ int StudyData_XAO::GetGroupDimension(const int theGroupID) return -1; } +int StudyData_XAO::GetGroupDimension(const int theGroupID) +{ + XAO::Group* aXaoGroup = myImport->getGroup(theGroupID); + return GetSelectionType(aXaoGroup->getDimension()); +} + std::list StudyData_XAO::GetGroupSelection(const int theGroupID) { XAO::Group* aXaoGroup = myImport->getGroup(theGroupID); @@ -126,3 +164,63 @@ std::list StudyData_XAO::GetGroupSelection(const int theGroupID) } return aResult; } + +int StudyData_XAO::GetValuesType(const int theFieldID) +{ + XAO::Field* aField = myImport->getField(theFieldID); + return (int)(aField->getType()); +} + +int StudyData_XAO::GetSelectionType(const int theFieldID) +{ + XAO::Field* aField = myImport->getField(theFieldID); + return GetSelectionType(aField->getDimension()); +} + +std::list StudyData_XAO::GetComponents(const int theFieldID) +{ + XAO::Field* aField = myImport->getField(theFieldID); + std::list aResult; + int aNum = aField->countComponents(); + for(int a = 0; a < aNum; a++) { + aResult.push_back(aField->getComponentName(a)); + } + return aResult; +} + +void StudyData_XAO::BeginSteps(const int theFieldID) +{ + myStepIterator = myImport->getField(theFieldID)->begin(); +} + +bool StudyData_XAO::More(const int theFieldID) +{ + return myStepIterator != myImport->getField(theFieldID)->end(); +} + +void StudyData_XAO::Next() +{ + myStepIterator++; +} + +int StudyData_XAO::GetStamp() +{ + return (*myStepIterator)->getStamp(); +} + +int StudyData_XAO::GetStepIndex() +{ + return (*myStepIterator)->getStep(); +} + +std::list StudyData_XAO::GetValues() +{ + std::list aResult; + int aComps = (*myStepIterator)->countComponents(); + int aVals = (*myStepIterator)->countValues(); + for(int a = 0; a < aVals; a++) { + aResult.push_back((*myStepIterator)->getStringValue(a / aComps, a % aComps)); + } + + return aResult; +} diff --git a/src/StudyData/StudyData_XAO.h b/src/StudyData/StudyData_XAO.h index bc96381..8b4e989 100644 --- a/src/StudyData/StudyData_XAO.h +++ b/src/StudyData/StudyData_XAO.h @@ -24,6 +24,8 @@ #include "StudyData.h" #include +#include +#include #include #include @@ -36,6 +38,10 @@ class StudyData_XAO XAO::Xao* myExport; ///< the XAO instance for export XAO::Xao* myImport; ///< the XAO instance for export std::map myGroups; ///< id of group to the group structure + std::map myFields; ///< id of field to the field structure + std::map > mySteps; ///< id of field to the field structure + int myCurrentElementID; ///< to add elements of step one by one + XAO::stepIterator myStepIterator; ///< contains a current steps iterator (on import) public: StudyData_EXPORT StudyData_XAO(); @@ -48,6 +54,18 @@ public: // sets the selection for an already added group StudyData_EXPORT void AddGroupSelection(const int theGroupID, const int theSelection); + // add a new field for export to XAO; returns id of this field + StudyData_EXPORT int AddField(const int theValType, const int theSelType, + const int theCompsNum, const std::string theFieldName); + // set the component name for the exported field + StudyData_EXPORT void SetFieldComponent(const int theFieldID, const int theCompIndex, + const std::string theCompName); + // adds a step to the exported field + StudyData_EXPORT void AddStep(const int theFieldID, const int theStepID, const int theStampID); + // adds the value to the step of the exported field. values must come one by one (row by row) + StudyData_EXPORT void AddStepValue( + const int theFieldID, const int theStepID, std::string theValue); + // performs the export to XAO StudyData_EXPORT void Export(const std::string theFileName); @@ -62,6 +80,25 @@ public: // Returns a selection list of indices of the group StudyData_EXPORT std::list GetGroupSelection(const int theGroupID); + + // Returns a value type of the imported field + StudyData_EXPORT int GetValuesType(const int theFieldID); + // Returns a selection type of the imported field + StudyData_EXPORT int GetSelectionType(const int theFieldID); + // Returns components names of the imported field + StudyData_EXPORT std::list GetComponents(const int theFieldID); + // Starts iteration of steps + StudyData_EXPORT void BeginSteps(const int theFieldID); + // Returns true if steps iteration contains a current step + StudyData_EXPORT bool More(const int theFieldID); + // Iterates the step iterator contains a current step + StudyData_EXPORT void Next(); + // Returns a stamp ID of the imported field step + StudyData_EXPORT int GetStamp(); + // Returns a step ID of the imported field step + StudyData_EXPORT int GetStepIndex(); + // Returns string values of the imported field step + StudyData_EXPORT std::list GetValues(); }; #endif // !StudyData_XAO_H -- 2.39.2