Click or drag to resize

IAnalysisModelService

[This is preliminary documentation and is subject to change.]

See IAnalysisModelService for technical documentation

This service is primarily used to modify an AnalysisModel

It allows you to add, update and remove IAnalysisObject objects

This page was last updated 2021-12-03

Examples
Retrieving the IAnalysisModelService from the container
IBootstrapper bootstrapper = new AnalysisDataModelBootstrapper();
using(IScopedServiceProvider scope = bootstrapper.CreateThreadedScope())
{
    IAnalysisModelService = scope.GetService<IAnalysisModelService>();
}
Adding an item to an AnalysisModel
IBootstrapper bootstrapper = new AnalysisDataModelBootstrapper();
AnalysisModel model = new AnalysisModel();
using(IScopedServiceProvider scope = bootstrapper.CreateThreadedScope())
{
  IAnalysisModelService modelService = scope.GetService<IAnalysisModelService>();

  StructuralPointConnection node = new StructuralPointConnection(Guid.NewGuid(), "N1", Length.Zero, Length.Zero, Length.Zero);

  // Note that node will be automatically validated before it is added to the model!
  if(modelService.AddItemToModel(model, node))
  {
    Console.WriteLine("Item added to model!");
  }
  else
  {
    Console.WriteLine("Failed to add item to model");
    // At this point, model.ValidationResults will contain an entry as to why it failed to add the item to the model. Look for an entry with an object identifier that has the same ID as the object you tried to add.
  }
}
Adding items to an AnalysisModel
IBootstrapper bootstrapper = new AnalysisDataModelBootstrapper();
AnalysisModel model = new AnalysisModel();
using(IScopedServiceProvider scope = bootstrapper.CreateThreadedScope())
{
  IAnalysisModelService modelService = scope.GetService<IAnalysisModelService>();

  StructuralPointConnection node = new StructuralPointConnection(Guid.NewGuid(), "N1", Length.Zero, Length.Zero, Length.Zero);
  StructuralPointConnection node2 = new StructuralPointConnection(Guid.NewGuid(), "N2", Length.FromMeters(2), Length.Zero, Length.Zero);
  StructuralPointConnection node3 = new StructuralPointConnection(Guid.NewGuid(), "N3", Length.FromMeters(2), Length.FromMeters(2), Length.Zero);
  StructuralPointConnection node4 = new StructuralPointConnection(Guid.NewGuid(), "N4", Length.Zero, Length.FromMeters(2), Length.Zero);

  // All objects will be automatically validated before being added to the model!
  IDictionary<Guid, bool> result = modelService.AddItemsToModel(model, node, node2, node3, node4)
  if(result.All(x => x.Value))
  {
    Console.WriteLine("Items added to model!");
  }
  else
  {
    Console.WriteLine("Failed to add (some) items to model");
    // At this point, model.ValidationResults will contain entries as to why it failed to add the item to the model. Look for an entry with an object identifier that has the same ID as the object you tried to add.
  }
}
Removing an item from an AnalysisModel
IBootstrapper bootstrapper = new AnalysisDataModelBootstrapper();
AnalysisModel model = new AnalysisModel();

using(IScopedServiceProvider scope = bootstrapper.CreateThreadedScope())
{
  IAnalysisModelService modelService = scope.GetService<IAnalysisModelService>();

  StructuralPointConnection node = model.OfType<StructuralPointConnection>().FirstOrDefault(x => x.Name == "N1");

  try
  {
    modelService.RemoveItemFromModel(model, node);
  }
  catch (ModelExchangerException mee) 
  {
    // An exception will be raised if the object you're trying to delete is being referenced by other objects (e.g. a beam that uses the node)
    // In this case, the node can't be removed
  }
}
See Also