Create a New Diagram

Skip to end of metadata
Go to start of metadata
Aspose.Diagram for .NET lets you create and work with Microsoft Visio diagrams from within your own applications. There are other ways of working with Visio files, most commonly, Microsoft Automation. Unfortunately, that has its limitations. Aspose.Diagram is powerful and fast and works independently of Microsoft applications.

This migration article shows how to use first VSTO and then Aspose.Diagram for .NET to create a new diagram and add some shapes to it. You'll notice that the Aspose.Diagram code is shorter than VSTO code. Feel free to use the code as a base for your own development and enhance it to meet your needs.

Creating a New Diagram with VSTO

VSTO lets you program with Microsoft Visio files. To create a new diagram:

  1. Create a Visio application object.
  2. Make the application object invisible.
  3. Create an empty diagram.
  4. Add shapes from Visio masters (stencils).
  5. Save the file as VDX.
C#
 
…….

using Visio = Microsoft.Office.Interop.Visio;
…….
 
Visio.ApplicationClass vdxApp = null;
Visio.Document vdxDoc = null;
try
{
    //Create Visio Application Object
    vdxApp = new Visio.ApplicationClass();

    //Make Visio Application Invisible
    vdxApp.Visible = false;

    //Create a new diagram
    vdxDoc = vdxApp.Documents.Add("");

    //Load Visio Stencil
    Visio.Documents visioDocs = vdxApp.Documents;
    Visio.Document visioStencil = visioDocs.OpenEx("Basic Shapes.vss",
        (short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenHidden);

    //Set active page
    Visio.Page visioPage = vdxApp.ActivePage;

    //Add a new rectangle shape
    Visio.Master visioRectMaster = visioStencil.Masters.get_ItemU(@"Rectangle");
    Visio.Shape visioRectShape = visioPage.Drop(visioRectMaster, 4.25, 5.5);
    visioRectShape.Text = @"Rectangle text.";

    //Add a new star shape
    Visio.Master visioStarMaster = visioStencil.Masters.get_ItemU(@"Star 7");
    Visio.Shape visioStarShape = visioPage.Drop(visioStarMaster, 2.0, 5.5);
    visioStarShape.Text = @"Star text.";

    //Add a new hexagon shape
    Visio.Master visioHexagonMaster = visioStencil.Masters.get_ItemU(@"Hexagon");
    Visio.Shape visioHexagonShape = visioPage.Drop(visioHexagonMaster, 7.0, 5.5);
    visioHexagonShape.Text = @"Hexagon text.";


    //Save diagram as VDX
    vdxDoc.SaveAs(AppDomain.CurrentDomain.BaseDirectory + @"\Drawing1.vdx");
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
finally
{
    //Close active document and quit
    vdxDoc.Close();
    vdxApp.Quit();
}

 
VB
 
…….
 
Imports Visio = Microsoft.Office.Interop.Visio 
…….
 
Dim vdxApp As Visio.ApplicationClass = Nothing
Dim vdxDoc As Visio.Document = Nothing
Try
	'Create Visio Application Object
	vdxApp = New Visio.ApplicationClass()

	'Make Visio Application Invisible
	vdxApp.Visible = False

	'Create a new diagram
	vdxDoc = vdxApp.Documents.Add("")

	'Load Visio Stencil
	Dim visioDocs As Visio.Documents = vdxApp.Documents
	Dim visioStencil As Visio.Document = visioDocs.OpenEx("Basic Shapes.vss", CShort(Fix(Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenHidden)))

	'Set active page
	Dim visioPage As Visio.Page = vdxApp.ActivePage

	'Add a new rectangle shape
	Dim visioRectMaster As Visio.Master = visioStencil.Masters.get_ItemU("Rectangle")
	Dim visioRectShape As Visio.Shape = visioPage.Drop(visioRectMaster, 4.25, 5.5)
	visioRectShape.Text = "Rectangle text."

	'Add a new star shape
	Dim visioStarMaster As Visio.Master = visioStencil.Masters.get_ItemU("Star 7")
	Dim visioStarShape As Visio.Shape = visioPage.Drop(visioStarMaster, 2.0, 5.5)
	visioStarShape.Text = "Star text."

	'Add a new hexagon shape
	Dim visioHexagonMaster As Visio.Master = visioStencil.Masters.get_ItemU("Hexagon")
	Dim visioHexagonShape As Visio.Shape = visioPage.Drop(visioHexagonMaster, 7.0, 5.5)
	visioHexagonShape.Text = "Hexagon text."


	'Save diagram as VDX
	vdxDoc.SaveAs(AppDomain.CurrentDomain.BaseDirectory & "\Drawing1.vdx")
Catch ex As Exception
	Console.WriteLine(ex.Message)
Finally
	'Close active document and quit
	vdxDoc.Close()
	vdxApp.Quit()
End Try

 

Creating a New Diagram with Aspose.Diagram for .NET

When programming with Aspose.Diagram, you do not need Microsoft Visio on the machine, and you can work independently of Microsoft Office Automation. To create a new diagram:

  1. Create an empty diagram.
  2. Add shapes from Visio masters (stencils).
  3. Save the file as VDX.
C#
 
…….
 
using Aspose.Diagram;
 
…….
  
string visioStencil = "Basic Shapes.vss";
// Create a new diagram
Diagram diagram = new Diagram(visioStencil);

//Add a new rectangle shape
long shapeId = diagram.AddShape(
    4.25, 5.5, 2, 1, @"Rectangle", 0);
Shape shape = diagram.Pages[0].Shapes.GetShape(shapeId);
shape.Text.Value.Add(new Txt(@"Rectangle text."));

//Add a new star shape
shapeId = diagram.AddShape(
    2.0, 5.5, 2, 2, @"Star 7", 0);
shape = diagram.Pages[0].Shapes.GetShape(shapeId);
shape.Text.Value.Add(new Txt(@"Star text."));

//Add a new hexagon shape
shapeId = diagram.AddShape(
    7.0, 5.5, 2, 2, @"Hexagon", 0);
shape = diagram.Pages[0].Shapes.GetShape(shapeId);
shape.Text.Value.Add(new Txt(@"Hexagon text."));

//Save the new diagram
diagram.Save("Drawing1.vdx", SaveFileFormat.VDX);

 
VB
 
…….
 
Imports Aspose.Diagram
 
…….
 
Dim visioStencil As String = "Basic Shapes.vss"
' Create a new diagram
Dim diagram As Diagram = New Diagram(visioStencil)

'Add a new rectangle shape
Dim shapeId As Long = diagram.AddShape(4.25, 5.5, 2, 1, "Rectangle", 0)
Dim shape As Shape = diagram.Pages(0).Shapes.GetShape(shapeId)
shape.Text.Value.Add(New Txt("Rectangle text."))

'Add a new star shape
shapeId = diagram.AddShape(2.0, 5.5, 2, 2, "Star 7", 0)
shape = diagram.Pages(0).Shapes.GetShape(shapeId)
shape.Text.Value.Add(New Txt("Star text."))

'Add a new hexagon shape
shapeId = diagram.AddShape(7.0, 5.5, 2, 2, "Hexagon", 0)
shape = diagram.Pages(0).Shapes.GetShape(shapeId)
shape.Text.Value.Add(New Txt("Hexagon text."))

'Save the new diagram
diagram.Save("Drawing1.vdx", SaveFileFormat.VDX)

 
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.