How to apply attributes through code? and how do they work?
-
I really sound like you are attaching it to some random entity. Without seeing the rest of the code I can't tell you specifically what you're doing wrong - but I'm pretty sure that
entities[0]
does not refer to the group you want to add the attributes to.
I ask you again: where are you getting theentities
variable from? What entities collection does it refer to?When you create a group for your box. You're using the
.add_group
method somewhere. That method returns a reference to the group you created. This is the entity you need to attach the attribute data to. -
here is the code i think its fine..
def draw_beam model = Sketchup.active_model entities = model.entities group = entities.add_group entities = group.entities pt = [] pt[0] = [0, 0, 0] pt[1] = [@b, 0, 0] pt[2] = [@b, 0, @h] pt[3] = [0, 0, @h] c_section = entities.add_face pt @length = -@length if( c_section.normal.dot(Y_AXIS) < 0 ) c_section.pushpull @length entity1 = entities[1] status = entity1.set_attribute "Dimensions", "Length", "#{@h} mm" status = entity1.set_attribute "Dimensions", "Width", "#{@b} mm" status = entity1.set_attribute "Dimensions", "Effective Length", "#{@d} mm" end
-
"Well, there you're problem!"
You are assigning the attributes to a random entity inside your group.
group = entities.add_group
Here: you have your group assigned the the variablegroup
. This is the value you want to attach your attributes to.p.s.
entities = model.entities group = entities.add_group
This will always create the group at the top level of the model. If the user opens a group or component when this method is run, the user might expect the group to be created in this context - but your code will not do that.
model.active_entities
always refer to the current context. -
Ahh there is my mistake! I love finding mistakes..
I thought group is a method and not a variable!
-
.add_group
is a method. But it returns a variable - reference to the newly created group. http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/entities.html#add_groupYou created the variable
group
and assigned it to the return value of.add_group
. -
@beam = Sketchup.active_model.selection[0] bbox = @beam.bounds point1 = bbox.max point2 = bbox.min b = (point1[0] - point2[0]).abs.to_mm.to_i length = (point1[1] - point2[1]).abs.to_mm.to_i h = (point1[2] - point2[2]).abs.to_mm.to_i @beam.set_attribute "Beam Information", "1. Type of Concrete", "#{@cstrength}"
Ok one problem now, I have selected active_model as either ComponentInstance or Group. When its a group I can apply the attributes with no problem! But when its a component I am not able to add the information. Is there a way I can add all this information to it? and Is there a way to apply the same attributes to all the ComponentInstances in the active_model?
-
@meeta said:
@beam = Sketchup.active_model.selection[0] bbox = @beam.bounds point1 = bbox.max point2 = bbox.min b = (point1[0] - point2[0]).abs.to_mm.to_i length = (point1[1] - point2[1]).abs.to_mm.to_i h = (point1[2] - point2[2]).abs.to_mm.to_i @beam.set_attribute "Beam Information", "1. Type of Concrete", "#{@cstrength}"
Ok one problem now, I have selected active_model as either ComponentInstance or Group. When its a group I can apply the attributes with no problem! But when its a component I am not able to add the information. Is there a way I can add all this information to it? and Is there a way to apply the same attributes to all the ComponentInstances in the active_model?
active_model
IS the model NEVER anything else like a ComponentInstance or a Group.
You must specify either the ComponentInstance or the Group, and thenset_attributes
for them.
If you want toset_attributes
for them, you must first specify them clearly...
IF the first thing in a selection is a Group you can apply attributes to it.
IF the first thing in a selection is a ComponentInstance you can apply attributes to it.
[You can even get the instance.definition of it and set_attributes for that]
I see no problems... except that you a not selecting the right type of thing to set_attributes to ?IF
@beam = Sketchup.active_model.selection[0]
IS the beam then it works... BUT is it the beam first thing in the selection ? You don't tell us enough to see what the@beam = Sketchup.active_model.selection[0]
might be ! -
apply the attibs to the definition.
-
what exactly is a component definition?
-
if (Sketchup.active_model.selection[0].typename == "Group" or Sketchup.active_model.selection[0].typename == "ComponentDefinition") menu.add_separator menu.add_item("Beam Analyser") { b = Beam.new b.bbox b.prompts . . . . . . def bbox @beam = Sketchup.active_model.selection[0] bbox = @beam.bounds point1 = bbox.max point2 = bbox.min b = (point1[0] - point2[0]).abs.to_mm.to_i length = (point1[1] - point2[1]).abs.to_mm.to_i h = (point1[2] - point2[2]).abs.to_mm.to_i end
does this make it more clear? If I choose a group, there is not problem. But if I choose a componentinstance, The attributes are applied without any problems. Unfortunately its not being applied to @beam.
-
Would it be that your use of
or
instead of||
makes this lines not evaluate correctly?
if (Sketchup.active_model.selection[0].typename == "Group" or Sketchup.active_model.selection[0].typename == "ComponentDefinition")or
has a lower precedence than||
http://www.techotopia.com/index.php/Ruby_Operator_Precedence -
Things [each called an
Entity
] that are 'visible' in a model are typically geometry [lines, faces etc], and groups and components [what is commonly called a component is actually a 'component-instance']. These groups and instances contain geometry - they can also contain sub-groups and other instances. This is a simplified picture since there's also Images, Text, Dimensions etc etc but lets keep it simple !
Within the model's 'database' there are other things you can't see but that you can use - inside 'collections' called Materials, Layers, Styles and ComponentDefinitions. Each of these will have entries like Material, Layer, Style and ComponentDefinition.So far so good ?
When you look at the Components in the Browser you are looking into the model's database and seeing the Component-definitions available for use.
When you select [highlight] something in a model you can get its 'class' [typename etc] so with the initial examples above you have Sketchup::Edge, Sketchup::Face, Sketchup::Group and Sketchup::ComponentInstance.
Let's assume you have somehow set a variable
instance=ss[i]
which is aSketchup::ComponentInstance
.
You can find its definition thusdefinition=instance.definition
Now you can find an
instance.definition
but conversely you can find adefinition.instances
[i.e. all of that definition's instances]
Any particular definition's instance doesn't contain anything at all - it is a marker for the definition itself, soinstance.definition.entities
gives you a list of entities inside the definition that that instance is using...
You can even 'swap' an instance's definition for another one:instance.definition=another_definition
...
Advertisement