C++ Sample Usage Guide
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 |
|---|---|
|
Open, close, save, and rename solutions; get solution information and project information lists. |
|
Register project running-state and acquisition-finished callbacks, run projects, and get output data (pick point poses). |
|
Operate project data storage and read/write stored data. |
|
Get parameter recipe lists and switch the current parameter recipe. |
|
Get and modify communication configurations of a solution (TCP/IP, Modbus, and so on). |
|
Get camera status information in a solution, such as connection status, temperature, and transfer rate. |
|
Read and write global variables in a solution (integer, floating-point, string, queue, and so on). |
|
Get Step information, pin Step output ports, and get Step output data (such as point poses) after running a project. |
|
Read and set Step parameters in JSON format (including access to multi-level parameters). |
|
Production Interface operations. |
|
Production Interface communication operations. |
Configure and Build Samples
The following steps use solution_basic as an example.
-
Open
CMakeLists.txtin the sample directory and setVISION_SDK_DIRto the actual SDK path:set(VISION_SDK_DIR path/to/development/cpp) -
Create a build directory in the sample directory and run CMake:
mkdir build && cd build cmake -DCMAKE_BUILD_TYPE=Release .. -
Open the generated
.slnfile with Visual Studio, or build with the command below:cmake --build . --config Release
Run Samples
-
Make sure Mech-Vision is running, or start the Vision service through SDK APIs (see Start the Vision service).
-
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"); -
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})");
}