It looks like your current method is counting every instance of blocks used in the file, including nested blocks within other blocks. To find the top-level blocks (Master Blocks), you need to identify blocks that are directly inserted into the model space and not just used within other blocks.
Here's how you can modify your method to achieve this:
-
Identify Nested Blocks: First, create a set to keep track of all instance definitions that are nested within other instance definitions.
-
Identify Used Blocks in Model Space: Then, create another set to keep track of instance definitions that are directly used in the model (i.e., not nested within other blocks).
-
Determine Master Blocks: Master Blocks are those that are used in the model space and are not nested within other blocks.
Here's the modified code with comments explaining each step:
public List<string> FindInstanceDefinitionsInFiles(string filePath)
{
// Load the 3dm file
var file = File3dm.Read(filePath);
List<string> masterBlocksName = new List<string>();
if (file == null)
{
Rhino.RhinoApp.WriteLine("Unable to read the file.");
return masterBlocksName;
}
// Retrieve all instance definitions
var instanceDefinitions = file.AllInstanceDefinitions;
// Set to store IDs of instance definitions that are nested within other instance definitions
var nestedDefinitionIds = new HashSet<Guid>();
// For each instance definition, find nested instance definitions
foreach (var parentDef in instanceDefinitions)
{
foreach (var obj in parentDef.GetObjects())
{
if (obj.Geometry is InstanceReferenceGeometry instanceRef)
{
// Add the ID of the nested instance definition
nestedDefinitionIds.Add(instanceRef.ParentIdefId);
}
}
}
// Set to store IDs of instance definitions that are used in the model space (not nested within other blocks)
var usedDefinitionIds = new HashSet<Guid>();
// Check which instance definitions are used directly in the model (file.Objects)
foreach (var obj in file.Objects)
{
if (obj.Geometry is InstanceReferenceGeometry instanceRef)
{
// Add the ID of the instance definition used in the model space
usedDefinitionIds.Add(instanceRef.ParentIdefId);
}
}
// The top-level blocks are those used in the model and not nested in other instance definitions
foreach (var id in usedDefinitionIds)
{
if (!nestedDefinitionIds.Contains(id))
{
var instanceDef = instanceDefinitions.FindId(id);
if (instanceDef != null)
{
masterBlocksName.Add(instanceDef.Name);
}
}
}
return masterBlocksName;
}
Explanation:
-
Nested Blocks Identification:
- We iterate over all instance definitions and their objects.
- If an object within an instance definition is itself an
InstanceReferenceGeometry, we add its ParentIdefId to nestedDefinitionIds. This set now contains all blocks that are nested within other blocks.
-
Used Blocks in Model Space:
- We go through all objects in
file.Objects (the model space objects).
- If an object is an
InstanceReferenceGeometry, we add its ParentIdefId to usedDefinitionIds. This set now contains all blocks used directly in the model space.
-
Determining Master Blocks:
- We iterate over all
usedDefinitionIds.
- If an ID is not present in
nestedDefinitionIds, it means the block is used directly in the model space and is not nested within another block. Therefore, it's a top-level block (Master Block).
- We add the name of this instance definition to
masterBlocksName.
This modification ensures that only the top-level blocks are returned, excluding any blocks that are only used as nested components within other blocks.
Note on Syntax Fixes:
-
In your original code, List<string> masterBlocksName = []; is incorrect syntax in C#. It should be initialized as new List<string>();.
-
Ensure that you have the necessary using directives at the top of your file:
using System;
using System.Collections.Generic;
using Rhino;
using Rhino.FileIO;
using Rhino.Geometry;
Testing the Method:
After making these changes, test the method with a Rhino file that you know contains nested blocks and top-level blocks to confirm that it correctly identifies the Master Blocks.
Additional Recommendations:
-
Error Handling: Consider adding more error handling to manage potential exceptions, especially when dealing with file I/O operations.
-
Performance: If you're dealing with very large files, you might want to optimize the code further by minimizing the number of iterations or by using parallel processing where appropriate.
I hope this helps you modify your method to correctly find all the top-level blocks in your Rhino file!