Ler, atualizar e gravar arquivo DWG
Contents
[
Hide
]Como ler, atualizar e gravar arquivo DWG
Problema: Como ler, atualizar e gravar arquivo DWG.
Dicas: Para fazer isso, você pode obter o arquivo usando o método load, obter as entidades necessárias e fazer alterações nelas, como mudar os pontos de início e fim ou a espessura das linhas.
Nota: Este trecho de código mostra um exemplo de leitura de um arquivo dwg, mudando objetos: as posições de linhas, círculos, valores de texto (você pode adicionar mudanças a outros objetos e seus valores para os quais a leitura/gravação é suportada) e então salvando em um novo arquivo. Assim, você pode abrir um novo arquivo no AutoCAD e ver objetos com valores alterados.
Exemplo:
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); |