Files
wg_cpso/CaeKnowledge/MeshTools.cs

134 lines
4.3 KiB
C#
Raw Permalink Normal View History

2026-03-25 18:20:24 +08:00
using CaeMesh;
using System;
using System.Collections.Generic;
using System.Linq;
namespace CaeKnowledge
{
public class MeshTools
{
public static double Distance(double x1, double y1, double z1, double x2, double y2, double z2)
{
return Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2) + Math.Pow(z1 - z2, 2));
}
private static double Distance(FeNode n1, FeNode n2)
{
return Math.Sqrt(Math.Pow(n1.X - n2.X, 2) + Math.Pow(n1.Y - n2.Y, 2) + Math.Pow(n1.Z - n2.Z, 2));
}
public static List<FeNode> GetClosestNodes(FeMesh mesh, FeNode _base, double minimum)
{
var list = new List<FeNode>();
foreach (var node in mesh.Nodes.Values)
{
var dist = Distance(node, _base);
if (dist < minimum) list.Add(node);
}
return list;
}
public static List<FeElement> GetClosestElements(FeMesh mesh, FeNode _base, double minimum)
{
var list = new List<FeElement>();
foreach (var element in mesh.Elements.Values)
{
var nodes = (from id in element.NodeIds select mesh.Nodes[id]).ToList();
if (nodes.All(node => Distance(node, _base) < minimum))
{
list.Add(element);
}
}
return list;
}
public static List<FeNode> GetClosestNodesInNodeSet(FeMesh mesh, string nodeSetName, FeNode _base, double minimum)
{
if (!mesh.NodeSets.TryGetValue(nodeSetName, out var nodeSet))
return null;
var nodes = (from id in nodeSet.Labels select mesh.Nodes[id]).ToList();
var list = new List<FeNode>();
nodes.ForEach(node =>
{
if (Distance(node, _base) < minimum)
list.Add(node);
});
return list;
}
public static List<Tuple<FeElement, FeFaceName>> FindAllElements(FeMesh mesh, string nodeSetName)
{
if (!mesh.NodeSets.TryGetValue(nodeSetName, out var nodeSet))
return null;
var list = new List<Tuple<FeElement, FeFaceName>>();
foreach (var element in from element
in mesh.Elements.Values.OfType<FeElement3D>()
select element)
{
// 四面体单元
foreach (var faceName in new[] { FeFaceName.S1, FeFaceName.S2, FeFaceName.S3, FeFaceName.S4 })
{
var ids = element.GetNodeIdsFromFaceName(faceName);
if (ids.All(x => nodeSet.Labels.Contains(x)))
list.Add(new Tuple<FeElement, FeFaceName>(element, faceName));
}
}
return list;
}
public static List<int> GetClosestFaces(FeMesh mesh, string nodeSetName, FeNode _base, double minimum)
{
var nodes = (from x in GetClosestNodesInNodeSet(mesh, nodeSetName, _base, minimum) select x.Id).ToList();
if (nodes.Count == 0) return null;
var allElements = FindAllElements(mesh, nodeSetName);
if (allElements == null || allElements.Count == 0) return null;
var list = new List<int>();
foreach (var (element, faceName) in allElements)
{
var ids = element.GetNodeIdsFromFaceName(faceName);
if (!ids.Any(x => nodes.Contains(x)))
continue;
var label = element.Id * 10 + (int)faceName - 1;
list.Add(label);
}
list.Sort();
return list;
}
public static List<FeFace> FindSurfaceAllFaces(string srfName, FeMesh mesh)
{
if (!mesh.Surfaces.TryGetValue(srfName, out var surface))
return null;
var faceIds = surface.FaceIds;
return (from faceId in faceIds
let elemId = faceId / 10
let faceTag = faceId % 10 + 1
let element = mesh.Elements[elemId]
let faceName = (FeFaceName)faceTag
select new FeFace(element, faceName)).ToList();
}
}
}