I’m making an attempt to get attribute values of parts inside parts. I’ve looked for some time, and getting shut, however caught. My utility is drawing bookshelves for a room. I’ve made varied part definitions to do that: room, base cupboard, base high, and bookshelves. For instance, for certainly one of a number of base cupboards, I create a “Base” definition and provides it a reputation (e.g., “BaseOuterLeft”) after which add an occasion of it. This “Base” definition’s physique creates a part definition (referred to as “Stable”, so I find yourself with parts referred to as Stable, Stable#1, Stable#2, and so on.) for every base half (left facet, proper facet, again, and so on.) with distinctive dimensions. I need to listing my ultimate half sizes. I at the moment can get dimensions, however I have to affiliate them with the precise half (e.g., BaseOuterLeft, again).
How can I connect a reputation to every half?
Code/outcomes:
Present dimensions gotten with:
> Sketchup.active_model.entities.every places entity.class.title; places entity.definition.title; entity.definition.entities.everyent
. . .
Sketchup::ComponentInstance
BaseOuterLeft
ConstructionPoint
0"
0"
0"
ComponentInstance
1' 11 1/4"
3/4"
2' 1/2"
ComponentInstance
1' 11 1/4"
3/4"
2' 1/2"
ComponentInstance
3/4"
2' 7"
2' 1/2"
. . .
. . .
With out seeing the remainder of your code, I can’t say for positive the place you wanted to take action, however as you create a brand new ComponentDefinition you possibly can assign it a significant title by way of its .title= methodology, and while you place a ComponentInstance you possibly can assign a significant title to the occasion by way of its .title= methodology. All of your snippet is doing for the nested cases is printing their typename, which is (doh!) ComponentInstance, so it doesn’t even present the ComponentDefinition names that SketchUp auto-generated. And if you’re reusing a Part to nest cases in a distinct father or mother, you are able to do so with out creating a brand new ComponentDefinition every time, because the names Stable, Stable#1, and so on. recommend you’re doing.
BTW: It isn’t an excellent observe to cram so many code statements onto a single line like that. It makes it very onerous to learn and does nothing to enhance efficiency.
1 Like
Thanks for the data. Yeah, “isn’t an excellent observe to cram “, I agree. This was simply me manually poking round within the Ruby console. In my interrogating line, within the entity for loop, I used to be making an attempt ent.title, but it surely doesn’t exist. So, assuming I assign names to nested definitions and cases, may you present me a normal type of accessing the suitable title? Thanks once more.
You possibly can check whether or not an object responds to a given methodology like …
if ent.respond_to?(:definition) # it is a group or part occasion
until ent.title.empty?
places ent.title
else
places ent.definition.title
finish
else # it is a primitive or complicated (dimension, part aircraft, and so on.)
if ent.respond_to?(:typename)
places ent.typename
else
places ent.class.title
finish
finish