MOving objects and sticky connections between them-can it be done with Ruby?

Hi guys

First of all I apologize if this sounds dumb, I have limited knowledge of Sketchup and next to zero about Ruby.

Here is what I am trying to achieve: I want to be able to use ruby to draw objects (not very complicated forms, let’s say cubes) and then connect them with lines.
I would like to be able to write a script to redraw the lines every time when I move an object.
Here is the algo
-for each object pick a center point (origin within the cube)
-draw each cube that I need to represent
-connect the centers of the cubes as per a list of connections that I will provide

Now let’s say I don’t like the position of some of the cubes on the map and I want to move one of them
When I move that I expect the script to be able to get the position of the center of the cube and to redraw all the lines connecting that point with other cube centers as they were before the cube in question was moved

Is this possible ?
Is there a better way to achieve line/connection stickiness between objects ?

Yes, but this is advanced programming and you should get more than limited knowledge of SketchUp, and learn Ruby basics.

Start by learning how to do simple things, before attempting complex things. Learning SKetchUp Ruby is the subject of many topics here (especially in the Ruby API sub-category. (Use forum search features, magnifying glass icon, top right.)

There is no actual built-in means of “stickiness” between lines and components in SketchUp.
This is mainly because SketchUp is a surface modeler and not CAD, so it has edges not lines.

SketchUp does have optional surface “glue to” behavior between components and face objects, for components that have this property set to use it.

If you thinking about diagramming, LayOut may be a better choice.

I am quite good with python. My Sketchup level was pretty good to do my house in 3D using layers, shadows, textures but I am certainly an amateur. I think it is good enough for what I need
It seemed pretty basic to me to import predefined components and position them where I want
All I need is a routine to read the position of my objects and redraw the lines as needed. It does not even need to be realtime, I can run it manually.

LayOut is not an option as I need 3D and layers. I need to be able to export those to 3D PDFs (question asked around here some time ago)

Okay, you’re a programmer with OOP experience, so you should pick up Ruby quite quickly, as long long as you don’t expect Ruby to share some of Python’s quirks, … (ie, forced indentation blocks, … no end keywords, automatic modules having the filename’s name, etc.)

Okay then … but … keep in mind …

SketchUp does not have true geometric layers (so deal with that.)
Does not have “lines”, it has 0 width edges (so you’d need to use edge style likely to give substance to the “connectors”.)
Does not have “sticky” connections (you’d have to fake it by assigning an attribute to an edge object that holds the persistent id of a component instance that it terminates to.)

It can be reactionary using observer(s). It is likely that moving any component instance will trigger the #onChangeEntity observer callback. In this, you can start a timer (allowing the current callback queue to finish,) then popup a messagebox asking to redraw the “connections”.

Doesn’t matter much as the “redraw” code would be in a common method called in both scenarios.
The drawing will need to leverage some factory methods of the Sketchup::Entities class.
To find objects you’ll need to learn to iterate this collection class using it’s iterator methods, and/or those mixed in from Ruby core’s Enumerable module (most especially the grep method.)
Also have posted examples here …

To get center points you’ll need to use the Geom::BoundingBox class, returned by Drawingelement#bounds.

To move things around geometrically you’ll need to understand transformations.

To “tag” objects with data, you’ll attach and use an AttributeDictionary. Dictionaries are shared space objects, so they need to be uniquely named. I suggest the same qualification as the extension’s module nesting, but with the :: scope operator replaced with underscore …

dictname = Module::nesting[0].name.gsub(/(::)/,'_')

From within a module nested as … CompanyX::NiftyWidget
the dictionary name string would be "CompanyX_NiftyWidget"


There is no magic wand I can wave and grant you the knowledge and ability.
You’ll just have to spend a week or two reading and studying extension examples.

I compiled a list of Ruby Learning links here in this forum …

There are several examples of tools by the SketchUp Team in the Extension Warehouse.

This one is a must read:

There are also others … Window Maker, Bezier Curve Tool, Rotate Rectangle Tool, etc.

Check out their extensions listing: http://extensions.sketchup.com/en/user/48/store

Thanks a lot Dan, that is more than I could hope.
I will have a look at these when I start the work on this personal project and I will get back here with more questions

thank you again!
MM

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.