Poniżej znajdziesz prosty edytor tekstu dla kodu CNC (Fanuc) napisany w Delphi (VCL).
Zawiera:
- Kolorowanie składni G-code (G, M, T, S, F, N, X, Y, Z, I, J, K, R itp.)
- Numerowanie linii
- Podświetlanie bieżącej linii
- Podstawowe funkcje: nowy, otwórz, zapisz, zapisz jako
🔧 Wymagania:
- Delphi z VCL (np. Delphi 10.3+)
- Komponent
TSynEdit z pakietu SynEdit (darmowy, open source)
📦 Instalacja SynEdit
- Pobierz SynEdit z GitHub: https://github.com/SynEdit/SynEdit
- Zainstaluj w Delphi:
Component > Install Packages > Add
✅ Krok po kroku
1. Utwórz nowy projekt VCL
File > New > VCL Forms Application
2. Dodaj komponenty na formularz
TSynEdit (nazwij SynEdit1)
TMainMenu (menu: Plik > Nowy/Otwórz/Zapisz/Zapisz jako/Wyjście)
TOpenDialog, TSaveDialog
TStatusBar (opcjonalnie)
3. Ustaw właściwości SynEdit1
Gutter.Visible := True
Gutter.ShowLineNumbers := True
Font.Name := 'Courier New'
Font.Size := 10
🧠 Kolorowanie składni G-code
4. Dodaj TSynCNCSyn (jeśli masz) lub użyj własnego highlightera
Jeśli nie masz TSynCNCSyn, użyj prostego highlightera:
unit CNCHighlighter;
interface
uses
SysUtils, Classes, Graphics, SynEditHighlighter, SynEditTypes;
type
TCNCHighlighter = class(TSynCustomHighlighter)
private
fGCodeAttri: TSynHighlighterAttributes;
fMCodeAttri: TSynHighlighterAttributes;
fCoordAttri: TSynHighlighterAttributes;
fNumberAttri: TSynHighlighterAttributes;
fCommentAttri: TSynHighlighterAttributes;
fIdentifierAttri: TSynHighlighterAttributes;
protected
function GetDefaultAttribute(Index: Integer): TSynHighlighterAttributes; override;
public
constructor Create(AOwner: TComponent); override;
function GetEol: Boolean; override;
function GetToken: string; override;
function GetTokenAttribute: TSynHighlighterAttributes; override;
function GetTokenKind: Integer; override;
function GetTokenPos: Integer; override;
procedure Next; override;
procedure SetLine(const NewValue: string; LineNumber: Integer); override;
end;
implementation
constructor TCNCHighlighter.Create(AOwner: TComponent);
begin
inherited;
fGCodeAttri := TSynHighlighterAttributes.Create('GCode', 'G-codes');
fGCodeAttri.Foreground := clBlue;
AddAttribute(fGCodeAttri);
fMCodeAttri := TSynHighlighterAttributes.Create('MCode', 'M-codes');
fMCodeAttri.Foreground := clGreen;
AddAttribute(fMCodeAttri);
fCoordAttri := TSynHighlighterAttributes.Create('Coord', 'Coordinates');
fCoordAttri.Foreground := clRed;
AddAttribute(fCoordAttri);
fNumberAttri := TSynHighlighterAttributes.Create('Number', 'Numbers');
fNumberAttri.Foreground := clNavy;
AddAttribute(fNumberAttri);
fCommentAttri := TSynHighlighterAttributes.Create('Comment', 'Comments');
fCommentAttri.Foreground := clGray;
fCommentAttri.Style := [fsItalic];
AddAttribute(fCommentAttri);
fIdentifierAttri := TSynHighlighterAttributes.Create('Identifier', 'Identifier');
fIdentifierAttri.Foreground := clBlack;
AddAttribute(fIdentifierAttri);
SetAttributesOnChange(DefHighlightChange);
end;
function TCNCHighlighter.GetDefaultAttribute(Index: Integer): TSynHighlighterAttributes;
begin
case Index of
SYN_ATTR_COMMENT: Result := fCommentAttri;
SYN_ATTR_IDENTIFIER: Result := fIdentifierAttri;
SYN_ATTR_WHITESPACE: Result := fIdentifierAttri;
else
Result := nil;
end;
end;
function TCNCHighlighter.GetEol: Boolean;
begin
Result := FTokenPos > Length(FLineText);
end;
function TCNCHighlighter.GetToken: string;
begin
Result := Copy(FLineText, FTokenPos, FTokenLen);
end;
function TCNCHighlighter.GetTokenAttribute: TSynHighlighterAttributes;
var
Token: string;
begin
Token := GetToken;
if Token.StartsWith('(') or Token.StartsWith(';') then
Exit(fCommentAttri)
else if Token.StartsWith('G') then
Exit(fGCodeAttri)
else if Token.StartsWith('M') then
Exit(fMCodeAttri)
else if Token.StartsWith('X') or Token.StartsWith('Y') or Token.StartsWith('Z') or
Token.StartsWith('I') or Token.StartsWith('J') or Token.StartsWith('K') or
Token.StartsWith('R') or Token.StartsWith('F') or Token.StartsWith('S') or
Token.StartsWith('T') or Token.StartsWith('N') then
Exit(fCoordAttri)
else if Token[1] in ['0'..'9', '.', '-'] then
Exit(fNumberAttri)
else
Exit(fIdentifierAttri);
end;
function TCNCHighlighter.GetTokenKind: Integer;
begin
Result := 0;
end;
function TCNCHighlighter.GetTokenPos: Integer;
begin
Result := FTokenPos - 1;
end;
procedure TCNCHighlighter.Next;
var
Len: Integer;
begin
Len := Length(FLineText);
if FTokenPos > Len then Exit;
// Komentarz
if FLineText[FTokenPos] = '(' then
begin
FTokenLen := Pos(')', FLineText, FTokenPos) - FTokenPos + 1;
if FTokenLen <= 0 then FTokenLen := Len - FTokenPos + 1;
Inc(FTokenPos, FTokenLen);
Exit;
end;
if FLineText[FTokenPos] = ';' then
begin
FTokenLen := Len - FTokenPos + 1;
Inc(FTokenPos, FTokenLen);
Exit;
end;
// Tokeny
if FLineText[FTokenPos] in ['G', 'M', 'X', 'Y', 'Z', 'I', 'J', 'K', 'R', 'F', 'S', 'T', 'N'] then
begin
FTokenLen := 1;
while (FTokenPos + FTokenLen <= Len) and
(FLineText[FTokenPos + FTokenLen] in ['0'..'9', '.', '-']) do
Inc(FTokenLen);
Inc(FTokenPos, FTokenLen);
Exit;
end;
// Liczby
if FLineText[FTokenPos] in ['0'..'9', '.', '-'] then
begin
FTokenLen := 1;
while (FTokenPos + FTokenLen <= Len) and
(FLineText[FTokenPos + FTokenLen] in ['0'..'9', '.']) do
Inc(FTokenLen);
Inc(FTokenPos, FTokenLen);
Exit;
end;
// Pozostałe
FTokenLen := 1;
Inc(FTokenPos, FTokenLen);
end;
procedure TCNCHighlighter.SetLine(const NewValue: string; LineNumber: Integer);
begin
inherited;
FLineText := NewValue;
FTokenPos := 1;
FTokenLen := 0;
end;
end.
5. Użyj highlightera w FormCreate
uses
CNCHighlighter;
procedure TForm1.FormCreate(Sender: TObject);
begin
SynEdit1.Highlighter := TCNCHighlighter.Create(Self);
end;
📁 Obsługa plików
procedure TForm1.mnuNewClick(Sender: TObject);
begin
SynEdit1.Lines.Clear;
FFileName := '';
Caption := 'Edytor CNC - Nowy plik';
end;
procedure TForm1.mnuOpenClick(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
SynEdit1.Lines.LoadFromFile(OpenDialog1.FileName);
FFileName := OpenDialog1.FileName;
Caption := 'Edytor CNC - ' + FFileName;
end;
end;
procedure TForm1.mnuSaveClick(Sender: TObject);
begin
if FFileName = '' then
mnuSaveAsClick(Sender)
else
SynEdit1.Lines.SaveToFile(FFileName);
end;
procedure TForm1.mnuSaveAsClick(Sender: TObject);
begin
if SaveDialog1.Execute then
begin
SynEdit1.Lines.SaveToFile(SaveDialog1.FileName);
FFileName := SaveDialog1.FileName;
Caption := 'Edytor CNC - ' + FFileName;
end;
end;
✅ Gotowe!
Masz teraz prosty, ale funkcjonalny edytor G-code dla Fanuc z kolorowaniem składni. Możesz rozbudować go o:
- Podgląd 3D (np. za pomocą OpenGL)
- Sprawdzanie błędów składni
- Podgląd narzędzi
- Symulację ruchu
Jeśli chcesz wersję w FMX (FireMonkey) lub Lazarus, daj znać — mogę przygotować.