Adding group to existing entities
-
I tried to create a box and add it to the cabinet like:
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) # here entities is array of entity(back, front side etc) box = group.to_component comp = cabinet.definition.entities.add_instance box.definition, transform #cabinet is an ComponentInstance object. box.erase! #If I'm not deleting this entity then 3 copies will be there.But here I'm getting two copies for every entity in the group.
1. as a group of component inside a instance i.e box as ComponentInstance.[/list]
2. as all separate ComponentInstance object for every entity in group.[/list]@unknownuser said:
So, while moving any of the ungrouped entities the entity inside the box object is also moving. and if I tried to delete the ungrouped entity then Sketchup itself crashed.
@unknownuser said:
I need some short of code that can add a group of entities into another entities [ComponentInstance.definition] group, without making another copy of it or the other copy should be deleted without crashing the Sketchup.
-
I think the problem is that you create the various panels in a method, probably as a group then you copy them into another group thus duplicating them.
-
You only give us half of the story !

Presumably each of your methods 'create...' returns a valid 'entity'.
These must be in themodel.active_entitiescontext for theadd_group(...)to work...
You somehow magic a 'transform' - in theadd_instance(...)what is that ?In principle the limited code you show us ought to work...
But of course you can easy screw it up in the parts you haven't shewn...

-
Here I'm copying the related method I used to create panel's
#This method will return a panel as ComponentInstance #RectangleBox is another class which will create a panel using 2 diagonal points, thickness 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 = #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 =[...] #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 =[...] #Position for panel to fit translate_panel panel, points panel end def create_back_panel panel = create_panel @back, @thickness points =[...] #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 =[...] #Position for panel to fit translate_panel panel, points panel end@unknownuser said:
Other than these methods I'm setting some value in the dictionary like x,y,z, lenx, leny etc.
One more thing that I observed is: If I'm not creating box in cabinet, then only one copy is created. but If I'm selecting panels in the cabinet and tried to create box then it will create 2 copies of of box.
-
Is you code inside a module ??
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 endLeaves unanswered questions...
Where do the Constants DESCRIPTION and BACK come from ?
what is 'b' that RectangleBox makes ?
Is it a 'box' in geometry ?
What is the '.draw' method for it ?
This method returns a 'component' ??
I'm sure your code could be less convoluted and thereby more easily understood...

-
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 endI called create_box method form menu by creating object for DBox
box = DBox.new box.create_boxI 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. -
The class is somewhat like a module in this context...
But still best to include the whole lot inside your own module...Can I suggest you revisit your various entities definitions.
Sometimes you usemodel.active_entities, anothermodel.entities, they might not always be the same thing...
Also if you make yourgroupand immediately add thing togroup.entitiesit might be easier...
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement