]> OCCT Git - occt.git/commitdiff
0033816: Data Exchange - Implementing common logic for scaling during Write procedure... CR33816
authoranv <anv@opencascade.com>
Wed, 28 Aug 2024 03:23:16 +0000 (04:23 +0100)
committeranv <anv@opencascade.com>
Tue, 17 Sep 2024 16:22:30 +0000 (17:22 +0100)
Implementation for Gltf and STEP formats.

src/DE/DE_ConfigurationNode.hxx
src/DE/DE_Wrapper.cxx
src/RWGltf/RWGltf_Provider.cxx
src/STEPCAFControl/STEPCAFControl_Provider.cxx
tests/de_wrapper/configuration/A3
tests/de_wrapper/configuration/A4
tests/de_wrapper/gltf/A6
tests/de_wrapper/step/A6

index 45b653338f10cfabc6eb8ce2802b218aa697e48a..d424dc92fa2f4340dc8b058db3d753754c4b5cf2 100644 (file)
@@ -154,7 +154,8 @@ public:
   //!< Internal parameters for transfer process
   struct DE_SectionGlobal
   {
-    Standard_Real LengthUnit = 1.0; //!< Scale length unit value from MM, default 1.0 (MM)
+    Standard_Real LengthUnit = 1.0; //!< Target Unit (scaling based on MM) for the transfer process, default 1.0 (MM)
+    Standard_Real SystemUnit = 1.0; //!< System Unit (scaling based on MM) to be used when initial unit is unknown, default 1.0 (MM)
   } GlobalParameters;
 
 private:
index 7a9531d490c8934df1877560dd37843cbc687f05..54ced338db7accff1c06270ee5448a512e4c901c 100644 (file)
@@ -290,6 +290,7 @@ Standard_Boolean DE_Wrapper::Load(const Handle(DE_ConfigurationContext)& theReso
                                   const Standard_Boolean theIsRecursive)
 {
   GlobalParameters.LengthUnit = theResource->RealVal("general.length.unit", GlobalParameters.LengthUnit, THE_CONFIGURATION_SCOPE());
+  GlobalParameters.SystemUnit = theResource->RealVal("general.system.unit", GlobalParameters.SystemUnit, THE_CONFIGURATION_SCOPE());
   if (theIsRecursive)
   {
     for (DE_ConfigurationFormatMap::Iterator aFormatIter(myConfiguration);
@@ -372,8 +373,10 @@ TCollection_AsciiString DE_Wrapper::Save(const Standard_Boolean theIsRecursive,
     aResult += "\n";
   }
   aResult += "!Global parameters. Used for all providers\n";
-  aResult += "!Length scale unit value. Should be more the 0. Default value: 1.0(MM)\n";
+  aResult += "!Length scale unit value. Should be more than 0. Default value: 1.0(MM)\n";
   aResult += THE_CONFIGURATION_SCOPE() + ".general.length.unit :\t " + GlobalParameters.LengthUnit + "\n";
+  aResult += "!System unit value. Should be more than 0. Default value: 1.0(MM)\n";
+  aResult += THE_CONFIGURATION_SCOPE() + ".general.system.unit :\t " + GlobalParameters.SystemUnit + "\n";
   if (theIsRecursive)
   {
     for (DE_ConfigurationFormatMap::Iterator aFormatIter(myConfiguration);
index 1331011aeb5ba336e82117c6d84275e23863dcf3..eeadfba355e58257e59a7e7bae5dd3b9d7a41ea7 100644 (file)
@@ -140,9 +140,20 @@ bool RWGltf_Provider::Write(const TCollection_AsciiString& thePath,
   Handle(RWGltf_ConfigurationNode) aNode = Handle(RWGltf_ConfigurationNode)::DownCast(GetNode());
 
   RWMesh_CoordinateSystemConverter aConverter;
-  aConverter.SetInputLengthUnit(aNode->GlobalParameters.LengthUnit / 1000);
+  Standard_Real aScaleFactorM = 1.;
+  if (!XCAFDoc_DocumentTool::GetLengthUnit(theDocument, aScaleFactorM))
+  {
+    aConverter.SetInputLengthUnit(aNode->GlobalParameters.SystemUnit / 1000.);
+    Message::SendWarning() << "Warning in the RWGltf_Provider during writing the file " <<
+      thePath << "\t: The document has no information on Units. Using global parameter as initial Unit.";
+  }
   aConverter.SetInputCoordinateSystem(aNode->InternalParameters.SystemCS);
-  aConverter.SetOutputLengthUnit(aNode->InternalParameters.FileLengthUnit);
+  if (aNode->GlobalParameters.LengthUnit != 1000.)
+  {
+    Message::SendWarning() << "Warning in the RWGltf_Provider during writing the file " <<
+      thePath << "\t: Target format doesn't support custom units. Model will be scaled to Meters";
+  }
+  aConverter.SetOutputLengthUnit(1.); // gltf units always Meters
   aConverter.SetOutputCoordinateSystem(aNode->InternalParameters.FileCS);
 
   TColStd_IndexedDataMapOfStringString aFileInfo;
index 969c54d220e114a3284907c359308210f97fe43d..354212e60cbd2d5982d211c9249ffb29e84fde34 100644 (file)
@@ -110,10 +110,6 @@ bool STEPCAFControl_Provider::Write(const TCollection_AsciiString& thePath,
     return false;
   }
   Handle(STEPCAFControl_ConfigurationNode) aNode = Handle(STEPCAFControl_ConfigurationNode)::DownCast(GetNode());
-
-  XCAFDoc_DocumentTool::SetLengthUnit(theDocument, 
-    UnitsMethods::GetLengthUnitScale(aNode->InternalParameters.WriteUnit,
-                                     UnitsMethods_LengthUnit_Millimeter), UnitsMethods_LengthUnit_Millimeter);
   personizeWS(theWS);
   STEPCAFControl_Writer aWriter;
   aWriter.Init(theWS);
@@ -124,9 +120,22 @@ bool STEPCAFControl_Provider::Write(const TCollection_AsciiString& thePath,
   aWriter.SetNameMode(aNode->InternalParameters.WriteName);
   aWriter.SetLayerMode(aNode->InternalParameters.WriteLayer);
   aWriter.SetPropsMode(aNode->InternalParameters.WriteProps);
-
-  TDF_Label aLabel;
   StepData_ConfParameters aParams;
+  Standard_Real aScaleFactorMM = 1.;
+  if (XCAFDoc_DocumentTool::GetLengthUnit(theDocument, aScaleFactorMM, UnitsMethods_LengthUnit_Millimeter))
+  {
+    aModel->SetLocalLengthUnit(aScaleFactorMM);
+  }
+  else
+  {
+    aModel->SetLocalLengthUnit(aNode->GlobalParameters.SystemUnit);
+    Message::SendWarning() << "Warning in the STEPCAFControl_Provider during writing the file " <<
+      thePath << "\t: The document has no information on Units. Using global parameter as initial Unit.";
+  }
+  UnitsMethods_LengthUnit aTargetUnit = UnitsMethods::GetLengthUnitByFactorValue(aNode->GlobalParameters.LengthUnit, UnitsMethods_LengthUnit_Millimeter);
+  aParams.WriteUnit = aTargetUnit;
+  aModel->SetWriteLengthUnit(aNode->GlobalParameters.LengthUnit);
+  TDF_Label aLabel;
   if (!aWriter.Transfer(theDocument, aParams, aMode, 0, theProgress))
   {
     Message::SendFail() << "Error in the STEPCAFControl_Provider during writing the file " <<
@@ -242,9 +251,20 @@ bool STEPCAFControl_Provider::Write(const TCollection_AsciiString& thePath,
   STEPControl_Writer aWriter;
   aWriter.SetWS(theWS);
   IFSelect_ReturnStatus aWritestat = IFSelect_RetVoid;
-  Handle(StepData_StepModel) aModel = aWriter.Model();
-  aModel->SetWriteLengthUnit(UnitsMethods::GetLengthUnitScale(aNode->InternalParameters.WriteUnit, UnitsMethods_LengthUnit_Millimeter));
+  Handle(StepData_StepModel) aModel = aWriter.Model();;
   StepData_ConfParameters aParams;
+  aModel->SetLocalLengthUnit(aNode->GlobalParameters.SystemUnit);
+  UnitsMethods_LengthUnit aTargetUnit = UnitsMethods::GetLengthUnitByFactorValue(aNode->GlobalParameters.LengthUnit, UnitsMethods_LengthUnit_Millimeter);
+  aParams.WriteUnit = aTargetUnit;
+  if (aTargetUnit == UnitsMethods_LengthUnit_Undefined)
+  {
+    aModel->SetWriteLengthUnit(1.0);
+    Message::SendWarning() << "Custom units are not supported by STEP format, but LengthUnit global parameter doesn't fit any predefined unit. Units will be scaled to Millimeters";
+  }
+  else
+  {
+    aModel->SetWriteLengthUnit(aNode->GlobalParameters.LengthUnit);
+  }
   aWritestat = aWriter.Transfer(theShape, aNode->InternalParameters.WriteModelType, aParams, true, theProgress);
   if (aWritestat != IFSelect_RetDone)
   {
index 61f2db99ce62abb699ab06c102eaff9f3c9d7a95..babb2a3cfa181a823472e00109b9b942ca66f820 100644 (file)
@@ -15,6 +15,7 @@ global.priority.XCAF :   OCC
 global.priority.IGES :   OCC
 global.priority.PLY :    OCC
 global.general.length.unit :     1
+global.general.system.unit :     1
 provider.STEP.OCC.read.iges.bspline.continuity :         1
 provider.STEP.OCC.read.precision.mode :  0
 provider.STEP.OCC.read.precision.val :   0.0001
index e760e2d5465757cece03cf484d0a97ffc62eb7f8..3fb307dd904e4b0101bf85581e9f2ab05a3e71e3 100644 (file)
@@ -15,6 +15,7 @@ global.priority.XCAF :   OCC
 global.priority.IGES :   OCC
 global.priority.PLY :    OCC
 global.general.length.unit :     1
+global.general.system.unit :     1
 provider.STEP.OCC.read.iges.bspline.continuity :         1
 provider.STEP.OCC.read.precision.mode :  0
 provider.STEP.OCC.read.precision.val :   0.0001
index ea78ba1bf1b8cddca68670f1b05470f57576fa05..7ea6cfcdab2290f6917200ecc84a1943e9262768 100644 (file)
@@ -20,15 +20,7 @@ if [catch {ReadGltf D0 $filename} catch_result] {
 }
 XGetOneShape S0 D0
 
-param xstep.cascade.unit M
-if [catch {ReadGltf D1 $filename} catch_result] {
-    puts "Error : Problem with reading file"
-} else {
-    puts "OK : Reading is correct"
-}
-XGetOneShape S1 D1
-
-if [catch {WriteFile D0 $write_path -conf "provider.GLTF.OCC.file.length.unit : 0.001 "} catch_result] {
+if [catch {WriteFile D0 $write_path -conf "global.general.length.unit : 1 "} catch_result] {
     puts "Error : Problem with writing file"
 } else {
     puts "OK : Writing is correct"
@@ -50,41 +42,49 @@ if [catch {readfile S3 $write_path} catch_result] {
     puts "OK : Reading is correct"
 }
 
-if [catch {WriteFile D1 $write_path -conf "provider.GLTF.OCC.file.length.unit : 1 "} catch_result] {
+if [catch {WriteFile D0 $write_path -conf "global.general.length.unit : 1 "} catch_result] {
     puts "Error : Problem with writing file"
 } else {
     puts "OK : Writing is correct"
 }
-if [catch {readfile S4 $write_path -conf "global.general.length.unit : 1000 "} catch_result] {
+if [catch {ReadFile D6 $write_path -conf "global.general.length.unit : 1 "} catch_result] {
+    puts "Error : Problem with reading file"
+} else {
+    puts "OK : Reading is correct"
+}
+XGetOneShape S6 D6
+
+param xstep.cascade.unit M
+if [catch {ReadGltf D1 $filename} catch_result] {
     puts "Error : Problem with reading file"
 } else {
     puts "OK : Reading is correct"
 }
+XGetOneShape S1 D1
 
-if [catch {writefile S1 $write_path} catch_result] {
+if [catch {WriteFile D1 $write_path -conf "global.general.length.unit : 1000 "} catch_result] {
     puts "Error : Problem with writing file"
 } else {
     puts "OK : Writing is correct"
 }
-if [catch {readfile S5 $write_path} catch_result] {
+if [catch {readfile S4 $write_path -conf "global.general.length.unit : 1000 "} catch_result] {
     puts "Error : Problem with reading file"
 } else {
     puts "OK : Reading is correct"
 }
 
-if [catch {WriteFile D0 $write_path -conf "provider.GLTF.OCC.file.length.unit : 0.001 "} catch_result] {
+if [catch {writefile S1 $write_path -conf "global.general.system.unit : 1000 "} catch_result] {
     puts "Error : Problem with writing file"
 } else {
     puts "OK : Writing is correct"
 }
-if [catch {ReadFile D6 $write_path -conf "global.general.length.unit : 1 "} catch_result] {
+if [catch {readfile S5 $write_path} catch_result] {
     puts "Error : Problem with reading file"
 } else {
     puts "OK : Reading is correct"
 }
-XGetOneShape S6 D6
 
-if [catch {writefile S1 $write_path -conf "provider.GLTF.OCC.file.length.unit : 1 "} catch_result] {
+if [catch {writefile S1 $write_path -conf "global.general.length.unit : 1000 \n global.general.system.unit : 1000 "} catch_result] {
     puts "Error : Problem with writing file"
 } else {
     puts "OK : Writing is correct"
@@ -96,7 +96,7 @@ if [catch {ReadFile D7 $write_path -conf "global.general.length.unit : 1000 "} c
 }
 XGetOneShape S7 D7
 
-array set areas {0 3.18785e+06 1 3.18785 2 3.18785e+06 3 3.18785e+06 4 3.18785 5 3.18785 6 3.18785e+06 7 3.18785e-06}
+array set areas {0 3.18785e+06 1 3.18785 2 3.18785e+06 3 3.18785e+06 4 3.18785 5 3.18785e+06 6 3.18785e+06 7 3.18785}
 array set results {0 S0 1 S1 2 S2 3 S3 4 S4 5 S5 6 S6 7 S7}
 for { set anind 0} { $anind < 8 } { incr anind } {
   checkprops $results($anind) -s $areas($anind) -eps 1e-2
index e7943453a5ee08faf008b63abff653d0ca51c83e..d74af7fef72832d27b1dc695b07b5d454e116582 100644 (file)
@@ -30,7 +30,7 @@ XGetOneShape S1 D1
 
 param xstep.cascade.unit MM
 
-if [catch {WriteFile D0 $write_path -conf "provider.STEP.write.step.unit : 1 "} catch_result] {
+if [catch {WriteFile D0 $write_path -conf "global.general.length.unit : 25.4 "} catch_result] {
     puts "Error : Problem with writing file"
 } else {
     puts "OK : Writing is correct"
@@ -41,7 +41,7 @@ if [catch {readfile S2 $write_path } catch_result] {
     puts "OK : Reading is correct"
 }
 
-if [catch {writefile S0 $write_path } catch_result] {
+if [catch {writefile S0 $write_path -conf "global.general.system.unit : 1 "} catch_result] {
     puts "Error : Problem with writing file"
 } else {
     puts "OK : Writing is correct"
@@ -52,7 +52,7 @@ if [catch {readfile S3 $write_path} catch_result] {
     puts "OK : Reading is correct"
 }
 
-if [catch {WriteFile D1 $write_path -conf "provider.STEP.write.step.unit : 6 "} catch_result] {
+if [catch {WriteFile D1 $write_path -conf "global.general.length.unit : 1000 "} catch_result] {
     puts "Error : Problem with writing file"
 } else {
     puts "OK : Writing is correct"
@@ -74,7 +74,7 @@ if [catch {readfile S5 $write_path} catch_result] {
     puts "OK : Reading is correct"
 }
 
-if [catch {WriteFile D0 $write_path -conf "provider.STEP.write.step.unit : 2 "} catch_result] {
+if [catch {WriteFile D0 $write_path -conf "global.general.length.unit : 1 "} catch_result] {
     puts "Error : Problem with writing file"
 } else {
     puts "OK : Writing is correct"
@@ -86,7 +86,7 @@ if [catch {ReadFile D6 $write_path -conf "global.general.length.unit : 1 "} catc
 }
 XGetOneShape S6 D6
 
-if [catch {writefile S1 $write_path -conf "provider.STEP.write.step.unit : 1 "} catch_result] {
+if [catch {writefile S1 $write_path -conf "global.general.length.unit : 25.4 "} catch_result] {
     puts "Error : Problem with writing file"
 } else {
     puts "OK : Writing is correct"
@@ -98,7 +98,7 @@ if [catch {ReadFile D7 $write_path -conf "global.general.length.unit : 0.0254"}
 }
 XGetOneShape S7 D7
 
-array set areas {0 2.52381e+06 1 2.52381 2 2.52381e+06 3 2.52381e+06 4 2.52381 5 2.52381 6 2.52381e+06 7 3911.92}
+array set areas {0 2.52381e+06 1 2.52381 2 2.52381e+06 3 2.52381e+06 4 2.52381e+06 5 2.52381 6 2.52381e+06 7 3911.92}
 array set results {0 S0 1 S1 2 S2 3 S3 4 S4 5 S5 6 S6 7 S7}
 for { set anind 0} { $anind < 8 } { incr anind } {
   checkprops $results($anind) -s $areas($anind) -eps 1e-2