[code] intersect_with example

[code] intersect_with example

Postby shirazbj » Mon Mar 16, 2009 12:25 pm

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(000)
    
p2 Geom::Point3d.new(200000)
    
p3 Geom::Point3d.new(20001500)
    
p4 Geom::Point3d.new(01500)
    
p5 Geom::Point3d.new(000)
    
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, -50500)
    
p2 Geom::Point3d.new(1500, -50500)
    
p3 Geom::Point3d.new(1500200500)
    
p4 Geom::Point3d.new(500200500)
    
p5 Geom::Point3d.new(500, -50500)
    
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 falsecut_transbasegroupbase_trans truebasegroup 
    
    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

Postby DavidBoulder » Tue Feb 08, 2011 10:07 pm

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
User avatar
DavidBoulder 
 

Re: [ruby doc] intersect_with example

Postby TIG » Tue Feb 08, 2011 10:40 pm

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
http://code.google.com/apis/sketchup/do ... rsect_with
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
User avatar
TIG 
Global Moderator
 

Re: [ruby doc] intersect_with example

Postby Dan Rathbun » Tue Feb 08, 2011 11:03 pm

Someone please..

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.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: [ruby doc] intersect_with example

    Postby DavidBoulder » Wed Feb 09, 2011 7:26 am

    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_model
    entities = model.active_entities
    selection = 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
    User avatar
    DavidBoulder 
     

    Re: [code] intersect_with example

    Postby TIG » Wed Feb 09, 2011 9:07 am

    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
    User avatar
    TIG 
    Global Moderator
     

    SketchUcation One-Liner Adverts

    by Ad Machine » 5 minutes ago



    Ad Machine 
    Robot
     



     

    Return to Developers' Forum

    Who is online

    Users browsing this forum: No registered users and 12 guests