sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🛣️ Road Profile Builder | Generate roads, curbs and pavements easily Download

    Model Hierarchy

    Scheduled Pinned Locked Moved Developers' Forum
    2 Posts 2 Posters 563 Views 2 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.
    • S Offline
      spring.freediver
      last edited by

      I don't know if something like this has been posted before. If so, here's yet another one:

      This script prints a diagram of the model hierarchy to the Ruby Console.

      TIG and Thomas: line 46 of the script is to compensate for instance names being blank. The reason I though there was a bug is that in the Outliner window, the instance has a name.

      Thomas: Can you try this on a model that has the Group.parent bug in it? I wasn't sure if I needed to use the real_parent work-around since this is traversing the hierarchy as instances rather than groups.

      
      require 'sketchup.rb'
      
      if (not file_loaded?("PrintHierarchy.rb"))
          UI.menu("Plugins").add_item("PrintHierarchy") { printHierarchy  }
      end
      file_loaded("PrintHierarchy.rb")
      
      def printHierarchy
         answer = UI.messagebox("Print Hierarchy to Ruby Console?", MB_YESNO)
         if (answer == 6)
            model = Sketchup.active_model
            #Sketchup.send_action "showRubyPanel;"
            h = ModelHierarchy.new(model)
            h.pdint(model)
            print("-------------------------------------------\n")
         end
      end
      
      class ModelHierarchy
      # Class to hold the model hierarchy, and example method to print diagram
      # Jeff RIchardson, October 2010
         attr_accessor ;definition, ;children, ;polygons
      
         def initialize(model)
            @definition = {}
            @children = {}
            @polygons = {}
            
            model.definitions.each do |comp|
               @polygons[comp] = 0
               comp.entities.each {|e| @polygons[comp] += 1 if (e.is_a?(Sketchup;;Face))}
               comp.instances.each do |inst|
                  @definition[inst] = comp
                  parent = inst.parent
                  @children[parent] = [] if (!@children.has_key?(parent))
                  @children[parent].push(inst)
               end
            end
         end
         
         def pdint(key, level=0)
            printf("Model %s [%d polygons]\n", key.title, key.number_faces) if (key.is_a?(Sketchup;;Model))
            if (@children.has_key?(key))
               @children[key].each do |k|
                  name = k.name
                  name = sprintf("Instance of %s", @definition[k].name) if (name.empty?)
                  print("|   "*level)
                  printf("|---%s(%s) [%d polygons]\n", name, k.typename, @polygons[@definition[k]])
                  pdint(@definition[k], level+1)
               end
            end
         end
         
      end
      
      
      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        A definition.name applies to every instance, as in instance.definition.name - which can't be "" - but an instance ALSO has it's own name - which might be "" - instance.name ?

        TIG

        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