]> OCCT Git - occt.git/commitdiff
0032989: Data Exchange, RWGltf_CafReader - fill in metadata map from asset.extras
authorkgv <kgv@opencascade.com>
Wed, 25 May 2022 18:19:43 +0000 (21:19 +0300)
committerafokin <afokin@opencascade.com>
Tue, 31 May 2022 16:26:25 +0000 (19:26 +0300)
src/RWGltf/RWGltf_GltfJsonParser.cxx
src/RWGltf/RWGltf_GltfJsonParser.hxx
src/XSDRAWSTLVRML/XSDRAWSTLVRML.cxx
tests/de_mesh/gltf_read/helmet

index 3423ebaee1cde543596437ca623bf6f0fe0e1b36..d6387a2e98fdafbed6f51d0e8a889218bcef89d6 100644 (file)
@@ -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());
+      }
+    }
+  }
 }
 
 // =======================================================================
index 6b078ed12e22117d89f0b913c2d007466e39c0d7..e53fb1dfa41fd083163815aeb6b2db2bc03ff5c4 100644 (file)
@@ -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
index d533ca1decd30e85f97a4dc69768c8c37a66cb65..6d3422963d89edd806f4647b0e163579a316bc04 100644 (file)
@@ -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<TCollection_AsciiString>::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<TCollection_AsciiString>::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"
index c54d675558beb46367e327db983501ec5b9f3cf3..adf703f1f148b0bd5fe1110030c1092b405e2174 100644 (file)
@@ -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