Created
June 10, 2024 18:46
Create Flowchart Diagram in C# | Flowchart Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace CreateFlowchart | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//schema for the diagram to be created | |
Input diagramObject = new Input() | |
{ | |
InputRectangles = new List<InputRectangle>() { | |
new InputRectangle() { | |
Name = "A", | |
Text = "Manager" | |
}, | |
new InputRectangle() { | |
Name = "B", | |
Text = "Team Leader" | |
}, | |
new InputRectangle() { | |
Name = "C", | |
Text = "Team Member" | |
}, | |
new InputRectangle() { | |
Name = "D", | |
Text = "Team Member" | |
}, | |
new InputRectangle() { | |
Name = "E", | |
Text = "Team Member" | |
} | |
}, | |
InputConnectors = new List<InputConnector>() { | |
new InputConnector() { | |
OriginShapeName = "A", | |
DestinationShapeName = "B" | |
}, | |
new InputConnector() { | |
OriginShapeName = "B", | |
DestinationShapeName = "C" | |
}, | |
new InputConnector() { | |
OriginShapeName = "B", | |
DestinationShapeName = "D" | |
}, | |
new InputConnector() { | |
OriginShapeName = "B", | |
DestinationShapeName = "E" | |
} | |
} | |
}; | |
Diagram diagram = new Diagram("BasicShapes.vss"); | |
Page page = diagram.Pages[0]; | |
Dictionary<string, long> shapeNames = new Dictionary<string, long>(); | |
//Adding shapes and connectors from the schema | |
foreach (var rectangle in diagramObject.InputRectangles) | |
{ | |
Shape shape = new Shape(); | |
var shapeId = diagram.AddShape(shape, @"Rectangle", 0); | |
shapeNames.Add(rectangle.Name, shapeId); | |
shape = page.Shapes.GetShape(shapeId); | |
shape.Text.Value.Add(new Txt(rectangle.Text)); | |
} | |
foreach (var connector in diagramObject.InputConnectors) | |
{ | |
var connectorId = diagram.AddShape(new Shape(), "Dynamic connector", 0); | |
page.ConnectShapesViaConnector(shapeNames[connector.OriginShapeName], | |
ConnectionPointPlace.Right, | |
shapeNames[connector.DestinationShapeName], | |
ConnectionPointPlace.Left, | |
connectorId); | |
} | |
LayoutOptions layoutOptions = new LayoutOptions() | |
{ | |
LayoutStyle = LayoutStyle.FlowChart, | |
Direction = LayoutDirection.LeftToRight, | |
SpaceShapes = 5, | |
EnlargePage = true | |
}; | |
diagram.Layout(layoutOptions); | |
page.PageSheet.PrintProps.PrintPageOrientation.Value = PrintPageOrientationValue.Landscape; | |
DiagramSaveOptions saveOptions = new DiagramSaveOptions() | |
{ | |
SaveFormat = SaveFileFormat.Vsdx, | |
AutoFitPageToDrawingContent = true | |
}; | |
diagram.Save("output.vsdx", saveOptions); | |
} | |
} | |
public class Input | |
{ | |
public System.Collections.Generic.List<InputRectangle> InputRectangles { get; set; } | |
public System.Collections.Generic.List<InputConnector> InputConnectors { get; set; } | |
} | |
public class InputRectangle | |
{ | |
public string Name { get; set; } | |
public string Text { get; set; } | |
} | |
public class InputConnector | |
{ | |
public string OriginShapeName { get; set; } | |
public string DestinationShapeName { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment