Load a PartFinder model file saved from NxView

A short code example of how to use PartFinder model files exported from NxView using the Save model button. The example will use the search parameters exported along with the model.

1. Loading the model

To successfully generate a model, a stereo camera must be opened and a disparity map must be calculated together with a previous capture command.

 1int loadedModelId = -1;
 2std::string modelFilePath = examplesDir + "/C++/nxPartFinder/SphereModel.zip";
 3
 4NxLibCommand loadModel(cmdPartFinder);
 5loadModel.parameters()[itmFunction] = valLoadModel;
 6loadModel.parameters()[itmModelPath] = modelFilePath;
 7loadModel.execute();
 8
 9if (loadModel.result()[itmModelId].exists()) {
10	// PartFinder model file was compatible and loaded/generated directly
11
12	// Use model id of loaded model
13	loadedModelId = loadModel.result()[itmModelId].asInt();
14} else {
15	// PartFinder model file was not compatible
16
17	// WARNING: We can still try to regenerate the model using the stored
18	// parameters, but there is no guarantee that this will succeed in this
19	// version of EnsensoSDK/PartFinder.
20
21	// Use the parameters from the 'GenerateModel' node to regenerate the model
22	// in the version supported by the current
23	NxLibCommand generateModel(cmdPartFinder);
24	generateModel.parameters()[itmFunction] = valGenerateModel;
25	generateModel.parameters() << loadModel.result()[itmGenerateModel];
26	generateModel.execute();
27
28	// Use model id of regenerated model
29	loadedModelId = generateModel.result()[itmModelId].asInt();
30}
  • The file name and path in the highlighted section can be customized to point to your own saved model file saved from inside the NxView PartFinder / Search tab.

  • loadedModelId is read out to be used to reference the ModelId in an upcoming Find subcommand.

  • findParamsAsJson keeps the search settings which were stored by NxView along with the model to use them in an upcoming Find subcommand.

1. Search for Parts

1std::string findParamsAsJson = loadModel.result()[itmSupplementary][itmFind].asJson();
2
3NxLibCommand find(cmdPartFinder);
4find.parameters()[itmFunction] = valFind;
5find.parameters() << findParamsAsJson;
6find.parameters()[itmModelId] = loadedModelId;
7find.execute();
  • Parameters will be filled with the search parameters kept in the findParamsAsJson variable of the LoadModel call from above.

  • ModelId has to be set to fit to a model currently available in NxLib, either by a previous GenerateModel or LoadModel subcommand. loadedModelId refers to the read out result of the GenerateModel call from above.