Ravichandran J.V. said:
Have you tried the Marshal.Sizeof instead of sizeof ?
The complete solution for me is this:
Much more complicated than expected but nevertheless fast and reliable.
Maybe in a next step I might lose the unsafe and fixed keywords too if I
can
find some time. ;-)
----------------------------
internal struct STLHEADER {
[MarshalAs (UnmanagedType.ByValArray, SizeConst = 80)]
public byte [] title;
public int dwCount;
}
internal struct STLFACET {
public STLVECTOR Normal;
public STLVECTOR V1;
public STLVECTOR V2;
public STLVECTOR V3;
public ushort dwColor;
}
----------------------------
The function to access the header and STL triangles is something like
this:
----------------------------
unsafe {
...
int iSTLHeaderSize=Marshal.SizeOf(typeof(STLHEADER));
...
IntPtr ptr = IntPtr.Zero;
try {
ptr = Marshal.AllocHGlobal(iSTLHeaderSize);
Marshal.Copy(STLFile.BufferPtr, 0x0, ptr, iSTLHeaderSize);
STLHEADER header = (STLHEADER)
Marshal.PtrToStructure(ptr,typeof(STLHEADER));
...
ASCIIEncoding enc = new ASCIIEncoding();
aModelBuffer.m_sTitle = enc.GetString(header.title);
...
int iCount=(int)header.dwCount;
if (iCount>iNrOfTriangles) {
...
fixed (byte *p=&STLFile.BufferPtr[iSTLHeaderSize]) {
STLFACET *pTriangle=(STLFACET *) p;
...
Vertex1.x=pTriangle->V1.x;
Vertex1.y=pTriangle->V1.y;
Vertex1.z=pTriangle->V1.z;
...
aModelBuffer.m_Model.m_Vertex[iIndex]=Vertex1;
....
}
...
}
}
}
----------------------------
And aModelBuffer.m_Model.m_Vertex[iIndex]=Vertex1 is defined like this:
----------------------------
public struct CVERTEX {
public float x;
public float y;
public float z;
}
public CVERTEX this[int aiIndex] {
get {
long iPos=Buffer.RecGetRecPosAt(aiIndex);
CVERTEX ReturnVertex=new CVERTEX();
unsafe {
fixed (void *pSource=&Buffer.BufferPtr[iPos]) {
CVERTEX *pSourceVertex=(CVERTEX *)pSource;
CVERTEX *pDestinationVertex=(CVERTEX *)&ReturnVertex;
*pDestinationVertex=*pSourceVertex;
}
}
return ReturnVertex;
}
set {
long iPos=Buffer.RecGetRecPosAt(aiIndex);
unsafe {
fixed (void *pDestination=&Buffer.BufferPtr[iPos]) {
CVERTEX *pSourceVertex=(CVERTEX *)&value;
CVERTEX *pDestinationVertex=(CVERTEX *)pDestination;
*pDestinationVertex=*pSourceVertex;
}
}
}
}