Prevent components of sharing same space

sketchup
mac
components

#1

I’m trying to check if there is any conflicts (intersection) between Clamps (nested component), so prevent clamps of sharing the same space on a common post and show a warning message.

So, how can I get clamps list, and check intersection using Ruby code?

Can anyone help me please? Thanks


#2

You cannot prevent them from occupying the same space, but can test for it if they are aligned with the major axes.

def interfere?(obj1,obj2)
  if obj1.respond_to?(:bounds) && obj2.respond_to?(:bounds)
    not(obj1.bounds.intersect(obj2.bounds).empty?)
  else
    false
  end
end

#3

Thanks for a quick reply.

Any idea how can I get objects (components) list of clamps, they are nested components?

Thank you again


#4

Assuming you have named your components sensibly, so the clamp component is named e.g. “Clamp” you can find all the instances by

instances = Sketchup.active_model.definitions["Clamp"].instances

Then test per Dan’s snippet for overlap between two instances.


#5

… and read these topic threads …


Tip: It is not hard to find previous discussions. Just click the magnifying glass icon at the top right. (If you are within a subcategory then the search will automatically be filtered to only that subcategory.)


#6

Also, note that the Bounding Boxes’ corner and center cocordinates are different if you are within the obejct’s geometric context (ie, they’ll be in local coordinates.)

So, you’ll likely wish to be at the model’s top level context since the clamp’s are both nested within another and separate geometric contexts. IE, …
Sketchup.active_model.entites == Sketchup.active_model.active_entities
When at the top level the coordinates returned should be in model coordinates rather than local coords.


#7

Thank you all again,

This is how I’m testing interfere? Dan’s method, it’s returning true, even that components are not intersecting each other (see screenshot below)

`def self.check_conflicts
  definitions = Sketchup.active_model.definitions["35-10350_POST CLAMP"]
  instances = definitions.instances

  puts self.interfere?(instances[0], instances[1])
end

def self.interfere?(obj1, obj2)
  if obj1.respond_to?(:bounds) && obj2.respond_to?(:bounds)
    not(obj1.bounds.intersect(obj2.bounds).empty?)
  else
    false
  end
end`


#8

I know very little about Ruby.
But you’ll need to figure out the space that each object takes. That’s less than their bounding boxes. (Except for rectangular blocks with their origin on the surface or inside the block).
Detecting overlapping bounding boxes is just the first step, the easiest part of collision detection.
See what their bounding boxes look like to see if true is true.


#9

Post a test model. I can only guess …

It looks as though the two clamp instances are the SAME instance because they are the same instance inserted into another component. If my guess is true, then yes the test would return true because the bounding boxes would be the same.

You may need to make each parent component unique, or be sure that you insert unique instances of each clamp into the model.


#10

I made a simple test model and got false, so I think this is related to the specific structure of your model somehow breaking the code’s logic. Could you upload an example model that returns true so we can investigate what is going on?

Edit: in my test, I got false because there is only one instance found so instances[1] is nil, which fails the respond_to?(:bounds) call. That’s what I would expect when the instance is nested in another component’s definition’s entities collection. There is only one instance regardless of how many times the parent is placed in the model.

So, I can’t explain why your test returned true without seeing the model.


#11

You are right, clamps are same instance.

The solution I was thinking about making clamps separate from each other was using make_unique method, but it’s not working…

Any other suggestion?

Thank you very much


#12

You likely need to make the parent unique as it is what is “sharing” the entities collection. Then each one of the parent components will have an unique entities collection which gives each an unique clamp instance.


#13

Thank you all