Created
October 6, 2026 22:01
-
-
Save StartAutomating/acc16242d3c402bceb598232928a5e73 to your computer and use it in GitHub Desktop.
Gist make me a cube
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| param( | |
| [double] | |
| $Width = 10, | |
| [double] | |
| $Height = 10, | |
| [double] | |
| $Depth = 3, | |
| [string] | |
| $Color = '#4488ff', | |
| [Numerics.Matrix4x4[]] | |
| $Transform, | |
| [ValidateSet( | |
| 'micron','millimeter', 'centimeter', 'inch', 'foot', 'meter' | |
| )] | |
| [string] | |
| $Unit = 'millimeter', | |
| [cultureinfo] | |
| $Culture = [CultureInfo]::CurrentUICulture, | |
| [string] | |
| $Path | |
| ) | |
| $vertices = @( | |
| [Numerics.Vector3]::new(0) | |
| [Numerics.Vector3]::new($width, 0,0 ) | |
| [Numerics.Vector3]::new($width, $height,0 ) | |
| [Numerics.Vector3]::new(0, $height,0 ) | |
| [Numerics.Vector3]::new(0, 0, $Depth ) | |
| [Numerics.Vector3]::new($width, 0,$Depth ) | |
| [Numerics.Vector3]::new($width, $height, $Depth ) | |
| [Numerics.Vector3]::new(0, $height, $Depth ) | |
| ) | |
| $model = [xml]@" | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <model unit="$($Unit.ToLower())" xml:lang="$Culture" | |
| xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02" | |
| xmlns:m="http://schemas.microsoft.com/3dmanufacturing/material/2015/02" | |
| xmlns:p="http://schemas.microsoft.com/3dmanufacturing/production/2015/06" | |
| xmlns:b="http://schemas.microsoft.com/3dmanufacturing/beamlattice/2017/02" | |
| xmlns:s="http://schemas.microsoft.com/3dmanufacturing/slice/2015/07"> | |
| <resources> | |
| <basematerials id="1"> | |
| <!-- Color in #RRGGBBAA format --> | |
| <base name="SomeMaterial" displaycolor="#4488ffff"/> | |
| </basematerials> | |
| <object id="2" name="" type="model" p:UUID="$([Guid]::NewGuid())"> | |
| <mesh> | |
| <!-- 8 vertices of a cube --> | |
| <vertices>$( | |
| foreach ($vertex in $Vertices) { | |
| if ($Transform) { | |
| foreach ($transformation in $transform) { | |
| $vertex = $vertex::Transform($vertex, $transformation) | |
| } | |
| } | |
| "<vertex x='$($vertex.X)' y='$($vertex.Y)' z='$($vertex.Z)' />" | |
| } | |
| )</vertices> | |
| <!-- Triangles: all referencing material ID=0 in basematerials --> | |
| <triangles> | |
| <triangle v1="0" v2="1" v3="2" pid="1" p1="0"/> | |
| <triangle v1="0" v2="2" v3="3" pid="1" p1="0"/> | |
| <triangle v1="4" v2="5" v3="6" pid="1" p1="0"/> | |
| <triangle v1="4" v2="6" v3="7" pid="1" p1="0"/> | |
| <triangle v1="0" v2="1" v3="5" pid="1" p1="0"/> | |
| <triangle v1="0" v2="5" v3="4" pid="1" p1="0"/> | |
| <triangle v1="1" v2="2" v3="6" pid="1" p1="0"/> | |
| <triangle v1="1" v2="6" v3="5" pid="1" p1="0"/> | |
| <triangle v1="2" v2="3" v3="7" pid="1" p1="0"/> | |
| <triangle v1="2" v2="7" v3="6" pid="1" p1="0"/> | |
| <triangle v1="3" v2="0" v3="4" pid="1" p1="0"/> | |
| <triangle v1="3" v2="4" v3="7" pid="1" p1="0"/> | |
| </triangles> | |
| </mesh> | |
| </object> | |
| </resources> | |
| <build p:UUID="$([Guid]::NewGuid())"> | |
| <item objectid="2" p:UUID="$([Guid]::NewGuid())" /> | |
| </build> | |
| </model> | |
| "@ | |
| $memoryStream = [IO.MemoryStream]::new() | |
| $3mf = | |
| [IO.Packaging.Package]::Open($memoryStream, 'OpenOrCreate') | |
| $3mf | | |
| Add-Member NoteProperty MemoryStream $memoryStream -Force | |
| $3dModelPart = $3mf.CreatePart("/3D/3dmodel.model","application/vnd.ms-package.3dmanufacturing-3dmodel+xml") | |
| $partStream = $3dModelPart.GetStream() | |
| $model.Save($partStream) | |
| $partStream.Close() | |
| $partStream.Dispose() | |
| if (-not $3mf.RelationshipExists('rel0')) { | |
| $null = | |
| $3mf.CreateRelationship('/3D/3dmodel.model','Internal', 'http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel', 'rel0') | |
| } | |
| $3mf.Close() | |
| if ($Path) { | |
| $newFile = New-Item -ItemType File -Path $path -Force | |
| if ($newFile) { | |
| switch ($newFile.Extension) { | |
| 3mf { | |
| [IO.File]::WriteAllBytes($newFile.FullName, $3mf.memoryStream.ToArray()) | |
| return [IO.FileInfo]$newFile.FullName | |
| } | |
| model { | |
| $model.Save($newFile.FullName) | |
| return [IO.FileInfo]$newFile.FullName | |
| } | |
| xml { | |
| $model.Save($newFile.FullName) | |
| return [IO.FileInfo]$newFile.FullName | |
| } | |
| } | |
| } | |
| } | |
| return [uri]"data:model/3mf;base64,$([Convert]::ToBase64String($3mf.MemoryStream.ToArray()))" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment