Change attributes

Hello,

I’m trying to create a plugin that can create components.
My question is: how can I set the component attributes when I create a new component?

E.g I can set the dimensions (LenX, LenY, LenZ) using this code:

    def self.makebox(x, y, z)
        model = Sketchup.active_model
        model.start_operation('Komponens létrehozása', true)
        group = model.active_entities.add_group
        entities = group.entities
        points = [
            Geom::Point3d.new(0,    0,      0),
            Geom::Point3d.new(x.mm, 0,      0),
            Geom::Point3d.new(x.mm, y.mm,   0),
            Geom::Point3d.new(0,    y.mm,   0)
        ]
        # Add the face to the entities in the model
        face = entities.add_face(points)
        face.pushpull(-z.mm)
        model.commit_operation
    end

But I would like to set other attributes of the new component too, like Name, Summary, Material end even add some custom attributes. How can I do that?

Thank you for your help.

This question has been asked previously, and has been discussed in this category and in the Dynamic Components category.

The Dynamic Components extension is proprietary and closed source, so there is no manual or API to “roll your own” dynamic component via code.

To do so you have to fully understand how DCs work and use an attribute inspector extension to watch both the definition and instance’s "dynamic_attributes" dictionaries to see how the extension creates and modifies attributes.

For the attributes name, summary, and custom ones, they can be attached as entity’s attributes using set_attribute.

Thank you!
I guess the “key” is the attribute name (Name, Summary, etc) but what should be the “dict_name”?

I think I answered this above. :arrow_up:

Thank you.
Unfortunately it does not work for some reason.
I changed the code:

def self.makebox(x, y, z)
        model = Sketchup.active_model
        model.start_operation('Komponens létrehozása', true)
        group = model.active_entities.add_group
        entities = group.entities
        points = [
            Geom::Point3d.new(0,    0,      0),
            Geom::Point3d.new(x.mm, 0,      0),
            Geom::Point3d.new(x.mm, y.mm,   0),
            Geom::Point3d.new(0,    y.mm,   0)
        ]
        # Add the face to the entities in the model
        face = entities.add_face(points)
        entity1 = entities[1]
        entity1.set_attribute("dynamic_attributes", "summary", "testsummary")
        entity1.set_attribute("dynamic_attributes", "name", "testname")
        face.pushpull(-z.mm)
        model.commit_operation
    end

But the attribute list is empty

If I add them manually in the Component Attributes window than they appear in the Attribute Inspector but the “summary” and “name” fields are empty. Only the lenx, leny and lenz fields contain correct values.

Where did I make a mistake?
Thank you.

  dcs = $dc_observers.get_latest_class
  dcs.redraw_with_undo(entity1)

https://sketchucation.com/forums/viewtopic.php?f=180&t=24241

Thanks, but it is not working.

I believe that a Dynamic Component wants there to be a component at it’s top level.
It can have both nested (child) dynamic group instances and dynamic component instances.
If you do not have your DC setup this way, the extension may not work properly.

Also, as I said previously, the DC extension is complex and has complex behavior.

Nested dynamic groups always have all dynamic attributes attached to an instance dictionary.

Nested dynamic components always have a dynamic dictionary attached to the definition that has all the default values. The nested dynamic component instances start out with only a few informational attributes in their dynamic dictionary, and may contain any overrides (deviations) from the default values in their definition’s dynamic dictionary.

Also, what the user sees as 1 dynamic property, is actually a set of dictionary attributes.
You also did not add the attribute for whether the user can see or edit them in the Options dialog.

The main mistake is that the code above is adding the DC attributes into a dictionary attached to an edge, and not to the group instance.

This code works when attaching the attributes to the group and not one of it’s edges:

module BoxTest

  DCO ||= $dc_observers.get_latest_class

  def self.makebox(x, y, z)
    model = Sketchup.active_model
    model.start_operation('Komponens létrehozása', true)
      # Define an array of points:
      points = [
        Geom::Point3d.new(0,    0,      0),
        Geom::Point3d.new(x.mm, 0,      0),
        Geom::Point3d.new(x.mm, y.mm,   0),
        Geom::Point3d.new(0,    y.mm,   0)
      ]
      # Add a group to the entities in the model:
      group = model.active_entities.add_group
      # Add the face to the entities in the group:
      face = group.entities.add_face(points)
      face.pushpull(-z.mm)
      #
      #entity1 = group.entities[1]
      #puts entity1.inspect # <<----<<< it's an Edge !
      #
      # Set the DC attributes on the group instance:
      group.set_attribute("dynamic_attributes", "summary", "testsummary")
      group.set_attribute("dynamic_attributes", "name", "testname")
      # Redraw the instance object:
      DCO.redraw_with_undo(group)
    model.commit_operation
  end
end

BoxTest::makebox(100,100,100)

Thank you. I tried to use your modification.
I got an error message: Error: #<NoMethodError: undefined method `get_latest_class’ for nil:NilClass>
But then I removed it and changed this line: DCO.redraw_with_undo(group)
To this one: $dc_observers.get_latest_class.redraw_with_undo(group)

And it works as expected.

Thank you so much!

This means that the DC extension was not yet loaded when this statement was evaluated.
If you have this in a file, then you need to be sure that the DC extension loads first.

At the top of the file put …

require "su_dynamiccomponents"