diff --git a/StructureHelper/Services/Exports/ExportToFileService.cs b/StructureHelper/Services/Exports/ExportToFileService.cs index d2d4e2c..20d8138 100644 --- a/StructureHelper/Services/Exports/ExportToFileService.cs +++ b/StructureHelper/Services/Exports/ExportToFileService.cs @@ -1,6 +1,7 @@ using StructureHelper.Windows.Errors; using StructureHelper.Windows.ViewModels.Errors; using StructureHelperCommon.Infrastructures.Exceptions; +using StructureHelperCommon.Services.Exports; using StructureHelperLogics.NdmCalculations.Analyses; using StructureHelperLogics.NdmCalculations.Analyses.ByForces; using System; diff --git a/StructureHelper/Windows/Shapes/PolygonShapeViewModel.cs b/StructureHelper/Windows/Shapes/PolygonShapeViewModel.cs index 75c7ee6..838fcfd 100644 --- a/StructureHelper/Windows/Shapes/PolygonShapeViewModel.cs +++ b/StructureHelper/Windows/Shapes/PolygonShapeViewModel.cs @@ -1,14 +1,18 @@ using netDxf; using netDxf.Entities; using netDxf.Header; +using netDxf.Tables; using StructureHelper.Infrastructure; using StructureHelper.Infrastructure.UI.GraphicalPrimitives; +using StructureHelper.Services.Exports; +using StructureHelper.Windows.BeamShears; using StructureHelper.Windows.Shapes.Logics; using StructureHelper.Windows.UserControls.WorkPlanes; using StructureHelper.Windows.ViewModels; using StructureHelperCommon.Infrastructures.Exceptions; using StructureHelperCommon.Infrastructures.Interfaces; using StructureHelperCommon.Models.Shapes; +using StructureHelperCommon.Services.Exports; using StructureHelperLogics.Models.BeamShears; using System; using System.Collections.Generic; @@ -21,6 +25,7 @@ namespace StructureHelper.Windows.Shapes public class PolygonShapeViewModel : OkCancelViewModelBase { private const int minVertexCount = 3; + private readonly IPoint2D absoluteCenter; private readonly IPoint2D localCenter; private readonly ILinePolygonShape polygonShape; @@ -76,24 +81,14 @@ namespace StructureHelper.Windows.Shapes private void ExportToDxf(object commandParameter) { - // your DXF file name - string file = "sample.dxf"; - - // create a new document, by default it will create an AutoCad2000 DXF version - DxfDocument doc = new DxfDocument(); - // an entity - List polylineVertices = []; - foreach (var item in vertices) + var inputData = new ExportToFileInputData { - Polyline2DVertex vertex = new Polyline2DVertex(item.Point.X, item.Point.Y); - polylineVertices.Add(vertex); - } - Polyline2D polyline2D = new Polyline2D(polylineVertices) { IsClosed = true}; - //polyline2D.Layer = - // add your entities here - doc.Entities.Add(polyline2D); - // save to file - doc.Save(file); + Filter = "dxf |*.dxf", + Title = "Save in *.dxf File" + }; + var logic = new ShapesExportToDxfLogic(polygonShape, LayerNames.StructiralPrimitives); + var exportService = new ExportToFileService(inputData, logic); + exportService.Export(); } private void FlipHorizontal(object obj) diff --git a/StructureHelperLogics/NdmCalculations/Analyses/IExportResultLogic.cs b/StructureHelperCommon/Services/Exports/IExportResultLogic.cs similarity index 80% rename from StructureHelperLogics/NdmCalculations/Analyses/IExportResultLogic.cs rename to StructureHelperCommon/Services/Exports/IExportResultLogic.cs index 9c8a10a..0987025 100644 --- a/StructureHelperLogics/NdmCalculations/Analyses/IExportResultLogic.cs +++ b/StructureHelperCommon/Services/Exports/IExportResultLogic.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace StructureHelperLogics.NdmCalculations.Analyses +namespace StructureHelperCommon.Services.Exports { public interface IExportResultLogic { diff --git a/StructureHelperCommon/Services/Exports/ShapesExportToDxfLogic.cs b/StructureHelperCommon/Services/Exports/ShapesExportToDxfLogic.cs new file mode 100644 index 0000000..13144ba --- /dev/null +++ b/StructureHelperCommon/Services/Exports/ShapesExportToDxfLogic.cs @@ -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 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); + } + } + } +} diff --git a/StructureHelperCommon/StructureHelperCommon.csproj b/StructureHelperCommon/StructureHelperCommon.csproj index f8c9854..94c46ef 100644 --- a/StructureHelperCommon/StructureHelperCommon.csproj +++ b/StructureHelperCommon/StructureHelperCommon.csproj @@ -11,6 +11,7 @@ + diff --git a/StructureHelperLogics/NdmCalculations/Analyses/ExportFrameWorkElementLogic.cs b/StructureHelperLogics/NdmCalculations/Analyses/ExportFrameWorkElementLogic.cs index ad9c671..4a7000d 100644 --- a/StructureHelperLogics/NdmCalculations/Analyses/ExportFrameWorkElementLogic.cs +++ b/StructureHelperLogics/NdmCalculations/Analyses/ExportFrameWorkElementLogic.cs @@ -1,12 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using StructureHelperCommon.Services.Exports; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; -using System.Xml.Linq; namespace StructureHelperLogics.NdmCalculations.Analyses { diff --git a/StructureHelperLogics/NdmCalculations/Analyses/ExportGeometryResultToCSVLogic.cs b/StructureHelperLogics/NdmCalculations/Analyses/ExportGeometryResultToCSVLogic.cs index d701a1e..e666771 100644 --- a/StructureHelperLogics/NdmCalculations/Analyses/ExportGeometryResultToCSVLogic.cs +++ b/StructureHelperLogics/NdmCalculations/Analyses/ExportGeometryResultToCSVLogic.cs @@ -1,10 +1,6 @@ -using StructureHelperLogics.NdmCalculations.Analyses.ByForces; +using StructureHelperCommon.Services.Exports; using StructureHelperLogics.NdmCalculations.Analyses.Geometry; -using System; -using System.Collections.Generic; -using System.Linq; using System.Text; -using System.Threading.Tasks; namespace StructureHelperLogics.NdmCalculations.Analyses { diff --git a/StructureHelperLogics/NdmCalculations/Analyses/ExportResultToBitmapLogic.cs b/StructureHelperLogics/NdmCalculations/Analyses/ExportResultToBitmapLogic.cs index 6e6da3d..cbb3c35 100644 --- a/StructureHelperLogics/NdmCalculations/Analyses/ExportResultToBitmapLogic.cs +++ b/StructureHelperLogics/NdmCalculations/Analyses/ExportResultToBitmapLogic.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using StructureHelperCommon.Services.Exports; using System.Windows.Media.Imaging; namespace StructureHelperLogics.NdmCalculations.Analyses diff --git a/StructureHelperLogics/NdmCalculations/Analyses/ExportToCSVLogicBase.cs b/StructureHelperLogics/NdmCalculations/Analyses/ExportToCSVLogicBase.cs index 879c362..65c7639 100644 --- a/StructureHelperLogics/NdmCalculations/Analyses/ExportToCSVLogicBase.cs +++ b/StructureHelperLogics/NdmCalculations/Analyses/ExportToCSVLogicBase.cs @@ -1,4 +1,5 @@ -using System.Text; +using StructureHelperCommon.Services.Exports; +using System.Text; namespace StructureHelperLogics.NdmCalculations.Analyses {