[code] intersect_with example

[code] intersect_with example

Hi,

I have tried to use two intersected cubes to demonstrate how to use the intersect_with command. This could be a example for the ruby doc.

Code: Select all
`    model = Sketchup.active_model    entities = model.entities    #Create a group to collect all entities for base cube    basegroup=entities.add_group    basegroupentities=basegroup.entities    p1 = Geom::Point3d.new(0, 0, 0)    p2 = Geom::Point3d.new(2000, 0, 0)    p3 = Geom::Point3d.new(2000, 150, 0)    p4 = Geom::Point3d.new(0, 150, 0)    p5 = Geom::Point3d.new(0, 0, 0)    points = [p1,p2,p3,p4,p5]    #Add a face entity into base group    base = basegroupentities.add_face points    normal = base.normal    if normal==[0,0,-1]      base = base.reverse!     end    base.pushpull 1000    base_trans=basegroup.transformation    #Create a group to collect all entities used for cutting cube    cutgroup=entities.add_group    cutgroupentities=cutgroup.entities    p1 = Geom::Point3d.new(500, -50, 500)    p2 = Geom::Point3d.new(1500, -50, 500)    p3 = Geom::Point3d.new(1500, 200, 500)    p4 = Geom::Point3d.new(500, 200, 500)    p5 = Geom::Point3d.new(500, -50, 500)    points = [p1,p2,p3,p4,p5]    #Add a face entity into cut group    base = cutgroupentities.add_face points    normal = base.normal    if normal==[0,0,-1]      base = base.reverse!     end    base.pushpull 1000    cut_trans=cutgroup.transformation        #Use all entities in cut group to cut through all entities in base group    #The middle basegroup variable is to specify that the intersection lines are drawn on base group entities.    cutgroupentities.intersect_with false, cut_trans, basegroup, base_trans , true, basegroup         status = basegroup.explode    model.active_view.zoom_extents    UI.messagebox("Click to delete cut group.")    cutgroup.erase! `
3
Last edited by thomthom on Wed Feb 09, 2011 8:30 am, edited 1 time in total.
Reason: Put the code into a code box and renamed the tag

shirazbj

Re: [ruby doc] intersect_with example

Thanks, that was really helpful, but I'm having problems with a simple variation on this.

The basic concept is a model with only groups at the top level. The user selects a group and then runs the script. The script should intersect that group with the model; the new geometry is created inside of that group. My next step after this is working is to let the user multi-select and iterate through their selection. That should be easy if I can just get the basic code working first.

This is what I want for the intersect_with line, but I just can't get it to work. I guess I need to get the name of the group that is in selection[0]? and then use that similar to your code.

selection[0].intersect_with true, selection[0].transformation, selection[0], [0,0,0], true, entities
0
--

David Goldwasser
OpenStudio Developer
National Renewable Energy Laboratory

DavidBoulder

Re: [ruby doc] intersect_with example

Your selection[0] will be an entity BUT the intersect_with method is for entities - that is the 'container-entities' holding that individual entity... see
Recast it something like... selection[0].parent.entities.intersect_with(true, selection[0].transformation, selection[0].parent.entities, selection[0].transformation, true, entities)***
***I'm not sure what your final argument 'entities' is - it should either be an 'entities object' or an 'array of entities'... it could be selection[0].parent.entities.to_a so all entities in the same context are used in the intersection
0
TIG

TIG
Global Moderator

Re: [ruby doc] intersect_with example

Codebox the script example, parenthesize the intersect_with call (it wraps,) and rename the topic title:
[ code ] Entities.intersect_with example
.. so that Jim's sticky code index script can find it.
0
I'm not here much anymore. But a PM will fire email notifications.

Dan Rathbun
PluginStore Author

Re: [ruby doc] intersect_with example

TIG, thanks for you help. That got me over the hump. Below is what I was looking for. If you select one or more top level groups and run this script, it will iterate through the groups adding necessary intersecting geometry under each group.

Code: Select all
`model = Sketchup.active_modelentities = model.active_entitiesselection = model.selection# loop through selection of top level groups to create new intersecting geomtry under each group.selection.each { |entity| entity.entities.intersect_with(true, entity.transformation, entity.entities.parent, entity.transformation, true, entity.parent.entities.to_a) }`
0
--

David Goldwasser
OpenStudio Developer
National Renewable Energy Laboratory

DavidBoulder

Re: [code] intersect_with example

I suggest you trap for non-groups
Code: Select all
`selection.each { |entity|###next if not entity.class==Sketchup::Group###entity.entities.intersect_with(true, entity.transformation, entity.entities.parent, entity.transformation, true, entity.parent.entities.to_a)}`

Also shouldn't entity.entities.parent be entity.entities.parent.entities
1
TIG

TIG
Global Moderator