Skip to main content

Get model file information

You can get the model file (or project file) to which the specified model belongs with the ModelUnit property (IModelUnit type) of the IModel object. You can access the model file information by using the IModelUnit property.

Model file path

public void GetModelFilePath(ICommandContext c, ICommandParams p) 
{
var model = c.App.Workspace.CurrentModel;

//Get model file path
var modelFilePath = model.ModelUnit?.UnitPath ?? "";

//...
}

Check if model file is read-only

public void CheckModelFileIsReadOnly(ICommandContext c, ICommandParams p) 
{
var model = c.App.Workspace.CurrentModel;

//Determine if it is read-only
var isModelFileReadOnly = model.ModelUnit?.IsReadonly ?? false;

//...
}
info

The IsReadonly method of the IModel object is used to check the ModelUnit You can check if a model file is read-only by shortcutting property access.

Example of extension method

It is useful to prepare an extension method as follows.

public static class IModelExtension 
{
///<summary>
///Get the path of the model file.
///</summary>
///<param name="self">Target model. </param>
///<returns>If associated with a model unit, the unit path, otherwise an empty string. </returns>
public static string GetModelUnitPath(this IModel self) => self.ModelUnit?.UnitPath ?? "";
}