MATLAB Code Generating DXF Sinusoidal Curve

This MATLAB script demonstrates how to generate a DNX file containing a sinusoidal curve represented as a polyline with a controlled number of points. All units are in millimeters (mm).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
function generateSinusoidalDXF(length1, numberOfPoints)
% GUIDELINE:
% This function generates a DXF file containing a sinusoidal curve represented
% as a polyline with a controlled number of points. All units are in millimeters (mm).
%
% INPUTS:
% length1 - Length of the x-axis in millimeters (scalar, double).
% numberOfPoints - Number of points to generate for the polyline (scalar, integer).
%
% OUTPUT:
% A DXF file named 'sinusoidal_curve_<numberOfPoints>_points.dxf' is saved in the
% current working directory.
%
% USAGE EXAMPLE:
% generateSinusoidalDXF(100, 200); % Generates a sinusoidal curve with 200 points over a length of 100 mm.
%
% CREATED BY: YX
% DATE: Today

% Close all figures and clear workspace variables (except inputs)
close all;
clc;

% Validate inputs
if nargin < 2
error('Two input arguments (length1 and numberOfPoints) are required.');
end
if ~isnumeric(length1) || ~isscalar(length1) || length1 <= 0
error('length1 must be a positive scalar value in millimeters.');
end
if ~isnumeric(numberOfPoints) || ~isscalar(numberOfPoints) || numberOfPoints <= 0
error('numberOfPoints must be a positive integer value.');
end
numberOfPoints = round(numberOfPoints); % Ensure integer value

% Parameters for the sinusoidal curve (all units in millimeters)
wavelength = 47.6; % Wavelength of the sinusoidal curve in mm
amplitude = 2.76; % Amplitude of the sinusoidal curve in mm
phaseShift = 0.16; % Phase shift of the sinusoidal curve in mm

% Derived parameters
waveNumber = 2 * pi / wavelength; % Wave number in radians/mm

% Generate x-coordinates in millimeters
x = linspace(0, length1, numberOfPoints);

% Generate y-coordinates for the sinusoidal curve in millimeters
y = amplitude * sin(waveNumber * (x + wavelength / 4));

% Combine x and y coordinates into a matrix
coordinates = [x(:), y(:)];

% Specify DXF file name
dxfFileName = sprintf('sinusoidal_curve_%d_points.dxf', numberOfPoints);

% Write coordinates to DXF file
writeDXF(dxfFileName, coordinates);

% Notify user
fprintf('DXF file saved as %s\n', dxfFileName);
end

function writeDXF(filename, coordinates)
% Open file for writing
fileID = fopen(filename, 'w');
if fileID == -1
error('Failed to open file for writing.');
end

% Write DXF Header with units set to millimeters
fprintf(fileID, '0\nSECTION\n2\nHEADER\n');
fprintf(fileID, '9\n$INSUNITS\n70\n4\n'); % 70 = integer value for units, 4 = mm
fprintf(fileID, '0\nENDSEC\n');

% Write TABLES, BLOCKS, and ENTITIES sections
fprintf(fileID, '0\nSECTION\n2\nTABLES\n0\nENDSEC\n');
fprintf(fileID, '0\nSECTION\n2\nBLOCKS\n0\nENDSEC\n');
fprintf(fileID, '0\nSECTION\n2\nENTITIES\n');

% Begin POLYLINE entity
fprintf(fileID, '0\nPOLYLINE\n8\n0\n66\n1\n70\n0\n'); % 70=0 indicates a 2D polyline

% Write each point (coordinates are in millimeters)
for i = 1:size(coordinates, 1)
fprintf(fileID, '0\nVERTEX\n8\n0\n10\n%f\n20\n%f\n', coordinates(i, 1), coordinates(i, 2));
end

% End POLYLINE entity
fprintf(fileID, '0\nSEQEND\n');

% End DXF
fprintf(fileID, '0\nENDSEC\n0\nEOF\n');

% Close file
fclose(fileID);
end


打赏
  • © 2020-2025 Yu Xia
  • Powered by Hexo Theme Ayer
    • PV:
    • UV:

Buy me a cup of coffee~

支付宝
微信