• CAMAPI
  • API Documentation
Show / Hide Table of Contents
  • Supported programming languages
    • C#
    • Delphi
    • C++
  • Tutorial lessons
    • Lesson 1 - general introduction to the extension ideology using the example of a C# project
    • Lesson 2 - connecting the SDK to a Delphi project using the build system
    • Lesson 3 - connecting the SDK to a C++ project using the build system
    • Lesson 4 - demonstration of methods for unloading an extension during main application is running
    • Lesson 5 - Creating a custom operation
    • Lesson 6 - Creating a C# application to interact with geometry in main application
    • Lesson 7 - Creating a C# application to connect to main application for managing it
  • System extensions
    • Extension.Util.Common.Dll
    • Extension.Util.Common.Exe
  • Debugging of extensions
    • Debugging a .NET extension using Visual Studio Code
    • Debugging a Delphi extension using RAD Studio
    • Debugging a C++ extension using Visual Studio
  • Entry points
    • Utilitiy in main form
    • Executor for utility in main form
    • New item to the operation's context menu
  • Machining Tools Import
    • Preparing the environment
    • Working with the tool library
    • Working with cutting tools
      • Milling Tools
      • Turning Tools
      • Custom Axial Shaped Tools
    • Working with the tool holder
  • API Documentation
  • External applications
    • Connecting in a C# application
    • Connecting in a Delphi application
    • Connecting in a C++ application

Connecting to main application in a C# application

The basic package of installed program includes:

  • CAMIPC.Helper.Cam.dll, which contains the export function CreateHelper. This function returns an object of the IIpcHelper interface. The methods of this interface allow you to connect to an already running instance or start a new one.
  • NativeLibUtils.dll - to simplify the loading and unloading of native DLLs.

File .csproj should contain:

  • reference with path to NativeLibUtils.dll;
  • package reference to CAMIPC SDK;
<ItemGroup>
    <PackageReference Include="SprutTechnology.CAMIPC.SDK.Net" Version="18.2.0" />
    <Reference Include="C:\Program Files\Sprut Technology\SprutCAM 18\Bin64\NativeLibUtils.dll" />
</ItemGroup>

Support singleton class

This class provides public method GetApplication(), which connects to running instance.

using System;
using System.IO;
using System.Runtime.InteropServices;
using CAMAPI.ResultStatus;
using CAMIPC.Application;
using CAMIPC.ExecuteContext;
using CAMIPC.Helper;
using SprutTechnology.NativeLibUtils;

namespace ApplicationEmptyNet;

public static class CamHelper
{
    private static IIpcHelper? _helper;
    private static ICamIpcApplication? _application;
    private static IntPtr _helperObjectPtr;
    private static IntPtr _helperDllPtr;
    private delegate IntPtr CreateHelperDelegate();
    
    /// <summary>
    /// Connect to CAM application
    /// </summary>
    private static IIpcHelper GetHelper()
    {
        if (_helper != null)
            return _helper;

        const string camFolder = @"C:\Program Files\Sprut Technology\SprutCAM NB 18\Bin64";
        var helperPath = Path.Combine(camFolder, "CAMIPC.Helper.Cam.dll");
        if (!File.Exists(helperPath))
            throw new Exception($"{helperPath} not found");

        _helper = NativeLibLoader.CreateComObject<IIpcHelper, CreateHelperDelegate>(
                      helperPath, "CreateHelper", out _helperObjectPtr, out _helperDllPtr)
                  ?? throw new Exception("Can't create helper");
        
        return _helper;
    }
    
    /// <summary>
    /// Get instance of running CAM application to interact with it
    /// </summary>
    public static ICamIpcApplication GetApplication()
    {
        if (_application != null)
            return _application;

        var executeContext = new TExecuteContext();
        var instances = GetHelper().GetRunningCamAppList(ref executeContext);
        if (executeContext.ResultStatus.Code == TResultStatusCode.rsError)
            throw new Exception(executeContext.ResultStatus.Description);
        if (instances.Count == 0)
            throw new Exception("SprutCAM running instance not found");

        _application = instances.Get(0, executeContext);
        if (executeContext.ResultStatus.Code == TResultStatusCode.rsError)
            throw new Exception(executeContext.ResultStatus.Description);
        
        return _application;
    }
    
    /// <summary>
    /// Destructor. Release COM objects
    /// </summary>
    public static void Clean()
    {
        if (_helper != null)
        {
            if (_application != null)
                Marshal.ReleaseComObject(_application);
            NativeLibLoader.FreeDll(_helper, _helperObjectPtr, _helperDllPtr);
        }
    }
}

Modificating support singleton class

IIpcHelper interface provides methods to:

  • Connect to the first found instance (as mentioned above);
  • Connect to SprutCAM by specifying the process PID;
  • Start new SprutCAM and connect to it.
In this article
Back to top Generated by DocFX