##############################
#CrossBeam generator
#
# copyright Cédric Doutriaux
# cc by sa 2014
#############################
#
#USAGE :
#Select 3 objects :
# 1 - first crossbeam (to be cutted by the tool shape) get "mother" in is name
# 2 - the tool shape : sort of cube to indicate the cut direction in the beam, placed at he intersection of two beams
#       (get "cut" in is name)
# 3 - second crossbeam (that intersect the first)




import bpy

# The following function is adapted from
# Nick Keeline "Cloud Generator" addNewObject
# from object_cloud_gen.py (an addon that comes with the Blender 2.6 package)
#
def duplicateObject(scene, name, copyobj):
 
    # Create new mesh
    mesh = bpy.data.meshes.new(name)
 
    # Create new object associated with the mesh
    ob_new = bpy.data.objects.new(name, mesh)
 
    # Copy data block from the old object into the new object
    ob_new.data = copyobj.data.copy()
    ob_new.scale = copyobj.scale
    ob_new.location = copyobj.location
    ob_new.rotation_euler= copyobj.rotation_euler
 
    # Link new object to the given scene and select it
    scene.objects.link(ob_new)
    ob_new.select = True
 
    return ob_new




if (len(bpy.context.selected_objects)!=3):
    print("invalide number of objects");
else:
    print("ok");#there are 3 objects					
    
    #assign objects
    for obj in bpy.context.selected_objects:
        if obj.name.find("cut")!=-1:
            Name_cut_tool=obj.name
            print("------------------------------------cutTool :")
            print (obj.name)
        elif obj.name.find("mother")!=-1:
                print("------------------------------------mother :")
                print (obj.name)
                Name_beam_A=obj.name
        else:
            print("------------------------------------father :")
            print (obj.name)
            Name_beam_B=obj.name
                
    #duplicate the mother beam 
    
    Name_node_object=Name_beam_A+"_node"    
    duplicateObject(bpy.context.scene,Name_node_object,bpy.data.objects[Name_beam_A] )    
    bpy.context.scene.objects.active = bpy.data.objects[Name_node_object] 
    node_object = bpy.context.scene.objects.active

    
    
    #boolean mother(inter)second beam   
    
    print("[1]>>>>I will do")
    print(Name_node_object)
    print("INTERSECT")
    print(Name_beam_B)
    print(".......") 
    
    boo = node_object.modifiers.new('Booh', 'BOOLEAN')   
    boo.object = bpy.context.scene.objects[Name_beam_B]    
    boo.operation = 'INTERSECT'
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Booh")
    
        ##grow node to avoid edges overlap errors
    boo = node_object.modifiers.new('Booh', 'DISPLACE')  
    boo.mid_level=0.6
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Booh")
    
    
    #-------------------------------------------------------
    # boolean cuttool (inter) node
    
    print("[2]>>>>I will do")
    print(Name_cut_tool)
    print("INTERSECT")
    print(Name_node_object)
    print(".......") 
    
    bpy.context.scene.objects.active = bpy.data.objects[Name_cut_tool] 
    cut_tool = bpy.context.scene.objects.active

    
    boo = cut_tool.modifiers.new('Booh', 'BOOLEAN')   
    boo.object = bpy.context.scene.objects[Name_node_object]    
    boo.operation = 'INTERSECT'
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Booh")
    
    
    ##grow cuttool to avoid edges overlap errors
    boo = cut_tool.modifiers.new('Booh', 'DISPLACE')  
    boo.mid_level=0.99
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Booh")
    
    
    ##----------------------------------------

    # boolean mother(minus) cuttool
    print("[3]>>>>I will do")
    print(Name_beam_A)
    print("SUBSTRACT")
    print(Name_cut_tool)
    print(".......")
    
    bpy.context.scene.objects.active = bpy.data.objects[Name_beam_A] 
    
    boo=bpy.data.objects[Name_beam_A].modifiers.new('Booh', 'BOOLEAN')
    boo.object = bpy.context.scene.objects[Name_cut_tool]
    boo.operation = 'DIFFERENCE'
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Booh")
    
    
    ## boolean mother(minus) second beam
    print("Now I will do==========")
    print(Name_beam_B)
    print("SUBSTRACT")
    print(Name_beam_A)
    print(".......")
    
    bpy.context.scene.objects.active = bpy.data.objects[Name_beam_B] 
    boo=bpy.data.objects[Name_beam_B].modifiers.new('Booh', 'BOOLEAN')
    boo.object = bpy.context.scene.objects[Name_beam_A]
    boo.operation = 'DIFFERENCE'
    #bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Booh")
    
    
    
 

    
print("OK..........")


