C# SolidWorks二次开发——面均匀采样并显示采样点(SolidWorks 2020 + VS 2019)
这里介绍如何在零件的某个面上进行均匀采样,并进行显示采样点。
均匀采样的效果如图所示,在零件面上均匀采样了64个采样点:
1、如何画一个点
这里给出了一个简单的示意函数 create3dPoint(MathPoint facePoint) 。
这里的传入值为SolidWorks中的MathPoint类,具体用法参考帮助文档,也可以不用MathPoint类,直接传入一个数组或三个点坐标值。
public void create3dPoint(MathPoint facePoint)
{
//读取XYZ坐标
double[] facePointXYZ = (double[])facePoint.ArrayData;
//画点
ISketchPoint point = iSwApp.IActiveDoc2.CreatePoint2(facePointXYZ[0], facePointXYZ[1], facePointXYZ[2]);
//如果创建点成功
if(point!=null)
{
//设置颜色,16进制格式为0xbbggrr,如0x0000ff为红色,0xff0000为蓝色
point.Color = 0x0000ff;
}
}
2、面均匀采样,返回采样点及其法线
这里给出了一个简单的示意函数FacePointUniformSample()。
传入参数1为IFace类型的面,将采样得到的采样点及法线保存在参数2和参数3的List中。
public List<MathPoint> FacePointUniformSample(IFace2 face, List<MathPoint> facePoint, List<MathVector> facePointNor)
{
List<MathPoint> currentFacePoint = new List<MathPoint>();
//控制采样的密度,最终单个面上采样点的个数为pointNumber*pointNumber。
int pointNumber = 8;
//读取UV坐标
double[] faceUV = (double[])face.GetUVBounds();
double minU = faceUV[0];
double maxU = faceUV[1];
double minV = faceUV[2];
double maxV = faceUV[3];
double stepU = (maxU - minU)