Help with ruby code

Hello, I’m trying to write a code that can select subcomponents inside a component greater than and copy them to another, I want to clarify that both are inside a model that contains them both

When you say “select” do you mean in the manual sense using the Select tool ?

Or, do you mean “choose” according to some criteria? If the latter, there are already topics threads on doing this in the Ruby API subcategory.

hello, thanks for answering, actually I’m new to ruby, but what I’m trying to do are two things, the first is to know if there is a possibility to copy and paste to site, like the command does in sketchup but in ruby, if so possible how to do it, and the second thing is if you can move selected components to another component that is within the same model, if it can be done as it would be done, are there examples or something that will help me, thanks

@dezmo has reassigned this topic to the Ruby API subcategory which is more appropriate for asking coding questions. (The Job Board category is for posting job openings or notices of developer availability.)

Yes it is possible, but is an obscure paradigm. The Sketchup.send_action method needs to be used. IE …

def copy

def cut

def paste

But this method is mostly meant for creating custom toolbar buttons. The reason is that it is asynchronous, meaning that the call returns immediately and your code will not know exactly when the action is actually complete.

The paste call will act upon the current edit context’s entities collection.
The others act upon the current selection set.

This means that your code may have to navigate the model hierarchy using Model#active_path= and find your objects and stuff them into the selection set. Then navigate into the desired component’s edit context and paste the selection.

Now what is normally done however, is copies of the instances are inserted into the desired component’s definition’s entities collection, then all the properties are copied. (Behavior, materials, gluing, etc.) Lastly the originals are deleted.
As said, there are topics on this code flow already.

Reassigning objects with the API can be dangerous if done incorrectly. (Ie cause a crash.)

If I remember correctly, both the objects being moved and the target component (or group) must exist side-by-side in the model’s active entities context.

There are uncountable example snippets throughout the topics already posted.

Go into the SketchUp Ruby API subcategory and use the search feature (and choose to limit the search by category.)

Regarding “how to do it” … we ask that you at least make an attempt, then when you run into trouble start a topic and post the problem code and ask a specific code question.


536 / 5,000

Resultados de traducción



Hello, thank you very much for the information, I already executed the code and it works very well, just one more question, I am working with relative references and they work fine, the components that are copied to the left side, that is, close to the origin in sketchup, have no problem. but the ones that go to the right side, that is, the part far from the origin, are being copied to me twice the distance in which they should be, I don’t know how that parameter can be changed in ruby, so that it positions them in their place, once Thank you very much for everything.

Component and Group Instances can have a scaling factor to their transformation.
This can affect the local coordinates within that instance and the size of objects you insert into the parent component.

But if you are within the edit context of an instance, (rather than at the top level model,) then many API methods return global coordinates instead of local coordinates.


ok I managed to correct it, it turned out very well, thanks for all the help, you are very kind, once again thanks

1 Like