From 53bc81b989e5e7480cc652a04aeb99cd6ad33e13 Mon Sep 17 00:00:00 2001 From: facat Date: Wed, 9 Jun 2021 23:03:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=9F=BA=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E6=97=8B=E8=BD=AC=E5=92=8C=E8=BE=93=E5=87=BAdxf=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: facat --- .gitignore | 5 ++ Program.cs | 22 +++++++ TransmissionGraphic.csproj | 75 ++++++++++++++++++++++ app.config | 3 + src/Graphic/Canvas.cs | 47 ++++++++++++++ src/Graphic/Line.cs | 72 +++++++++++++++++++++ src/Transformation/Rotation.cs | 28 +++++++++ src/Transformation/Transformation.cs | 94 ++++++++++++++++++++++++++++ src/Type3D/Vector3D.cs | 47 ++++++++++++++ 9 files changed, 393 insertions(+) create mode 100644 .gitignore create mode 100644 Program.cs create mode 100644 TransmissionGraphic.csproj create mode 100644 app.config create mode 100644 src/Graphic/Canvas.cs create mode 100644 src/Graphic/Line.cs create mode 100644 src/Transformation/Rotation.cs create mode 100644 src/Transformation/Transformation.cs create mode 100644 src/Type3D/Vector3D.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0ece3af --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +cadlib +bin +MathNet.Numerics.4.15.0 +obj +Properties diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..a5906c4 --- /dev/null +++ b/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using TransmissionGraphic.Graphic; +using TransmissionGraphic.Type3D; + +namespace TransmissionGraphic +{ + class Program + { + static void Main(string[] args) + { + Canvas canvas = new Canvas(); + canvas.init_canvas(); + Line line = new Line(canvas,new TGVector3D(10, 20, 10), new TGVector3D(400, 25, 35), 0.32e-3, 400); + canvas.draw(line.curve()); + canvas.save("abc.dxf"); + + } + } +} diff --git a/TransmissionGraphic.csproj b/TransmissionGraphic.csproj new file mode 100644 index 0000000..7b58ed9 --- /dev/null +++ b/TransmissionGraphic.csproj @@ -0,0 +1,75 @@ + + + + Debug + x86 + 8.0.30703 + 2.0 + {9A62FBFE-70BE-4CAD-A0D5-402024C8FB26} + Exe + Properties + TransmissionGraphic + TransmissionGraphic + v4.0 + + + 512 + + + x86 + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + x86 + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + MathNet.Numerics.4.15.0\lib\net40\MathNet.Numerics.dll + + + + + + + + + + cadlib\WW.dll + + + cadlib\WW.Cad.dll + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app.config b/app.config new file mode 100644 index 0000000..e365603 --- /dev/null +++ b/app.config @@ -0,0 +1,3 @@ + + + diff --git a/src/Graphic/Canvas.cs b/src/Graphic/Canvas.cs new file mode 100644 index 0000000..de01fa6 --- /dev/null +++ b/src/Graphic/Canvas.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using WW.Cad.Model; +using WW.Cad.Model.Entities; +using WW.Cad.IO; +using MathNet.Numerics.LinearAlgebra; + +namespace TransmissionGraphic.Graphic +{ + class Canvas + { + private DxfModel _model; + public Canvas() + { + this._model = null; + } + + public void init_canvas() + { + DxfModel model = new DxfModel(DxfVersion.Dxf13); + this._model = model; + } + + public void draw(Matrix points) + { + DxfModel model=this._model; + DxfPolyline3D polyline = new DxfPolyline3D(); + DxfVertex3D[] vertex = new DxfVertex3D[points.RowCount]; + for (int foo = 0; foo < points.RowCount; foo++) + { + vertex[foo] = new DxfVertex3D(points.At(foo, 0), points.At(foo, 1), points.At(foo, 2)); + } + polyline.Closed = false; + polyline.Vertices.AddRange(vertex); + model.Entities.Add(polyline); + } + + public void save(string filePath) + { + DxfModel model = this._model; + DxfWriter.Write(filePath, model); + + } + } +} diff --git a/src/Graphic/Line.cs b/src/Graphic/Line.cs new file mode 100644 index 0000000..8513956 --- /dev/null +++ b/src/Graphic/Line.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using TransmissionGraphic.Type3D; +using MathNet.Numerics.LinearAlgebra; +using TransmissionGraphic.Transformation; +using MathNet.Numerics; + +namespace TransmissionGraphic.Graphic +{ + class Line + { + private TGVector3D _start_p; + private TGVector3D _end_p; + private double _tension_k; + private int _n_point; + private double _span; + private Matrix _points; + private Rotation _rotation; + private Canvas _canvas; + public Line(Canvas canvas, TGVector3D start_p, TGVector3D end_p, double tension_k, int n_point) + { + this._start_p = start_p; + this._end_p = end_p; + this._tension_k = tension_k; + this._n_point = n_point; + this._span = 0; + this._points = null; + this._rotation = null; + this._canvas = canvas; + + } + + public Matrix curve(bool draw = false) + { + if (this._points != null) + { + return this._points; + } + TGVector3D start_p = this._start_p; + TGVector3D end_p = this._end_p; + double tension_k = this._tension_k; + int n_point = this._n_point; + // 右手坐标系,Z朝上 + TGVector3D line = new TGVector3D(end_p.x, end_p.y, end_p.z) - new TGVector3D(start_p.x, start_p.y, start_p.z); + // 计算与X轴的角度 + TGVector3D xy_project = new TGVector3D(line.x, line.y, line.z); // 投影到xy平面上 + xy_project.z = 0; + double x_abs_angel = xy_project.angle_to(new TGVector3D(1, 0, 0)); + double x_angel = x_abs_angel * Math.Sign(xy_project.y); + double height = end_p.z - start_p.z; + double span = xy_project.abs(); + this._span = span; + Vector span_l = Vector.Build.DenseOfArray(Generate.LinearSpaced(n_point, 0, span)); // 档距 + Vector z_points = start_p.z + span_l * height / span - span_l.PointwiseMultiply(span - span_l)* tension_k / Math.Cos(Math.Atan(height / span)); + double[,] p_points = new double[n_point, 3]; + for (int foo = 0; foo < n_point; foo++) + { + p_points[foo, 0] = span_l[foo] + start_p.x; + p_points[foo, 1] = start_p.y; + p_points[foo, 2] = z_points[foo]; + } + // 绕Z轴旋转 + Rotation rotation = new Rotation(x_angel, new TGVector3D(start_p.x, start_p.y, start_p.z), new TGVector3D(start_p.x, start_p.y, start_p.z + 1)); + + this._rotation = rotation; + Matrix points = rotation.rotate(Matrix.Build.DenseOfArray(p_points)); + return points; + } + } +} diff --git a/src/Transformation/Rotation.cs b/src/Transformation/Rotation.cs new file mode 100644 index 0000000..5332711 --- /dev/null +++ b/src/Transformation/Rotation.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using TransmissionGraphic.Type3D; +using MathNet.Numerics.LinearAlgebra.Double; +using MathNet.Numerics.LinearAlgebra; +//using TransmissionGraphic.Transformation; +namespace TransmissionGraphic.Transformation +{ + class Rotation + { + private Matrix _r_transform; + public Rotation(double angel, TGVector3D axis_start, TGVector3D axis_end) + { + this._r_transform = Transformation.rotation_matrix(angel, axis_start, axis_end); + } + + public Matrix rotate(Matrix point) + { + Matrix r_transform = this._r_transform; + Matrix onesMatrix=Matrix.Build.Dense(point.RowCount,1,1); + Matrix expand_point = point.Append(onesMatrix); + Matrix transformed_points = r_transform * expand_point.Transpose(); + return Matrix.Build.DenseOfRows(transformed_points.EnumerateRows(0, transformed_points.RowCount-1)).Transpose(); + } + } +} diff --git a/src/Transformation/Transformation.cs b/src/Transformation/Transformation.cs new file mode 100644 index 0000000..a4cd45f --- /dev/null +++ b/src/Transformation/Transformation.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using TransmissionGraphic.Type3D; +using MathNet.Numerics.LinearAlgebra; +namespace TransmissionGraphic.Transformation +{ + class Transformation + { + public static Matrix create_rx(TGVector3D unit_axis) + { + double a = unit_axis.x; + double b = unit_axis.y; + double c = unit_axis.z; + double d = Math.Pow((b * b + c * c), 0.5); + Matrix m = Matrix.Build.DenseOfArray(new double[,]{ + {1, 0, 0, 0}, + {0, c / d, -b / d, 0}, + {0, b / d, c / d, 0}, + {0, 0, 0, 1} + }); + return m; + } + + public static Matrix create_ry(TGVector3D unit_axis) + { + double a = unit_axis.x; + double b = unit_axis.y; + double c = unit_axis.z; + double d = Math.Pow((b * b + c * c), 0.5); + Matrix m = Matrix.Build.DenseOfArray(new double[,]{ + {d, 0, -a, 0}, + {0, 1, 0, 0}, + {a, 0, d, 0}, + {0, 0, 0, 1} + }); + return m; + } + + + public static Matrix create_rz(double angel) + { + Matrix m = Matrix.Build.DenseOfArray(new double[,]{ + {Math.Cos(angel), -Math.Sin(angel), 0, 0}, + {Math.Sin(angel), Math.Cos(angel), 0, 0}, + {0, 0, 1, 0}, + {0, 0, 0, 1} + }); + return m; + } + + public static Matrix create_t(TGVector3D axis) + { + double x1, y1, z1; + x1 = axis.x; + y1 = axis.y; + z1 = axis.z; + Matrix m = Matrix.Build.DenseOfArray(new double[,]{ + {1, 0, 0, -x1}, + {0, 1, 0, -y1}, + {0, 0, 1, -z1}, + {0, 0, 0, 1} + }); + return m; + } + + + public static Matrix rotation_matrix(double angel, TGVector3D axis_start, TGVector3D axis_end) + { + TGVector3D axis = axis_end - axis_start; + TGVector3D unit_axis = axis / axis.abs(); + Matrix rz = create_rz(angel); + Matrix rx = create_rx(unit_axis); + Matrix ry = create_ry(unit_axis); + Matrix t = create_t(axis_start); + Matrix t_i = t.Inverse() ; + Matrix rx_i = rx.Inverse() ; + Matrix ry_i = ry.Inverse() ; + Matrix r_transform = t_i * rx_i * ry_i * rz * ry * rx * t; + return r_transform; + } + + public static Matrix rotation(double angel, TGVector3D axis_start, TGVector3D axis_end, Matrix points) + { + + Rotation _rotation = new Rotation(angel, axis_start, axis_end); + return _rotation.rotate(points); + } + + + + } +} diff --git a/src/Type3D/Vector3D.cs b/src/Type3D/Vector3D.cs new file mode 100644 index 0000000..de9f8a3 --- /dev/null +++ b/src/Type3D/Vector3D.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace TransmissionGraphic.Type3D +{ + public class TGVector3D + { + public double x; + public double y; + public double z; + public TGVector3D(double x, double y, double z) + { + this.x = x; + this.y = y; + this.z = z; + } + + + public static TGVector3D operator- (TGVector3D me, TGVector3D other) + { + return new TGVector3D(me.x - other.x, me.y - other.y, me.z - other.z); + } + + public static TGVector3D operator /(TGVector3D me, double other) + { + return new TGVector3D(me.x / other, me.y / other, me.z / other); + } + + + public double abs() + { + return Math.Pow((this.x *this.x + this.y*this.y + this.z*this.z),.5); + } + + + // 计算两向量间夹角 + public double angle_to(TGVector3D other) + { + return Math.Acos( + Math.Abs(this.x * other.x + this.y * other.y + this.z * other.z) / this.abs() / other.abs() + ); + } + + } +}