Hello!
I’m currently working on a plugin to add functions to Dynamic Components (DCs) in order to simplify the process of feasibility studies. While I already have a DC that helps me and my students in our classes, we’ve realized that more functionalities could greatly improve our use and teaching processes. Currently, in order to calculate certain aspects of our studies, we need to create reports and manually input the data into software tables. However, if we can access more information through the DC functions, we could display most, if not all, of the data instantly in the component option window, thereby streamlining the design process and validation in our classes and freeing up more time for actual teaching.
One of our initial ideas is to add a function that can count the number of entities inside the DC by name. For instance, if we want to count the number of “floor 01” components, we would simply add the function “=count_entities(“type_floor_1”)”. This would enable us to relate the data with urban parameters and validate or invalidate the study using the native DC logic functions. While we have more needs, we plan to take one step at a time.
To accomplish this, I’ve begun writing the necessary Ruby code. However, I’m encountering difficulties in getting it to work. The code is as follows:
require 'sketchup'
require 'su_dynamiccomponents.rb'
if defined?($dc_observers)
class DCFunctionsV1
protected
# DC Function Usage: =count_entities("ComponentDefinitionName")
# or =count_entities() to count all instances
def count_entities(*args)
if args.empty?
count = @source_entity.definition.instances.count
else
component_name = args.first
definition = Sketchup.active_model.definitions.find { |d| d.name == component_name }
count = definition ? definition.count_used_instances { |instance| instance.model == Sketchup.active_model } : 0
end
return count
end
end
end
Unfortunately, whenever I run the function, it always returns zero. I’ve tried various other versions of the code, including attempting to count by IfcClass name (IfcBuildingStorey), but the result is always the same. Can anyone help me with this issue?