Trouble adding entity to component definition
-
I'm trying to add an instance of a component definition as an entity in another component definition, but I'm getting an error. I searched here but didn't find anything that helped yet. Any thoughts or guidance on how to diagnose and fix? Code and error below, with error line marked. Thanks.
def self.create_component_definition_piece(width, height, depth, angle) @componentDefinitionPiece=@definitions.add "Piece" ptLL=[0,0,0] ptLR=[width,0,0] ptUR=[width,0,height] ptUL=[0,0,height] new_face = @componentDefinitionPiece.entities.add_face ptLL, ptLR, ptUR, ptUL if new_face.normal == [0,-1,0] new_face.pushpull depth else new_face.pushpull -depth end end def self.create_component_definition_laddershelves() @componentDefinitionLadderShelves=@definitions.add "LadderShelves" self.create_component_definition_piece(2.5,48,0.75,0) @componentDefinitionLadderShelves.entities.add_instance(@componentDefinitionPiece,@transform000) <<<<< Error on this line. end # Draw the ladder shelves self.create_component_definition_laddershelves() transform=Geom;;Transformation.new([0,0,0]) rotateAndTransform=transform * Geom;;Transformation.rotation([0,0,0],[0,1,0],0) @entities.add_instance(@componentDefinitionLadderShelves,rotateAndTransform) # puts "Number of entities in LadderShelves=",@componentDefinitionLadderShelves.entities.count
Error:
Error: #<TypeError: reference to deleted ComponentDefinition>
C:/Users/gkd/SketchupRubyScripts/ladderShelves2.rb:86:inentities' C:/Users/gkd/SketchupRubyScripts/ladderShelves2.rb:86:in
create_component_definition_laddershelves'
C:/Users/gkd/SketchupRubyScripts/ladderShelves2.rb:99:in<module:LadderShelves>' C:/Users/gkd/SketchupRubyScripts/ladderShelves2.rb:5:in
<top (required)>' -
Your published code only gives us part of the story.
The posted error-messages refer to something else !
Why not make the new 'container' definition first, then create your new definition and add the nested instance ?
Also you code seems to have no 'model.start...' operation or a 'commit' which might help ?? -
I can't tell you why but if you change
def self.create_component_definition_laddershelves() @componentDefinitionLadderShelves=@definitions.add "LadderShelves" self.create_component_definition_piece(2.5,48,0.75,0) @componentDefinitionLadderShelves.entities.add_instance(@componentDefinitionPiece,@transform000)
to
def self.create_component_definition_laddershelves() self.create_component_definition_piece(2.5,48,0.75,0) @componentDefinitionLadderShelves=@definitions.add "LadderShelves" @componentDefinitionLadderShelves.entities.add_instance(@componentDefinitionPiece,@transform000)
it works! I think it is as TIG suggested, you need to complete the creation of one definition before starting the other.
-
Wow. Thanks TIG and sdmitch, that works!
But why might that be? I would think that making a new component definition without yet any entities would not be that fragile or incomplete. Is there any documentation that states this, or suggests this? Why isn't this like declaring a variable but not initializing it? It also would have helped if the error message indicated which component definition it didn't like. Is this a ruby thing or a Sketchup thing? Thanks. -
SketchUp's Ruby can do its 'Garbage Collection' as it goes, so if you create a definition [component or group] it will not persist for long unless it contains entities.
So make it immediately before you use it, that way SketchUp won't tidy up and remove it.
Once it has entities it will be safe - so add those asap - you could also think about adding a single cpoint and erasing it later once some more entities are added... -
A workaround we employ is to stick a temporary cpoint into the entities (at it's origin,) and then delete it later after we've loaded it up with geometry. The temp cpoint stops SketchUp's garbage collector from deleting the object.
Advertisement