IFC Export - labelname sync definition

Hello,

I was wondering if theres a way to have the definition information (entity info) automatically added to IFC dynamic options. for example. if i have component type IFCbeam. i would like the name (ifclabel : …) to be the same as my component definition.

thanks in advance.

Unfortunately it is not possible to link the IFC parameters, such as IfcLabel, to the Component Attributes. But you can automate this with a Ruby script.

Dear,

is it possible to get some help around ruby console? I dont know where to start.

No problem, here is a small part of the script that I use for automatically assigning IFC data to component definitions.

Ruby script
### IFC PARAMETER FILL ### ------------------------------------------------------
mod = Sketchup.active_model # Open model
defs = mod.definitions # All definitions in model

# Check if IFC classifications are loaded in model
if mod.classifications['IFC 2x3']

  # Update all classified definitions in model
	defs.each do |definition|
		classify_type = definition.get_attribute 'AppliedSchemaTypes', 'IFC 2x3'
		if classify_type
			# Overwrite the IFC label name with the definition name
			path = ['IFC 2x3', classify_type.to_s, 'Name', 'IfcLabel']
			definition.set_classification_value(path, definition.name)
		end
	end
	UI.messagebox("Update completed")
end
To Do
  1. Copy the Ruby script.

  2. Open de Ruby Console in SketchUp.
    Window menu → Ruby Console

  3. Paste the Ruby script into the Ruby Console and press the Enter key.

When the script is executed, each IFC 2x3 classified component definition will have the IfcLabel parameter provided with the component definition name.

1 Like

Wow thanks nnijmeijer.
works like a charm.

is it possible to create a button that executes this ruby console coding automatically aswell?
and is there a place where i can check these codes to add maybe more data?

Nnijmeijer,

i’ve tried to add something but i keep on getting error messages.
the goal is to add the material name to objecttype in ifc.

IFC PARAMETER FILL ### ------------------------------------------------------

mod = Sketchup.active_model # Open model
defs = mod.definitions # All definitions in model

Check if IFC classifications are loaded in model

if mod.classifications[‘IFC 2x3’]

Update all classified definitions in model

defs.each do |definition|
	classify_type = definition.get_attribute 'AppliedSchemaTypes', 'IFC 2x3'
	if classify_type
		# Overwrite the IFC label name with the definition name
		path = ['IFC 2x3', classify_type.to_s, 'Name', 'IfcLabel']
		definition.set_classification_value(path, 

definition.name)
end
if classify_type
path = [‘IFC 2x3’, classify_type.to_s, ‘objecttype’, ‘IfcLabel’]
definition.set_classification_value(path, material=name)

	end
end
UI.messagebox("Update completed")

end

Just a few things in advance.
It is important that you post the Ruby code correctly on this forum so that it is readable.

If you want to do more with Ruby scripting in SketchUp or perhaps extension development, it is important to learn how to program with Ruby. Here are a number of useful links that give you more information

https://www.techotopia.com/index.php/Ruby_Essentials

https://ruby-doc.org/core-2.7.1/

The SketchUp Developers website explains more about how to get started with the Ruby API.

https://developer.sketchup.com/developers/welcome

The SketchUp Developers website explains more about how to get started with the Ruby API.

Now coming back to your questions.
You can package this script as an extension so that you can “start” it using a button in the toolbar.
And you can also enter other parameters automatically, but then you have to change the path.
See here the documentation from the Ruby API regarding the assignment of classification values.

In addition, I see a number of errors in the code.

I expect you will want to read the material of the component? In that case you have to read the material from the component instance of the definition. Multiple instances of a definition can each contain a different material.

And you must also pay close attention to how you describe the values ​​in the path. This is case sensitive!

Change this piece of code

path = ['IFC 2x3', classify_type.to_s, 'Name', 'IfcLabel']
definition.set_classification_value(path, definition.name)

Into this

path = ['IFC 2x3', classify_type.to_s, 'ObjectType', 'IfcLabel']
definition.set_classification_value(path, definition.instances[0].material.display_name.to_s)

In this case you read the material name from the first component instance of the relevant definition.
The ObjectType IfcLabel will now contain this value.

1 Like

dear,

what i have done now is inputted the line you suggested but i get this error message now.
I only need 2 definitions in my IFC parameter fill. name and material name.

code failed with an error: NoMethodError: undefined method `display_name' for nil:NilClass
```ruby
## IFC PARAMETER FILL ### ------------------------------------------------------
mod = Sketchup.active_model # Open model
defs = mod.definitions # All definitions in model

# Check if IFC classifications are loaded in model
if mod.classifications['IFC 2x3']

  # Update all classified definitions in model
	defs.each do |definition|
		classify_type = definition.get_attribute 'AppliedSchemaTypes', 'IFC 2x3'
		if classify_type
			# Overwrite the IFC label name with the definition name
	path = ['IFC 2x3', classify_type.to_s, 'ObjectType', 'IfcLabel']
definition.set_classification_value(path, definition.instances[0].material.display_name.to_s)
		end
	end
	UI.messagebox("Update completed")
	end

The code snippit is not yet “bullet proof”.
It only works if all definitions contain a material property.

image

If this is not the case, as shown above, then you will indeed get an error.

So you have to find out whether the instance of the definition contains a material.
If not, I will leave in this case, the variable inst_mat contains an empty string.

# Get the material from the first instance of the definition
# Or get a empty string ""
inst_mat = definition.instances[0].material.nil? ? "" : definition.instances[0].material.display_name
			
# Overwrite the IFC label ObjectType with the first definition instance material
path = ['IFC 2x3', classify_type.to_s, 'ObjectType', 'IfcLabel']
definition.set_classification_value(path, inst_mat)

You can combine the snippits of the Name and ObjectType in the IF statement.

Dear,
I use materials add a collor or texture to my component. Some of these materials i have made myself. and named aswell. but i never use the instance entry box in entity info.

so what i would like the ruby to do is go check each component for which material is linked to it and use the name as saved in the material library.
if thats impossible its no big deal for me to input the material manually in the in textbox.

i keep getting an error code when importing your lines. I tried a few different ways but always same error

Code failed with an error: NoMethodError: undefined method `material' for nil:NilClass


```ruby 
### IFC PARAMETER FILL ### ------------------------------------------------------
mod = Sketchup.active_model # Open model
defs = mod.definitions # All definitions in model

# Check if IFC classifications are loaded in model
if mod.classifications['IFC 2x3']

  # Update all classified definitions in model
	defs.each do |definition|
		classify_type = definition.get_attribute 'AppliedSchemaTypes', 'IFC 2x3'
		if classify_type
			# Overwrite the IFC label name with the definition name
			inst_mat = definition.instances[0].material.nil? ? "niet bepaald" : definition.instances[0].material.display_name
			
			path = ['IFC 2x3', classify_type.to_s, 'ObjectType', 'IfcLabel']
definition.set_classification_value(path, inst_mat)
		end
	end
	defs.each do |definition|
		classify_type = definition.get_attribute 'AppliedSchemaTypes', 'IFC 2x3'
		if classify_type
			path = ['IFC 2x3', classify_type.to_s, 'Name', 'IfcLabel']
			definition.set_classification_value(path, definition.name)
		end
	end

	UI.messagebox("Update completed")
end

sorry if i do stupid things.

That is not necessary.

The component definition contains / defines the content of a SketchUp component. Components are a collection of entities that can be applied and reused multiple times in a model. When a component is applied, it is an instance that refers to the component definition.

Restart SketchUp and run the code below.
This should work.

Ruby Code
### IFC PARAMETER FILL ### ------------------------------------------------------
mod = Sketchup.active_model # Open model
defs = mod.definitions # All definitions in model

# Check if IFC classifications are loaded in model
if mod.classifications['IFC 2x3']

  # Update all classified definitions in model
	defs.each do |definition|
		classify_type = definition.get_attribute 'AppliedSchemaTypes', 'IFC 2x3'
		if classify_type
			# Overwrite the IFC label name with the definition name
			path = ['IFC 2x3', classify_type.to_s, 'Name', 'IfcLabel']
			definition.set_classification_value(path, definition.name)
			
			# Get the material from the first instance of the definition
			# Or get a empty string ""
			inst_mat = definition.instances[0].material.nil? ? "niet bepaald" : definition.instances[0].material.display_name
			
			# Overwrite the IFC label ObjectType with the first definition instance material
			path = ['IFC 2x3', classify_type.to_s, 'ObjectType', 'IfcLabel']
			definition.set_classification_value(path, inst_mat)
		end
	end
	UI.messagebox("Update completed")
end

Dear,

thanks again for your effort. its much appreciated.
i got it working in an almost empty file but for some reason it wont work in an existing drawing file.
what could be the isseu here. the fact not everything is a component?
or other possible reason i cant recall for the moment.

Dear,

got it working its just an error, but the things that need to be done are done.

thank you for all your effort.

i’m going to close this topic

The script ran through the entire definitions list, and assumed that there was an instance of each definition in the 3D model. If there are definitions in the model, but no instances, the script will crash.

To Do

  1. Make sure you purge the project first (Purge Unused).
  2. Then run the script.