From a1efa3f5e9d48a9d2289c40680e3a48ceb90521d Mon Sep 17 00:00:00 2001 From: babayaga Date: Tue, 25 Feb 2025 22:45:47 +0100 Subject: [PATCH] research - solidworks 2 glb :) --- packages/cad/docs/glb.md | 355 +++++++++++++++++++++++++++ packages/cad/scripts/todos.md | 5 + packages/cad/scripts/todos.sh | 0 packages/cad/scripts/update-todos.sh | 10 + 4 files changed, 370 insertions(+) create mode 100644 packages/cad/docs/glb.md create mode 100644 packages/cad/scripts/todos.md create mode 100644 packages/cad/scripts/todos.sh create mode 100644 packages/cad/scripts/update-todos.sh diff --git a/packages/cad/docs/glb.md b/packages/cad/docs/glb.md new file mode 100644 index 00000000..b798e36d --- /dev/null +++ b/packages/cad/docs/glb.md @@ -0,0 +1,355 @@ +# Converting SolidWorks Files to GLTF/GLB Format + +This documentation provides approaches for converting SolidWorks files to gLTF/GLB format using the SolidWorks Interop API and C#. + +## Overview + +gLTF (GL Transmission Format) and its binary variant GLB are file formats for 3D models widely used in web applications and AR/VR. Converting SolidWorks files to these formats allows for broader compatibility and usage. + +## Approaches for Conversion + +Here are several approaches to convert SolidWorks files to gLTF/GLB using the Interop API: + +### Approach 1: SolidWorks to Intermediate Format to gLTF + +This approach uses SolidWorks API to export to an intermediate format (e.g., STL, STEP, or OBJ) that can then be converted to gLTF/GLB using a third-party library. + +```csharp +using SolidWorks.Interop.sldworks; +using SolidWorks.Interop.swconstants; +using System.Runtime.InteropServices.Com; +using System; + +public class SolidWorksToGLTFConverter +{ + private SldWorks swApp; + private ModelDoc swModel; + + public SolidWorksToGLTFConverter() + { + // Connect to SolidWorks (must be installed) + swApp = (SldWorks) Marshal.GetActiveObject("SldWorks.Application"); + // If SolidWorks is not running, start it + if (swApp == null) + { + swApp = (SldWorks) Marshal.BindToMonoNamed("SldWorks.Application", "SldWorks.Application"); + swApp.Visible = true; + swApp.UserControl = false; + } + } + + public void ConvertToGLTF(string sourcePath, string destinationPath) + { + try + { + // Open the SolidWorks file + int errors = 0; + int warnings = 0; + swModel = swApp.OpenDoc(sourcePath, (int)swOpenDocOptions_e.SwOpenDocOption_Silent, (int)swDocType_e.swDocPART, ref errors, ref warnings); + + if (swModel == null) + { + CheckLastError(); + throw new Exception("Failed to open the SolidWorks file."); + } + + // Generate an intermediate file (STL) + string intermediatePath = System.IO.Path.ChangeExtension(destinationPath, ".stl"); + int status = swModel.Extension.SaveAs(intermediatePath, (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOption_e.swSaveAsOption_Copy, null, ref errors, ref warnings); + + if (status != 1) + { + throw new Exception("Failed to save the STL file."); + } + + // Close the document + swApp.CloseDoc(sourcePath); + + // Now use a library like AssimpNet to convert from STL to gLTF + // This is an example using AssimpNet (requires NuGet package) + ConvertSTLToGLTF(intermediatePath, destinationPath); + + // Clean up the intermediate file + System.IO.File.Delete(intermediatePath); + } + catch (Exception ex) + { + Console.WriteLine("Error: " + ex.Message); + throw; + } + } + + private void ConvertSTLToGLTF(string stlPath, string gltfPath) + { + // Using AssimpNet library (install via NuGet: AssimpNet) + /* + // Install-Package AssimpNet + using Assimp; + using Assimp.Gltf; + + // Create a new importer and scene + var importer = new Assimp.Assimp(); + var scene = importer.ImportFile(stlPath); + + if (scene == null || scene.SceneFlags == PostProcessSteps.TargetRealTime_Max) + { + throw new Exception("Failed to load STL file: " + Assimp.GetErrorString()); + } + + // Export to gLTF +/GLB + var exportFormat = new Gltf2ExportFormat(); + var exporter = new Assimp.Exporter(); + var gltfPathExtension = System.IO.Path.GetExtension(gltfPath).ToLower(); + + if (gltfPathExtension == ".glb") + { + exportFormat.PrettyPrinted = false; // Binary format + } + else + { + exportFormat.PrettyPrinted = true; // JSON format + } + + exporter.Export(scene, ExportFormat.Gltf2, gltfPath, exportFormat); + */ + + // NOTE: This is just a placeholder for the AssimpNet code + // You need to add the proper AssimpNet NuGet package and uncomment the code above + Console.WriteLine("Converting stl to gLTF/GLB..."); + } + + private void CheckLastError() + { + int errorCode = 0; + string errorMessage = swApp.GetLastError(ref errorCode); + if (errorCode != 0) + { + Console.WriteLine("SolidWorks Error: (" + errorCode + ") " + errorMessage); + } + } + + public void Dispose() + { + // Clean up SolidWorks instance + if (swApp != null) + { + swApp.Exit(); + Marshal.ReleaseComObject(swApp); + } + } +} +``` + +### Approach 2: Direct Export using SolidWorks GTF Addin + +If you have a SolidWorks addin that can export to gLTF/GLB, you can use the Interop API to automate the process:; + +```csharp +public bool ExportToGLTFUsingAddin(string sourcePath, string destinationPath) +{ + try + { + // Connect to SolidWorks + SldWorks swApp = (SldWorks) Marshal.GetActiveObject("SldWorks.Application"); + if (swApp == null) + { + swApp = (SldWorks) Marshal.BindToMonoNamed("SldWorks.Application", "SldWorks.Application"); + swApp.Visible = true; + } + + // Open the SolidWorks file + int errors = 0; + int warnings = 0; + ModelDoc swModel = swApp.OpenDoc(sourcePath, (int)swOpenDocOptions_e.SwOpenDocOption_Silent, (int)swDocType_e.swDocPART, ref errors, ref warnings); + + if (swModel == null) + { + // Handle error + return false; + } + + // Get the GLTF Export addin (assuming it's installed) + object gltfAddin = null; + try { + // This is a placeholder - actual addin name will vary based on what's installed + gltfAddin = swApp.GetAddinObject("GLTFExporter.Addin"); + } + catch (Exception exp) { + Console.WriteLine("Error loading GLTF export addin: " + exp.Message); + return false; + } + + if (gltfAddin != null) { + // Call the addin's export method (the exact method name and parameters will depend on the addin) + // Example: + // gltfAddin.ExportGltf(swModel, destinationPath); + + // Close the model + swApp.CloseDoc(sourcePath); + return true; + } + else { + Console.WriteLine("GLTF export addin not found or not properly loaded."); + return false; + } + } + catch (Exception e) + { + Console.WriteLine("Error: " + e.Message); + return false; + } +} +``` + +### Approach 3: Using SolidWorks eCommerce Technologies| Com�View API + +If you have access to SolidWorks eCommerce Technologies' CompView, you can use its API to generate gLTF files. + +```csharp +using EdsCompViewLib; +using System; + +public class CompViewGLTFExporter +{ + public void ExportToGLTF(string sourcePath, string destinationPath) + { + try + { + // Initialize CompView (exact code will depend on version) + EdsCompViewLib.EdsCompView compView = new EdsCompViewLib.EdsCompView(); + compView.Initialize(); + + // Optional: Set license information if required + // compView.License("license_key"); + + // Open the SolidWorks file + var openStatus = compView.OpenDocument(sourcePath); + if (!openStatus) + { + throw new Exception("Failed to open document in CompView."); + } + + // Configure gLTF export settings + var settings = new GltfExportSettings(); + settings.EmbedTextures = true; + settings.IncludeMaterials = true; + settings.UseBinaryFormat = System.IO.Path.GetExtension(destinationPath).ToLower() == ".glb"; + + // Export the gLTF file + bool exportSuccess = compView.ExportGLTF(destinationPath, settings); + if (!exportSuccess) + { + throw new Exception("Failed to export gLTF/GLB."); + } + + // Close the document and cleanup CompView + compView.CloseDocument(); + compView.Uninitialize(); + } + catch (Exception ex) + { + Console.WriteLine("Error while exporting to gLTF: " + ex.Message); + throw; + } + } +} + +// Placeholder class for example purposes +public class GltfExportSettings +{ + public bool EmbedTextures { get; set; } + public bool IncludeMaterials { get; set; } + public bool UseBinaryFormat { get; set; } +} +``` + +### Approach 4: Using SolidWorks DocumentManager API + +If you have SolidWorks PDD or Enterprise PDM/PLC, you can use the DocumentManager API: + +```csharp +using EDPLibrary7; // Enterprise PDM API +using System; + +public class PDMGLTFExporter +{ + public bool ExportToGLTF(string fileId, string destinationPath) + { + try + { + // Connect to EDSW + IEdmVault7 vault = new EdmVault7(); + if (!vault.IsLoggedIn) + { + var loggedIn = vault.Login("username", "password", "vault_name"); + if (!loggedIn) + { + Console.WriteLine("Failed to log in to PDM."); + return false; + } + } + + // Get the file from PDM + IEdmFile7 edmFile = vault.GetFileById(fileId); + if (edmFile == null) + { + Console.WriteLine("File not found in PDM."); + return false; + } + + // Get local copy of the file + string localPath = edmFile.Get(); + + // Now use either Approach 1 or 2 above to convert the local file to gLTF/GLB + // Example using the first approach: + using (var converter = new SolidWorksToGLTFConverter()) + { + converter.ConvertToGLTF(localPath, destinationPath); + } + + // Cleanup local file if needed + edmFile.UndoGet(); // Optional: releases local copy + + return true; + } + catch (Exception ex) + { + Console.WriteLine("Error: " + ex.Message); + return false; + } + } +} +``` + +## Tips and Best Practices + +1. **Test With Simple Models First**: Start with simple models before processing complex assemblies. + +2. **Handle Materials Carefully**: gLTF supports PBR (Physically-Based Rendering) materials which may need special handling when converting from SolidWorks. + +3. **Consider Textures and Specification Compliance**: Ensure textures are handled correctly and the output is compliant with the gLTF specification. + +4. **Use Validation Tools**: Validate the generated gLTF/GLB files using tools like [gltf-validator](https://github.com/KhronosGroup/gltf-validator) or [Babylon.js Sandbox](https://sandbox.babylonjs.com/). + +5. **Handle Large Assemblies with Care**: Very large assemblies might need to be broken down or optimized for web viewing. + +## Third-Party Libraries and Tools + +1. [AssimpNet](https://github.com/assimpnet/Assimp) - C# bindings for Assimp, a powerful 3D model import/export library that supports gLTF/GLB export. + +2. [SharpGLTF](https://github.com/vk.com/sharpgltf) - A C# glTF 2.0 io library to create gLTF files from scratch. + +3. [Open3D Model Viewer](https://github.com/microsoft/Open3D.ModelViewer) - Microsoft's Open3D Model Viewer supports gLTF/GLB and includes conversion utilities. + +4. [ComView](https://www.edsplm.com/products/compview) - An engineering file viewing solution with API for converting files to various formats including gLTF. + +## Conclusion + +There are several approaches to convert SolidWorks files to gLTF/GLB format using the SolidWorks Interop API and C#. The best approach depends on your specific requirements, available tools, and the complexity of your models. Whether you use an intermediate format, a direct export via an addin, or a third-party tool, make sure to validate the output gLTF/GLB files and optimize them for your intended use case. + +``` + +## Prompt + +suggest ways to convert solidwork files via interop API into .gltf or .glb, via SDK (#c), store in ./docs/glb.md, with code examples diff --git a/packages/cad/scripts/todos.md b/packages/cad/scripts/todos.md new file mode 100644 index 00000000..21cad9bf --- /dev/null +++ b/packages/cad/scripts/todos.md @@ -0,0 +1,5 @@ +## Todos + +skip marked todos + +- [ ] suggest ways to convert solidwork files via interop API into .gltf or .glb, via SDK (#c), store in ./docs/glb.md, with code examples diff --git a/packages/cad/scripts/todos.sh b/packages/cad/scripts/todos.sh new file mode 100644 index 00000000..e69de29b diff --git a/packages/cad/scripts/update-todos.sh b/packages/cad/scripts/update-todos.sh new file mode 100644 index 00000000..e5fd439b --- /dev/null +++ b/packages/cad/scripts/update-todos.sh @@ -0,0 +1,10 @@ +kbotd modify \ + --prompt="./scripts/todos.md" \ + --router2=openai \ + --model=anthropic/claude-3.7-sonnet:thinking \ + --include2="src/commands/run.ts" \ + --include2="src/zod_schema.ts" \ + --include2="src/client.ts" \ + --disable="npm,terminal,git,user,search,email,web,interact" \ + --disableTools="file_exists,list_files,read_files" \ + --dst="./.kbot/todos-log.md"