How to get the price or length of Components


#1

i just want to get the length/price and the name of the instance.i am a beginner,thanks
i mean get them by use ruby api


#2

You need to give us some useful information about your model. Share an example SKP file.

Most likely you can do what you want with Dynamic Components and the Report Generator.


#3


here is the image about my question
just want to know price size , in the RubyMine


#5

i mean get them in ruby api


#6

The Price attribute must have been assigned to a component by the creator of the component, otherwise the text fields are empty.

If you want to get the price attribute (and others) from the component through the Ruby API, use entity.get_attribute('SU_DefinitionSet', 'Price').

You can iterate over the attribute dictionaries and print the existing keys so that you know how they are called

entity.attribute_dictionaries.each{|d| d.each{|k,v| puts("#{d.name} #{k} = #{v}") }

or use Attribute Inspector.


#7

image
it is right to get the like this:

model = Sketchup.active_model
entities = model.active_entities
edge = entities.add_line([0, 0, 0], [9, 9, 9])
value = edge.get_attribute("MyExtension", "MyProperty", 42)

but how to get it ,if it’s a component definitions,just like the image.


#8

You can only get attributes from a component definition if you have a reference to a component definition. So your question is:

How do I get a reference to a ComponentDefinition?


By the way, you can format code nicely in this forum if you write ``` like this:

```
model = Sketchup.active_model
entities = model.active_entities
edge = entities.add_line([0, 0, 0], [9, 9, 9])
value = edge.get_attribute("MyExtension", "MyProperty", 42)
```

It gives:

model = Sketchup.active_model
entities = model.active_entities
edge = entities.add_line([0, 0, 0], [9, 9, 9])
value = edge.get_attribute("MyExtension", "MyProperty", 42)

#9

you mean i nedd to set the definition “Price”?
but

Starting from SketchUp 2018+, the ComponentDefinition class contains a new default attribute dictionary named “SU_DefinitionSet” with default keys named named “Price”, “Size”, “Url”. See the Help article for more information. The dictionary cannot be deleted via ruby and an ArgumentError will be raised. The key/value pairs in the dictionary can be deleted safely.


#10

Instead of the reference edge you need a reference to the component definition with the name 组件#1.
Then you can get in the dictionary “SU_DefinitionSet” the attribute “Price”.

component_definition = model.definitions.find{ |definition| definition.name == "组件#1" }
if !component_definition.nil? # If it has been found
  component_definition.get_attribute("SU_DefinitionSet", "Price")
end

#11
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection

component_definition = mod.definitions.find{ |definition| definition.name == "组件#1" }
if !component_definition.nil? # If it has been found
  component_definition.get_attribute("SU_DefinitionSet", "Price")
end

see below,Thanks ALL!!


#12

If you want the price for the currently selected instance:

# get the active model
mod = Sketchup.active_model
# get the current selection
sel = mod.selection
# we just assume that there is at least 1 element selected and that the first selected element is a ComponentInstance
component_instance = sel[0]
# get the definition for the selected instance
component_definition = component_instance.definition
# now get the value for the price attribute
price = component_definition.get_attribute("SU_DefinitionSet", "Price")
puts "price: #{price}"