Skip to content

Commit

Permalink
Data Exchange, Gltf Export - Metadata support Open-Cascade-SAS#79
Browse files Browse the repository at this point in the history
Add supporting of metadata key-value export into extras section of each node.
  • Loading branch information
ika authored and dpasukhi committed Sep 30, 2024
1 parent 3462ea5 commit 5cc7cbe
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 0 deletions.
112 changes: 112 additions & 0 deletions src/RWGltf/RWGltf_CafWriter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1978,6 +1978,8 @@ void RWGltf_CafWriter::writeNodes (const Handle(TDocStd_Document)& theDocument,
#ifdef HAVE_RAPIDJSON
Standard_ProgramError_Raise_if (myWriter.get() == NULL, "Internal error: RWGltf_CafWriter::writeNodes()");

Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(theDocument->Main());

// Prepare full indexed map of scene nodes in correct order.
RWGltf_GltfSceneNodeMap aSceneNodeMapWithChildren; // indexes starting from 1
for (XCAFPrs_DocumentExplorer aDocExplorer (theDocument, theRootLabels, XCAFPrs_DocumentExplorerFlags_None);
Expand Down Expand Up @@ -2130,6 +2132,18 @@ void RWGltf_CafWriter::writeNodes (const Handle(TDocStd_Document)& theDocument,
myWriter->String (aNodeName.ToCString());
}
}
{
Handle(TDataStd_NamedData) aNamedData = aShapeTool->GetNamedProperties(aDocNode.Label);
Handle(TDataStd_NamedData) aRefNamedData = aShapeTool->GetNamedProperties(aDocNode.RefLabel);
if (!aNamedData.IsNull() || !aRefNamedData.IsNull())
{
myWriter->Key("extras");
myWriter->StartObject();
writeExtrasAttributes(aNamedData);
writeExtrasAttributes(aRefNamedData);
myWriter->EndObject();
}
}
myWriter->EndObject();
}
myWriter->EndArray();
Expand All @@ -2142,6 +2156,104 @@ void RWGltf_CafWriter::writeNodes (const Handle(TDocStd_Document)& theDocument,
#endif
}

// =======================================================================
// function : writeExtrasAttributes
// purpose :
// =======================================================================
void RWGltf_CafWriter::writeExtrasAttributes(const Handle(TDataStd_NamedData)& theNamedData)
{
#ifdef HAVE_RAPIDJSON
Standard_ProgramError_Raise_if(myWriter.get() == NULL, "Internal error: RWGltf_CafWriter::writeExtrasAttributes()");

if (theNamedData.IsNull())
return;
theNamedData->LoadDeferredData();
if (theNamedData->HasIntegers())
{
const TColStd_DataMapOfStringInteger& anIntProperties = theNamedData->GetIntegersContainer();
for (TColStd_DataMapIteratorOfDataMapOfStringInteger anIter(anIntProperties);
anIter.More(); anIter.Next())
{
TCollection_AsciiString aKey(anIter.Key());
myWriter->Key(aKey.ToCString());
myWriter->Int(anIter.Value());
}
}
if (theNamedData->HasReals())
{
const TDataStd_DataMapOfStringReal& aRealProperties = theNamedData->GetRealsContainer();
for (TDataStd_DataMapIteratorOfDataMapOfStringReal anIter(aRealProperties);
anIter.More(); anIter.Next())
{
TCollection_AsciiString aKey(anIter.Key());
myWriter->Key(aKey.ToCString());
myWriter->Double(anIter.Value());
}
}
if (theNamedData->HasStrings())
{
const TDataStd_DataMapOfStringString& aStringProperties = theNamedData->GetStringsContainer();
for (TDataStd_DataMapIteratorOfDataMapOfStringString anIter(aStringProperties);
anIter.More(); anIter.Next())
{
TCollection_AsciiString aKey(anIter.Key());
TCollection_AsciiString aValue(anIter.Value());
myWriter->Key(aKey.ToCString());
myWriter->String(aValue.ToCString());
}
}
if (theNamedData->HasBytes())
{
const TDataStd_DataMapOfStringByte& aByteProperties = theNamedData->GetBytesContainer();
for (TDataStd_DataMapOfStringByte::Iterator anIter(aByteProperties);
anIter.More(); anIter.Next())
{
TCollection_AsciiString aKey(anIter.Key());
myWriter->Key(aKey.ToCString());
myWriter->Int(anIter.Value());
}
}
if (theNamedData->HasArraysOfIntegers())
{
const TDataStd_DataMapOfStringHArray1OfInteger& anArrayIntegerProperties =
theNamedData->GetArraysOfIntegersContainer();
for (TDataStd_DataMapOfStringHArray1OfInteger::Iterator anIter(anArrayIntegerProperties);
anIter.More(); anIter.Next())
{
TCollection_AsciiString aKey(anIter.Key());
myWriter->Key(aKey.ToCString());
myWriter->StartArray();
for (TColStd_HArray1OfInteger::Iterator anSubIter(anIter.Value()->Array1());
anSubIter.More(); anSubIter.Next())
{
myWriter->Int(anSubIter.Value());
}
myWriter->EndArray();
}
}
if (theNamedData->HasArraysOfReals())
{
const TDataStd_DataMapOfStringHArray1OfReal& anArrayRealsProperties =
theNamedData->GetArraysOfRealsContainer();
for (TDataStd_DataMapOfStringHArray1OfReal::Iterator anIter(anArrayRealsProperties);
anIter.More(); anIter.Next())
{
TCollection_AsciiString aKey(anIter.Key());
myWriter->Key(aKey.ToCString());
myWriter->StartArray();
for (TColStd_HArray1OfReal::Iterator anSubIter(anIter.Value()->Array1());
anSubIter.More(); anSubIter.Next())
{
myWriter->Double(anSubIter.Value());
}
myWriter->EndArray();
}
}
#else
(void)theNamedData;
#endif
}

// =======================================================================
// function : writeSamplers
// purpose :
Expand Down
5 changes: 5 additions & 0 deletions src/RWGltf/RWGltf_CafWriter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class RWMesh_FaceIterator;
class RWGltf_GltfOStreamWriter;
class RWGltf_GltfMaterialMap;
class RWGltf_GltfSceneNodeMap;
class TDataStd_NamedData;
class TDocStd_Document;

//! glTF writer context from XCAF document.
Expand Down Expand Up @@ -349,6 +350,10 @@ protected:
//! @param theMaterialMap [out] map of materials, filled with textures
Standard_EXPORT virtual void writeTextures (const RWGltf_GltfSceneNodeMap& theSceneNodeMap);

//! Write nodes.extras section with key-value attributes.
//! @param theNamedData [in] attributes map to process.
Standard_EXPORT virtual void writeExtrasAttributes(const Handle(TDataStd_NamedData)& theNamedData);

protected:

//! Shape + Style pair.
Expand Down
3 changes: 3 additions & 0 deletions tests/metadata/gltf_export/A1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# !!!! This file is generated automatically, do not edit manually! See end script
set filename bug28345_30338.stp
set ref_size 5896
3 changes: 3 additions & 0 deletions tests/metadata/gltf_export/A2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# !!!! This file is generated automatically, do not edit manually! See end script
set filename bug28389_CONFIDENTIAL_SHEET_METAL_F3D.stp
set ref_size 86278
3 changes: 3 additions & 0 deletions tests/metadata/gltf_export/A3
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# !!!! This file is generated automatically, do not edit manually! See end script
set filename bug28444_nist_ftc_06_asme1_ct5240_rd.stp
set ref_size 85383
3 changes: 3 additions & 0 deletions tests/metadata/gltf_export/A4
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# !!!! This file is generated automatically, do not edit manually! See end script
set filename bug29525_rev_part_neu_01.prt_converted_from_datakit.stp
set ref_size 80996
3 changes: 3 additions & 0 deletions tests/metadata/gltf_export/A5
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# !!!! This file is generated automatically, do not edit manually! See end script
set filename bug29633_nist_ctc_05_asme1_ap242-1.stp
set ref_size 69902
3 changes: 3 additions & 0 deletions tests/metadata/gltf_export/A6
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# !!!! This file is generated automatically, do not edit manually! See end script
set filename bug29803.stp
set ref_size 17032
3 changes: 3 additions & 0 deletions tests/metadata/gltf_export/A7
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# !!!! This file is generated automatically, do not edit manually! See end script
set filename sp7_04-do-242.stp
set ref_size 224779
3 changes: 3 additions & 0 deletions tests/metadata/gltf_export/A8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# !!!! This file is generated automatically, do not edit manually! See end script
set filename bug32087_part.stp
set ref_size 15789
3 changes: 3 additions & 0 deletions tests/metadata/gltf_export/A9
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# !!!! This file is generated automatically, do not edit manually! See end script
set filename nist_ftc_08_asme1_ap242-2.stp
set ref_size 118200
1 change: 1 addition & 0 deletions tests/metadata/gltf_export/begin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pload MODELING
39 changes: 39 additions & 0 deletions tests/metadata/gltf_export/end
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Set flag dump_file to 1 in order to regenerate script files with actual data
# used as reference. In this mode all tests intentionally report failure.
set dump_file 0

# Read original file
if { [string length $filename] > 1} {
set path_file [locate_data_file $filename]
if { [catch { ReadFile D $path_file } catch_result] } {
set err_msg "Error: file was not read - exception "
puts $err_msg
}
}

# mesh the shape before Gltf writing
XGetOneShape a D
incmesh a 0.1

# write file
WriteGltf D $imagedir/${casename}_D_First.gltf
set aSize [file size $imagedir/${casename}_D_First.gltf]

if { $dump_file == 1 } {
set fd_stream [open $dirname/$groupname/$gridname/$casename w]
puts $fd_stream "# !!!! This file is generated automatically, do not edit manually! See end script"
puts $fd_stream "set filename $filename"
puts $fd_stream "set ref_size $aSize"
close $fd_stream
puts "Error : Running in regeneration mode, comparison was not performed!"
} else {
if {$aSize != $ref_size} {
puts "Error: Wrong file size $aSize instead of $ref_size"
}
}

# finalize scenario
Close D
file delete $imagedir/${casename}_D_First.gltf
file delete $imagedir/${casename}_D_First.bin
puts "TEST COMPLETED"
1 change: 1 addition & 0 deletions tests/metadata/grids.list
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
001 step
002 gltf_export
File renamed without changes.

0 comments on commit 5cc7cbe

Please sign in to comment.