How to generate a thumbnail for each assembly component

This article proposes a solution to a common problem in CAD applications: saving a bitmap for each leaf node in an assembly. 

The suggested method is based on Eyeshot's assembly navigation system, so it is recommended that you read the article Assembly Navigation first.

Our Pulley Transmission article is used to build a sample scene:

Snag_3dd5100d.png

The assembly tree contains some duplicate nodes (such as Bearing, Support, Washer, and Nut) and some branch nodes (Axle, Bearing, Seal1, and Seal2 for example).

To visit each node of the tree and save a bitmap of each leaf node without duplicates, you could use the following method:

public void SaveThumbsRecursive(Design d, IList<Entity> entList, Dictionary<string, Bitmap> result)
{
    foreach (Entity e in entList)
    {
        if (e is BlockReference br)
        {
            d.SetCurrent(br, false);

            if (d.CurrentBlock.Entities.Any(ent => ent is BlockReference))
            {
                //  it's not a leaf node, continue the tree traversal
                SaveThumbsRecursive(d, br.GetEntities(d.Blocks), result);
            }
            else // it's a leaf node, open the block and save a bitmap
            {
                if (!result.ContainsKey(d.CurrentBlockReference.BlockName))
                {
                    d.OpenCurrentBlock(true);

                    d.ZoomFit();

                    result[d.CurrentBlock.Name] = d.RenderToBitmap(d.Size);

                    d.CloseOpenBlock(false);
                }
            }

            d.SetParentAsCurrent(false);
        }
    }

    // recompute the scene's BBox before returning
    if (d.CurrentBlockReference == null)
        d.UpdateBoundingBox();
}

The method recursively opens each block, so that parents' transformations are ignored and the only visible entities are the ones inside the block.

By calling the method above on the entire entity collection

SaveThumbsRecursive(design1, design1.Entities, blockNameBmpDictionary);

you will get the following output:Snag_3dca37de.png

 

 

Was this article helpful?
1 out of 1 found this helpful

Comments

0 comments

Please sign in to leave a comment.

Articles in this section

See more