C#开源库ACadSharp读取dwg图元的示例
·
介绍
开源库ACadSharp的地址:https://github.com/DomCR/ACadSharp
可以在NuGet中搜索到该库并安装。
数据示例
数据是一个绘制了以下简单图元的dwg数据:

读取图元属性
创建了.net6控制台项目,通过NuGet安装了ACadSharp库。
测试了以下几种图元的读取:
- Line
- Arc
- Circle
- Ellipse
- LwPolyline
- MText
- Text
using ACadSharp.IO;
using ACadSharp;
using ACadSharp.Entities;
namespace TestACadSharp
{
internal class Program
{
static void Main(string[] args)
{
var dwgPath = "./test-data/demo.dwg";
CadDocument doc = DwgReader.Read(dwgPath, onNotification);
//遍历所有实体
foreach (var entity in doc.Entities)
{
PrintInfo(entity);
Console.WriteLine();
}
Console.WriteLine("Over!");
}
static void PrintInfo(Entity ent)
{
switch (ent)
{
case Line:
var e = ent as Line;
Console.WriteLine($"Line: X1={e.StartPoint.X},Y1={e.StartPoint.Y}," +
$"X2={e.EndPoint.X},Y2={e.EndPoint.Y},Handle={e.Handle}");
break;
case Arc:
var a = ent as Arc;
Console.WriteLine($"Arc: r={a.Radius},X={a.Center.X},Y={a.Center.Y}," +
$"StartAngle={a.StartAngle},EndAngle={a.EndAngle}");
break;
case Circle:
var c = ent as Circle;
Console.WriteLine($"Circle: r={c.Radius},X={c.Center.X},Y={c.Center.Y}");
break;
case Ellipse:
var ee = ent as Ellipse;
Console.WriteLine($"Ellipse: X={ee.Center.X},Y={ee.Center.Y}," +
$"MajorAxis={ee.MajorAxis},MinorAxis={ee.MinorAxis}," +
$"Rotation={ee.Rotation}");
break;
case Polyline:
var p = ent as Polyline;
Console.WriteLine($"Polyline: IsClosed={p.IsClosed}");
for (int i = 0; i < p.Vertices.Count; i++)
{
var v = p.Vertices[i];
Console.WriteLine($"Bulge={v.Bulge},X={v.Location.X},Y={v.Location.Y}");
}
break;
case LwPolyline:
var wp = ent as LwPolyline;
Console.WriteLine($"LwPolyline: IsClosed={wp.IsClosed}");
for (int i = 0; i < wp.Vertices.Count; i++)
{
var v = wp.Vertices[i];
Console.WriteLine($"Bulge={v.Bulge},X={v.Location.X},Y={v.Location.Y}");
}
break;
case MText:
var mt = ent as MText;
Console.WriteLine($"MText: X={mt.InsertPoint.X},Y={mt.InsertPoint.Y},Text={mt.Value},Height={mt.Height}");
break;
case TextEntity:
var t = ent as TextEntity;
Console.WriteLine($"Text: X={t.InsertPoint.X},Y={t.InsertPoint.Y},Text={t.Value},Height={t.Height}");
break;
default:
break;
}
}
// Process a notification form the reader
private static void onNotification(object sender, NotificationEventArgs e)
{
Console.WriteLine(e.Message);
}
}
}
在控制台打印了图元的部分属性:
Line: X1=0,Y1=0,X2=100,Y2=100,Handle=591
Circle: r=20,X=100,Y=100
LwPolyline: IsClosed=False
Bulge=0,X=100,Y=0
Bulge=0,X=200,Y=100
Bulge=0,X=200,Y=200
Arc: r=50,X=200,Y=0,StartAngle=1.5707963267948961,EndAngle=3.5853960407017067
LwPolyline: IsClosed=True
Bulge=0,X=0,Y=200
Bulge=0,X=20,Y=200
Bulge=0,X=100,Y=300
Bulge=0,X=20,Y=300
LwPolyline: IsClosed=False
Bulge=1.7160122950374628,X=144.12226845286386,Y=251.44355233800798
Bulge=0,X=203.49903695426838,Y=270.9970954309483
Bulge=0,X=168.5715230805963,Y=344.3228820294762
Bulge=0,X=91.0324529571044,Y=367.36812645092596
Ellipse: X=325.5331742656035,Y=139.27755185746537,MajorAxis=167.47948810551864,MinorAxis=61.59293601510967,Rotation=-2.3304093705041717
Text: X=334.465413122849,Y=340.1926829662716,Text=hello,Height=77.01746524013924
MText: X=427.1373896780551,Y=226.34077414221065,Text=world\P!!!!,Height=77.01746524013924
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐

所有评论(0)