All those method are defined inside class like:
class DBox
DESCRIPTION = 'Description'
BACK = "Back" # It means nothing yet.
def get_all_points
@left = [[0, 0, 0], [500.mm, 150.mm, 0]]
@right = [[0, 0, 0], [500.mm, 150.mm, 0]]
@front = [[0, 0, 0], [200.mm, 150.mm, 0]]
@back = [[0, 0, 0], [200.mm, 150.mm, 0]]
@bottom = [[0, 0, 0], [480.mm, 180.mm, 0]]
@thickness = 18
end
def create_box
entities = []
entities<< create_left_panel
entities<< create_right_panel
entities<< create_front_panel
entities<< create_back_panel
entities<< create_bottom_panel
group = Sketchup.active_model.entities.add_group(entities)
box = group.to_component
rotate_panel box, [0,0,0], X_AXIS, 270
add_box_in_cabinet(box)
end
def add_box_in_cabinet box
selection = Sketchup.active_model.selection
if selection.count!=3
UI.messagebox("Please select 3 panels and try again!")
return
end
h_panel=nil
v_panels=[]
selection.each{|panel|
if panel.name == "HShelf"
h_panel = panel
else
v_panels << panel
end
}
definition = h_panel.parent
parent_component = definition.instances[0]
points = get_box_position_in_cabinet box
if !points
puts "Points are not valid"
return
end
trans_move = Geom;;Transformation.translation points
component = parent_component.definition.entities.add_instance box.definition, trans_move
box.erase!
end
def get_box_position_in_cabinet h_panel, v_panels
points = []
dict1 = nil
if v_panels.length == 2
vdict1 = SketchupInfo.rootDictionary v_panels[0]
vdict2 = SketchupInfo.rootDictionary v_panels[1]
x1 = vdict1['x'].to_l.inch
x2 = vdict2['x'].to_l.inch
panel1= nil
if x1 > x2
points.push((x2+18).mm)
else
points.push((x1+18).mm)
end
end
dict = SketchupInfo.rootDictionary h_panel
points.push(dict['y'].to_l.inch)
points.push(dict['z'].to_l.inch)
if !points
return false
end
points
end
def create_panel pts, thickness
ply_type ="PLY"
info = Hash.new
info[DESCRIPTION] = BACK
b = RectangleBox.new(pts[0], pts[1], thickness)
component = b.draw
end
def rotate_panel component, points, axis, angle
trans_rotate = Geom;;Transformation.rotation points, axis, angle.degrees
component.transform! trans_rotate
end
def translate_panel component, points
trans_move = Geom;;Transformation.translation points
component.transform! trans_move
end
def create_left_panel
panel = create_panel @left, @thickness
rotate_panel panel, [0,0,0], Y_AXIS, 90
points =[0,0,0] #Position for panel to fit
translate_panel panel, points
panel
end
def create_right_panel
panel = create_panel @right, @thickness
rotate_panel panel, [0,0,0], Y_AXIS, 270
points =[200.mm,0,0] #Position for panel to fit
translate_panel panel, points
panel
end
def create_front_panel
panel = create_panel @front, @thickness
rotate_panel panel, [0,0,0], Y_AXIS, 180
points =[18.mm,0,0] #Position for panel to fit
translate_panel panel, points
panel
end
def create_back_panel
panel = create_panel @back, @thickness
points =[18.mm,0,482.mm] #Position for panel to fit
translate_panel panel, points
panel
end
def create_bottom_panel
panel = create_panel @bottom, @thickness
rotate_panel panel, [0,0,0], Y_AXIS, 90
points =[10.mm, 140.mm, 490.mm] #Position for panel to fit
translate_panel panel, points
panel
end
end
class RectangleBox
DESCRIPTION = 'Description'
attr_reader ;near, ;far, ;thickness
def initialize( near, far, thickness, ply_type, info = {})
@near = near; @far = far
@thickness= thickness
@ply_type = ply_type
@description = info[DESCRIPTION]
end
def draw(*args)
model=Sketchup.active_model()
if args.length == 0
ents = model.entities()
group = ents.add_group()
else
group = args[0]
end
corners = get_corners()
face = group.entities().add_face(corners[0], corners[1], corners[2], corners[3])
face.pushpull(@thickness.mm)
panel= group.to_component
panel.definition.name = "#{@ply_type}"
panel.name = @description
return panel
end
def get_corners()
ret = []
ret[0] = @near
ret[2] = @far
ret[1] = [ @near[r], @far[g], @far[b] ]
ret[3] = [ @far[r], @near[g], @near[b] ]
return ret
end
end
class SketchupInfo
def self.getDictionary(entity, dictionary)
dicts = entity.attribute_dictionaries
if !dicts.nil?
dicts[dictionary]
end
end
def self.rootDictionary(entity)
SketchupInfo.getDictionary(entity,'dynamic_attributes')
end
end
I called create_box method form menu by creating object for DBox
box = DBox.new
box.create_box
I removed some of the method which are not used till now.
If I'm creating box independently (not inside Cabinet) then it creates only one box as ComponentInstance, but when I tried to create it withing Cabinet then one extra box is creating which is not even a group(all panels are individual) & if I'm moving one of the panel the the panel inside grouped box is also moving. or if I tried to delete any panel then Sketchup got crashed.