Hello
I create a plugin to send meshes from Rhino/Grasshopper to Ansys discovery live
I follow the documentation of Ansys but i don't find anything about update or replace existing mesh
I use this way:
1. Hide oldmesh.
2. Create new mesh.
3. Delete the old one.
but i prefer a better solution if exist
public static void ReplaceMeshes(List<Rhino.Geometry.Mesh> meshes, string name)
{
Solver.Pause();
WriteBlock.ExecuteTask("Replace Mesh", () => ReplaceMeshSmooth(meshes, name));
Solver.Start();
}
private static void ReplaceMeshSmooth(List<Rhino.Geometry.Mesh> meshes, string baseName)
{
var window = Window.ActiveWindow;
var mainPart = window.Document.MainPart;
// Get existing meshes
var existingMeshes = mainPart.Meshes.Where(m => m.Name.Contains(baseName)).ToList();
var existingProperties = new Dictionary<string, MeshProperties>();
// Store properties of existing meshes
foreach (var mesh in existingMeshes)
{
existingProperties[mesh.Name] = new MeshProperties
{
Visibility = mesh.IsVisible,
// Add other properties as needed
};
}
// Hide existing meshes instead of deleting
foreach (var mesh in existingMeshes)
{
mesh.SetVisibility(null, false);
}
// Create new meshes
for (int i = 0; i < meshes.Count; i++)
{
var mesh = meshes[i];
var meshName = baseName + "_new_" + i;
mesh.Faces.ConvertQuadsToTriangles();
var vertices = Spoints(mesh.Vertices.ToList());
var faces = Sfaces(mesh.Faces.ToList());
var smesh = SpaceClaim.Api.V19.DesignMesh.Create(mainPart, meshName, vertices, faces);
// Apply stored properties
if (existingProperties.ContainsKey(baseName + "_" + i))
{
var props = existingProperties[baseName + "_" + i];
smesh.SetVisibility(null, props.Visibility);
}
smesh.KeepAlive(true);
}
// Cleanup old meshes after a delay (optional)
System.Threading.Tasks.Task.Delay(100).ContinueWith(t =>
{
WriteBlock.ExecuteTask("Cleanup Old Meshes", () =>
{
foreach (var mesh in existingMeshes.Where(m => m != null))
{
try { mesh.Delete(); } catch { }
}
});
});
}