Get products
An example of getting the features and products of a product line using the extension API.
public void OutputFeatureAndProducts(ICommandContext c, ICommandParams p)
{
var project = c.App.Workspace.CurrentProject;
//Check that the project is a target for product line development.
if (!project.IsProductLineSupported) {
c.App.Output.WriteLine("sample", "This project has not started product line development.");
return;
}
//Get features under the feature model.
IFeatureModelCollection featureModels = project.ProductLineModel.FeatureModels;
foreach (IFeatureModel featureModel in featureModels)
{
//Output the feature model name.
c.App.Output.WriteLine("sample", $"Feature model: {featureModel.Name}");
foreach (IFeature rootFeature in featureModel.RootFeatures)
{
//Output the feature name and feature type of the first level.
c.App.Output.WriteLine("sample", $" Root feature: {rootFeature.Name} (Kind: {rootFeature.VariationKind})");
foreach (IFeature subFeature in rootFeature.SubFeatures)
{
//Outputs the feature name and feature type of the second level.
c.App.Output.WriteLine("sample", $" Sub feature: {subFeature.Name} (Kind: {subFeature.VariationKind})");
//In this sample, only the second level is retrieved by default,
//The third level and below can also be retrieved using the subFeature.SubFeatures property.
//By using recursive calls, you can retrieve all the features down to the end of the tree, regardless of the depth of the tree.
}
}
}
//Get products and features for each product
IConfigurationModel configurationModel = project.ProductLineModel.ConfigurationModel;
foreach (IProduct product in configurationModel.AllProducts)
{
//Output product name
c.App.Output.WriteLine("sample", $"Product: {product.Name}");
foreach (IFeature selectedFeature in product.SelectedFeatures)
{
//Output feature name for each product (feature name selected in product)
c.App.Output.WriteLine("sample", $" Selected feature: {selectedFeature.Name}");
}
}
}