MATLAB Code Generating DXF Sinusoidal Curve

This MATLAB script demonstrates how to generate a DXF 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
99
100
101
102
103
104
105
106
107
108
function generateSinusoidalDXF(length1, numberOfPoints, wavelength, amplitude)
% 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).
% wavelength - Wavelength of the curve in millimeters (scalar, double).
% amplitude - Amplitude of the curve in millimeters (scalar, double).
%
% OUTPUT:
% A DXF file with a descriptive name is saved in the current working directory.
%
% USAGE EXAMPLE:
% generateSinusoidalDXF(100, 200, 47.6, 2.76);
%
% MODIFIED BY: YX
% DATE: 27/Jan/2026

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

% --- Validate inputs ---
if nargin < 4
error('Four input arguments required: length1, numberOfPoints, wavelength, and amplitude.');
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

if ~isnumeric(wavelength) || ~isscalar(wavelength) || wavelength <= 0
error('wavelength must be a positive scalar value in millimeters.');
end

if ~isnumeric(amplitude) || ~isscalar(amplitude)
error('amplitude must be a numeric scalar value in millimeters.');
end

numberOfPoints = round(numberOfPoints); % Ensure integer value

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

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

% Generate y-coordinates for the sinusoidal curve in millimeters
% Note: Kept the logic (x + wavelength/4) from original code, which acts as a phase shift
y = amplitude * sin(waveNumber * (x + wavelength / 4));

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

% --- Specify DXF file name ---
% Filename now includes parameters to distinguish files
dxfFileName = sprintf('sinusoidal_curve_%dpts_W%.1f_A%.2f.dxf', ...
numberOfPoints, wavelength, amplitude);

% --- 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-2026 Yu Xia
  • Powered by Hexo Theme Ayer
    • PV:
    • UV:

Buy me a cup of coffee~

支付宝
微信