Add logic for export to DXF

This commit is contained in:
Evgeny Redikultsev
2025-10-29 22:07:41 +05:00
parent c84dc47134
commit 693e0648c4
9 changed files with 149 additions and 35 deletions

View File

@@ -1,6 +1,7 @@
using StructureHelper.Windows.Errors; using StructureHelper.Windows.Errors;
using StructureHelper.Windows.ViewModels.Errors; using StructureHelper.Windows.ViewModels.Errors;
using StructureHelperCommon.Infrastructures.Exceptions; using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Services.Exports;
using StructureHelperLogics.NdmCalculations.Analyses; using StructureHelperLogics.NdmCalculations.Analyses;
using StructureHelperLogics.NdmCalculations.Analyses.ByForces; using StructureHelperLogics.NdmCalculations.Analyses.ByForces;
using System; using System;

View File

@@ -1,14 +1,18 @@
using netDxf; using netDxf;
using netDxf.Entities; using netDxf.Entities;
using netDxf.Header; using netDxf.Header;
using netDxf.Tables;
using StructureHelper.Infrastructure; using StructureHelper.Infrastructure;
using StructureHelper.Infrastructure.UI.GraphicalPrimitives; using StructureHelper.Infrastructure.UI.GraphicalPrimitives;
using StructureHelper.Services.Exports;
using StructureHelper.Windows.BeamShears;
using StructureHelper.Windows.Shapes.Logics; using StructureHelper.Windows.Shapes.Logics;
using StructureHelper.Windows.UserControls.WorkPlanes; using StructureHelper.Windows.UserControls.WorkPlanes;
using StructureHelper.Windows.ViewModels; using StructureHelper.Windows.ViewModels;
using StructureHelperCommon.Infrastructures.Exceptions; using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Infrastructures.Interfaces; using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Models.Shapes; using StructureHelperCommon.Models.Shapes;
using StructureHelperCommon.Services.Exports;
using StructureHelperLogics.Models.BeamShears; using StructureHelperLogics.Models.BeamShears;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -21,6 +25,7 @@ namespace StructureHelper.Windows.Shapes
public class PolygonShapeViewModel : OkCancelViewModelBase public class PolygonShapeViewModel : OkCancelViewModelBase
{ {
private const int minVertexCount = 3; private const int minVertexCount = 3;
private readonly IPoint2D absoluteCenter; private readonly IPoint2D absoluteCenter;
private readonly IPoint2D localCenter; private readonly IPoint2D localCenter;
private readonly ILinePolygonShape polygonShape; private readonly ILinePolygonShape polygonShape;
@@ -76,24 +81,14 @@ namespace StructureHelper.Windows.Shapes
private void ExportToDxf(object commandParameter) private void ExportToDxf(object commandParameter)
{ {
// your DXF file name var inputData = new ExportToFileInputData
string file = "sample.dxf";
// create a new document, by default it will create an AutoCad2000 DXF version
DxfDocument doc = new DxfDocument();
// an entity
List<Polyline2DVertex> polylineVertices = [];
foreach (var item in vertices)
{ {
Polyline2DVertex vertex = new Polyline2DVertex(item.Point.X, item.Point.Y); Filter = "dxf |*.dxf",
polylineVertices.Add(vertex); Title = "Save in *.dxf File"
} };
Polyline2D polyline2D = new Polyline2D(polylineVertices) { IsClosed = true}; var logic = new ShapesExportToDxfLogic(polygonShape, LayerNames.StructiralPrimitives);
//polyline2D.Layer = var exportService = new ExportToFileService(inputData, logic);
// add your entities here exportService.Export();
doc.Entities.Add(polyline2D);
// save to file
doc.Save(file);
} }
private void FlipHorizontal(object obj) private void FlipHorizontal(object obj)

View File

@@ -4,7 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StructureHelperLogics.NdmCalculations.Analyses namespace StructureHelperCommon.Services.Exports
{ {
public interface IExportResultLogic public interface IExportResultLogic
{ {

View File

@@ -0,0 +1,129 @@
using netDxf;
using netDxf.Entities;
using netDxf.Tables;
using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Models.Shapes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Services.Exports
{
public enum LayerNames
{
StructiralPrimitives,
StructuralOpenings,
StructuralRebars
}
public class ShapesExportToDxfLogic : IExportResultLogic
{
private const string PrimitivesLayer = "STR-PRM";
private const string OpeningsLayer = "STR-OPN";
private const string RebarLayer = "STR-REB";
private const string LayerNameIsUnKnown = ": Layer name is unknown";
private const string ShapeTypeIsUnknown = ": Shape is unknown";
private readonly List<(IShape shape, LayerNames layerName)> shapes = [];
public ShapesExportToDxfLogic(List<(IShape shape, LayerNames layerName)> shapes)
{
this.shapes.AddRange(shapes);
}
public ShapesExportToDxfLogic(IShape shape, LayerNames layerName)
{
shapes.Add((shape, layerName));
}
public string FileName { get; set; }
public void Export()
{
DxfDocument dxf = new DxfDocument();
foreach (var shape in shapes)
{
ProcessShape(dxf, shape);
}
Layer primitiveLayer = new Layer(PrimitivesLayer)
{
Color = AciColor.Blue
};
dxf.Layers.Add(primitiveLayer);
// save to file
dxf.Save(FileName);
}
private void ProcessShape(DxfDocument dxf, (IShape shape, LayerNames layerName) shape)
{
Layer layer = GetOrCreateLayer(dxf, shape.layerName);
ProcessShape(dxf, shape.shape, layer);
}
private void ProcessShape(DxfDocument dxf, IShape shape, Layer layer)
{
if (shape is ILinePolygonShape linePolygon)
{
ProcessLinePolygon(dxf, linePolygon, layer);
}
else
{
throw new StructureHelperException(ErrorStrings.ObjectTypeIsUnknownObj(shape) + ShapeTypeIsUnknown);
}
}
private void ProcessLinePolygon(DxfDocument dxf, ILinePolygonShape linePolygon, Layer layer)
{
List<Polyline2DVertex> polylineVertices = [];
foreach (var item in linePolygon.Vertices)
{
Polyline2DVertex vertex = new Polyline2DVertex(item.Point.X, item.Point.Y);
polylineVertices.Add(vertex);
}
Polyline2D polyline2D = new Polyline2D(polylineVertices)
{
Layer = layer,
IsClosed = linePolygon.IsClosed,
};
dxf.Entities.Add(polyline2D);
}
private Layer GetOrCreateLayer(DxfDocument dxf, LayerNames layerName)
{
string newLayerName = GetLayerName(layerName);
Layer newLayer = dxf.Layers.Contains("newLayerName")
? dxf.Layers["newLayerName"]
: new Layer("newLayerName")
{
Color = GetLayerColor(layerName)
};
if (!dxf.Layers.Contains(newLayerName))
dxf.Layers.Add(newLayer);
return newLayer;
}
private string GetLayerName(LayerNames layerName)
{
if (layerName == LayerNames.StructiralPrimitives) { return PrimitivesLayer; }
else if (layerName == LayerNames.StructuralOpenings) { return OpeningsLayer; }
else if (layerName == LayerNames.StructuralRebars) { return RebarLayer; }
else
{
throw new StructureHelperException(ErrorStrings.ObjectTypeIsUnknownObj(layerName) + LayerNameIsUnKnown);
}
}
private AciColor GetLayerColor(LayerNames layerName)
{
if (layerName == LayerNames.StructiralPrimitives) { return AciColor.Blue; }
else if (layerName == LayerNames.StructuralOpenings) { return AciColor.DarkGray; }
else if (layerName == LayerNames.StructuralRebars) { return AciColor.Magenta; }
else
{
throw new StructureHelperException(ErrorStrings.ObjectTypeIsUnknownObj(layerName) + LayerNameIsUnKnown);
}
}
}
}

View File

@@ -11,6 +11,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="ExcelDataReader" Version="3.8.0" /> <PackageReference Include="ExcelDataReader" Version="3.8.0" />
<PackageReference Include="netDxf" Version="2023.11.10" />
<PackageReference Include="NLog" Version="6.0.5" /> <PackageReference Include="NLog" Version="6.0.5" />
</ItemGroup> </ItemGroup>

View File

@@ -1,12 +1,7 @@
using System; using StructureHelperCommon.Services.Exports;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace StructureHelperLogics.NdmCalculations.Analyses namespace StructureHelperLogics.NdmCalculations.Analyses
{ {

View File

@@ -1,10 +1,6 @@
using StructureHelperLogics.NdmCalculations.Analyses.ByForces; using StructureHelperCommon.Services.Exports;
using StructureHelperLogics.NdmCalculations.Analyses.Geometry; using StructureHelperLogics.NdmCalculations.Analyses.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;
namespace StructureHelperLogics.NdmCalculations.Analyses namespace StructureHelperLogics.NdmCalculations.Analyses
{ {

View File

@@ -1,8 +1,4 @@
using System; using StructureHelperCommon.Services.Exports;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
namespace StructureHelperLogics.NdmCalculations.Analyses namespace StructureHelperLogics.NdmCalculations.Analyses

View File

@@ -1,4 +1,5 @@
using System.Text; using StructureHelperCommon.Services.Exports;
using System.Text;
namespace StructureHelperLogics.NdmCalculations.Analyses namespace StructureHelperLogics.NdmCalculations.Analyses
{ {