1

I had a hard time finding how to use HOperatorSet.SetDrawingObjectCallback(HTuple drawID, HTuple drawObjectEvent, HTuple callbackFunction) (Docu) in C#, specifically the part of the callback HTuple callbackFunction. Apart from a Chinese website (Link), I could not locate any examples on how to properly do this. The website itself was also not straight forward to find and the code used there throws a fatal exception. In order for other people to have a better resource on how to use the HOperatorSet.SetDrawingObjectCallback method, I decided to create this question and answer it myself.

Florian H.
  • 89
  • 10

1 Answers1

0

The key here is that the HTuple callbackFunction is a function pointer that points to a delegate of the type HDrawingObject.HDrawingObjectCallback. This delegate then links to you proper callback method. The callback method includes an intptr to the drawingId and the windowId as well as a string type.

Here is the code snippet, that hopefully will make your day better:

public class SomeClass
{
    public SomeClass()
    {
        DrawingObjectCallback = DrawingObjectCallbackHandler;
    }
    
    public void AddDrawingObject()
    {
        HOperatorSet.CreateDrawingObjectCircle(50, 50, 100, out var drawId);
        
        //Get the pointer to HDrawingObjectCallback which links to our handler
        var ptr = Marshal.GetFunctionPointerForDelegate(DrawingObjectCallback);
                
        //Select the events you want to listen to
        var listenTo = new HTuple("on_resize", "on_drag");
        
        //Finally call the method
        HOperatorSet.SetDrawingObjectCallback(drawId, listenTo, ptr);
        
        HOperatorSet.AttachDrawingObjectToWindow(HalconWindow.HalconID, drawId);
    }

    public HDrawingObject.HDrawingObjectCallback DrawingObjectCallback { get; }
    public void DrawingObjectCallbackHandler(IntPtr drawId, IntPtr windowHandle, string type)
    {
        int id = new HTuple(drawId);
        int windowId  = new HTuple(windowHandle);
        
        //Now you have the two Ids as integers and can work from here!
    }
}
Florian H.
  • 89
  • 10