SilverScreen Solid Modeler

Traversing entities with BosList

Traversing entities with BosList

Previous topic Next topic  

Traversing entities with BosList

Previous topic Next topic JavaScript is required for the print function  

SilverSharpAPI

 

Traversing Entities with BosList

 

 


One of the ways that SilverSharp extends the SilverEngine platform is by providing enumerators which allow C# programmers to make use of the foreach statement to traverse the geometry of a SilverScreen drawing much more simply. While recursion is an elegant way to traverse a tree, it can be a difficult concept to grasp (there isn't anything like a self-referential definition in the natural world). We at SDC think that the BosList class will quickly become a favorite because its methods simplify a complex task without losing functionality.

 


The best way to illustrate the worth of this new technique is to compare it with the old technique. The first example will use SilverC code to traverse the current drawing, recursively searching for objects beginning with the prefix "door" and counting them. The second example will use C# code to perform the same function.

 


 

The following code counts objects whose names begin with the prefix "door". It uses recursion to visit all of the OBJECT_NODEs in a SilverScreen drawing.

 

SilverC Code

 

 void main(void)

    {

    // Initialize the counting variable

 

    int door_count = 0;

 

    // Get a pointer to the root block

 

    BLOCK_NODE *root = (BLOCK_NODE *)get_bos("\\");

 

    // visit all descendants of the root block

 

    count_the_doors(root, &door_count);

 

    // Display the results

 

    error_message("The number of doors is %d", door_count);

    }

 

 

 void count_the_doors(BLOCK_NODE *blk, int *counter)

    {

    BOS_NODE *bos;

 

    for (bos = blk->first_bos; bos; bos = bos->next_node)

       {

       if ( bos->bits1 & BITS_BLOCK )

          count_the_doors((BLOCK_NODE *)bos), counter);

       else

       if ( bos->bits1 & BITS_OBJECT )

          {

          if ( ! strnicmp(bos->name, "door", 4) )

             ++ *counter;

          }

       }

    }

 

 

 

 


The following code also counts objects whose names begin with the prefix "door":

 

C# Code

 

 using SilverSharp;

 

 . . .

 

 int     door_count = 0;

 BosList bl = BosList.EveryBosNode();

   

 foreach ( BOS_NODE bos in bl )

    {

    if ( bos.IsObject && bos.name.StartsWith("door") )

       ++ door_count;

    }

 

 SC.error_message("The door count is {0}", door_count);

 

 

For a detailed look at the use of BosList to traverse a SilverScreen drawing see the Lister SilverSharp sample.

 

For a detailed look at the use of recursion to traverse a SilverScreen drawing see the Lister SilverC sample.