Trouble adding entity to component definition

Trouble adding entity to component definition

Postby gkd720 » Wed May 24, 2017 7:35 pm

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.

Code: Select all
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:in `entities'
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)>'
0

gkd720 
 

Re: Trouble adding entity to component definition

Postby TIG » Wed May 24, 2017 9:21 pm

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

Re: Trouble adding entity to component definition

Postby sdmitch » Wed May 24, 2017 10:14 pm

I can't tell you why but if you change
Code: Select all
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
Code: Select all
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.
0
Nothing is worthless, it can always be used as a bad example.

http://sdmitch.blogspot.com/
User avatar
sdmitch 
PluginStore Author
PluginStore Author
 

Re: Trouble adding entity to component definition

Postby gkd720 » Thu May 25, 2017 12:39 am

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.
0

gkd720 
 

Re: Trouble adding entity to component definition

Postby TIG » Thu May 25, 2017 11:42 am

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

Re: Trouble adding entity to component definition

Postby Dan Rathbun » Sun Jun 11, 2017 9:10 pm

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.
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    SketchUcation One-Liner Adverts

    by Ad Machine » 5 minutes ago



    Ad Machine 
    Robot
     



     

    Return to Developers' Forum

    Who is online

    Users browsing this forum: Bing [Bot], maxiedaniels and 3 guests

    Visit our sponsors: