NShape Basic Tutorial

Basic Tutorial: Libraries

<< Click to Display Table of Contents >>

Navigation:  Basic Tutorial >

NShape Basic Tutorial

Basic Tutorial: Libraries

Previous pageReturn to chapter overviewNext page

In this step you will learn how to add shape libraries to a new NShape project.

Currently the WebVisits application still loads the Circles diagram at start-up. That was just for demonstration and is actually completely unnecessary. What you actually want is not loading an existing project but creating a new one.

How to create a new NShape project:

1.Replace the line

project1.Open();

by

project1.Create();

2.Remove the lines

project1.AutoLoadLibraries = true;

and

display1.LoadDiagram("Diagram 1");

3.Run the application. What you see now is an empty display and an empty toolbox.

4.When you select File > Load Statistics, you get an exception because the shape type Ellipse is not registered.

Why is Ellipse not registered? That is because you now have a complete fresh project with no shape libraries attached. In NShape, each project must determine by itself, which types of shapes it requires and the default is no shapes at all.

Therefore, you somehow must get some shape types from a library and there are two ways to do that: Dynamic and static loading of shape types. Dynamic loading means that you can load assemblies that contain shape types (so-called shape libraries) at run-time and those libraries will then be added to the project. That is how the NShape Designer delivered with the NShape package works, because that enables you to add your own shape libraries if you wish so.

Static loading means that the shape library is referenced from the project. The assembly needs not to be loaded explicitly but the shape types must still be registered with the project. And that is what you should do now, since WebVisits does not require dynamic libraries.

How to add a library to a project:

1.Add the line

project1.AddLibrary(typeof(Ellipse).Assembly, false);

right before the project creation.

2.Run the application and see how everything works again.

A new project requires a least one library to offer shapes.

A new project requires a least one library to offer shapes.

You may wonder how the libraries were loaded in the previous step. How did NShape know, after all, which library it had to load? For the answer look at this snippet from the "Circles.nspj" project file:

<?xml version="1.0" encoding="utf-8"?>
<dataweb_nshape version="2">
  <project id="00000000-0000-0000-0000-000000000000" last_saved_utc="2009-10-09 08:38:07">
  <libraries>
    <library name="GeneralShapes" assembly_name="Dataweb.NShape.GeneralShapes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1512080cd664963c" repository_version="2" />
  </libraries>

As you can see, when a project is saved, the list of required libraries is part of the stored information. So, whenever NShape opens a project it tries to find the required libraries (in the directories defined by LibrarySearchPaths) and loads them (if AutoLoadLibraries is allowed).

Tutorial Navigation