C++ Sample Usage Guide

You are currently viewing the documentation for a pre-release version (2.2.0). To access documentation for other versions, click the "Switch Version" button located in the upper-right corner of the page.

■ If you're unsure about the version of the product you are using, please contact Mech-Mind Technical Support for assistance.

This section introduces how to configure and run C++ samples of Mech-Vision SDK on Windows.

Prerequisites

  • SDK environment setup has been completed.

  • CMake 3.20 or later.

  • Visual Studio 2017 or later (with the MSVC compiler).

  • Mech-Vision is installed and can run properly.

Sample Overview

The C++ samples are located in development/cpp/examples/ and include:

Sample directory Description

solution_basic

Open, close, save, and rename solutions; get solution information and project information lists.

project_basic

Register project running-state and acquisition-finished callbacks, run projects, and get output data (pick point poses).

project_data_storage

Operate project data storage and read/write stored data.

project_param_recipe

Get parameter recipe lists and switch the current parameter recipe.

solution_comm_config

Get and modify communication configurations of a solution (TCP/IP, Modbus, and so on).

solution_device_status

Get camera status information in a solution, such as connection status, temperature, and transfer rate.

solution_global_variable

Read and write global variables in a solution (integer, floating-point, string, queue, and so on).

step_basic

Get Step information, pin Step output ports, and get Step output data (such as point poses) after running a project.

step_props

Read and set Step parameters in JSON format (including access to multi-level parameters).

production_interface

Production Interface operations.

production_interface_com

Production Interface communication operations.

Configure and Build Samples

The following steps use solution_basic as an example.

  1. Open CMakeLists.txt in the sample directory and set VISION_SDK_DIR to the actual SDK path:

    set(VISION_SDK_DIR path/to/development/cpp)
  2. Create a build directory in the sample directory and run CMake:

    mkdir build && cd build
    cmake -DCMAKE_BUILD_TYPE=Release ..
  3. Open the generated .sln file with Visual Studio, or build with the command below:

    cmake --build . --config Release

Run Samples

  1. Make sure Mech-Vision is running, or start the Vision service through SDK APIs (see Start the Vision service).

  2. Update the solution path in sample code by replacing "D:/data/vision_sdk_example" with the actual path:

    error = solution.open("D:/path/to/your/solution");
  3. Run the compiled executable file.

Code Examples

Open a Solution and Get Project Information (solution_basic)

#include "vision_sdk/vision_sdk.h"
using namespace mmind;

int main()
{
    vision::initialize();

    vision::ErrorStatus error;

    // Open a solution
    vision::Solution solution;
    error = solution.open("D:/data/vision_sdk_example");

    // Get solution information
    vision::SolutionInfo solutionInfo;
    error = solution.getInfo(solutionInfo);

    // Get project information list
    std::vector<vision::ProjectInfo> projectInfos;
    error = solution.getAllProjectInfos(projectInfos);
    for (const auto& info : projectInfos) {
        // Process project information
    }

    // Save and close the solution
    error = solution.save();
    error = solution.close();

    vision::uninitialize();
    return 0;
}

Run a Project and Get Output Data (project_basic)

#include "vision_sdk/vision_sdk.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
using namespace mmind;

void example()
{
    vision::Solution solution;
    solution.open("D:/data/vision_sdk_example");

    // Get project name
    std::vector<vision::ProjectInfo> projectInfos;
    solution.getAllProjectInfos(projectInfos);
    vision::Project project(projectInfos.front().name);

    // Register running-state callback
    project.setRunningChangedCallback(
        [](const vision::ProjectRunningChangedCallbackData& cbData) {
            // Handle running-state changes
        });

    // Run the project (wait until it finishes)
    vision::ProjectResult projectResult;
    project.run(vision::ProjectRunWaitState::Finished, "request_1", projectResult);

    // Parse output data (JSON format)
    const auto joOutput =
        QJsonDocument::fromJson(projectResult.outputJson.c_str()).object();
    const auto jaPickPoints =
        joOutput["workobject_data"].toObject()["pick_points"].toArray();
}

Read and Set Step Parameters (step_props)

#include "vision_sdk/vision_sdk.h"
#include <QJsonDocument>
#include <QJsonArray>
using namespace mmind;

void example()
{
    vision::Solution solution;
    solution.open("D:/data/vision_sdk_example");

    // Get the confidence threshold of a 3D Matching step
    vision::Step step("3D Matching_1", "Matching");
    std::string propsJson;
    step.getPropertiesJson(R"(["confidenceThreshold"])", propsJson);

    // Set step parameters
    step.setPropertiesJson(R"({"confidenceThreshold": 0.8})");
}

Is this page helpful?

You can give a feedback in any of the following ways:

We Value Your Privacy

We use cookies to provide you with the best possible experience on our website. By continuing to use the site, you acknowledge that you agree to the use of cookies. If you decline, a single cookie will be used to ensure you're not tracked or remembered when you visit this website.