From e9c43fee293f528cc6e2e4e02c538482d5af3490 Mon Sep 17 00:00:00 2001 From: kgv Date: Wed, 25 May 2022 21:19:43 +0300 Subject: [PATCH] 0032989: Data Exchange, RWGltf_CafReader - fill in metadata map from asset.extras --- src/RWGltf/RWGltf_GltfJsonParser.cxx | 24 +++++- src/RWGltf/RWGltf_GltfJsonParser.hxx | 4 + src/XSDRAWSTLVRML/XSDRAWSTLVRML.cxx | 105 ++++++++++++++------------- tests/de_mesh/gltf_read/helmet | 4 + 4 files changed, 85 insertions(+), 52 deletions(-) diff --git a/src/RWGltf/RWGltf_GltfJsonParser.cxx b/src/RWGltf/RWGltf_GltfJsonParser.cxx index 3423ebaee1..d6387a2e98 100644 --- a/src/RWGltf/RWGltf_GltfJsonParser.cxx +++ b/src/RWGltf/RWGltf_GltfJsonParser.cxx @@ -188,7 +188,8 @@ RWGltf_GltfJsonParser::RWGltf_GltfJsonParser (TopTools_SequenceOfShape& theRootS myToSkipEmptyNodes (true), myToLoadAllScenes (false), myUseMeshNameAsFallback (true), - myToProbeHeader (false) + myToProbeHeader (false), + myToReadAssetExtras (true) { myCSTrsf.SetInputLengthUnit (1.0); // meters myCSTrsf.SetInputCoordinateSystem (RWMesh_CoordinateSystem_glTF); @@ -287,6 +288,27 @@ void RWGltf_GltfJsonParser::gltfParseAsset() myMetadata->Add ("copyright", aCopyRight->GetString()); } } + + if (const RWGltf_JsonValue* anExtras = myToReadAssetExtras ? findObjectMember (*anAsset, "extras") : nullptr) + { + for (ConstMemberIterator aChildIter = anExtras->MemberBegin(); aChildIter != anExtras->MemberEnd(); ++aChildIter) + { + if (!aChildIter->name.IsString()) + { + continue; + } + + const TCollection_AsciiString aKey (aChildIter->name.GetString()); + if (aChildIter->value.IsString()) + { + myMetadata->Add (aKey, aChildIter->value.GetString()); + } + if (aChildIter->value.IsNumber()) + { + myMetadata->Add (aKey, aChildIter->value.GetDouble()); + } + } + } } // ======================================================================= diff --git a/src/RWGltf/RWGltf_GltfJsonParser.hxx b/src/RWGltf/RWGltf_GltfJsonParser.hxx index 6b078ed12e..e53fb1dfa4 100644 --- a/src/RWGltf/RWGltf_GltfJsonParser.hxx +++ b/src/RWGltf/RWGltf_GltfJsonParser.hxx @@ -94,6 +94,9 @@ public: //! Set metadata map. void SetMetadata (TColStd_IndexedDataMapOfStringString& theMetadata) { myMetadata = &theMetadata; } + //! Set flag to translate asset.extras into metadata. + void SetReadAssetExtras (bool theToRead) { myToReadAssetExtras = theToRead; } + //! Return transformation from glTF to OCCT coordinate system. const RWMesh_CoordinateSystemConverter& CoordinateSystemConverter() const { return myCSTrsf; } @@ -439,6 +442,7 @@ protected: bool myToLoadAllScenes; //!< flag to load all scenes in the document, FALSE by default bool myUseMeshNameAsFallback; //!< flag to use Mesh name in case if Node name is empty, TRUE by default bool myToProbeHeader; //!< flag to probe header without full reading, FALSE by default + bool myToReadAssetExtras; //!< flag to translate asset.extras into metadata, TRUE by default #ifdef HAVE_RAPIDJSON GltfElementMap myGltfRoots[RWGltf_GltfRootElement_NB]; //!< glTF format root elements diff --git a/src/XSDRAWSTLVRML/XSDRAWSTLVRML.cxx b/src/XSDRAWSTLVRML/XSDRAWSTLVRML.cxx index d533ca1dec..6d3422963d 100644 --- a/src/XSDRAWSTLVRML/XSDRAWSTLVRML.cxx +++ b/src/XSDRAWSTLVRML/XSDRAWSTLVRML.cxx @@ -194,6 +194,7 @@ static Standard_Integer ReadGltf (Draw_Interpretor& theDI, Standard_Boolean toKeepLateData = Standard_True; Standard_Boolean toPrintDebugInfo = Standard_False; Standard_Boolean toLoadAllScenes = Standard_False; + Standard_Boolean toPrintAssetInfo = Standard_False; Standard_Boolean isNoDoc = (TCollection_AsciiString(theArgVec[0]) == "readgltf"); for (Standard_Integer anArgIter = 1; anArgIter < theNbArgs; ++anArgIter) { @@ -203,33 +204,18 @@ static Standard_Integer ReadGltf (Draw_Interpretor& theDI, && (anArgCase == "-nocreate" || anArgCase == "-nocreatedoc")) { - toUseExistingDoc = Standard_True; - if (anArgIter + 1 < theNbArgs - && Draw::ParseOnOff (theArgVec[anArgIter + 1], toUseExistingDoc)) - { - ++anArgIter; - } + toUseExistingDoc = Draw::ParseOnOffIterator (theNbArgs, theArgVec, anArgIter); } else if (anArgCase == "-parallel") { - isParallel = Standard_True; - if (anArgIter + 1 < theNbArgs - && Draw::ParseOnOff (theArgVec[anArgIter + 1], isParallel)) - { - ++anArgIter; - } + isParallel = Draw::ParseOnOffIterator (theNbArgs, theArgVec, anArgIter); } else if (anArgCase == "-doubleprec" || anArgCase == "-doubleprecision" || anArgCase == "-singleprec" || anArgCase == "-singleprecision") { - isDoublePrec = Standard_True; - if (anArgIter + 1 < theNbArgs - && Draw::ParseOnOff (theArgVec[anArgIter + 1], isDoublePrec)) - { - ++anArgIter; - } + isDoublePrec = Draw::ParseOnOffIterator (theNbArgs, theArgVec, anArgIter); if (anArgCase.StartsWith ("-single")) { isDoublePrec = !isDoublePrec; @@ -237,40 +223,20 @@ static Standard_Integer ReadGltf (Draw_Interpretor& theDI, } else if (anArgCase == "-skiplateloading") { - toSkipLateDataLoading = Standard_True; - if (anArgIter + 1 < theNbArgs - && Draw::ParseOnOff (theArgVec[anArgIter + 1], toSkipLateDataLoading)) - { - ++anArgIter; - } + toSkipLateDataLoading = Draw::ParseOnOffIterator (theNbArgs, theArgVec, anArgIter); } else if (anArgCase == "-keeplate") { - toKeepLateData = Standard_True; - if (anArgIter + 1 < theNbArgs - && Draw::ParseOnOff (theArgVec[anArgIter + 1], toKeepLateData)) - { - ++anArgIter; - } + toKeepLateData = Draw::ParseOnOffIterator (theNbArgs, theArgVec, anArgIter); } else if (anArgCase == "-allscenes") { - toLoadAllScenes = Standard_True; - if (anArgIter + 1 < theNbArgs - && Draw::ParseOnOff (theArgVec[anArgIter + 1], toLoadAllScenes)) - { - ++anArgIter; - } + toLoadAllScenes = Draw::ParseOnOffIterator (theNbArgs, theArgVec, anArgIter); } else if (anArgCase == "-toprintinfo" || anArgCase == "-toprintdebuginfo") { - toPrintDebugInfo = Standard_True; - if (anArgIter + 1 < theNbArgs - && Draw::ParseOnOff (theArgVec[anArgIter + 1], toPrintDebugInfo)) - { - ++anArgIter; - } + toPrintDebugInfo = Draw::ParseOnOffIterator (theNbArgs, theArgVec, anArgIter); } else if (anArgCase == "-listexternalfiles" || anArgCase == "-listexternals" @@ -278,7 +244,12 @@ static Standard_Integer ReadGltf (Draw_Interpretor& theDI, || anArgCase == "-external" || anArgCase == "-externalfiles") { - toListExternalFiles = Standard_True; + toListExternalFiles = Draw::ParseOnOffIterator (theNbArgs, theArgVec, anArgIter); + } + else if (anArgCase == "-assetinfo" + || anArgCase == "-metadata") + { + toPrintAssetInfo = Draw::ParseOnOffIterator (theNbArgs, theArgVec, anArgIter); } else if (aDestName.IsEmpty()) { @@ -294,6 +265,13 @@ static Standard_Integer ReadGltf (Draw_Interpretor& theDI, return 1; } } + if (aFilePath.IsEmpty() && !aDestName.IsEmpty()) + { + if (toListExternalFiles || toPrintAssetInfo) + { + std::swap (aFilePath, aDestName); + } + } if (aFilePath.IsEmpty()) { Message::SendFail() << "Syntax error: wrong number of arguments"; @@ -302,7 +280,7 @@ static Standard_Integer ReadGltf (Draw_Interpretor& theDI, Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator (theDI, 1); Handle(TDocStd_Document) aDoc; - if (!toListExternalFiles + if (!aDestName.IsEmpty() && !isNoDoc) { Handle(TDocStd_Application) anApp = DDocStd::GetApplication(); @@ -323,12 +301,14 @@ static Standard_Integer ReadGltf (Draw_Interpretor& theDI, return 1; } } + Standard_Real aScaleFactorM = 1.; if (!XCAFDoc_DocumentTool::GetLengthUnit(aDoc, aScaleFactorM, UnitsMethods_LengthUnit_Meter)) { XSAlgo::AlgoContainer()->PrepareForTransfer(); // update unit info aScaleFactorM = UnitsMethods::GetCasCadeLengthUnit(UnitsMethods_LengthUnit_Meter); } + RWGltf_CafReader aReader; aReader.SetSystemLengthUnit (aScaleFactorM); aReader.SetSystemCoordinateSystem (RWMesh_CoordinateSystem_Zup); @@ -339,13 +319,9 @@ static Standard_Integer ReadGltf (Draw_Interpretor& theDI, aReader.SetToKeepLateData (toKeepLateData); aReader.SetToPrintDebugMessages (toPrintDebugInfo); aReader.SetLoadAllScenes (toLoadAllScenes); - if (toListExternalFiles) + if (aDestName.IsEmpty()) { aReader.ProbeHeader (aFilePath); - for (NCollection_IndexedMap::Iterator aFileIter (aReader.ExternalFiles()); aFileIter.More(); aFileIter.Next()) - { - theDI << "\"" << aFileIter.Value() << "\" "; - } } else { @@ -361,6 +337,32 @@ static Standard_Integer ReadGltf (Draw_Interpretor& theDI, Draw::Set (aDestName.ToCString(), aDrawDoc); } } + + bool isFirstLine = true; + if (toPrintAssetInfo) + { + for (TColStd_IndexedDataMapOfStringString::Iterator aKeyIter (aReader.Metadata()); aKeyIter.More(); aKeyIter.Next()) + { + if (!isFirstLine) + { + theDI << "\n"; + } + isFirstLine = false; + theDI << aKeyIter.Key() << ": " << aKeyIter.Value(); + } + } + if (toListExternalFiles) + { + if (!isFirstLine) + { + theDI << "\n"; + } + for (NCollection_IndexedMap::Iterator aFileIter (aReader.ExternalFiles()); aFileIter.More(); aFileIter.Next()) + { + theDI << "\"" << aFileIter.Value() << "\" "; + } + } + return 0; } @@ -2338,7 +2340,7 @@ void XSDRAWSTLVRML::InitCommands (Draw_Interpretor& theCommands) //XSDRAW::LoadDraw(theCommands); theCommands.Add ("ReadGltf", - "ReadGltf Doc file [-parallel {on|off}] [-listExternalFiles] [-noCreateDoc] [-doublePrecision {on|off}]" + "ReadGltf Doc file [-parallel {on|off}] [-listExternalFiles] [-noCreateDoc] [-doublePrecision {on|off}] [-assetInfo]" "\n\t\t: Read glTF file into XDE document." "\n\t\t: -listExternalFiles do not read mesh and only list external files" "\n\t\t: -noCreateDoc read into existing XDE document" @@ -2347,9 +2349,10 @@ void XSDRAWSTLVRML::InitCommands (Draw_Interpretor& theCommands) "\n\t\t: -skipLateLoading data loading is skipped and can be performed later" "\n\t\t: (false by default)" "\n\t\t: -keepLate data is loaded into itself with preservation of information" - "\n\t\t: about deferred storage to load/unload this data later.", + "\n\t\t: about deferred storage to load/unload this data later." "\n\t\t: -allScenes load all scenes defined in the document instead of default one (false by default)" "\n\t\t: -toPrintDebugInfo print additional debug information during data reading" + "\n\t\t: -assetInfo print asset information", __FILE__, ReadGltf, g); theCommands.Add ("readgltf", "readgltf shape file" diff --git a/tests/de_mesh/gltf_read/helmet b/tests/de_mesh/gltf_read/helmet index c54d675558..adf703f1f1 100644 --- a/tests/de_mesh/gltf_read/helmet +++ b/tests/de_mesh/gltf_read/helmet @@ -3,6 +3,10 @@ puts "0030691: test glTF reader on standard sample models" puts "========" ReadGltf D [locate_data_file bug30691_DamagedHelmet.gltf] + +set anAssetInfo [ReadGltf [locate_data_file bug30691_DamagedHelmet.gltf] -assetInfo] +if { "$anAssetInfo" != "generator: Khronos Blender glTF 2.0 exporter" } { puts "Error: unexpected asset info" } + XGetOneShape s D checknbshapes s -face 1 -compound 0 checktrinfo s -tri 15452 -nod 14556