Help creating a global attribute option box

Hi, can anyone help me with creating a global attribute option box, whereby I can use custom attribute values as references for my own dynamic components? Or let me know where I can learn about teh specific code to create it?

I don’t want to use the method of creating a ‘swatch’ or ‘reference component’ within the model. As I understand it, the sketchup model is itself a Parent component to anything created within it, but it doesn’t have it’s own attributes box. So once I have the code then I can create a toolbar button to access it.

Thanks!

The model has its own attribute dictionaries

Because the Dynamic Components extension does not treat the model itself as a dynamic instance with a dynamic definition. In fact, the model object does not have a definition unless it is imported into another model, in which case the import is converted to a component.

Anyway, creating a model-level "dynamic_attributes" dictionary will not likely work very well with the Dynamic Components extension. The DC extension in normal operation, creates two levels of dynamic dictionary, … one attached to the definition (where most all of the dynamic properties are defined,) and one attached conditionally to each dynamic instance (if the properties differ from the defaults as defined upon the definition.)
So, if you wrote code to create a "dynamic_attributes" dictionary attached to the model, saved it, and then imported into another model, which of two level dictionaries would it be, … the definition or the instance?

For a dynamic component file opened directly by SketchUp, the DC extension will not give you access to the dictionary. You would need to use one of the 3rd party attribute inspector extensions to see the dictionary.

We’ve discussed this numerous times in this category and the Dynamic Components category.

In addition to Ruby, you will need to learn HTML, CSS and JavaScript to code a dialog interface.

2 Likes

I’ve been thinking about a way round this that could work for me. So I can use a reference Component within the model (which will have the global attributes I want) but then hide it. Having just tested this I can see that other DC’s can reference it even when it’s hidden.
So the next step which I would like to do is to create a custom button which can access the Attribute Options box of the ‘global’ component - I can create the button using the custom Toolbar extension - but my question is, where can I find the code to open the attribute options of the hidden component? Anyone?

Again, I am sure that I already posted this information. Did you do a search using my member name ( @DanRathbun ) and “options” in this category or the DC category ?

I know this sounds like a dumb question but how do I search for @DanRathbun and “Options” at the same time? I tried adding a comma between the words and also a “+” but it didn’t give me any search results. Is there a guide how to use the forum so I can search for the information?

Can you send me a link to the info / post, I am sure you could do it very quickly whereas I will have to spend a lot of time looking. I don’t mind reading up and trying to learn myself before posting questions but I just need to know where to look.

Learning how to search the forum is a skill all forum members should learn.

  • Do not separate search terms with comma, use space.
  • Prefix forum category name with #
  • Prefix forum member names with @

Example:

#developers:ruby-api @DanRathbun DC Options dialog

… will bring up an abbreviated results list. For more results, click the More … link at the bottom of the list.

With the above search terms, you will find the answer I posted as the first result that gives code example for your request and more.

1 Like

Thanks for that.
I have read through that post answer and seen the code. But how does this script know which DC to open the Options box for? Is there some code I can add which selects the relevant DC (in my case the swatch with the global attribute values)?

It does not. The DC dialogs work on a selected component (if it is the single object selected and is dynamic.)

You can affect the selection set. Normally you don’t change the user’s selection though.
If you do then make sure you save what was previously selected and afterward, restore it.

See:

Thanks, but I don’t understand how to write the code. Do I have to use ‘#include?’ somewhere and then = the component name?

Are you able to just write the code here so I can copy it into the model?

Questions in this category are usually about asking how to code something. You will not learn how to code if other people are doing your work.

We ask that you at least make an attempt at coding the solutions to your challenges. Then come here to ask specific questions when you “hit the proverbial brick wall”.


If you instead would like to hire a contractor to write an extension for you, then this is the wrong category.
You can post job requests in the Developer Job Board category.

Personally, this is not something I am interested in coding nor have the time for. Perhaps someone else.

Thanks for clarifying and I appreciate what you are saying however I would like to offer my point of view in response which I would also appreciate if you would consider and take on board. It’s a small gripe which I would like to address in a constructive and postive way and I suspect many other users at my level who are looking to learn the Ruby API might feel the same.

The point is I would learn much quicker and more effectively having access to small code examples, such as I have requested, and I can use them as examples from which to build my knowledge upon. For me this would make the learning process much more engaging and motivating. It seems that a lot of related answers on this forum on this topic (beginner coding) is answered in the same way - a basic explanation which is often difficult to understand itself, with the answer a little too much beyond reach. So instead of being inspiring to use it as a stepping stone to learn further, it’s like coming up against a brick wall. Surely this forum should be about helping and encouraging users to learn all aspects of Sketchup which should have the result of making it more appealing a more appealing design program rather than putting them off altogether.

I should also say that I have spent many hours reading and watching tutorials on Ruby and the Sketchup API but I am not from a progamming or developer background and it will take me longer than most to get a good grasp on it, although I am determined to keep trying (time schedule permitting). I also respect the time and dedication developers and experts have put in to attain the knowledge that they have - I am not expecting anyone to write me a free extension or anything that would be an unfair exchange of their time and expertise. All I am asking is for a few lines of code to perform a very simple task which I can learn from.

So in summary, I just think that giving a few simple code examples can be much more beneficial for new users and be a big help in the learning process. I would be interested to hear other user’s thoughts on this.

I’m in a similar boat.

Your idea would be easier for me to attempt if you’d back up and provide something like a pseudocode flowchart simply outlining each step you want your app to perform. As it is, I read your posts but am not clear on what you are trying to do.

For example:

Select DC in Model->
Click Toolbar button to start GAOB->
Select (L-click) target component to launch Prompt->
Component selected?
Enter Favorite Ice Cream Flavor into Input Field->
etc., etc…

A second thing to do would be to share the code. For example, if you zipped your extension folder with a MyExtension.rb registration file and Main.rb file anyone could unzip and move to the Plugins folder, drag MyExtension.rb into the Plugins folder, give it a try and open the Main.rb in a code editor to see what’s going on. I think that is the rote work that an ‘apprentice’ ought to do to make assisting easier for experts. I might try it, fail dozens of times and report nothing back. But someone with a lot of skill might take interest and work some magic.

You can start here… there are some examples.
And every Ruby instruction has an example here

I started that way…

Thanks for your repsonse.

So what I am trying to achieve to assign some code to a custom button (using the Custom Toolbar Plugin), so when clicking the button it will:

Select a DC in the model called ‘Global1’ →
Open it’s Attribute Options Box →

That’s all.

I have looked on the class list ( Class: Sketchup::Selection) but I can’t see how to select a DC by name…

Thanks for that, I’ll have a read up and look on these links…

If you use the search feature (at top right) you will find I’ve already posted code to find groups or components by name. To determine if it is dynamic, you test if the instance has a "dynmanic_attributes" dictionary, if not test it’s definition. If it fails both tests, it is not dynamic.

An easy way around your problem is to select the whole model, right click and choose Make Component. Then you can add a dynamic dictionary in the normal way using the DC Attributes dialog. Doesn’t require learning to code.

So it’s possible to create a toolbar with a button that, when pressed, can 1) search and find a DC, “Global1” and 2) select that component. However, it may not be possible to have the toolbar button also, 3) open either the “Component Options” or “Component Attributes” dialogs.

Here is some code that can basically do 1 and 2. *Formatting mistake: it’s tabbed instead of double spaced.

IDK_Programming_gaob.rb (549 Bytes)

main.rb (2.5 KB)

You will need to create a folder, “IDK_Programming_gaob” to contain “main.rb”. You’ll also need to create an “Icons” folder inside of that folder if you want an icon (name it “gaob_icon.png”, or use your own name).

You can see some puts statements in the Ruby Console to check that the DC has been found. If not, a prompt will state otherwise. Also, the definition name of the DC will be displayed and an indication that the components is selected will be shown. If the component only has a definition and no instance name, the selected component field in the puts statement will be empty. Otherwise, it will show the instance name.

This extension does very little and is probably a dead end.

Another approach, Option 2, would be to use the toolbar button to launch Global1 and then use a modified version of WolgangOrlinski’s AutoOptionsDcDialog. There is a toggle_show_dialog_on_open method in there that might be used to show the DC dialogs when they are opened with the toolbar button. You’d basically have the DC Global1 in the extension folder, so it would be ready to be loaded when the button is pressed. This approach might be a work around to the (my?) limitation (3) of opening the “Component Options” or “Component Attributes” dialogs of a DC in the model space.

Press toolbar button to launch Global1 →
Load Global1->
Show Comp Options or Comp Attributes dialogs of Global1.
???->

Your goals aren’t clear to me but as Dan wrote, just using Make Component might be the way to go. Or, the Orlinksi extension mentioned earlier can open the DC dialogs when either a DC is created, or opened. So, using that extension with Make Component, or loading a DC with a launcher toolbar might be a solution.

Also, as Rafa suggested, you could use an HTML Dialog. I don’t know what you’d want to do with to do with it (e.g., apply materials), so I don’t have a good idea on that. But you’d have more options. For example, the puts statements you see in the Ruby Console in the example I provided could be displayed, attributes could be created, components selected, etc.

Hi, thanks for your message. I will try these and see how I get on!
Nick