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.
-
Visual Studio 2017 or later.
-
.NET Framework 4.7.2 or later.
-
Mech-Vision is installed and can run properly.
Sample Overview
The C# samples are located in development/csharp/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
-
Open the
.slnsolution file in the sample directory with Visual Studio. -
Right-click the project and select Add Reference. Add the following DLL files in
development/csharp/libas project references:-
MMind.Vision.dll -
Other dependency DLL files (if any)
-
-
Update the solution path in code (see the example below), then build the solution (F6).
Run Samples
-
Ensure that Mech-Vision is running.
-
Update the solution path in sample code:
string path = "D:/path/to/your/solution"; -
Press F5 to run, or run the executable file generated in
bin/Release/.
Code Examples
Open a Solution and Get Project Information (solution_basic)
using MMind.Vision;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
MmindVision.Initialize();
// Open a solution
var solution = new Solution();
var error = solution.Open("D:/data/vision_sdk_example",
string.Empty, string.Empty);
// Get solution information
var solutionInfo = new SolutionInfo();
error = solution.GetInfo(ref solutionInfo);
System.Console.WriteLine($"Solution: {solutionInfo.Name}");
// Get project information list
var projectInfos = new List<ProjectInfo>();
error = solution.GetAllProjectInfos(ref projectInfos);
foreach (var info in projectInfos)
System.Console.WriteLine($" Project: {info.Name}");
// Save and close the solution
error = solution.Save();
error = solution.Close();
MmindVision.Uninitialize();
}
}
Run a Project and Get Output Data (project_basic)
using MMind.Vision;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
class Example
{
static void Run()
{
var solution = new Solution();
solution.Open("D:/data/vision_sdk_example", string.Empty, string.Empty);
// Get project name
var projectInfos = new List<ProjectInfo>();
solution.GetAllProjectInfos(ref projectInfos);
var project = new Project(projectInfos[0].Name);
// Register running-state callback
project.SetRunningChangedCallback((cbData) => {
System.Console.WriteLine($"Project running: {cbData.IsRunning}");
});
// Run the project (wait until it finishes)
var projectResult = new ProjectResult();
project.Run(ProjectRunWaitState.Finished, "request_1", ref projectResult);
// Parse output data
var joOutput = JObject.Parse(projectResult.OutputJson);
var jaPickPoints = joOutput["workobject_data"]["pick_points"] as JArray;
}
}
Read and Set Step Parameters (step_props)
using MMind.Vision;
using Newtonsoft.Json.Linq;
class Example
{
static void Run()
{
var solution = new Solution();
solution.Open("D:/data/vision_sdk_example", string.Empty, string.Empty);
var step = new Step("3D Matching_1", "Matching");
// Read step parameters
var jaPropNames = new JArray { "confidenceThreshold" };
string propsJson = string.Empty;
step.GetPropertiesJson(jaPropNames.ToString(), ref propsJson);
// Set step parameters
var joProps = new JObject { { "confidenceThreshold", 0.8 } };
step.SetPropertiesJson(joProps.ToString());
}
}