Loading Models, etc. : an Overview


Introduction

This first task for any application that uses MSBN3 is to load or create a model. Other common operations are copying, renaming, removing, and saving.

This overview illustrates the operations by annotating a simple Visual Basic program. The full program is available in Samples\VB\LoadingModels.bas.

Creating a MSBN object and Loading a Model

Here we create a new MSBN object. Then we use the Models collection's Add method to load file "auto.dsc" and call it "Auto". Any error message from the loading go into file "errorfile.log".

' Creating a new MSBN object
Dim aMSBN As New MSBN3Lib.MSBN

'Loading a model
Dim model1 As MSBN3Lib.Model
Set model1 = aMSBN.Models.Add("Auto", FileName:="auto.dsc", ErrorFilename:="errorfile.log")

Creating an Empty Model

An empty model also created with Add, but without the FileName argument.

'Creating an empty model
Dim model2 As MSBN3Lib.Model
Set model2 = aMSBN.Models.Add("Model 2")

Renaming and Loading a Model

To rename a model, change the value of its Name property.

'Rename it
model2.Name = "cat"

. The Load method on a model will replace the current contents of the model with the contents of a file. If a FileName is given, that file will be used. If no FileName is given, the last filename associated with the file (given by the Model's FileName property) is used. The Load method can include the name of an error file for messages generated during the load.

'Load
model2.Load FileName:="cat.dsc", ErrorFilename:="errorfile.log"

Saving a Model

The Save method on a model will save the contents of the model to a file. A FileName and FileFormat can specified. If either is not specified then their last values (found in the properties FileName and FileFormat) are used.

'Save the auto it to a different place and format
model1.Save FileName:="c:\temp\Auto0.xbn", FileFormat:=fileformat_Xml

Loading the Same Model Twice

The Models collection can not have two models with the same name, so here we don't specify a name. This causes a new unique name to be generated and allows us to load the auto model twice.

'Load in another copy of the auto model
aMSBN.Models.Add FileName:="auto.dsc", ErrorFilename:="errorfile.log"

Copying a Model

The Copy method applied to a model, returns a copy of that model. The copy is also added to the Models collection. If no name is given, the copy will have a generated name.

'Copy the cat model
model2.Copy

Listing All Models

Like any collection, the Models collection can be enumerated (see Collections and Maps: an Overview). Its Description property can create a string listing the name of all its models. For example:

'Print the name of the all models
Debug.Print aMSBN.Models.Description

produces "Auto,cat,Model(1),cat(2)".

Removing Models

As with any collection, elements of the Models collection can be removed with the Remove method. See Collections and Maps: an Overview for more information.

'Remove the cat model
aMSBN.Models.Remove "cat"

Details