When I Insert a Block instance (linked) into my Rhino document everything seems OK. But if I remove the block instance from the document (using the Block Manager or by simply deleting the block), some info about the (completely removed linkd block) remains in the document. I wouldn’t have noticed this if i hadn’t made a C# script trying to pick up the block.
Fig.1. Only one linked Block exists in the file, but after inserting and removing three times…:
Fig 2. After deleting the (only) existing block:
Only after reloading the Rhino document the “block remains” are removed.
After a number of insertions/deletions (I still have only one instance of the linked block definition in my file) my script reports the following to the Rhino command line:
There shouldn’t be any problem with the following “debug script” (I prevent it from crashing, because there’s actually only one (1) instancedefinition in the file):
private void RunScript(string BlockName, ref object Block)
{
RhinoApp.ClearCommandHistoryWindow();
var block_defs = doc.InstanceDefinitions;
var block_defs_count = block_defs.Count;
if (block_defs_count == 0)
{
RhinoApp.WriteLine("Document contains no instance definitions.");
return;
}
for (var i = 0; i < block_defs_count;i++)
{
RhinoApp.WriteLine("-------------------------------");
RhinoApp.WriteLine(String.Format("Reading block definition #{0} from file.", i));
var block = doc.InstanceDefinitions[i];
if (block == null)
continue;
RhinoApp.WriteLine(String.Format("Found {0} block definitions.", block_defs_count));
RhinoApp.WriteLine(String.Format("Block Name: {0}", block.Name));
RhinoApp.WriteLine(String.Format("Block Type: {0}", block.GetType()));
RhinoApp.WriteLine(String.Format("Block IsReference: {0}", block.IsReference));
// Block ID's
var id = block.GetObjectIds();
RhinoApp.WriteLine(String.Format("Id.Length = {0}", id.Length));
if (id == null || id.Length < i+1)
{
RhinoApp.WriteLine(String.Format("Ops, Id #{0} doesn't actually exist!", i));
continue;
}
RhinoApp.WriteLine(String.Format("Block Id: {0}", id[i]));
// Block Objects
var objs = block.GetObjects();
if (objs == null || objs.Length == 0)
{
RhinoApp.WriteLine(String.Format("Ops, Block #{0} doesn't actually exist!", i));
continue;
}
RhinoApp.WriteLine(String.Format("Block Object: {0}", objs[i]));
}
I tried Purge, but no definitions were purged:
This means that the excess number of block definitions remains in the file an “Count” returns the wrong numbers, and so any script trying to access or iterate over the non-existing ghost InstanceDefinitions will crash with a groan.
Is there any workaround I could apply to avoid, or to clean up this messy remnant info so that scripts can iterate over the InstanceDefinitions without crashing?
// Rolf