We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Creating diagrams and shapes from scratch

So I’m testing Aspose’s eval to see if it works for my company’s needs. I want to start by simply drawing a few colored rectangles on a page and arranging them. When I run my console app (code below), everything seems to work except none of the coloring of the shapes works. In fact, even within visio (2007 on windows 7) I cannot modify the shape’s line or background color. I don’t fully understand the concept of a “Master”, and when I generate a blank VDX document and load that, there are zero Masters, so the examples in the documentation never work for me. Can you shed some light on this?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aspose.Diagram;

namespace diagramtest1
{
class Program
{
static void Main(string[] args)
{
Diagram diagram = new Diagram();
Page page0 = diagram.Pages[0];

Shape rectangle = new Shape();
rectangle.ID = GetNextShapeID(diagram);
rectangle.XForm.PinX.Value = 5;
rectangle.XForm.PinY.Value = 5;
rectangle.Type = TypeValue.Shape;
rectangle.Text.Value.Add(new Txt(“Hello World”));
rectangle.Line.LineColor.Value = “#ff0000”;
rectangle.Line.LineWeight.Value = 0.03041666666666667;
rectangle.Line.Rounding.Value = 0.1;
rectangle.Fill.FillBkgnd.Value = “#ff00ff”;
rectangle.Fill.FillForegnd.Value = “#0000ff”;
page0.Shapes.Add(rectangle);

diagram.Save(“Diagram1.vdx”, SaveFileFormat.VDX);
}

private static long _id = 0L;
private static long GetNextShapeID(Diagram diagram)
{
foreach (Page page in diagram.Pages)
foreach (Shape shape in page.Shapes)
{
long temp = GetMaxShapeID(shape);
if (temp > _id)
_id = temp;
}
_id++;
return _id;
}

private static long GetMaxShapeID(Shape shape)
{
long max = shape.ID;
foreach (Shape child in shape.Shapes)
{
long temp = GetMaxShapeID(child);
if (temp > max)
max = temp;
}
return max;
}
}
}


(Attached is the output VDX of this program) – which by the way the uploader won’t let me upload VDX files

Thanks in advance,
Chris Ray

Hi Chris,

You need to add masters from an existing diagram, template or stencil e.g. use attached stencil. Please install the latest version of Aspose.Diagram for .NET and use the code from http://docs.aspose.com/display/diagramnet/Add+shapes to create a new diagram and add shapes in it and feel free to contact us in case you have further comments or questions.

Best Regards,

Ok your suggestion seems to work.

Two more quick questions:
  1. Does the current version of Aspose.Diagram support gradients background colors? If not when is this proposed to be added?
  2. I took the code from the link you gave and modified it slightly, to simply add the three shapes in a loop 100 times (so there should be 300 shapes). But there is always only 10 shapes. Is this a limitation of the evaluation version?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aspose.Diagram;

namespace diagramtest1
{
class Program
{
static void Main(string[] args)
{
// Load a master from any existing diagram, stencil or template
// and add in the new diagram
string visioStencil = "Basic Shapes.vss";

string rectangleMaster = @"Rectangle", starMaster = @"Star 7", hexagonMaster = @"Hexagon";
int pageNumber = 0;
double width = 2, height = 2, pinX = 4.25, pinY = 5.5;

// Create a new diagram
Diagram diagram = new Diagram(visioStencil);

for (int i = 0; i < 100; i++)
{
pinY = i/10;
//Add a new rectangle shape
long shapeId = diagram.AddShape(pinX, pinY, width, height, rectangleMaster, pageNumber);
Shape shape = diagram.Pages[pageNumber].Shapes.GetShape(shapeId);
shape.Text.Value.Add(new Txt(@"Rectangle text."));
shape.Name = "Rectangle1";
shape.Line.LineColor.Value = "7";
shape.Line.LineWeight.Value = 0.03;
shape.Fill.FillBkgnd.Value = "1";
shape.Fill.FillForegnd.Value = "3";

//Add a new star shape
pinX = 2.0;
shapeId = diagram.AddShape(pinX, pinY, width, height, starMaster, pageNumber);
shape = diagram.Pages[pageNumber].Shapes.GetShape(shapeId);
shape.Text.Value.Add(new Txt(@"Star text."));
shape.Name = "Star1";
shape.Line.LineColor.Value = "#ff0000";
shape.Line.LineWeight.Value = 0.03;
shape.Fill.FillBkgnd.Value = "#ff00ff";
shape.Fill.FillForegnd.Value = "#0000ff";

//Add a new hexagon shape
pinX = 7.0;
shapeId = diagram.AddShape(pinX, pinY, width, height, hexagonMaster, pageNumber);
shape = diagram.Pages[pageNumber].Shapes.GetShape(shapeId);
shape.Text.Value.Add(new Txt(@"Hexagon text."));
shape.Name = "Hexagon1";
shape.Line.LineWeight.Value = 0.03;
shape.Fill.FillBkgnd.Value = "1";
shape.Fill.FillForegnd.Value = "3";
}
//Save the new diagram
diagram.Save("Drawing1.vdx", SaveFileFormat.VDX);
}
}
}

Hi Chris,

1. You can use FillPattern for this purpose as you can see in the following code.

shape.Fill.FillBkgnd.Value = "THEMEGUARD(RGB(255,255,0))";<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

shape.Fill.FillForegnd.Value = "THEMEGUARD(RGB(0,176,240))";

shape.Fill.FillPattern.Value = 31;

2. You are right, evaluation version allows 10 shapes only. Please visit http://docs.aspose.com/display/diagramnet/Evaluation+version+limitations for more details.

Please feel free to contact us in case you have further comments or questions.

Best Regards,