Implement Dxf Writer

This commit is contained in:
Sergey Chebotar 2023-04-19 21:04:13 +03:00
parent e295709e65
commit 4fb498138a

View File

@ -1,5 +1,8 @@
using netDxf; using netDxf;
using netDxf.Blocks;
using netDxf.Entities;
using netDxf.Header; using netDxf.Header;
using System.Diagnostics;
using System.IO; using System.IO;
using Line = netDxf.Entities.Line; using Line = netDxf.Entities.Line;
@ -7,43 +10,446 @@ namespace RhSolutions.Services;
public class RhDxfWriter : IExcelWriter public class RhDxfWriter : IExcelWriter
{ {
private readonly string file;
private readonly DxfDocument doc;
private Dictionary<Product, double> productDict;
private readonly Block tableBlock;
public RhDxfWriter()
{
string filepath = RhSolutionsAddIn.Excel.ActiveWorkbook.Path;
string filename = Path.GetFileNameWithoutExtension(RhSolutionsAddIn.Excel.ActiveWorkbook.Name);
file = Path.Combine(filepath, $"{filename}.dxf");
doc = new DxfDocument();
tableBlock = CreateTable();
}
public void WriteProducts(Dictionary<Product, double> products) public void WriteProducts(Dictionary<Product, double> products)
{ {
WriteProducts(new[] { (string.Empty, products) }); WriteProducts(new[] { (string.Empty, products) });
} }
public void WriteProducts(IEnumerable<(string, Dictionary<Product, double>)> products) public void WriteProducts(IEnumerable<(string, Dictionary<Product, double>)> products)
{ // your DXF file name {
string file = "sample.dxf"; productDict = products.First().Item2;
doc.Layers.Add(new netDxf.Tables.Layer("Таблицы"));
int tablesCount = productDict.Count / 27 + (productDict.Count % 27 == 0 ? 0 : 1);
int tablePosition = 0;
for (int i = 0; i < tablesCount; i++)
{
var insertion = new Insert(tableBlock, new Vector2(tablePosition, 0))
{
Layer = doc.Layers["Таблицы"]
};
var pArray = products.First().Item2.Select(p => p.Key).ToArray();
for (int row = 0; row < 27 && i * 27 + row < pArray.Length; row++)
{
insertion.Attributes.AttributeWithTag($"Name{row}").Value = pArray[i * 27 + row].Name;
insertion.Attributes.AttributeWithTag($"ProductSku{row}").Value = pArray[i * 27 + row].ProductSku;
insertion.Attributes.AttributeWithTag($"Rh{row}").Value = "«РЕХАУ»";
insertion.Attributes.AttributeWithTag($"Amount{row}").Value = productDict[pArray[i * 27 + row]].ToString();
}
insertion.Attributes.AttributeWithTag("Sheet").Value = (i + 1).ToString();
tablePosition += 43000;
doc.Entities.Add(insertion);
}
// create a new document, by default it will create an AutoCad2000 DXF version
DxfDocument doc = new DxfDocument();
// an entity
Line entity = new Line(new Vector2(5, 5), new Vector2(10, 5));
// add your entities here
doc.Entities.Add(entity);
// save to file
doc.Save(file); doc.Save(file);
// this check is optional but recommended before loading a DXF file
DxfVersion dxfVersion = DxfDocument.CheckDxfFileVersion(file); DxfVersion dxfVersion = DxfDocument.CheckDxfFileVersion(file);
// netDxf is only compatible with AutoCad2000 and higher DXF versions if (dxfVersion < DxfVersion.AutoCad2000)
if (dxfVersion < DxfVersion.AutoCad2000) return; return;
// load file
DxfDocument loaded = DxfDocument.Load(file);
string docPath =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, file)))
{
outputFile.Write(loaded);
} }
private Block CreateTable()
{
List<EntityObject> entities = new();
entities.AddRange(new[]
{
new Line(new Vector2(0, 29700), new Vector2(0, 0)),
new Line(new Vector2(0, 0), new Vector2(42000, 0)),
new Line(new Vector2(42000, 0), new Vector2(42000, 29700)),
new Line(new Vector2(42000, 29700), new Vector2(0, 29700)),
});
entities.AddRange(new[]
{
new Line(new Vector2(2000, 29200), new Vector2(2000, 500))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(2000, 500), new Vector2(41500, 500))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(41500, 500), new Vector2(41500, 29200))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(41500, 29200), new Vector2(2000, 29200))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(800, 9000), new Vector2(2000, 9000))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(2000, 6500), new Vector2(800, 6500))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(2000, 3000), new Vector2(800, 3000))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(2000, 500), new Vector2(800, 500))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(800, 500), new Vector2(800, 9000))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(1300, 500), new Vector2(1300, 9000))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(23000,2000), new Vector2(41500, 2000))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(23000, 1500), new Vector2(29500, 1500))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(23000, 1000), new Vector2(29500, 1000))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(23000, 500), new Vector2(23000, 2000))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(24000, 500), new Vector2(24000, 2000))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(25000, 500), new Vector2(25000, 2000))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(26000, 500), new Vector2(26000, 2000))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(27000, 500), new Vector2(27000, 2000))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(28500, 500), new Vector2(28500, 2000))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(29500, 500), new Vector2(29500, 2000))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(40500, 500), new Vector2(40500, 2000))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(40500, 1300), new Vector2(41500, 1300))
{
Lineweight = Lineweight.W30
},
});
entities.AddRange(new[]
{
new Line(new Vector2(4000, 29200), new Vector2(4000, 4200))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(17000, 29200), new Vector2(17000, 4200))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(23000, 29200), new Vector2(23000, 4200))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(26500, 29200), new Vector2(26500, 4200))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(31000, 29200), new Vector2(31000, 4200))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(33000, 29200), new Vector2(33000, 4200))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(35000, 29200), new Vector2(35000, 4200))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(37500, 29200), new Vector2(37500, 4200))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(4000, 29200), new Vector2(4000, 4200))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(41500, 26500), new Vector2(2000, 26500))
{
Lineweight = Lineweight.W30
},
new Line(new Vector2(41500, 25700), new Vector2(2000, 25700))
{
Lineweight = Lineweight.W30
}
});
int y = 4200;
for (int i = 0; i < 27; i++)
{
var line = new Line(new Vector2(41500, y), new Vector2(2000, y));
entities.Add(line);
y += 800;
}
entities.AddRange(new[]
{
new Text("Инв.N подл.", new Vector2(1150, 700), 250)
{
Rotation = 90,
WidthFactor = 0.85
},
new Text("Подпись и дата", new Vector2(1150, 3130), 250)
{
Rotation = 90,
WidthFactor = 0.85
},
new Text("Взам.инв.N", new Vector2(1150, 6580), 250)
{
Rotation = 90,
WidthFactor = 0.85
},
new Text("Позиция", new Vector2(3000, 27850), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("1", new Vector2(3000, 26200), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("Наименование и техническая характеристика", new Vector2(10500, 27850), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("2", new Vector2(10500, 26200), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("Тип, марка оборудования.", new Vector2(20000, 28250), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("Обозначение документа,", new Vector2(20000, 27850), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("опросного листа", new Vector2(20000, 27450), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("3", new Vector2(20000, 26200), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("Код оборудования,", new Vector2(24750, 28250), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("изделия,", new Vector2(24750, 27850), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("материала", new Vector2(24750, 27450), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("4", new Vector2(24750, 26200), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("Завод-изготовитель", new Vector2(28750, 27850), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("5", new Vector2(28750, 26200), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("Единица", new Vector2(32000, 27850), 250)
{
Alignment = TextAlignment.BottomCenter,
WidthFactor = 0.85
},
new Text("измерения", new Vector2(32000, 27850), 250)
{
Alignment = TextAlignment.TopCenter,
WidthFactor = 0.85
},
new Text("6", new Vector2(32000, 26200), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("Коли-", new Vector2(34000, 27850), 250)
{
Alignment = TextAlignment.BottomCenter,
WidthFactor = 0.85
},
new Text("чество", new Vector2(34000, 27850), 250)
{
Alignment = TextAlignment.TopCenter,
WidthFactor = 0.85
},
new Text("7", new Vector2(34000, 26200), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("Масса,", new Vector2(36250, 28250), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("единицы,", new Vector2(36250, 27850), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("кг", new Vector2(36250, 27450), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("8", new Vector2(36250, 26200), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("Примечания", new Vector2(39500, 27850), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("Изм.", new Vector2(23100, 525), 300)
{
Alignment = TextAlignment.BottomLeft,
WidthFactor = 0.65
},
new Text("Кол.уч", new Vector2(24100, 525), 300)
{
Alignment = TextAlignment.BottomLeft,
WidthFactor = 0.65
},
new Text("Лист", new Vector2(25100, 525), 300)
{
Alignment = TextAlignment.BottomLeft,
WidthFactor = 0.65
},
new Text("Nдок.", new Vector2(26100, 525), 300)
{
Alignment = TextAlignment.BottomLeft,
WidthFactor = 0.65
},
new Text("Подп.", new Vector2(27100, 525), 300)
{
Alignment = TextAlignment.BottomLeft,
WidthFactor = 0.65
},
new Text("Дата", new Vector2(28600, 525), 300)
{
Alignment = TextAlignment.BottomLeft,
WidthFactor = 0.65
},
new Text("Лист", new Vector2(41000, 1650), 300)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.65
}
});
Block block = new("Таблица спецификации", entities);
y = 25400;
for (int i = 0; i < 27; i++)
{
block.AttributeDefinitions.Add(new AttributeDefinition($"Name{i}")
{
Position = new Vector3(4180, y, 0),
Alignment = TextAlignment.MiddleLeft,
WidthFactor = 0.85,
Height = 250
});
block.AttributeDefinitions.Add(new AttributeDefinition($"ProductSku{i}")
{
Position = new Vector3(24750, y, 0),
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85,
Height = 250
});
block.AttributeDefinitions.Add(new AttributeDefinition($"Rh{i}")
{
Position = new Vector3(28750, y, 0),
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85,
Height = 250
});
block.AttributeDefinitions.Add(new AttributeDefinition($"Amount{i}")
{
Position = new Vector3(34000, y, 0),
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85,
Height = 250
});
y -= 800;
}
block.AttributeDefinitions.Add(new AttributeDefinition($"Sheet")
{
Position = new Vector3(41000, 950, 0),
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.65,
Height = 300
});
return block;
} }
public void Dispose() public void Dispose()
{ {
Process.Start(file);
} }
} }