Create a window in a wall

Hi!

I`m new with Ruby for Sketch Up and I think I need some help.
I want to create walls with windows by a loop from a .txt-file on my computer.

The reading part works really fine - all the wall and window solids are getting created so far. But I have problems to substract the two solids from each other.

I had the idea to make groups with an unique name (f.e. Wall1 / Window1) so I can select them by name but I don`t know I should do it.

Thats the code for building and naming the wall:


#First Wall

pt1 = .....
pt2 = .....
pt3 = .....
pt4 = .....

group = entities.add_group
group.name = "Wall1"

new_face = group.entities.add_face pt1, pt2, pt3, pt4

if normal = -1.0
new_face.pushpull 3.0
else
new_face.pushpull -3.0
end

#First Window

group = entities.add_group
group.name = "Window1"

new_face = group.entities.add_face pt1, pt2, pt3, pt4

.....
.....

How is it possible to select these two solids and create and boolean operation?

I hope you can help me! Im working on these problems for days!
Thanks!

Chris

You need to create object references that are distinct for the two objects instead of reassigning the same reference.

The points used to create the two objects would likely be different as well and so also would have distinct references.

Native boolean operations use API methods that will only work in Pro editions. They will not work on any Free/Make edition. The Pro boolean methods are instance methods of the Sketchup::Group and Sketchup::ComponentInstance classes.

Ie, see: Sketchup::Group#subtract

Thanks for your response. Can you give me an example how I should modify the code?

A few minutes ago I tried this:


instance1 = entities[numberwall]
instance2 = entities[number]
result = instance2.subtract(instance1)

This code worked fine, but I think after the first boolean operation the entities numbers are changing and so I can`t take this code for a loop.
I would need a way which I could select the entities by name.

Ruby comment begin with a # character.

Correct. Boolean operations are destructive. The original objects no longer exist after the operation.

So there is no simple solution to this?
Can you tell me how the numbers are changing after an boolean operation?

Firstly I went away and had some yard work to do.

Secondly, please post (or edit your original) snippet as valid code, with real points so we have a starting point.

Thirdly, often we will tell you how to do it yourself so that you learn best, rather than just coding the solution. (I myself have a policy not to give code until a newb at least tries to apply suggestions themselves.) You’ve not tried the 2 of the suggestions.

In order to do the boolean operation, you need only have references to the two group objects. But, as I said before, they need to be unique.
group1 and group2 would work. So would wall_group and window_group.

You even said this in your original post …

Yes you should. But these are not names, they are references to objects. (Ruby is a 100% object oriented programming language.)

You can also give SketchUp groups and component instances persistent names as they have a name property set via their .name= method, accessed via their .name method.

It seems that you are confounding a name which is an API property of group and instances, and the Ruby object reference (called in other languages, a variable.) Each are different things and have separate uses.

We (and you) should not worry about numbers, which are the ordinal position of an entity within the C++ entities collection. The position in the collection is something we will never control and cannot rely upon remaining static on the Ruby side of things.

The boolean methods return a result reference. You test it for nil … ie …

if result.nil?
  # one or both of the groups was not a watertight manifold region
else
  wall_with_hole = result
end

FYI …

1 Like

Hi Dan,

thanks for your adivce!
Now I´m able to put one opening in each wall.


# Code for building the wall

group_wall = entities.add_group
group_wall.name = name1

new_face = group_wall.entities.add_face pt1, pt2, pt3, pt4
new_face.pushpull 3.0

# Code for building the opening

group_window = entities.add_group
group_window.name = name2

new_face = group_window.entities.add_face pt1, pt2, pt3, pt4
new_face.pushpull 1.0

# Substraction

result = group_window.subtract(group_wall)

If I want to substract another window out of the wall I`ve got the problem that the new wall with hole is not longer in the “group_wall”. Can you give me a hint how to put the result-wall back in the group “group_wall”?

I just solved it! Now it works for all my windows…
Thanks again for your tips!

1 Like

FTR, I showed a snippet above on how to test the boolean result.

To use your specific reference identifier … group_wall …

if result.nil?
  # one or both of the groups was not a watertight manifold region
else
  group_wall = result
end

… OR …

group_wall = result unless result.nil?