Add import of polygon from dxf
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services.Exports.Factories
|
||||
{
|
||||
public enum FileInputDataType
|
||||
{
|
||||
Csv,
|
||||
Png,
|
||||
Dxf
|
||||
}
|
||||
public static class FileInputDataFactory
|
||||
{
|
||||
public static FileIOInputData GetFileIOInputData(FileInputDataType dataType)
|
||||
{
|
||||
if (dataType == FileInputDataType.Csv)
|
||||
{
|
||||
return new FileIOInputData
|
||||
{
|
||||
Filter = "csv |*.csv",
|
||||
Title = "Save in csv File"
|
||||
};
|
||||
}
|
||||
else if (dataType == FileInputDataType.Png)
|
||||
{
|
||||
return new FileIOInputData
|
||||
{
|
||||
Filter = "png |*.png",
|
||||
Title = "Save in *.png File"
|
||||
};
|
||||
}
|
||||
else if (dataType == FileInputDataType.Dxf)
|
||||
{
|
||||
return new FileIOInputData
|
||||
{
|
||||
Filter = "dxf |*.dxf",
|
||||
Title = "Save in *.dxf File"
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new StructureHelperException(ErrorStrings.ObjectTypeIsUnknownObj(dataType));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace StructureHelperCommon.Services.Exports
|
||||
{
|
||||
public class FileIOInputData : IFileIOnputData
|
||||
{
|
||||
public string FileName { get; set; } = "New file";
|
||||
public string Filter { get; set; }
|
||||
public string Title { get; set; }
|
||||
}
|
||||
}
|
||||
54
StructureHelperCommon/Services/Exports/GetDxfLayerLogic.cs
Normal file
54
StructureHelperCommon/Services/Exports/GetDxfLayerLogic.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using netDxf;
|
||||
using netDxf.Tables;
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services.Exports
|
||||
{
|
||||
public class GetDxfLayerLogic : IGetDxfLayerLogic
|
||||
{
|
||||
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";
|
||||
public 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;
|
||||
}
|
||||
public 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);
|
||||
}
|
||||
}
|
||||
|
||||
public 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
7
StructureHelperCommon/Services/Exports/IExportLogic.cs
Normal file
7
StructureHelperCommon/Services/Exports/IExportLogic.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace StructureHelperCommon.Services.Exports
|
||||
{
|
||||
public interface IExportLogic
|
||||
{
|
||||
void Export();
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,8 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services.Exports
|
||||
{
|
||||
public interface IExportResultLogic
|
||||
public interface IExportToFileLogic : IExportLogic
|
||||
{
|
||||
string FileName { get; set; }
|
||||
void Export();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace StructureHelperCommon.Services.Exports
|
||||
{
|
||||
public interface IFileIOnputData
|
||||
{
|
||||
string FileName { get; set; }
|
||||
string Filter { get; set; }
|
||||
string Title { get; set; }
|
||||
}
|
||||
}
|
||||
12
StructureHelperCommon/Services/Exports/IGetDxfLayerLogic.cs
Normal file
12
StructureHelperCommon/Services/Exports/IGetDxfLayerLogic.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using netDxf;
|
||||
using netDxf.Tables;
|
||||
|
||||
namespace StructureHelperCommon.Services.Exports
|
||||
{
|
||||
public interface IGetDxfLayerLogic
|
||||
{
|
||||
AciColor GetLayerColor(LayerNames layerName);
|
||||
string GetLayerName(LayerNames layerName);
|
||||
Layer GetOrCreateLayer(DxfDocument dxf, LayerNames layerName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services.Exports
|
||||
{
|
||||
public interface IImportFromFileLogic : IImportLogic
|
||||
{
|
||||
string FileName { get; set; }
|
||||
}
|
||||
}
|
||||
13
StructureHelperCommon/Services/Exports/IImportLogic.cs
Normal file
13
StructureHelperCommon/Services/Exports/IImportLogic.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services.Exports
|
||||
{
|
||||
public interface IImportLogic
|
||||
{
|
||||
void Import();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using netDxf;
|
||||
using netDxf.Entities;
|
||||
using netDxf.Header;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services.Exports
|
||||
{
|
||||
public class ShapesAllImportFromDxfLogic : IImportFromFileLogic
|
||||
{
|
||||
public string FileName { get; set; }
|
||||
public List<EntityObject> Entities { get; set; } = [];
|
||||
public void Import()
|
||||
{
|
||||
// this check is optional but recommended before loading a DXF file
|
||||
DxfVersion dxfVersion = DxfDocument.CheckDxfFileVersion(FileName);
|
||||
// netDxf is only compatible with AutoCad2000 and higher DXF versions
|
||||
if (dxfVersion < DxfVersion.AutoCad2000) return;
|
||||
// load file
|
||||
DxfDocument dxf = DxfDocument.Load(FileName);
|
||||
Entities.Clear();
|
||||
if (dxf != null)
|
||||
{
|
||||
Entities.AddRange(dxf.Entities.All);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,7 @@ 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
|
||||
{
|
||||
@@ -17,14 +13,12 @@ namespace StructureHelperCommon.Services.Exports
|
||||
StructuralOpenings,
|
||||
StructuralRebars
|
||||
}
|
||||
public class ShapesExportToDxfLogic : IExportResultLogic
|
||||
public class ShapesExportToDxfLogic : IExportToFileLogic
|
||||
{
|
||||
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 const double metresToMillimeters = 1000.0;
|
||||
private readonly List<(IShape shape, LayerNames layerName)> shapes = [];
|
||||
private IGetDxfLayerLogic layerLogic = new GetDxfLayerLogic();
|
||||
|
||||
public ShapesExportToDxfLogic(List<(IShape shape, LayerNames layerName)> shapes)
|
||||
{
|
||||
@@ -45,19 +39,12 @@ namespace StructureHelperCommon.Services.Exports
|
||||
{
|
||||
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);
|
||||
Layer layer = layerLogic.GetOrCreateLayer(dxf, shape.layerName);
|
||||
ProcessShape(dxf, shape.shape, layer);
|
||||
}
|
||||
|
||||
@@ -78,7 +65,7 @@ namespace StructureHelperCommon.Services.Exports
|
||||
List<Polyline2DVertex> polylineVertices = [];
|
||||
foreach (var item in linePolygon.Vertices)
|
||||
{
|
||||
Polyline2DVertex vertex = new Polyline2DVertex(item.Point.X, item.Point.Y);
|
||||
Polyline2DVertex vertex = new Polyline2DVertex(item.Point.X * metresToMillimeters, item.Point.Y * metresToMillimeters);
|
||||
polylineVertices.Add(vertex);
|
||||
}
|
||||
Polyline2D polyline2D = new Polyline2D(polylineVertices)
|
||||
@@ -88,42 +75,5 @@ namespace StructureHelperCommon.Services.Exports
|
||||
};
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
using netDxf.Entities;
|
||||
using System.Linq;
|
||||
|
||||
namespace StructureHelperCommon.Services.Exports
|
||||
{
|
||||
public class SinglePolyline2DImportFromDxfLogic : IImportFromFileLogic
|
||||
{
|
||||
public string FileName { get; set; }
|
||||
public Polyline2D Polyline2D { get; private set; }
|
||||
public void Import()
|
||||
{
|
||||
var importLogic = new ShapesAllImportFromDxfLogic() { FileName = FileName};
|
||||
importLogic.Import();
|
||||
var entities = importLogic.Entities;
|
||||
Polyline2D = entities.Single(x => x is Polyline2D) as Polyline2D;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user