Skip to content

Instantly share code, notes, and snippets.

@danielbierwirth
Created December 16, 2024 10:27
Show Gist options
  • Save danielbierwirth/97856b336cbf07b2af5db01785dc7eb8 to your computer and use it in GitHub Desktop.
Save danielbierwirth/97856b336cbf07b2af5db01785dc7eb8 to your computer and use it in GitHub Desktop.
Simple VisualStudio C# .Net Program that useless the PiXYZ C# SDK to import, optimise, and export the CAD file from local storage.
using System;
using System.IO;
// PiXYZ SDK namespaces
using UnityEngine.Pixyz.Algo;
using UnityEngine.Pixyz.API;
using UnityEngine.Pixyz.Scene;
using UnityEngine.Pixyz.IO;
using UnityEngine.Pixyz.Core;
namespace PixyzDemo
{
class Program
{
static void Main()
{
const string modelFilePath = @"C:\PathToCADFile\PiXYZ-DEMO-BRAKES.CATProduct";
PiXYZAPI api = PiXYZAPI.Initialize();
ConfigureLicenseAndTokens(api);
if (!api.Core.CheckLicense())
{
Console.WriteLine("No License Available");
return;
}
Console.WriteLine("License Available");
int root = ImportModel(api, modelFilePath);
if (root == 0)
{
Console.WriteLine("Failed to import the model.");
return;
}
PrepareModel(api, root);
OptimizeModel(api, root);
ExportModel(api, modelFilePath, "_new.glb", root);
}
static void ConfigureLicenseAndTokens(PiXYZAPI api)
{
if (!api.Core.CheckLicense())
{
api.Core.ConfigureLicenseServer("license server", 27000, true);
Console.WriteLine("Configured license server.");
}
Console.WriteLine("Adding available tokens.");
foreach (var token in api.Core.ListTokens().list)
{
try
{
api.Core.NeedToken(token);
Console.WriteLine($"Token {token} added successfully.");
}
catch (Exception e)
{
Console.WriteLine($"Failed to add token {token}: {e.Message}");
}
}
}
static int ImportModel(PiXYZAPI api, string filepath)
{
if (string.IsNullOrWhiteSpace(filepath) || !File.Exists(filepath))
{
Console.WriteLine("Invalid file path or file does not exist.");
return 0;
}
Console.WriteLine($"Importing {filepath}...");
return (int)api.IO.ImportScene(filepath);
}
static void PrepareModel(PiXYZAPI api, int root)
{
const double tolerance = 0.1;
var occurrences = new OccurrenceList(root);
Console.WriteLine("Repairing CAD...");
api.Algo.RepairCAD(occurrences, tolerance, false);
Console.WriteLine("Repairing Meshes...");
api.Algo.RepairMesh(occurrences, tolerance, true, false);
Console.WriteLine("Tessellating Meshes...");
api.Algo.Tessellate(occurrences, tolerance, -1, -1);
}
static void OptimizeModel(PiXYZAPI api, int root)
{
Console.WriteLine("Before optimization:");
PrintStats(api, root);
var occurrences = new OccurrenceList(root);
Console.WriteLine("Removing Holes...");
api.Algo.RemoveHoles(occurrences, true, false, false, 10, 0);
Console.WriteLine("Deleting Patches...");
api.Algo.DeletePatches(occurrences, true);
Console.WriteLine("Decimating...");
api.Algo.Decimate(occurrences, 1, 0.1, 3, -1, false);
Console.WriteLine("Removing Hidden Geometries...");
api.Algo.RemoveOccludedGeometries(occurrences, SelectionLevel.Polygons, 1024, 16, 90, false, 1);
Console.WriteLine("Optimized:");
PrintStats(api, root);
}
static void PrintStats(PiXYZAPI api, int root)
{
api.Core.ConfigureInterfaceLogger(false, false, false);
var occurrences = new OccurrenceList(root);
int triangles = (int)api.Scene.GetPolygonCount(occurrences, true, false, false);
int vertices = (int)api.Scene.GetVertexCount(occurrences, false, false, false);
int parts = api.Scene.GetPartOccurrences((uint)root).length;
api.Core.ConfigureInterfaceLogger(true, true, true);
Console.WriteLine("Model stats:");
Console.WriteLine($"Triangles: {triangles}");
Console.WriteLine($"Vertices: {vertices}");
Console.WriteLine($"Parts: {parts}");
}
static void ExportModel(PiXYZAPI api, string filepath, string extension, int root)
{
var folderPath = Path.GetDirectoryName(filepath);
var filename = Path.GetFileNameWithoutExtension(filepath);
string finalPath = Path.Combine(folderPath, $"{filename}{extension}");
Console.WriteLine($"Exporting {finalPath}...");
api.IO.ExportScene(finalPath, (uint)root);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment