RhSolutions-AddIn/RhSolutions.AddIn/Services/DxfWriter.cs

468 lines
16 KiB
C#
Raw Normal View History

2023-04-17 08:49:26 +03:00
using netDxf;
2023-04-19 21:04:13 +03:00
using netDxf.Blocks;
using netDxf.Entities;
2023-04-17 08:49:26 +03:00
using netDxf.Header;
2023-04-19 21:04:13 +03:00
using System.Diagnostics;
2023-04-17 08:49:26 +03:00
using System.IO;
using Line = netDxf.Entities.Line;
namespace RhSolutions.Services;
2023-04-20 09:37:07 +03:00
public class DxfWriter : IWriter
2023-04-17 08:49:26 +03:00
{
2023-04-19 21:04:13 +03:00
private readonly string file;
private readonly DxfDocument doc;
private Dictionary<Product, double> productDict;
private readonly Block tableBlock;
2023-04-20 09:37:07 +03:00
public DxfWriter()
2023-04-19 21:04:13 +03:00
{
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();
}
2023-04-17 08:49:26 +03:00
public void WriteProducts(Dictionary<Product, double> products)
{
WriteProducts(new[] { (string.Empty, products) });
}
2023-05-04 08:02:57 +03:00
private IEnumerable<EntityObject> WriteRow(int x, int y, Product product, double amount)
{
string measure = product.ProductMeasure switch
{
Measure.Kg => "кг",
Measure.M => "м",
Measure.M2 => "м2",
Measure.P => "шт",
_ => string.Empty
};
return new[]
{
new Text(product.Name, new Vector2(x + 4180, y), 250)
{
Alignment = TextAlignment.MiddleLeft,
WidthFactor = 0.85
},
2023-05-05 08:21:26 +03:00
new Text(product.ProductSku.ToString(), new Vector2(x + 24750, y), 250)
2023-05-04 08:02:57 +03:00
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text("«РЕХАУ»", new Vector2(x + 28750, y), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text(measure, new Vector2(x + 32000, y), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
new Text(amount.ToString(), new Vector2(x + 34000, y), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
}
};
}
2023-04-17 08:49:26 +03:00
public void WriteProducts(IEnumerable<(string, Dictionary<Product, double>)> products)
2023-04-19 21:04:13 +03:00
{
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++)
{
2023-05-04 08:02:57 +03:00
int x = i * 43000;
2023-04-19 21:04:13 +03:00
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++)
{
2023-05-04 08:02:57 +03:00
int y = 25400 - row * 800;
doc.Entities.Add(WriteRow(x, y, pArray[i * 27 + row], productDict[pArray[i * 27 + row]]));
2023-04-19 21:04:13 +03:00
}
insertion.Attributes.AttributeWithTag("Sheet").Value = (i + 1).ToString();
tablePosition += 43000;
doc.Entities.Add(insertion);
}
2023-04-17 08:49:26 +03:00
doc.Save(file);
DxfVersion dxfVersion = DxfDocument.CheckDxfFileVersion(file);
2023-04-19 21:04:13 +03:00
if (dxfVersion < DxfVersion.AutoCad2000)
return;
}
2023-04-17 08:49:26 +03:00
2023-04-19 21:04:13 +03:00
private Block CreateTable()
{
List<EntityObject> entities = new();
2023-04-17 08:49:26 +03:00
2023-04-19 21:04:13 +03:00
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
},
2023-04-21 06:45:18 +03:00
new Text("9", new Vector2(39500, 26200), 250)
{
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.85
},
2023-04-19 21:04:13 +03:00
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);
2023-04-17 08:49:26 +03:00
2023-04-19 21:04:13 +03:00
block.AttributeDefinitions.Add(new AttributeDefinition($"Sheet")
{
Position = new Vector3(41000, 950, 0),
Alignment = TextAlignment.MiddleCenter,
WidthFactor = 0.65,
Height = 300
});
return block;
2023-04-17 08:49:26 +03:00
}
2023-05-04 08:02:57 +03:00
2023-04-17 08:49:26 +03:00
public void Dispose()
{
2023-04-19 21:04:13 +03:00
Process.Start(file);
2023-04-17 08:49:26 +03:00
}
}