Skip to content

Instantly share code, notes, and snippets.

@camnewnham
Created February 8, 2024 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camnewnham/c5a2fa71f703131641a15fd6bef98eef to your computer and use it in GitHub Desktop.
Save camnewnham/c5a2fa71f703131641a15fd6bef98eef to your computer and use it in GitHub Desktop.
Join Groups Into Meshes
using System;
using Rhino;
using Rhino.DocObjects;
using Rhino.Geometry;
using System.Collections.Generic;
Dictionary<int, List<RhinoObject>> map = new Dictionary<int, List<RhinoObject>>();
MeshingParameters meshingParameters = MeshingParameters.Minimal;
meshingParameters.SimplePlanes = true;
meshingParameters.MinimumEdgeLength = 5 * RhinoMath.UnitScale(UnitSystem.Millimeters, RhinoDoc.ActiveDoc.ModelUnitSystem);
meshingParameters.GridAspectRatio = 1000;
meshingParameters.GridMinCount = 1;
foreach (RhinoObject obj in Rhino.RhinoDoc.ActiveDoc.Objects.GetObjectList(new ObjectEnumeratorSettings()
{
DeletedObjects = false,
HiddenObjects = false,
LockedObjects = false,
SelectedObjectsFilter = true
}))
{
if (obj is MeshObject || obj.IsMeshable(Rhino.Geometry.MeshType.Preview))
{
int[] groups = obj.GetGroupList();
if (groups == null || groups.Length == 0) continue;
//int ind = groups[0];
foreach (var ind in groups)
{
if (!map.TryGetValue(ind, out List<RhinoObject> list)) {
list = new List<RhinoObject>();
map.Add(ind, list);
}
list.Add(obj);
}
}
}
Rhino.RhinoDoc.ActiveDoc.Views.RedrawEnabled = false;
try {
foreach (var kvp in map) {
if (kvp.Value.Count <= 1) continue;
var mesh = new Mesh();
var layerIndex = kvp.Value[0].Attributes.LayerIndex;
foreach (var obj in kvp.Value)
{
if (obj.IsDeleted) continue;
if (obj is MeshObject meshObj)
{
mesh.Append(meshObj.MeshGeometry);
if (!meshObj.MeshGeometry.IsValidWithLog(out string slog))
{
}
}
else {
obj.CreateMeshes(MeshType.Preview, meshingParameters, false);
foreach (var m in obj.GetMeshes(MeshType.Preview)) {
mesh.Append(m);
}
}
Rhino.RhinoDoc.ActiveDoc.Objects.Delete(obj, true);
}
mesh.Faces.CullDegenerateFaces();
mesh.Vertices.CullUnused();
Rhino.RhinoDoc.ActiveDoc.Objects.Add(mesh, new ObjectAttributes() {
LayerIndex = layerIndex,
});
}
}
finally {
Rhino.RhinoDoc.ActiveDoc.Views.RedrawEnabled = true;
}
Console.WriteLine("OK");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment