Hi @Mahdiyar, @eirannejad
I’ve been working on translating Gmsh code into C# using Gmsh.Net, but I’m running into some issues when trying to integrate Gmsh.Net.dll
in the Rhino Script Editor. It seems the problem might also be related to the gmsh.dll
, which is part of the library dependencies.
I downloaded the required DLL files from the following GitHub repository:
https://github.com/noy1993/Gmsh.Net/tree/master/dll/windows.
Do you have any experience or suggestions regarding how to properly use Gmsh.Net.dll
in a C# workflow? Any advice on resolving this issue would be greatly appreciated!
Code:
using Rhino;
using Rhino.Geometry;
using System.Collections.Generic;
using System.Linq;
using GmshNet; // If using Gmsh.Net
void RunScript(List<Point3d> outer, List<List<Point3d>> inners, double lengthMin, double lengthMax,
bool quad, List<Mesh> nodes, ref object A)
{
// 1. Initialize Gmsh
Gmsh.Initialize();
Gmsh.Clear();
// 2. Set mesh properties
Gmsh.Option.SetNumber("Mesh.CharacteristicLengthMin", lengthMin);
Gmsh.Option.SetNumber("Mesh.CharacteristicLengthMax", lengthMax);
// 3. Geometry definition
int CreateLoop(List<Point3d> pts)
{
List<int> pointTags = pts.Select(
p => Gmsh.Model.Geo.AddPoint(p.X, p.Y, p.Z, lengthMin)).ToList();
pointTags.Add(pointTags[0]);
List<int> lineTags = new List<int>();
for (int i = 0; i < pointTags.Count - 1; i++)
{
lineTags.Add(Gmsh.Model.Geo.AddLine(pointTags[i], pointTags[i + 1]));
}
return Gmsh.Model.Geo.AddCurveLoop(lineTags.ToArray());
}
var outerLoop = CreateLoop(outer);
List<int> loops = new List<int> { outerLoop };
foreach (var inner in inners)
{
loops.Add(CreateLoop(inner));
}
int planeSurface = Gmsh.Model.Geo.AddPlaneSurface(loops.ToArray());
// 4. Configure meshing field (if target nodes are provided)
if (nodes != null && nodes.Count > 0)
{
var pointTags = nodes.Select(node => Gmsh.Model.Geo.AddPoint(node.Vertices[0].X,
node.Vertices[0].Y,
node.Vertices[0].Z)).ToList();
int field = Gmsh.Model.Mesh.Field.Add("Distance");
Gmsh.Model.Mesh.Field.SetNumbers(field, "PointsList", pointTags);
int thresholdField = Gmsh.Model.Mesh.Field.Add("Threshold");
Gmsh.Model.Mesh.Field.SetNumber(thresholdField, "IField", field);
Gmsh.Model.Mesh.Field.SetNumber(thresholdField, "LcMin", lengthMin / 50);
Gmsh.Model.Mesh.Field.SetNumber(thresholdField, "LcMax", lengthMax * 2);
Gmsh.Model.Mesh.Field.SetNumber(thresholdField, "DistMin", lengthMin * 2);
Gmsh.Model.Mesh.Field.SetNumber(thresholdField, "DistMax", lengthMin * 10 + lengthMax * 10);
Gmsh.Model.Mesh.Field.SetAsBackgroundMesh(thresholdField);
}
// 5. Enable quadrangular meshing if specified
if (quad)
{
Gmsh.Model.Mesh.Recombine(2, planeSurface);
}
Gmsh.Model.Geo.Synchronize();
// 6. Generate the mesh
Gmsh.Model.Mesh.Generate(2);
// 7. Extract mesh nodes and connectivity
var nodesArray = Gmsh.Model.Mesh.GetNodes();
var coords = nodesArray.Item2;
var elements = Gmsh.Model.Mesh.GetElements();
var elementNodes = elements.Item3[0];
// Convert data to a Rhino.Geometry.Mesh
Mesh mesh = new Mesh();
for (int i = 0; i < coords.Length; i += 3)
{
mesh.Vertices.Add(coords[i], coords[i + 1], coords[i + 2]);
}
int faceSize = quad ? 4 : 3;
for (int i = 0; i < elementNodes.Length; i += faceSize)
{
if (quad)
{
mesh.Faces.AddFace((int)elementNodes[i] - 1, (int)elementNodes[i + 1] - 1,
(int)elementNodes[i + 2] - 1, (int)elementNodes[i + 3] - 1);
}
else
{
mesh.Faces.AddFace((int)elementNodes[i] - 1, (int)elementNodes[i + 1] - 1,
(int)elementNodes[i + 2] - 1);
}
}
mesh.RebuildNormals();
mesh.Compact();
// 8. Output the mesh to Grasshopper
A = mesh;
// Finalize Gmsh
Gmsh.Finalize();
}
@eirannejad !!!Grasshopper script editor is busy gmsh.dll and can’t load it !!