Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create csharp windows folder watcher sample #15

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,20 @@
*.idea
**/__pycache__/
LoginDetails.txt
.vs/

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/
25 changes: 25 additions & 0 deletions csharp/Windows/FolderWatcher/FolderWatcher/FolderWatcher.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FolderWatcher", "FolderWatcher\FolderWatcher.csproj", "{C6242277-730C-4AC6-B162-503D5D093D1C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C6242277-730C-4AC6-B162-503D5D093D1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C6242277-730C-4AC6-B162-503D5D093D1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C6242277-730C-4AC6-B162-503D5D093D1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C6242277-730C-4AC6-B162-503D5D093D1C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {137C35CA-CBE2-4914-83A2-F306FC8D23B1}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@

using Newtonsoft.Json;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

namespace PiXYZDemo
{
class FolderWatcher
{
private static void WatchFolder (string configFile)
{
var (inputFolder, outputFolder, extensions, optimization, publishToAssetmanager, orgid, projid, collectionPath, tags) = ReadConfig(configFile);

using (FileSystemWatcher watcher = new FileSystemWatcher())
{
watcher.Path = inputFolder;
watcher.Filter = "*.*";
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;

watcher.Created += (sender, e) => OnChanged(e.FullPath, inputFolder, outputFolder, extensions, optimization, publishToAssetmanager, orgid, projid, collectionPath, tags);

watcher.EnableRaisingEvents = true;

Console.WriteLine("\nWaiting for files to process...\n");
Console.ReadLine(); // Keep the application running
}

}

private static void OnChanged(string filePath, string inputFolder, string outputFolder, string[] extensions, bool optimization, string publishToAssetmanager, string orgid, string projid, string collectionPath, string[] tags)
{
string fileExtension = GetFileExtension(filePath);

if (extensions.Contains(fileExtension))
{
Console.WriteLine($"Processing file: {filePath}");

// Add your processing logic here
ExecutePixyzSDK(filePath, outputFolder, extensions, optimization, publishToAssetmanager, orgid, projid, collectionPath, tags);

// Remove the file after processing if needed:
File.Delete(filePath);
Console.WriteLine($"File deleted: {filePath}");
}
}

private static (string, string, string[], bool, string, string, string, string, string[]) ReadConfig(string configFile)
{
var configContent = File.ReadAllText("../../../"+configFile);
dynamic inputs = JsonConvert.DeserializeObject(configContent);

string inputFolder = Path.GetFullPath((string)inputs.input_folder);
string outputFolder = Path.GetFullPath((string)inputs.output_folder);
string[] extensions = ((IEnumerable<object>)inputs.extensions).Select(x => x.ToString()).ToArray();
bool optimization = (bool)inputs.optimization;
string publishToAssetmanager = (string)inputs.publish_to_assetmanager;
string orgid = (string)inputs.orgid;
string projid = (string)inputs.projid;
string collectionPath = (string)inputs.collectionpath;
string[] tags = inputs.tags.ToObject<string[]>();

Console.WriteLine("\n");
Console.WriteLine($"Input folder: {inputFolder}\n");
Console.WriteLine($"Output folder: {outputFolder}\n");
Console.WriteLine($"Extensions: {string.Join(" ", extensions)}\n");
Console.WriteLine($"Publish to Asset Manager: {publishToAssetmanager}\n");
Console.WriteLine($"Organisation ID: {orgid}\n");
Console.WriteLine($"Project ID: {projid}\n");
Console.WriteLine($"Collection Path: {collectionPath}\n");
Console.WriteLine($"Asset Tags: {string.Join(" ", tags)}\n");

return (inputFolder, outputFolder, extensions, optimization, publishToAssetmanager, orgid, projid, collectionPath, tags);
}

private static void ExecutePixyzSDK(string inputFile, string outputFolder, string[] extensions, bool optimization, string publishToAssetmanager, string orgid, string projid, string collectionPath, string[] tags)
{
PixyzInit.GetPixyzLicense();

if (PixyzInit.api.Core.CheckLicense())
{
ImportOptimiseExport.ConvertFile(inputFile, outputFolder, extensions.ToList(), optimization, orgid, projid, collectionPath, tags.ToList());
}
else
{
Console.WriteLine("No License Available");
}

}

private static string GetFileExtension(string file)
{
return Path.GetExtension(file);
}

static void PrintLogo()
{
Console.WriteLine("");
Console.WriteLine(" ####### %###### ");
Console.WriteLine(" ############## &#############& ");
Console.WriteLine("###### ###### ###### &#####");
Console.WriteLine("#### &########### ####");
Console.WriteLine("####% ###### ####");
Console.WriteLine(" #####& &###### #### #####%");
Console.WriteLine(" ###### ###### ###### &###### ");
Console.WriteLine(" ######& ### &########## ");
Console.WriteLine(" ####### ###### ");
Console.WriteLine(" &###########& #### &###### ");
Console.WriteLine(" ###### ###### &###### ######& ");
Console.WriteLine(" ##### ##% ###### &####%");
Console.WriteLine("####% %####### ####");
Console.WriteLine("####% ############& ####");
Console.WriteLine(" #####& %#####% ######% #####%");
Console.WriteLine(" ############ ############# ");
Console.WriteLine(" ####### %###### ");
}
static void Main(string[] args)
{
PixyzInit.InitPixyz();
PrintLogo();
WatchFolder("config.json");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<StartupObject>PiXYZDemo.FolderWatcher</StartupObject>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\..\..\ImportOptimiseExport\ImportOptimiseExport\ImportOptimiseExport.cs" Link="ImportOptimiseExport.cs" />
<Compile Include="..\..\..\shared\shared_utils\MathUtils.cs" Link="MathUtils.cs" />
<Compile Include="..\..\..\shared\shared_utils\PixyzInit.cs" Link="PixyzInit.cs" />
<Compile Include="..\..\..\shared\shared_utils\PixyzUtils.cs" Link="PixyzUtils.cs" />
</ItemGroup>

<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Runtime.Loader.UseRidGraph" Value="true" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="PiXYZCSharpAPI" Version="2024.3.0.8-win64" />

</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"input_folder": "_input/",
"output_folder": "_output/",
"extensions":
[
".pxz",
".fbx",
".glb",
".obj",
".3dxml"
],
"optimization": "True",
"publish_to_assetmanager": "False",
"orgid": "ORG_ID",
"projid": "PROJ_ID",
"collectionpath": "COLLECTION_PATH",
"tags": ["TAG"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace PiXYZDemo
{
using UnityEngine.Pixyz.Algo;
using UnityEngine.Pixyz.Scene;
using UnityEngine.Pixyz.IO;

using static System.Formats.Asn1.AsnWriter;
using System.Xml.Linq;
using UnityEngine.Pixyz.API;

class ImportOptimiseExport
{
static void OptimiseModel(uint root, string fileName)
{
var stats = PixyzUtils.GetStats(root);
var t0 = stats.Item1;
var n_triangles = stats.Item2;
var n_vertices = stats.Item3;
var n_parts = stats.Item4;

var occurrences = new OccurrenceList((int)root);

Console.WriteLine("Removing Holes...");
double diameter = 10;
PixyzInit.api.Algo.RemoveHoles(occurrences, true, false, false, diameter, 0);

Console.WriteLine("Delete Patches...");
PixyzInit.api.Algo.DeletePatches(occurrences, true);

Console.WriteLine("Decimating...");
PixyzInit.api.Algo.Decimate(occurrences, 1, 0.1, 3, -1, false);

Console.WriteLine("Removing Hidden Geometries...");
PixyzInit.api.Algo.RemoveOccludedGeometries(occurrences, SelectionLevel.Polygons, 1024, 16, 90, false, 1);

var newStats = PixyzUtils.GetStats(root);
var t1 = newStats.Item1;
PixyzUtils.PrintStats(fileName, t1 - t0, n_triangles, newStats.Item2, n_vertices, newStats.Item3, n_parts, newStats.Item4);
}

public static void ConvertFile(string inputFile, string outputFolder, List<string> extensions, bool optimization, string orgId, string projId, string collectionPath, List<string> tags)
{
var modelFiles = new List<string>();
var fileName = Path.GetFileNameWithoutExtension(inputFile);

PixyzInit.api.Core.SetLogFile(Path.Combine(outputFolder, fileName + ".log"));

var root = ImportModel(inputFile);

PrepareModel(root);

if (optimization)
{
OptimiseModel(root, fileName);
}

foreach (var extension in extensions)
{
Console.WriteLine($"Exporting {extension}...");
PixyzInit.api.IO.ExportScene(Path.Combine(outputFolder, fileName + extension));
modelFiles.Add(Path.Combine(outputFolder, fileName + extension));
}
}

static uint ImportModel(string filepath)
{
Console.WriteLine($"Importing {filepath}...");
return PixyzInit.api.IO.ImportScene(filepath);
}

static void PrepareModel(uint root)
{
var occurrences = new OccurrenceList((int)root);

Console.WriteLine("Calculating Tolerances... ");
double tolerance = Math.Min(MathUtils.AabbDiagLength(PixyzInit.api.Scene.GetAABB(occurrences)) / 1000, 0.1);

Console.WriteLine("Repairing CAD... ");
PixyzInit.api.Algo.RepairCAD(occurrences, tolerance, false);

Console.WriteLine("Repairing Meshes... ");
PixyzInit.api.Algo.RepairMesh(occurrences, tolerance, true, false);

Console.WriteLine("Tessellating Meshes... ");
PixyzInit.api.Algo.Tessellate(occurrences, tolerance, -1, -1);
}

static void ExportModel(string filepath, string extension, uint root)
{
var finalPath = Path.Combine(Path.GetDirectoryName(filepath), Path.GetFileNameWithoutExtension(filepath) + extension);
Console.WriteLine($"Exporting {finalPath}...");
PixyzInit.api.IO.ExportScene(finalPath, root);
}

static void Main(string[] args)
{

string modelFilePath = "../../../../../../../python/Windows/shared/shared_models/SkidLoaderSolidworks/Skid Loader ASSM 11-4-21.SLDASM";

PixyzInit.InitPixyz();
PixyzInit.GetPixyzLicense();

if (PixyzInit.api.Core.CheckLicense())
{
var root = ImportModel(modelFilePath);
PixyzUtils.SaveScreenshot((int)root, Path.Combine(Path.GetDirectoryName(modelFilePath), "before.png"), Orientation.LEFT);
PrepareModel(root);
PixyzUtils.SaveScreenshot((int)root, Path.Combine(Path.GetDirectoryName(modelFilePath), "after.png"), Orientation.FRONT);
PixyzUtils.ExtractHierarchyToJson(root, Path.Combine(Path.GetDirectoryName(modelFilePath), $"{Path.GetFileNameWithoutExtension(modelFilePath)}.json"));
ExportModel(modelFilePath, "_new.glb", root);
}
else
{
Console.WriteLine("No License Available");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\..\shared\shared_utils\MathUtils.cs" Link="MathUtils.cs" />
<Compile Include="..\..\shared\shared_utils\PixyzInit.cs" Link="PixyzInit.cs" />
<Compile Include="..\..\shared\shared_utils\PixyzUtils.cs" Link="PixyzUtils.cs" />
</ItemGroup>

<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Runtime.Loader.UseRidGraph" Value="true" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="PiXYZCSharpAPI" Version="2024.3.0.8-win64" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImportOptimiseExport", "ImportOptimiseExport.csproj", "{17025DAA-A491-4612-B9FE-EC84A85436F1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{17025DAA-A491-4612-B9FE-EC84A85436F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17025DAA-A491-4612-B9FE-EC84A85436F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17025DAA-A491-4612-B9FE-EC84A85436F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17025DAA-A491-4612-B9FE-EC84A85436F1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {89105A4A-78D1-4B36-B961-639B2216DFBD}
EndGlobalSection
EndGlobal
Loading