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

    How can I access the layer name in a DC via Ruby?

    Scheduled Pinned Locked Moved Dynamic Components
    sketchup
    7 Posts 3 Posters 881 Views 3 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.
    • erkanE Offline
      erkan
      last edited by

      I want to access to the layer name of the DC as a part of the formula, the closest I can get is via the use of class DCFunctionsV1 mentioned in the post, which I couldn't repurpose for this case; http://sketchucation.com/forums/viewtopic.php?f=180&t=37083

      I'm wondering if there's an easier way, many thanks in advance.

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        Hi erkan,

        There isn't a built-in DC function to get the Layer. If you want to do it, you will need to write your own function for it. Follow the examples in the topic you linked to.

        This can also be done with a stand-alone Ruby script in SketchUp, but outside the DC framework. It could go through the model, identify DC's, then assign the DC's Layer to an attribute.

        Hi

        1 Reply Last reply Reply Quote 0
        • erkanE Offline
          erkan
          last edited by

          thanks Jim, just as I thought πŸ˜„

          so here's how I try to tinker with that example, basically I've taken out the volume and tried to replace it with the Layer Name of the DC as per below;

          if defined?($dc_observers)
            # Open SketchUp's Dynamic Component Functions (V1) class.
            # only if DC extension is active
            
          class DCFunctionsV1
          
            protected
            # return layername
            # Usage; =layername()
            def layername(a)
              return @source_entity.layer.name
            end
            protected;layername
            
          end#class
          
          end#if
          

          And I've found that @source_entity.layer.name is not correct, and couldn't make much of meaning from the existing resources unless I start understanding the Ruby. Can anybody show me how to access the Layer Name of the DC (@source_entity?)

          many thanks

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

            That line of code should return the selected component-instance's layer's name ?
            However, once the DC code has assigned the name you might need to refresh [apply] the DC code again to show the correct layer-name if you have changed the instance's layer since it was last interrogated. πŸ˜•

            TIG

            1 Reply Last reply Reply Quote 0
            • erkanE Offline
              erkan
              last edited by

              In theory yes, it should return the Layer Name that the DC lies on, so that I can use it for conditional calculations. In the planned workflow there will be no Layer changes, so that should be fine.

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

                I have used it on a DC and it worked for me ??

                require('sketchup')
                require('dynamiccomponents.rb')
                
                if defined?($dc_observers)
                  # Open SketchUp's Dynamic Component Functions (V1) class.
                  # only if DC extension is active
                  
                class DCFunctionsV1
                
                  protected
                
                  # Usage; =layername()
                  if not DCFunctionsV1.method_defined?(;layername)
                    def layername(a)
                      ###puts @source_entity.layer.name
                      return @source_entity.layer.name
                    end
                    protected;layername
                  end
                  
                end#class
                
                end#if
                

                πŸ˜•

                TIG

                1 Reply Last reply Reply Quote 0
                • erkanE Offline
                  erkan
                  last edited by

                  ehem πŸ’š worked for me too β˜€ , apparently I missed the first two lines.

                  thanks 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