Page 1 of 1

OpenGL in standalone App?

Posted: Sun Oct 08, 2023 8:43 pm
by JeromeP
Hello, I'm creating a VB.net application to insert OpenGL objects in my model space.

Doing it with an add-in works great like this example from Artem:
https://www.codestack.net/solidworks-ap ... trahedron/

But I can't make it work on a standalone application with the OnBufferSwapNotify event.
The event fires but nothing is drawn and after awhile it crashes with 'AccessViolationException:Attempted to read or write protected memory'.
Is it even possible to do? Do I need to use an other method?

Here is the strip-down code that I use:

Code: Select all

Imports SolidWorks.Interop.sldworks
Module SolidWorksFunctions
    Private swApp As SldWorks
    Private swModel As ModelDoc2
    Private swView As ModelView

    Public Sub SwIni()
        swApp = GetObject(Nothing, "SldWorks.Application")
        swModel = swApp.ActiveDoc
        If swModel Is Nothing Then Exit Sub
        swView = swModel.ActiveView

        AddHandler swView.BufferSwapNotify, AddressOf OnBufferSwapNotify
    End Sub

    Private Function OnBufferSwapNotify() As Integer
        Console.WriteLine("OnBufferSwapNotify")
        Dim a() As Double = {0, 0, 0}
        Dim b() As Double = {1, 0, 0}
        Dim c() As Double = {0.5, Math.Sqrt(3) / 2, 0}
        Dim d() As Double = {0.5, Math.Sqrt(3) / 6, Math.Sqrt(6) / 3}

        DrawTriangle(a, c, b, Color.Green, True, False, False, 3.0F)
        DrawTriangle(a, d, c, Color.Green, True, False, False, 3.0F)
        DrawTriangle(c, d, b, Color.Green, True, False, False, 3.0F)
        DrawTriangle(d, a, b, Color.Green, True, False, False, 3.0F)

        Return 0
    End Function

    Private Sub DrawTriangle(a() As Double, b() As Double, c() As Double, color As Color, fill As Boolean, wireframe As Boolean, dashed As Boolean, width As Single)
        OpenGl.glPolygonMode(OpenGl.GL_FRONT_AND_BACK, IIf(fill, OpenGl.GL_FILL, OpenGl.GL_LINE))
        OpenGl.glDisable(OpenGl.GL_LIGHTING)

        If wireframe Then
            OpenGl.glEnable(OpenGl.GL_LINE_SMOOTH)
            If dashed Then
                OpenGl.glEnable(OpenGl.GL_LINE_STIPPLE)
                OpenGl.glLineStipple(4, &HAAAA)
            End If
        End If

        OpenGl.glBegin(IIf(wireframe, opengl.GL_LINE_LOOP, opengl.GL_TRIANGLES))

        If wireframe Then OpenGl.glLineWidth(width)

        OpenGl.glColor4f(color.R / 255.0F, color.G / 255.0F, color.B / 255.0F, color.A / 255.0F)
        opengl.glVertex3d(a(0), a(1), a(2))
        opengl.glVertex3d(b(0), b(1), b(2))
        opengl.glVertex3d(c(0), c(1), c(2))

        OpenGl.glEnd()

        OpenGl.glDisable(opengl.GL_LINE_SMOOTH)
        opengl.glDisable(opengl.GL_LINE_STIPPLE)
    End Sub
End Module

Re: OpenGL in standalone App?

Posted: Thu Oct 12, 2023 11:15 am
by Frederick_Law
Addin and macro are running in Solidwork's process and memory space.
Standalone is not. So Windows see Standalone try to modify Solidwork's memory.
Usually Standalone is to do things without openning or interacting with the program, SW or IV or Excel.