How to apply attributes through code? and how do they work?
-
You pretty much have it there in your first post. To add attributes to an entity you use
.set_attribute.As your plugin creates the group with the box, you attach the data you want to that group.
-
Yes it all goes well without any errors to debug. But When I right-click and click 'Show Attributes' there is nothing there but this in the dialog box:::
%(#8080FF)[**Add Category...
You can create/delete categories of Attributes and add/edit and delete individual attributes within a category.
To get started, click on the Add Category... link above.Tip: You can create standard categories by clicking here.
Add an attribute Delete Category Cancel Accept**]
Is there another way of access the attributes I applied? And say I save this box, will the attributes be saved too?
-
I sounds like you haven't added the attributes to the correct entity then.
I take it the first post included parts of the code you used - question is: where does the
entities[0]variable come from? -
I think the different entities are stored in an array format right? I copied the exact thing from the Google API.
[url]
http://code.google.com/apis/sketchup/docs/ourdoc/entity.html#set_attribute[/url] -
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 theentitiesvariable from? What entities collection does it refer to?When you create a group for your box. You're using the
.add_groupmethod 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_groupThis 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_entitiesalways 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_groupis 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
groupand 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_modelIS the model NEVER anything else like a ComponentInstance or a Group.
You must specify either the ComponentInstance or the Group, and thenset_attributesfor them.
If you want toset_attributesfor 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 enddoes 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
orinstead of||makes this lines not evaluate correctly?
if (Sketchup.active_model.selection[0].typename == "Group" or Sketchup.active_model.selection[0].typename == "ComponentDefinition")orhas 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.definitionNow you can find an
instance.definitionbut 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.entitiesgives 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...
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement