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_name
My aim is to get Parent Name and Type of exact entity
P.S. This code snipet is something similar like TIG ComponentReporter++.rb -
Group
andImage
entities are special instances ofComponentDefinition
.Model.definition
doesn't just include the definitions forComponentInstance
, but alsoImage
andGroup
. 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.name
you 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.name
will return the name of the definition.
Because a component can have several instances you cannot get theinstance.name
simply 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 severalpickhelpers
which identify the instance too and so you can get the instance and from thatinstance.name
andinstance.definition
etc etc... -
Thanks, it helped me a lot
Basically i usedif parent.group?
to understand if entities parent is group and
parent.instances[0].name
to 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.id
The code is returns me exact ID and also warning in Ruby console:
warning; Object#id will be deprecated; use Object#object_id
Is this warning very important or it is optional ?
-
Yes it will be removed in a future Ruby version.
use
object_id
or__id__
instead.Add: The id integers will change from session to session!
-
Thanks
Advertisement