AnsweredAssumed Answered

(C#) Using OpenGL to render in Solidworks

Question asked by Daniel Mansour on Oct 22, 2017
Latest reply on Apr 13, 2018 by Jacob Corder

Hello, I'm using Solidworks 2016, the Solidworks C# addin template, and OpenTK.

 

I am having a problem when attempting to render anything programmatically with OpenGL in Solidworks. I have run into several conflicting statements regarding rendering what I want in the window.

 

Source 1: Using OpenGL with VBO's in SolidWorks problem

Tony Van De Velde is attempting to use VBOs to render using the ModelView.BufferSwapNotify event handler. He initially is able to render using OpenGL's Immediate Mode, but finds that rendering VBOs needs to happen in the event handler so that it may be rendered after Solidworks Layer0 which is the part/assembly graphical output.

 

Source 2: Help me about OpenGL!

Lenny Yang is attempting to draw into SolidWorks using the OpenGL interface in Visual Basic. Several other questions are asked and answered regarding the context used to draw in. The answer by Artem Taturevych is vague regarding why we should ignore what the API documentation recommends using for drawing to the context.

 

I currently am getting visible OpenGL renderings when I draw using the following code:

 

Code
// in EventHandling.cs

public class DocView
{
  ISldWorks iSwApp;
  SwAddin userAddin;
  ModelView mView;
  DocumentEventHandler parent;

  private GLControl glControl;

 

  public DocView(SwAddin addin, IModelView mv, DocumentEventHandler doc)
  {
     userAddin = addin;
     mView = (ModelView)mv;
     iSwApp = (ISldWorks)userAddin.SwApp;
     parent = doc;

     glControl = new GLControl();         // Program does not work without this
     glControl.Context.MakeCurrent(null); // Program does not work without this
  }

 

  public bool AttachEventHandlers()

  {

     mView.DestroyNotify2 += new DModelViewEvents_DestroyNotify2EventHandler(OnDestroy);

     mView.RepaintNotify += new DModelViewEvents_RepaintNotifyEventHandler(OnRepaint);

     mView.BufferSwapNotify += OnBufferSwapNotify;

     return true;

  }

 

    public bool DetachEventHandlers()

  {

     mView.DestroyNotify2 -= new DModelViewEvents_DestroyNotify2EventHandler(OnDestroy);

     mView.RepaintNotify -= new DModelViewEvents_RepaintNotifyEventHandler(OnRepaint);

     mView.BufferSwapNotify -= OnBufferSwapNotify;

     parent.DetachModelViewEventHandler(mView);

     return true;

  }

 

  public int OnBufferSwapNotify()

  {

      #region RENDER_SIMPLE_LINE
      GL.LineWidth(3);

 

      GL.Begin(PrimitiveType.Triangles);
          GL.Vertex3(0.0f, 0.0f, 0.0f);
          GL.Vertex3(0.5f, 0.5f, 0.5f);
          GL.Vertex3(0.0f, 1.0f, 0.0f);
      GL.End();
      #endregion

      return 0;
  }

}

 

 

Note the green highlighted lines. The program does not run without them. What they do is set an OpenGL context and then release the context. I'm not understanding what context I'm drawing into since the program does not run without first setting a custom context but then releasing it.

 

My question: Why does drawing to the context work like this? Which GL context should I aim to draw to?

 

I'm not sure what's going on but I am getting renderings with whatever shading was available prior to the event handler drawing. I want to know why this is possible and why I should be ignoring what the API documentation says since that doesn't seem to work when I try to mimic it.

 

2017 SOLIDWORKS API Help - InitializeShading Method (IModelView)

 

Maybe I'm misunderstanding what I need to be rendering with the InitializeShading method? Should I draw the vertices prior to calling InitializeShading and then set the lighting/textures after?

 

Any help is appreciated.

 

Message was edited by: Daniel Mansour Reason: Cleaned formatting of code. Made question more specific.

Outcomes