您现在的位置是:首页 >技术交流 >通过cad中块获取块的略缩图——cad c# 二次开发网站首页技术交流
通过cad中块获取块的略缩图——cad c# 二次开发
简介通过cad中块获取块的略缩图——cad c# 二次开发
通过cad中块获取块的略缩图,并保存桌面,效果如下:
附部分代码:
using Autodesk.AutoCAD.Internal;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using Color = Autodesk.AutoCAD.Colors.Color;
namespace IfoxDemo
{
public static class 保存略缩图
{
[CommandMethod("tt")]
public static void 保存略缩图tt()
{
//新建一个块,把块的律所图存到桌面。
using var tr = new DBTrans();
var line = new Line(new Point3d(0, 0, 0), new Point3d(100, 100, 0));
var pl = line.CurveToPolyline();
line.ColorIndex = 1;
var id = tr.BlockTable.Add("test",
btr =>
{
btr.Origin = new Point3d(0, 0, 0);
},
() => // 图元
{
var ty = new List<Entity> { pl };
pl.ColorIndex = 1;
pl.ConstantWidth =3;
return ty;
},
() => // 属性定义
{
var id1 = new AttributeDefinition() { Position = new Point3d(0, 0, 0), Tag = "start", Height = 0.02 };
var id2 = new AttributeDefinition() { Position = new Point3d(1, 1, 0), Tag = "end", Height = 0.02 };
return new List<AttributeDefinition> { id1, id2 };
}
);
var pr = Utils.GetBlockImage(id, 2000, 1600, Autodesk.AutoCAD.Colors.Color.FromRgb(0, 0, 0));
Bitmap bmp = Bitmap.FromHbitmap(pr);
bmp.SaveDesktop();
//
}
public static Polyline CurveToPolyline(this Curve cur)
{
if (cur is Polyline pl1) return pl1;
else if (cur is Line line)
{
Polyline pl = new Polyline();
pl.AddVertexAt(0, line.StartPoint.Point2d(), 0, 0, 0);
pl.AddVertexAt(1, line.EndPoint.Point2d(), 0, 0, 0);
return pl;
}
else if (cur is Arc arc)
{
var bug = Math.Tan(arc.TotalAngle / 4);
Polyline pl = new Polyline();
pl.AddVertexAt(0, arc.StartPoint.Point2d(), bug, 0, 0);
pl.AddVertexAt(1, arc.EndPoint.Point2d(), 0, 0, 0);
return pl;
}
else if (cur is Spline sl)
{
return (Polyline)sl.ToPolyline();
}
else if (cur is Ellipse ellipse)
{
return (Polyline)ellipse.Spline.ToPolyline();
}
else if (cur is Circle circle)
{
Polyline pl = new Polyline();
var p1 = circle.StartPoint;
var p2 = circle.EndPoint;
var bug = Math.Tan(Math.PI / 4);
pl.AddVertexAt(0, p1.Point2d(), bug, 0, 0);
pl.AddVertexAt(1, p1.Point2d(), bug, 0, 0);
pl.Closed = true;
return pl;
}
else if (cur is Polyline2d poly)
{
try
{
Polyline pl = new Polyline();
pl.ConvertFrom(poly, false);//false代表不写入数据库?
return pl;
}
catch (Exception)
{
Polyline pl = new Polyline();
return pl;
}
}
//else if (cur is Polyline3d poly3d)//尚未完善
//{
// Polyline pl = new Polyline();
// pl.ConvertFrom(poly3d, false);//false代表不写入数据库?
// return pl;
//}
else
{
"
curve转polyline失败!
".Print();
Polyline pl = new Polyline();
return pl;
}
}
}
}
using System.Drawing;
using Path = System.IO.Path;
using System.Drawing.Imaging;
namespace IfoxDemo
{
public static class SaveBitmapToDesktop
{
public static void SaveDesktop(this Bitmap bmp)
{
// 获取桌面路径
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// 构建文件路径和名称,例如 "MyBitmap.png"
string filePath = Path.Combine(desktopPath, "MyBitmap.png");
// 确保文件不会因名称冲突而被覆盖(可选)
int counter = 1;
string baseName = Path.GetFileNameWithoutExtension(filePath);
string extension = Path.GetExtension(filePath);
while (File.Exists(filePath))
{
filePath = Path.Combine(desktopPath, $"{baseName}_{counter}{extension}");
counter++;
}
// 保存Bitmap到文件
try
{
bmp.Save(filePath, ImageFormat.Png); // 使用PNG格式保存,你也可以选择其他格式,如Jpeg
MessageBox.Show($"Bitmap saved to {filePath}"); // 显示消息框(可选)
}
catch (Exception ex)
{
MessageBox.Show($"Failed to save bitmap: {ex.Message}"); // 显示错误消息(可选)
}
}
}
}
完整代码(cad插件定制、二次开发)↓↓↓
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。