阅读、更新和写入 DWG 文件
Contents
[
Hide
]如何阅读、更新和写入 DWG 文件
问题: 如何阅读、更新和写入 DWG 文件。
提示: 要做到这一点,您可以使用 load 方法获取文件,获取所需的实体并对其进行更改,例如更改起始点和结束点或线条的厚度。
注意: 该代码片段展示了读取 dwg 文件的示例,更改对象:线条、圆形、文本值的位置(您可以对其他支持读取/写入的对象及其值进行更改)然后保存到新文件。因此,您可以在 AutoCAD 中打开一个新文件并查看值已更改的对象。
示例:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fileName = "TestDwg"; | |
var inputFile = BaseDataPath + fileName + ".dwg"; | |
var outFile = GetFileInOutputFolder(fileName + "-rewritten.dwg"); | |
using var cadImage = (CadImage) Image.Load(inputFile); | |
var cadLines = cadImage.Entities.OfType<CadLine>().ToList(); | |
if (cadLines.Any()) | |
{ | |
foreach (var cadLine in cadLines) | |
{ | |
cadLine.FirstPoint.X *= 2; | |
cadLine.FirstPoint.Y *= 2; | |
cadLine.SecondPoint.X *= 3; | |
cadLine.SecondPoint.Y *= 3; | |
} | |
} | |
var cadCircles = cadImage.Entities.OfType<CadCircle>().ToList(); | |
if (cadCircles.Any()) | |
{ | |
foreach (var cadCircle in cadCircles) | |
{ | |
cadCircle.CenterPoint.X *= 1.5; | |
cadCircle.CenterPoint.Y *= 1.5; | |
cadCircle.Radius *= 0.5; | |
} | |
} | |
var cadTexts = cadImage.Entities.OfType<CadText>().ToList(); | |
if (cadTexts.Any()) | |
{ | |
foreach (var cadText in cadTexts) | |
{ | |
cadText.DefaultValue = $"New {cadText.DefaultValue}"; | |
} | |
} | |
cadImage.Save(outFile); |