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

**URL:** <https://forums.sketchup.com/t/moving-objects-and-sticky-connections-between-them-can-it-be-done-with-ruby/61035>\
**Category:** Developers\
**Created:** [February 1, 2018, 5:31pm UTC](https://forums.sketchup.com/t/moving-objects-and-sticky-connections-between-them-can-it-be-done-with-ruby/61035 "2018-02-01T17:31:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MiniMe](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@MiniMe](https://forums.sketchup.com/u/MiniMe)\
**Post date:** [February 1, 2018, 5:31pm UTC](https://forums.sketchup.com/t/moving-objects-and-sticky-connections-between-them-can-it-be-done-with-ruby/61035/1 "2018-02-01T17:31:03Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [February 2, 2018, 12:16am UTC](https://forums.sketchup.com/t/moving-objects-and-sticky-connections-between-them-can-it-be-done-with-ruby/61035/2 "2018-02-02T00:16:28Z")

</div>

> [@MiniMe](#):
>
> Is this possible ?

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.)

> [@MiniMe](#):
>
> Is there a better way to achieve line/connection stickiness between objects ?

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.

---

<div class="post-metadata">

**Author:** ![MiniMe](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@MiniMe](https://forums.sketchup.com/u/MiniMe)\
**Post date:** [February 2, 2018, 1:37am UTC](https://forums.sketchup.com/t/moving-objects-and-sticky-connections-between-them-can-it-be-done-with-ruby/61035/3 "2018-02-02T01:37:09Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [February 2, 2018, 2:52am UTC](https://forums.sketchup.com/t/moving-objects-and-sticky-connections-between-them-can-it-be-done-with-ruby/61035/4 "2018-02-02T02:52:43Z")

</div>

> [@MiniMe](#):
>
> I am quite good with python.

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.)

> [@MiniMe](#):
>
> I think it _[Sketchup level]_ is good enough for what I need

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.)

> [@MiniMe](#):
>
> All I need is a routine to read the position of my objects and redraw the lines as needed.

It **can** be reactionary using observer(s). It is likely that moving any component instance will trigger the [#onChangeEntity](http://ruby.sketchup.com/Sketchup/EntityObserver.html#onChangeEntity-instance_method) observer callback. In this, you can [start a timer](http://ruby.sketchup.com/UI.html#start_timer-class_method) (allowing the current callback queue to finish,) then popup a [messagebox](http://ruby.sketchup.com/UI.html#messagebox-class_method) asking to redraw the “connections”.

> [@MiniMe](#):
>
> It does not even need to be realtime, I can run it manually.

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`**](http://ruby.sketchup.com/Sketchup/Entities.html) 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`**](http://ruby-doc.org/core-2.2.4/Enumerable.html) module (most especially the [**`grep`**](http://ruby-doc.org/core-2.2.4/Enumerable.html#method-i-grep) method.)  
Also have posted examples here …

> [@Selecting Specific Entity, Group or Component (By Name)](https://forums.sketchup.com/t/selecting-specific-entity-group-or-component-by-name/11469/12):
>
> Very similar for groups, because groups are disguised components. That means they have definitions. And their definitions have a separate name attribute, than the group instances. But there are a few quirks. The group.description method is an wrapper for group.definition.description. (So like a component instance, they share their definition’s description attribute.) So to find groups through the definition list, we need to filter out the definitions for images and normal components. dlist …

To get center points you’ll need to use the [Geom::BoundingBox](http://ruby.sketchup.com/Geom/BoundingBox.html) class, returned by [**`Drawingelement#bounds`**](http://ruby.sketchup.com/Sketchup/Drawingelement.html#bounds-instance_method).

To move things around geometrically you’ll need to understand [transformations](http://ruby.sketchup.com/Geom/Transformation.html).

To “tag” objects with data, you’ll attach and use an [AttributeDictionary](http://ruby.sketchup.com/Sketchup/AttributeDictionary.html). 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 …

```ruby
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 …

- [Ruby Learning Resources [WikiLists]](https://forums.sketchup.com/t/ruby-learning-resources-wikilists/22861/)

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

This one is a must read:

- [Extension | SketchUp Extension Warehouse](http://extensions.sketchup.com/en/content/example-ruby-scripts)

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

Check out their extensions listing: [Page Not Found | SketchUp Extension Warehouse](http://extensions.sketchup.com/en/user/48/store)

---

<div class="post-metadata">

**Author:** ![MiniMe](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@MiniMe](https://forums.sketchup.com/u/MiniMe)\
**Post date:** [February 2, 2018, 9:23am UTC](https://forums.sketchup.com/t/moving-objects-and-sticky-connections-between-them-can-it-be-done-with-ruby/61035/5 "2018-02-02T09:23:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/sketchup/original/3X/f/2/f2a121500b085ed60806607a82720b092d800bb3.png) [@system](https://forums.sketchup.com/u/system)\
**Post date:** [May 5, 2018, 1:37am UTC](https://forums.sketchup.com/t/moving-objects-and-sticky-connections-between-them-can-it-be-done-with-ruby/61035/6 "2018-05-05T01:37:31Z")

</div>

Closed on May 5, 2018, 1:37am UTC

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