Search for a model on your own
You can use the search function of Next Design from the API. To use the search function, use the following method.
- Create a search object by using the
ISearchManager.Createmethod for theSearchproperty ofIApplication. - Add the search results to the search object obtained in 1 by using the
AddSearchResultmethod of theISearchobject. - When the search is complete, call the
EndSearchmethod of theISearchobject for the search object obtained in 1. This will display the search results on the Next Design screen.
public void SearchModels(ICommandContext c, ICommandParams p)
{
//Search for models whose name contains the keyword
const string KEYWORD = "TBD";
const string FIELD_NAME = "Name";
//Get all models
var allModels = c.App.Workspace.CurrentProject.GetAllChildren();
//Start the search
var search = c.App.Search.Create();
search.BeginSearch("Keyword search for model name", "match");
foreach (IModel model in allModels)
{
var fieldString = model.GetFieldString(FIELD_NAME);
if (fieldString.Contains(KEYWORD))
{
//Register the search result if the keyword is found
search.AddSearchResult(model, FIELD_NAME, $"Model name: {model.Name}");
}
}
//End the search
search.EndSearch();
//Output the search results
foreach (ISearchResultEntry result in App.Search.AllResults)
{
c.App.Output.WriteLine("sample", $"{result.Type}, {result.Fields}, {result.Message}, {result.Model.ModelPath}");
}
}