首页 > HFSS > HFSS教程 > 利用C#在hfss中绘制像素画

利用C#在hfss中绘制像素画

录入:edatop.com    点击:

利用C#在hfss中绘制像素画

HFSS – High Frequency Structure Simulator,Ansoft公司推出的三维电磁仿真软件,目前已被ANSYS公司收购;是世界上第一个商业化的三维结构电磁场仿真软件,业界公认的三维电磁场设计和分析的工业标准。HFSS提供了一简洁直观的用户设计界面、精确自适应的场解器、拥有空前电性能分析能力的功能强大后处理器,能计算任意形状三维无源结构的S参数和全波电磁场。同时该软件预留了可以运行脚本的接口,我们可以通过运行脚本实现建模,求解,优化等操作。因为本人比较熟悉的C#,就决定使用C#来实现对hfss的控制。折腾了一下,做了个像素画,下面分享一下,怎么编写的C#脚本。

hfss像素画

以下就是利用脚本在hfss中绘制的像素画:
在这里插入图片描述
1.首先编写一个提取图片像素的函数:

//返回一个图片像素位置和颜色和二维数组Color[,],imgfillle为图片的位置路径
public static System.Drawing.Color[,] img2color(String imgfile)
        {
            Bitmap img = new Bitmap(imgfile);
            System.Drawing.Color[,] allcolor = new System.Drawing.Color[img.Height, img.Width];
            for (int h = 0; h < img.Height; h++)
                for (int w = 0; w < img.Width; w++)
                {
                    allcolor[h, w] = img.GetPixel(w, h);
                }
            GC.Collect();
            return allcolor;
        }

2.然后编写一个在hfss中生成矩形的函数:

//x,y为生成矩形的坐标位置,w,h为矩形的长和宽,r,g,b为矩形的颜色
 private string hfss_draw_Rec_color(double x, double y, double w, double h, int r, int g, int b, string name)
        {
            string r_str = "oEditor.CreateRectangle(" + "\r\n";
            r_str += "\t[" + "\r\n";
            r_str += "\t\t\"NAME:RectangleParameters\"," + "\r\n";
            r_str += "\t\t\"IsCovered:=\"\t\t, True," + "\r\n";
            r_str += "\t\t\"XStart:=\"\t\t, \"" + x + "mm\"," + "\r\n";
            r_str += "\t\t\"YStart:=\"\t\t, \"" + y + "mm\"," + "\r\n";
            r_str += "\t\t\"ZStart:=\"\t\t, \"0mm\"," + "\r\n";
            r_str += "\t\t\"Width:=\"\t\t, \"" + w + "mm\"," + "\r\n";
            r_str += "\t\t\"Height:=\"\t\t, \"" + h + "mm\"," + "\r\n";
            r_str += "\t\t\"WhichAxis:=\"\t\t, \"Z\"" + "\r\n";
            r_str += "\t]," + "\r\n";
            r_str += "\t[" + "\r\n";
            r_str += "\t\t\"NAME:Attributes\"," + "\r\n";
            r_str += "\t\t\"Name:=\"\t\t, \"" + name + "\"," + "\r\n";
            r_str += "\t\t\"Flags:=\"\t\t, \"\"," + "\r\n";
            r_str += "\t\t\"Color:=\"\t\t, \"("+r+" "+g+" "+ b + ")\"," + "\r\n";
            r_str += "\t\t\"Transparency:=\"\t, 0," + "\r\n";
            r_str += "\t\t\"PartCoordinateSystem:=\", \"Global\"," + "\r\n";
            r_str += "\t\t\"UDMId:=\"\t\t, \"\"," + "\r\n";
            r_str += "\t\t\"MaterialValue:=\"\t, \"\\\"vacuum\\\"\"," + "\r\n";
            r_str += "\t\t\"SolveInside:=\"\t\t, True," + "\r\n";
            r_str += "\t\t\"IsMaterialEditable:=\"\t, True" + "\r\n";
            r_str += "\t])" + "\r\n";
            return r_str;
        }

3.最后将图片的像素位置颜色赋值到hfss的矩形中:

//提取图片的像素,保存到allcolor数组中
System.Drawing.Color[,] allcolor = img2color(System.IO.Path.GetFullPath(filePath));
//按照像素分行赋值给hfss中矩形,allcolor.GetLength为图片的长度
for (int i = 0; i < allcolor.GetLength(0); i++)
		{	
			for (int j = 0; j < allcolor.GetLength(1); j++)
				{				
                        //删除部分白色的像素,简化脚本,减少一些无用的矩形
                        if(allcolor[i, j].R<245|| allcolor[i, j].G<245|| allcolor[i, j].B<245)
						{
						    sw.Write(hfss_draw_Rec_color(i * 0.5, j * 0.5, 0.5, 0.5, allcolor[i, j].R, allcolor[i, j].G, allcolor[i, j].B, "rec" + (allcolor.GetLength(0) * i + j).ToString()));
						}
				}
		}

**hfss工程文件链接:**https://download.csdn.net/download/qq_23176133/10783252

HFSS 学习培训课程套装,专家讲解,视频教学,帮助您全面系统地学习掌握HFSS

上一篇:HFSS14破解步骤
下一篇:HFSS 计算面上的电磁场

HFSS视频培训课程推荐详情>>
HFSS教程推荐

  网站地图