Sergio - 2025-06-14

Hi friends. I think I am using the more recente version of GLscene (see my USES below)
When I compile the Procedure below - to save a freeform as STL - I get the error message - Undeclared identifier: 'TriangleIndices' - in this line
v1 := Mesh.Vertices[Mesh.TriangleIndices[i * 3]];

Can you help me with the right code.
//////////// the code //////////
USES (without errors )
SysUtils, Classes, Graphics, Controls, Forms,
ComCtrls, StdCtrls, ExtCtrls, dialogs,system.math, Winapi.Windows,
GLS.ArchiveManager, GLS.SimpleNavigation, GLSL.CustomShader, GLSL.Shader,
GLS.AVIRecorder, GLS.Material, GLSL.LineShaders, GLS.VectorFileObjects,
GLS.Collision, GLS.FBORenderer, GLS.Objects, GLS.Scene, GLS.HUDObjects,
GLS.SpaceText, GLS.MultiPolygon, GLS.Extrusion, GLS.Coordinates,
GLS.BaseClasses, GLS.BitmapFont, GLS.WindowsFont, GLS.Cadencer,
GLS.SceneViewer, Stage.VectorTypes, Stage.VectorGeometry, GLS.GeomObjects, GLS.ODEmanager ,
ODE.Import, GLS.isolines, GLS.color, GLS.CompositeImage, GLS.Mesh ,
Winapi.OpenGL, Winapi.OpenGLext, Stage.OpenGLTokens,
Stage.Strings, GLS.XOpenGL, GLS.Context, GLS.State,
GLS.RenderContextInfo, GLS.VectorLists, GLS.FileSTL, GLS.File3ds, System.IOUtils,
GLS.Texture,GLS.FileMS3D,GLS.MeshUtils, GLS.Octree,GBE.CubeMap,GBE.grass,GXS.FileMS3D,
GXS.MeshUtils,GXS.Octree
;
////////////////// the procedure //////////////////
procedure TF_3DD.SaveMeshAsBinarySTL(const Mesh: TGLMeshObject; const FileName: string);
var
FS: TFileStream;
i: Integer;
v1, v2, v3, normal: TAffineVector;
triCount: Cardinal;
dummyHeader: array[0..79] of AnsiChar;
begin
FS := TFileStream.Create(FileName, fmCreate);
try
// Write 80-byte header
FillChar(dummyHeader, SizeOf(dummyHeader), 0);
FS.WriteBuffer(dummyHeader, SizeOf(dummyHeader));

// Number of triangles
triCount := Mesh.TriangleCount;
WriteUInt32(FS, triCount);

// Write triangle data
for i := 0 to triCount - 1 do
begin
  // Get triangle vertices
  v1 := Mesh.Vertices[Mesh.TriangleIndices[i * 3]];   // <<<<<<<<<<<< ERROR HERE 
  v2 := Mesh.Vertices[Mesh.TriangleIndices[i * 3 + 1]];
  v3 := Mesh.Vertices[Mesh.TriangleIndices[i * 3 + 2]];

  // Compute normal
  normal := VectorCrossProduct(VectorSubtract(v2, v1), VectorSubtract(v3, v1));
  NormalizeVector(normal);

  // Write normal
  WriteFloat32(FS, normal.X);
  WriteFloat32(FS, normal.Y);
  WriteFloat32(FS, normal.Z);

  // Write vertices
  WriteFloat32(FS, v1.X);
  WriteFloat32(FS, v1.Y);
  WriteFloat32(FS, v1.Z);

  WriteFloat32(FS, v2.X);
  WriteFloat32(FS, v2.Y);
  WriteFloat32(FS, v2.Z);

  WriteFloat32(FS, v3.X);
  WriteFloat32(FS, v3.Y);
  WriteFloat32(FS, v3.Z);

  // Attribute byte count = 0
  WriteUInt16(FS, 0);
end;

finally
FS.Free;
end;
end;