protected override void InitDiagram() { base.InitDiagram(); m_DrawingDocument.HistoryService.Pause(); try { NDrawing drawing = m_DrawingDocument.Content; NPage activePage = drawing.ActivePage; // Hide grid and ports drawing.ScreenVisibility.ShowGrid = false; drawing.ScreenVisibility.ShowPorts = false; // Create all shapes NBrainstormingShapeFactory factory = new NBrainstormingShapeFactory(); factory.DefaultSize = new NSize(60, 60); for (int i = 0; i < factory.ShapeCount; i++) { NShape shape = factory.CreateShape(i); shape.HorizontalPlacement = ENHorizontalPlacement.Center; shape.VerticalPlacement = ENVerticalPlacement.Center; shape.HorizontalPlacement = ENHorizontalPlacement.Center; shape.VerticalPlacement = ENVerticalPlacement.Center; shape.Text = factory.GetShapeInfo(i).Name; MoveTextBelowShape(shape); activePage.Items.Add(shape); } // Arrange them NList <NShape> shapes = activePage.GetShapes(false); NLayoutContext layoutContext = new NLayoutContext(); layoutContext.BodyAdapter = new NShapeBodyAdapter(m_DrawingDocument); layoutContext.GraphAdapter = new NShapeGraphAdapter(); layoutContext.LayoutArea = activePage.GetContentEdge(); NTableFlowLayout tableLayout = new NTableFlowLayout(); tableLayout.HorizontalSpacing = 30; tableLayout.VerticalSpacing = 50; tableLayout.Direction = ENHVDirection.LeftToRight; tableLayout.MaxOrdinal = 5; tableLayout.Arrange(shapes.CastAll <object>(), layoutContext); // size page to content activePage.Layout.ContentPadding = new NMargins(40); activePage.SizeToContent(); } finally { m_DrawingDocument.HistoryService.Resume(); } }
/// <summary> /// Overriden to create a random graph template in the specified document. /// </summary> /// <param name="document">The document to create a graph in.</param> protected override void CreateTemplate(NDrawingDocument document) { if (m_nEdgeCount < m_nVertexCount - 1) { throw new Exception("##The number of edges must be greater than or equal to the (number of vertices - 1) in order to generate a connected graph"); } if (m_nEdgeCount > MaxEdgeCount(m_nVertexCount)) { throw new Exception("##Too many edges wanted for the graph"); } int i; Random random = new Random(); NPage activePage = document.Content.ActivePage; NShape[] vertices = new NShape[m_nVertexCount]; NList <NPointI> edges = GetRandomMST(m_nVertexCount); NPointI edgeInfo; NSizeI minSize = m_MinVerticesSize.Round(); NSizeI maxSize = m_MaxVerticesSize.Round(); maxSize.Width++; maxSize.Height++; // Create the vertices for (i = 0; i < m_nVertexCount; i++) { vertices[i] = CreateVertex(m_VerticesShape); double width = random.Next(minSize.Width, maxSize.Width); double height = random.Next(minSize.Height, maxSize.Height); vertices[i].SetBounds(new NRectangle(0, 0, width, height)); activePage.Items.AddChild(vertices[i]); } // Generate the edges for (i = m_nVertexCount - 1; i < m_nEdgeCount; i++) { do { // Generate a new edge edgeInfo = new NPointI(random.Next(m_nVertexCount), random.Next(m_nVertexCount)); }while (edgeInfo.X == edgeInfo.Y || edges.Contains(edgeInfo) || edges.Contains(new NPointI(edgeInfo.Y, edgeInfo.X))); edges.Add(edgeInfo); } // Create the edges for (i = 0; i < m_nEdgeCount; i++) { edgeInfo = edges[i]; NShape edge = CreateEdge(ENConnectorShape.RoutableConnector); activePage.Items.AddChild(edge); edge.GlueBeginToGeometryIntersection(vertices[edgeInfo.X]); edge.GlueEndToShape(vertices[edgeInfo.Y]); } // Apply a table layout to the generated graph NTableFlowLayout tableLayout = new NTableFlowLayout(); tableLayout.MaxOrdinal = (int)Math.Sqrt(m_nVertexCount) + 1; tableLayout.HorizontalSpacing = m_VerticesSize.Width / 5; tableLayout.VerticalSpacing = m_VerticesSize.Width / 5; NDrawingLayoutContext context = new NDrawingLayoutContext(document, activePage); tableLayout.Arrange(new NList <object>(NArrayHelpers <NShape> .CastAll <object>(vertices)), context); }