From 44e4abde0e18e71d7cda0b21906c65beaca7e533 Mon Sep 17 00:00:00 2001
From: Wayne Stambaugh <stambaughw@gmail.com>
Date: Wed, 12 Jan 2022 18:55:18 -0500
Subject: [PATCH] Fix X3D model parser.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/10385
---
 plugins/3d/vrml/x3d/x3d_appearance.cpp | 13 +++++++++----
 plugins/3d/vrml/x3d/x3d_ifaceset.cpp   | 14 +++++++++-----
 plugins/3d/vrml/x3d/x3d_shape.cpp      | 13 +++++++++----
 plugins/3d/vrml/x3d/x3d_transform.cpp  | 14 ++++++++++----
 4 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/plugins/3d/vrml/x3d/x3d_appearance.cpp b/plugins/3d/vrml/x3d/x3d_appearance.cpp
index 1f47afa326..7cdf1074e5 100644
--- a/plugins/3d/vrml/x3d/x3d_appearance.cpp
+++ b/plugins/3d/vrml/x3d/x3d_appearance.cpp
@@ -2,7 +2,7 @@
  * This program source code file is part of KiCad, a free EDA CAD application.
  *
  * Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
- * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
+ * Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -232,9 +232,14 @@ SGNODE* X3DAPP::TranslateToSG( SGNODE* aParent )
 {
     S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
 
-    wxCHECK_MSG( aParent && ( ptype == S3D::SGTYPE_SHAPE ), nullptr,
-                 wxString::Format( wxT( "Appearance does not have a Shape parent (parent ID: %d)" ),
-                                   ptype ) );
+    if( nullptr != aParent && ptype != S3D::SGTYPE_SHAPE )
+    {
+        wxLogTrace( traceVrmlPlugin,
+                    wxT( " * [BUG] Appearance does not have a Shape parent (parent ID: %d)" ),
+                    ptype );
+
+        return nullptr;
+    }
 
     wxLogTrace( traceVrmlPlugin,
                 wxT( " * [INFO] Translating Appearance node with %zu children, %zu"
diff --git a/plugins/3d/vrml/x3d/x3d_ifaceset.cpp b/plugins/3d/vrml/x3d/x3d_ifaceset.cpp
index f16be96e91..d2ee0ae164 100644
--- a/plugins/3d/vrml/x3d/x3d_ifaceset.cpp
+++ b/plugins/3d/vrml/x3d/x3d_ifaceset.cpp
@@ -2,7 +2,7 @@
  * This program source code file is part of KiCad, a free EDA CAD application.
  *
  * Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
- * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
+ * Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -227,10 +227,14 @@ SGNODE* X3DIFACESET::TranslateToSG( SGNODE* aParent )
 {
     S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
 
-    wxCHECK_MSG( aParent && ( ptype == S3D::SGTYPE_SHAPE ), nullptr,
-                 wxString::Format( wxT( "IndexedFaceSet does not have a valid Shape parent "
-                                        "(parent ID: %d)" ),
-                                   ptype ) );
+    if( nullptr != aParent && ptype != S3D::SGTYPE_SHAPE )
+    {
+        wxLogTrace( traceVrmlPlugin,
+                    wxT( " * [BUG] IndexedFaceSet does not have a valid Shape parent "
+                         "(parent ID: %d)" ), ptype );
+
+        return nullptr;
+    }
 
     wxLogTrace( traceVrmlPlugin,
                 wxT( " * [INFO] Translating IndexedFaceSet with %zu children, %zu references, "
diff --git a/plugins/3d/vrml/x3d/x3d_shape.cpp b/plugins/3d/vrml/x3d/x3d_shape.cpp
index 8a189f92b8..3fe93476bf 100644
--- a/plugins/3d/vrml/x3d/x3d_shape.cpp
+++ b/plugins/3d/vrml/x3d/x3d_shape.cpp
@@ -2,7 +2,7 @@
  * This program source code file is part of KiCad, a free EDA CAD application.
  *
  * Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
- * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
+ * Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -252,9 +252,14 @@ SGNODE* X3DSHAPE::TranslateToSG( SGNODE* aParent )
 
     S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
 
-    wxCHECK_MSG( aParent && ( ptype == S3D::SGTYPE_TRANSFORM ), nullptr,
-                 wxString::Format( wxT( "Shape does not have a Transform parent (parent ID: %d)" ),
-                                   ptype ) );
+    if( nullptr != aParent && ptype != S3D::SGTYPE_TRANSFORM )
+    {
+        wxLogTrace( traceVrmlPlugin,
+                    wxT( " * [BUG] Shape does not have a Transform parent (parent ID: %d)" ),
+                    ptype );
+
+        return nullptr;
+    }
 
     if( m_sgNode )
     {
diff --git a/plugins/3d/vrml/x3d/x3d_transform.cpp b/plugins/3d/vrml/x3d/x3d_transform.cpp
index 7eb45e0c2e..97c56ae1de 100644
--- a/plugins/3d/vrml/x3d/x3d_transform.cpp
+++ b/plugins/3d/vrml/x3d/x3d_transform.cpp
@@ -2,7 +2,7 @@
  * This program source code file is part of KiCad, a free EDA CAD application.
  *
  * Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
- * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
+ * Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -38,6 +38,7 @@ X3DTRANSFORM::X3DTRANSFORM() : X3DNODE()
     init();
 }
 
+
 X3DTRANSFORM::X3DTRANSFORM( X3DNODE* aParent ) : X3DNODE()
 {
     m_Type = X3D_TRANSFORM;
@@ -267,9 +268,14 @@ SGNODE* X3DTRANSFORM::TranslateToSG( SGNODE* aParent )
 
     S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
 
-    wxCHECK_MSG( aParent && ( ptype == S3D::SGTYPE_TRANSFORM ), nullptr,
-                 wxString::Format( wxT( "Transform does not have a Transform parent "
-                                        "(parent ID: %d)" ), ptype ) );
+    if( nullptr != aParent && ptype != S3D::SGTYPE_TRANSFORM )
+    {
+        wxLogTrace( traceVrmlPlugin,
+                    wxT( " * [BUG] Transform does not have a Transform parent (parent ID: %d)" ),
+                    ptype );
+
+        return nullptr;
+    }
 
     if( m_sgNode )
     {