2024-12-25 19:14:10 +03:00
program gcodegenerator_v2;
uses
SysUtils, StrUtils, Math;
type
TPoint = record
X, Y: double ;
end ;
type
TStatorParams = record
BaseDiameter: Double ;
BaseRadius: Double ;
NumberOfRays: Integer ;
RayShape: string ;
RayDiameter: Double ;
RayWidth: Double ;
RayHeight: Double ;
RayLength: Double ;
RayTopShape: string ;
RayTopDiameter: Double ;
RayTopWidth: Double ;
RayTopHeight: Double ;
RayCenterOffset: Double ;
WireDiameter: Double ;
NeedleDiameter: Double ;
PathClearance: Double ;
WorkSpeed : integer ;
end ;
var
InputFileName, OutputFileName: string ;
StatorParams: TStatorParams;
InFile, OutFile: TextFile ;
Line: string ;
CoilRadius, CoilWidth, CoilHeight : double ;
CurrentZ: double ;
coords: TPoint;
normalizecoords: TPoint;
i, j, k: Integer ;
CoilLength : double ;
CurrentCoilTurns, CoilTurnsSum : Integer ;
AngleBetweenRays : double ;
RequiredSpacing: Double ;
LayerNumber : Integer ;
MaxLayers : Integer ;
angle : double ;
MaxDepth, MaxPath : double ;
MoveForward: boolean ;
function ParseLine( Line: string ) : Boolean ;
var
Parts: array of string ;
Value: string ;
TrimmedLine: string ;
begin
TrimmedLine : = Trim( Line) ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
if Length( TrimmedLine) = 0 then
begin
exit( true ) ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> - <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
end
else if TrimmedLine[ 1 ] in [ ';' , '#' ] then
begin
exit( true ) ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> - <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
end ;
Parts : = SplitString( TrimmedLine, '=' ) ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> TrimmedLine
Result : = Length( Parts) = 2 ;
if Result then
begin
Value : = LowerCase( Trim( Parts[ 1 ] ) ) ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
case Trim( Parts[ 0 ] ) of
'base_dia' : begin
StatorParams. BaseDiameter : = StrToFloat( Value) ;
writeln( 'Base Diameter: ' , StatorParams. BaseDiameter: 8 : 2 ) ;
writeln( ) ;
StatorParams. BaseRadius : = StatorParams. BaseDiameter/ 2 ;
writeln( 'Base Radius: ' , StatorParams. BaseRadius: 8 : 2 ) ;
writeln( ) ;
end ;
'num_rays' : begin
StatorParams. NumberOfRays : = StrToInt( Value) ;
writeln( 'Number of Rays: ' , StatorParams. NumberOfRays) ;
AngleBetweenRays : = 3 6 0 / StatorParams. NumberOfRays;
writeln( 'Angle Between Rays: ' , AngleBetweenRays) ;
writeln( ) ;
end ;
'ray_shape' : begin
if Pos( Value, 'circle, rect, superellipse' ) = 0 then
raise Exception. Create( 'Invalid value for ray_shape: ' + Value) ;
StatorParams. RayShape : = Value;
writeln( 'Ray Shape: ' , StatorParams. RayShape) ;
writeln( ) ;
end ;
'ray_dia' : begin
StatorParams. RayDiameter : = StrToFloat( Value) ;
writeln( 'Ray Diameter: ' , StatorParams. RayDiameter: 8 : 2 ) ;
writeln( ) ;
end ;
'ray_w' : begin
StatorParams. RayWidth : = StrToFloat( Value) ;
writeln( 'Ray Width: ' , StatorParams. RayWidth: 8 : 2 ) ;
writeln( ) ;
end ;
'ray_h' : begin
StatorParams. RayHeight : = StrToFloat( Value) ;
writeln( 'Ray Height: ' , StatorParams. RayHeight: 8 : 2 ) ;
writeln( ) ;
end ;
'ray_len' : begin
StatorParams. RayLength : = StrToFloat( Value) ;
writeln( 'Ray Length: ' , StatorParams. RayLength: 8 : 2 ) ;
writeln( ) ;
end ;
'raytop_shape' : begin
if Pos( Value, 'circle, rect, superellipse' ) = 0 then
raise Exception. Create( 'Invalid value for ray_top: ' + Value) ;
StatorParams. RayTopShape : = Value;
writeln( 'Ray Top Shape: ' , StatorParams. RayTopShape) ;
end ;
'raytop_dia' : begin
StatorParams. RayTopDiameter : = StrToFloat( Value) ;
writeln( 'Ray Top Diameter: ' , StatorParams. RayTopDiameter: 8 : 2 ) ;
writeln( ) ;
end ;
'raytop_w' : begin
StatorParams. RayTopWidth : = StrToFloat( Value) ;
writeln( 'Ray Top Width: ' , StatorParams. RayTopWidth: 8 : 2 ) ;
writeln( ) ;
end ;
'raytop_h' : begin
StatorParams. RayTopHeight : = StrToFloat( Value) ;
writeln( 'Ray Top Height: ' , StatorParams. RayTopHeight: 8 : 2 ) ;
writeln( ) ;
end ;
'ray_offset' : begin
StatorParams. RayCenterOffset : = StrToFloat( Value) ;
writeln( 'Ray Center Offset: ' , StatorParams. RayCenterOffset: 8 : 2 ) ;
writeln( ) ;
end ;
'wire_diameter' : begin
StatorParams. WireDiameter : = StrToFloat( Value) ;
writeln( 'Wire Diameter: ' , StatorParams. WireDiameter: 8 : 2 ) ;
writeln( ) ;
end ;
'needle_diameter' : begin
StatorParams. NeedleDiameter : = StrToFloat( Value) ;
writeln( 'Needle Diameter: ' , StatorParams. NeedleDiameter: 8 : 2 ) ;
writeln( ) ;
end ;
'path_clearance' : begin
StatorParams. PathClearance : = StrToFloat( Value) ;
writeln( 'Path Clearance: ' , StatorParams. PathClearance: 8 : 2 ) ;
writeln( ) ;
end ;
'work_speed' : begin
StatorParams. WorkSpeed : = StrToInt( Value) ;
writeln( 'Work Speed: ' , StatorParams. WorkSpeed) ;
writeln( ) ;
end ;
else
Result : = False ;
end ;
if not Result then
begin
writeln( 'Error: Unknown parameter: ' , Parts[ 0 ] ) ;
exit;
end ;
end ;
end ;
procedure ReadInputFile( ) ;
Begin
// **Opening the input file**
AssignFile( InFile, InputFileName) ;
try
Reset( InFile) ; // **This line opens the file for reading**
while not EOF( InFile) do
begin
ReadLn( InFile, Line) ;
if Length( Line) > 0 then
if not ParseLine( Line) then
writeln( 'Error: Invalid line: ' , Line) ;
end ;
except
on E: Exception do
begin
writeln( 'Error opening or reading input file: ' , E. Message ) ;
Halt( 1 ) ;
end ;
end ;
CloseFile( InFile) ;
end ;
function CircleCoordinates( diameter, angleDegrees: Double ) : TPoint;
var
radius: Double ;
angleRadians: Double ;
begin
radius : = diameter / 2 ;
// angleRadians := -1*angleDegrees * PI / 180; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
angleRadians : = - 1 * DegToRad( angleDegrees) ;
Result . X : = radius * Cos( angleRadians) ;
Result . Y : = radius * Sin( angleRadians) ;
end ;
function CalculateAngle( opposite, adjacent: Double ) : Double ;
begin
if adjacent = 0 then
raise Exception. Create( 'Adjacent side cannot be zero' ) ;
// CalculateAngle := ArcTan(opposite / adjacent) * 180 / PI;
CalculateAngle : = RadToDeg( ArcTan( opposite / adjacent) ) ;
end ;
function NormalizeZ( radius: Real ; thetaDeg: Real ) : Real ;
var
thetaRad, alphaRad, xKas, yKas, mTang, bTang: Real ;
begin
// 1. <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <20> <> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> .
thetaRad : = DegToRad( thetaDeg) ;
// 2. <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> (<28> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> 0,0).
xKas : = radius * Cos( thetaRad) ;
yKas : = radius * Sin( thetaRad) ;
// 3. <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> , <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> -<2D> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> (<28> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ).
alphaRad : = thetaRad + PI / 2 ;
// 4. <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> .
mTang : = Tan( alphaRad) ;
// 5. <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> (y = mTang * x + bTang).
bTang : = yKas - mTang * xKas;
// 6. <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> X <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <20> <> <EFBFBD> <EFBFBD> X (y = 0).
// 0 = mTang * x + bTang
// x = -bTang / mTang
if mTang = 0 then
begin
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> X - <20> <> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> . <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> NaN.
NormalizeZ : = NaN;
end
else
begin
NormalizeZ : = - bTang / mTang;
end ;
end ;
function DepthCheck( thickness, lineLength, angleDeg: double ) : double ;
var
xIntersection, distanceToIntersection, radius: double ;
begin
xIntersection : = ( thickness/ 2 + thickness/ 2 * cos( DegToRad( angleDeg) ) ) / sin( DegToRad( angleDeg) ) ;
distanceToIntersection : = sqrt( sqr( xIntersection) + sqr( 1.2 ) ) ;
radius : = lineLength / ( 2 * tan( DegToRad( angleDeg) / 2 ) ) ;
DepthCheck : = distanceToIntersection + radius;
end ;
function CalculateMaxPath( length , width, angleDegrees: double ) : double ;
var
topLeftX, topLeftY, rotatedX, rotatedY, angleRadians, distance: double ;
begin
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
topLeftX : = - length ;
topLeftY : = width / 2 ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <20> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> (<28> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> )
angleRadians : = degToRad( - angleDegrees) ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
rotatedX : = - length * cos( angleRadians) - ( - width / 2 ) * sin( angleRadians) ;
rotatedY : = - length * sin( angleRadians) + ( - width / 2 ) * cos( angleRadians) ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
distance : = sqrt( sqr( rotatedX - topLeftX) + sqr( rotatedY - topLeftY) ) ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
MaxPath : = distance;
end ;
begin
// Command line argument handling
if ParamCount < 2 then
begin
Writeln( 'Usage: GCodeGenerator <input_file.txt> <output_file.gcode>' ) ;
Halt( 1 ) ;
end ;
InputFileName : = ParamStr( 1 ) ;
OutputFileName : = ParamStr( 2 ) ;
ReadInputFile( ) ;
// G-code generation
AssignFile( OutFile, OutputFileName) ;
try
Rewrite( OutFile) ;
// *** Your G-code generation logic here ***
writeln( OutFile, '; G-code for stator winding' ) ;
writeln( OutFile, 'G90 ; Absolute coordinate system' ) ;
writeln( OutFile, 'G1 Y-10' ) ;
writeln( OutFile, 'G28 X Y Z' ) ;
writeln( OutFile, 'G1 X0 Y0' ) ;
//writeln(OutFile, 'G28 O'); //Home all "untrusted" axes
writeln( OutFile) ;
// Move to center of ray along Y axis and reset coordinate
writeln( OutFile, 'G1 X' , StatorParams. RayCenterOffset: 0 : 2 ) ;
writeln( OutFile, 'G92 X0' ) ;
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD>
CoilRadius: = ( StatorParams. RayTopHeight/ 2 + StatorParams. NeedleDiameter/ 2 + StatorParams. PathClearance) ;
CoilWidth: = ( StatorParams. RayTopWidth+ StatorParams. NeedleDiameter+ StatorParams. PathClearance* 2 ) ;
CoilHeight: = ( StatorParams. RayTopHeight+ StatorParams. NeedleDiameter+ StatorParams. PathClearance* 2 ) ;
writeln( 'CoilRadius = ' , CoilRadius: 0 : 3 ) ;
writeln( OutFile, 'G1 X' , - 1 * CoilRadius: 0 : 3 ) ;
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> .
CurrentZ: = ( StatorParams. BaseDiameter/ 2 ) + StatorParams. PathClearance;
writeln( OutFile, 'G1 Z' , CurrentZ: 0 : 2 , ' F' , StatorParams. WorkSpeed) ;
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> , <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
writeln( OutFile, 'M0' ) ;
writeln( OutFile, ';Start coil' ) ;
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> .
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> , <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> .
MaxPath: = CalculateMaxPath( ( StatorParams. RayLength+ StatorParams. BaseRadius) , StatorParams. RayWidth, AngleBetweenRays) ;
// MaxLayers :=trunc(((MaxPath-RequiredSpacing)/2/StatorParams.WireDiameter));
MaxLayers : = round( ( ( MaxPath- RequiredSpacing) / 2 / StatorParams. WireDiameter) ) ;
writeln( OutFile, ';MaxLayers ' , MaxLayers) ;
Inc( LayerNumber) ;
MoveForward: = true ;
if ( StatorParams. RayShape = 'circle' ) then
begin
for j : = 0 to CurrentCoilTurns do
begin
for i : = 0 to 3 6 0 do
begin
coords : = CircleCoordinates( CoilRadius* 2 , i+ 1 8 0 ) ;
2025-01-10 12:27:32 +03:00
// writeln(OutFile,'CoilRadius*2= ',CoilRadius*2:0:5,' X=', coords.X:0:3, ' Y=', coords.Y:0:5);
2024-12-25 19:14:10 +03:00
angle : = CalculateAngle( coords. Y, CurrentZ) ;
// writeln(OutFile, ';G1 X', coords.X:0:3, ' Y', angle:0:5,' Z', CurrentZ:0:5);
writeln( OutFile, 'G1 X' , coords. X: 0 : 3 , ' Y' , angle: 0 : 5 , ' Z' , NormalizeZ( CurrentZ, angle) : 0 : 5 ) ;
// writeln(OutFile);
CurrentZ: = CurrentZ+ StatorParams. WireDiameter/ 3 6 0 ;
end ;
writeln( OutFile, ';Next coil.' ) ;
end ;
writeln( OutFile, ';Second coil' ) ;
Inc( LayerNumber) ;
// RequiredSpacing:=StatorParams.RayWidth+StatorParams.NeedleDiameter+StatorParams.PathClearance*2+LayerNumber*StatorParams.WireDiameter*2;
// writeln(OutFile, ';RequiredSpacing = ',RequiredSpacing:0:5);
RequiredSpacing: = StatorParams. RayTopWidth;
writeln( OutFile, ';RequiredSpacing = ' , RequiredSpacing: 0 : 5 ) ;
RequiredSpacing: = RequiredSpacing+ StatorParams. NeedleDiameter;
writeln( OutFile, ';RequiredSpacing = ' , RequiredSpacing: 0 : 5 ) ;
RequiredSpacing: = RequiredSpacing+ StatorParams. PathClearance* 2 + LayerNumber* StatorParams. WireDiameter* 2 ;
writeln( OutFile, ';RequiredSpacing = ' , RequiredSpacing: 0 : 5 ) ;
CurrentZ: = RequiredSpacing/ ( 2 * tan( AngleBetweenRays* PI/ 1 8 0 / 2 ) ) ;
writeln( OutFile, ';CurrentZ = ' , CurrentZ: 0 : 3 ) ;
end
else if ( StatorParams. RayShape = 'rect' ) then
begin
2025-01-10 12:27:32 +03:00
writeln( OutFile, ';Rect? ' ) ;
2024-12-25 19:14:10 +03:00
while ( LayerNumber < MaxLayers) do
begin
2025-01-10 12:27:32 +03:00
writeln( OutFile, ';Layer ' , LayerNumber) ;
2024-12-25 19:14:10 +03:00
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
RequiredSpacing: = StatorParams. PathClearance* 2 + ( LayerNumber- 1 ) * StatorParams. WireDiameter* 2 + StatorParams. NeedleDiameter;
2025-01-10 12:27:32 +03:00
writeln( OutFile, ';RayWidth ' , StatorParams. RayWidth: 0 : 5 ) ;
2024-12-25 19:14:10 +03:00
writeln( OutFile, ';RequiredSpacing ' , RequiredSpacing: 0 : 5 ) ;
2025-01-10 12:27:32 +03:00
writeln( OutFile, ';AngleBetweenRays ' , AngleBetweenRays: 0 : 5 ) ;
2024-12-25 19:14:10 +03:00
MaxDepth: = DepthCheck( StatorParams. RayWidth, RequiredSpacing, AngleBetweenRays) ;
2025-01-10 12:27:32 +03:00
writeln( OutFile, ';MaxDepth ' , MaxDepth: 0 : 5 ) ;
writeln( OutFile) ;
2024-12-25 19:14:10 +03:00
If ( MaxDepth < ( StatorParams. BaseRadius) ) then CoilLength: = StatorParams. RayLength else if ( MaxDepth > ( StatorParams. RayLength+ StatorParams. BaseRadius) ) then break else
CoilLength: = StatorParams. RayLength+ StatorParams. BaseRadius- MaxDepth;
writeln( OutFile, ';CoilLength ' , CoilLength: 0 : 5 ) ;
2025-01-10 12:27:32 +03:00
// CurrentCoilTurns := ceil(CoilLength/StatorParams.WireDiameter);
CurrentCoilTurns : = round( CoilLength/ StatorParams. WireDiameter) ;
2024-12-25 19:14:10 +03:00
writeln( OutFile, ';CurrentCoilTurns ' , CurrentCoilTurns) ;
2025-01-10 12:27:32 +03:00
2024-12-25 19:14:10 +03:00
CoilTurnsSum : = CoilTurnsSum+ CurrentCoilTurns;
writeln( OutFile, ';CoilTurnsSum ' , CoilTurnsSum) ;
writeln( OutFile) ;
writeln( OutFile, ';We make ' , CurrentCoilTurns, ' turns on ' , LayerNumber, ' layer.' ) ;
2025-01-10 12:27:32 +03:00
writeln( OutFile, ';CoilWidth = ' , CoilWidth: 0 : 5 ) ;
2024-12-25 19:14:10 +03:00
for j : = 0 to CurrentCoilTurns do
begin
2025-01-10 12:27:32 +03:00
writeln( OutFile, ';Coil<69> = ' , j) ;
writeln( OutFile, 'M117 ' , j) ;
//angle := CalculateAngle(CoilWidth/2, CurrentZ);
angle : = AngleBetweenRays/ 2 ;
//Divide the path into 100 points
( * for k : = 1 to 1 0 0 do
begin
end ; * )
2024-12-25 19:14:10 +03:00
writeln( OutFile, 'G1 X' , - 1 * CoilHeight/ 2 : 0 : 3 , ' Y' , angle: 0 : 5 , ' Z' , NormalizeZ( CurrentZ, angle) : 0 : 5 ) ; // Top Left Corner
if MoveForward then CurrentZ: = CurrentZ+ StatorParams. WireDiameter/ 4 else CurrentZ: = CurrentZ- StatorParams. WireDiameter/ 4 ;
2025-01-10 12:27:32 +03:00
//writeln(OutFile, 'M0');
2024-12-25 19:14:10 +03:00
// angle := CalculateAngle(CoilWidth/2, CurrentZ);
writeln( OutFile, 'G1 X' , CoilHeight/ 2 : 0 : 3 , ' Y' , angle: 0 : 5 , ' Z' , NormalizeZ( CurrentZ, angle) : 0 : 5 ) ; // Top Right Corner
if MoveForward then CurrentZ: = CurrentZ+ StatorParams. WireDiameter/ 4 else CurrentZ: = CurrentZ- StatorParams. WireDiameter/ 4 ;
2025-01-10 12:27:32 +03:00
//writeln(OutFile, 'M0');
2024-12-25 19:14:10 +03:00
2025-01-10 12:27:32 +03:00
// angle := CalculateAngle(-1*CoilWidth/2, CurrentZ);
writeln( OutFile, 'G1 X' , CoilHeight/ 2 : 0 : 3 , ' Y' , - angle: 0 : 5 , ' Z' , NormalizeZ( CurrentZ, angle) : 0 : 5 ) ; // Bottom Right Corner
2024-12-25 19:14:10 +03:00
if MoveForward then CurrentZ: = CurrentZ+ StatorParams. WireDiameter/ 4 else CurrentZ: = CurrentZ- StatorParams. WireDiameter/ 4 ;
2025-01-10 12:27:32 +03:00
//writeln(OutFile, 'M0');
2024-12-25 19:14:10 +03:00
// angle := CalculateAngle(CoilWidth/2, CurrentZ);
2025-01-10 12:27:32 +03:00
writeln( OutFile, 'G1 X' , - 1 * CoilHeight/ 2 : 0 : 3 , ' Y' , - angle: 0 : 5 , ' Z' , NormalizeZ( CurrentZ, angle) : 0 : 5 ) ; // Bottom Left Corner
2024-12-25 19:14:10 +03:00
if MoveForward then CurrentZ: = CurrentZ+ StatorParams. WireDiameter/ 4 else CurrentZ: = CurrentZ- StatorParams. WireDiameter/ 4 ;
2025-01-10 12:27:32 +03:00
//writeln(OutFile, 'M0');
2024-12-25 19:14:10 +03:00
end ;
2025-01-10 12:27:32 +03:00
writeln( OutFile, 'M0' ) ;
2024-12-25 19:14:10 +03:00
// writeln(OutFile, 'G91');
// writeln(OutFile, 'G1 Y50');
// writeln(OutFile, 'G90');
Inc( LayerNumber) ;
CoilWidth: = CoilWidth+ StatorParams. WireDiameter* 2 ;
MoveForward: = not MoveForward;
writeln( OutFile, MoveForward) ;
end ;
writeln( OutFile) ;
writeln( OutFile, ';Second coil' ) ;
Inc( LayerNumber) ;
RequiredSpacing: = StatorParams. PathClearance* 2 + LayerNumber* StatorParams. WireDiameter* 2 + StatorParams. NeedleDiameter;
writeln( OutFile, ';RequiredSpacing = ' , RequiredSpacing: 0 : 5 ) ;
For i: = 1 to 2 0 do
begin
LayerNumber: = i;
writeln( OutFile, ';Layer ' , i) ;
RequiredSpacing: = StatorParams. PathClearance* 2 + ( LayerNumber- 1 ) * StatorParams. WireDiameter* 2 + StatorParams. NeedleDiameter;
writeln( OutFile, ';RequiredSpacing ' , RequiredSpacing: 0 : 5 ) ;
MaxDepth: = DepthCheck( StatorParams. RayWidth, RequiredSpacing, AngleBetweenRays) ;
writeln( OutFile, ';MaxDepth ' , MaxDepth: 0 : 5 ) ;
If ( MaxDepth < ( StatorParams. BaseRadius) ) then CoilLength: = StatorParams. RayLength else if ( MaxDepth > ( StatorParams. RayLength+ StatorParams. BaseRadius) ) then break else
CoilLength: = StatorParams. RayLength+ StatorParams. BaseRadius- MaxDepth;
writeln( OutFile, ';CoilLength ' , CoilLength: 0 : 5 ) ;
// CurrentCoilTurns := round(CoilLength/StatorParams.WireDiameter);
// CurrentCoilTurns := ceil(CoilLength/StatorParams.WireDiameter);
CurrentCoilTurns : = floor( CoilLength/ StatorParams. WireDiameter) ;
writeln( OutFile, ';CurrentCoilTurns ' , CurrentCoilTurns) ;
CoilTurnsSum : = CoilTurnsSum+ CurrentCoilTurns;
writeln( OutFile, ';CoilTurnsSum ' , CoilTurnsSum) ;
writeln( OutFile) ;
end ;
end ;
// *** Your G-code generation logic here ***
except
on E: Exception do
begin
writeln( 'Error writing to output file: ' , E. Message ) ;
Halt( 1 ) ;
end ;
end ;
CloseFile( OutFile) ;
writeln( 'G-code generated to: ' , OutputFileName) ;
writeln( 'Press Enter... ' ) ;
Readln;
end .