Get parent name and type of entity
- 
Hi there,
I have question about, how to get parent name of current enitie.
It is no problem with ComponentInstance parents but doesnt work with Group parents.Parent_name(Sketchup.active_model.entities.parent)
def self.Parent_name(parent) if parent.kind_of? Sketchup;;Model pname=Sketchup.active_model.title+" <Model>" elsif parent.kind_of? Sketchup;;Group pname=parent.name+" <Group>" elsif parent.kind_of? Sketchup;;ComponentInstance pname=parent.name+" <Component>" elsif parent.kind_of? Sketchup;;ComponentDefinition pname=parent.name+" <Component>" end#if end #Parent_nameMy aim is to get Parent Name and Type of exact entity
P.S. This code snipet is something similar like TIG ComponentReporter++.rb - 
GroupandImageentities are special instances ofComponentDefinition.Model.definitiondoesn't just include the definitions forComponentInstance, but alsoImageandGroup. Make use ofdefinition.image?anddefinition.group?to detect what the definition represent.For a detailed breakdown of definitions and instances in SketchUp, have a look at this article: http://www.thomthom.net/thoughts/2012/02/definitions-and-instances-in-sketchup/
 - 
Btw, ruby convension is that method names start with lower case letters. Upper case is allowed, but they will then look like constants and can cause confusion.
 - 
The 'parent' is going to be the '
Sketchup.active_model' or a 'ComponentDefinition' - these are the two entities 'containers'.
If it's the latter, then it could be any one of three 'types' of 'ComponentDefinition'.
Usingif parent.image?is unlikely to be needed [unless you already have invented non API other ways of delving into an image and getting its face and four edges !]
if.parent.group?will tell you that it's a group, if you return itsparent.nameyou get something like "Group#123" - BUT if you want the group's 'displayed' name [which might be ''] then you can get it usingparent.instances[0].name- Note that this is usually going to be what you want... BUT be warned... if there are copies of the group that have never been edited [which would have auto-make-uniqued them] then other instances might have different names [just like you can individually 'name' component-instances] - so this method only returns the current name of the original instance of a group !
The final version is 'none-of-the-above' - aka an "ordinary" 'component-definition'.
Usingparent.namewill return the name of the definition.
Because a component can have several instances you cannot get theinstance.namesimply from knowing the parent - unless of course there is onlyparent.instances[0]in which case it'sparent.instances[0].name[similar to the 'group' example].
If you have a tool that's picking objects - like a picked face inside a picked component-instance [for example] - it will return the definition and thereby its name, but your tool can have severalpickhelperswhich identify the instance too and so you can get the instance and from thatinstance.nameandinstance.definitionetc etc... - 
Thanks, it helped me a lot

Basically i usedif parent.group?to understand if entities parent is group and
parent.instances[0].nameto get parents name.
As i understand as long as I will not be duplicating my group entities I will not get in troubles.
 - 
Also about entities parents.
Aim is to get parent ID in case the parent Name will be similar to at least 2 entities with:
parent.idThe code is returns me exact ID and also warning in Ruby console:
warning; Object#id will be deprecated; use Object#object_idIs this warning very important or it is optional ?
 - 
Yes it will be removed in a future Ruby version.
use
object_idor__id__instead.Add: The id integers will change from session to session!
 - 
Thanks

 
Advertisement