sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Get parent name and type of entity

    Scheduled Pinned Locked Moved Developers' Forum
    8 Posts 4 Posters 1.4k Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K Offline
      krisjaniszakis
      last edited by

      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

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        Group and Image entities are special instances of ComponentDefinition. Model.definition doesn't just include the definitions for ComponentInstance, but also Image and Group. Make use of definition.image? and definition.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/

        Thomas Thomassen β€” SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          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.

          Thomas Thomassen β€” SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            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'.
            Using if 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 its parent.name you get something like "Group#123" - BUT if you want the group's 'displayed' name [which might be ''] then you can get it using parent.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'.
            Using parent.name will return the name of the definition.
            Because a component can have several instances you cannot get the instance.name simply from knowing the parent - unless of course there is only parent.instances[0] in which case it's parent.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 several pickhelpers which identify the instance too and so you can get the instance and from that instance.name and instance.definition etc etc...

            TIG

            1 Reply Last reply Reply Quote 0
            • K Offline
              krisjaniszakis
              last edited by

              Thanks, it helped me a lot πŸ˜„
              Basically i used

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

              1 Reply Last reply Reply Quote 0
              • K Offline
                krisjaniszakis
                last edited by

                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 ?

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  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!

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • K Offline
                    krisjaniszakis
                    last edited by

                    Thanks πŸ˜„

                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    • First post
                      Last post
                    Buy SketchPlus
                    Buy SUbD
                    Buy WrapR
                    Buy eBook
                    Buy Modelur
                    Buy Vertex Tools
                    Buy SketchCuisine
                    Buy FormFonts

                    Advertisement