Smrf.NodeXL.ExcelTemplate.SubgraphImageCreator.CloneVertexIntoSubgraph C# (CSharp) Method

CloneVertexIntoSubgraph() protected method

protected CloneVertexIntoSubgraph ( IVertex oOriginalVertex, IGraph oSubgraph, Decimal decLevels ) : IVertex
oOriginalVertex IVertex
oSubgraph IGraph
decLevels Decimal
return IVertex
    CloneVertexIntoSubgraph
    (
        IVertex oOriginalVertex,
        IGraph oSubgraph,
        Decimal decLevels
    )
    {
        Debug.Assert(oOriginalVertex != null);
        Debug.Assert(oSubgraph != null);
        Debug.Assert(decLevels >= 0);
        AssertValid();

        // Get the original vertices and edges to clone.  For the vertex
        // dictionary, the key is the IVertex and the value is the vertex's
        // level, which is the distance of the vertex from oOriginalVertex.
        // For the edge HashSet, the key is the IEdge.

        Dictionary<IVertex, Int32> oOriginalVerticesToClone;
        HashSet<IEdge> oOriginalEdgesToClone;

        SubgraphCalculator.GetSubgraph(oOriginalVertex, decLevels, true,
            out oOriginalVerticesToClone, out oOriginalEdgesToClone);

        // Clone the vertices.  This dictionary maps the IDs of the original
        // vertices to their clones.

        Dictionary<Int32, IVertex> oOriginalToSubgraphVertexMapper =
            new Dictionary<Int32, IVertex>();

        IVertexCollection oSubgraphVertices = oSubgraph.Vertices;

        foreach (IVertex oOriginalVertexToClone in
            oOriginalVerticesToClone.Keys)
        {
            IVertex oSubgraphVertex =
                oOriginalVertexToClone.Clone(false, false);

            oSubgraphVertices.Add(oSubgraphVertex);

            oOriginalToSubgraphVertexMapper.Add(oOriginalVertexToClone.ID,
                oSubgraphVertex);
        }

        // This dictionary is no longer needed.

        oOriginalVerticesToClone.Clear();
        oOriginalVerticesToClone = null;

        // Clone the edges.

        IEdgeCollection oSubgraphEdges = oSubgraph.Edges;

        foreach (IEdge oOriginalEdgeToClone in oOriginalEdgesToClone)
        {
            IVertex [] aoOriginalVertices = oOriginalEdgeToClone.Vertices;

            IVertex oSubgraphVertex1 =
                oOriginalToSubgraphVertexMapper[aoOriginalVertices[0].ID];

            IVertex oSubgraphVertex2 =
                oOriginalToSubgraphVertexMapper[aoOriginalVertices[1].ID];

            oSubgraphEdges.Add(oSubgraphVertex1, oSubgraphVertex2,
                oOriginalEdgeToClone.IsDirected);
        }

        Debug.Assert( oOriginalToSubgraphVertexMapper.ContainsKey(
            oOriginalVertex.ID) );

        return ( oOriginalToSubgraphVertexMapper[oOriginalVertex.ID] );
    }