• Login
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!
🫛 Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

Selected Dim to Component Instance

Scheduled Pinned Locked Moved Developers' Forum
4 Posts 3 Posters 363 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.
  • K Offline
    ktkoh
    last edited by 15 Nov 2014, 14:11

    I am starting with the following code (after selecting a dim in the model)

    mod = Sketchup.active_model # Open model
    ent = mod.entities # All entities in model
    sel = mod.selection # Current selection
    
    s_ent = sel[0]
    type = s_ent.typename
    if type == "DimensionLinear"
      
       arr = s_ent.start
    if arr[0].nil?
      UI.messagebox("No attached entity. Point is " + arr[1].to_s)
     else
       UI.messagebox("Attached entity; " + arr[0].to_s)
      type = arr[0].typename
     end 
      parent1 = arr[0].parent
      l_name = parent1.layer.name
     
    end
    

    This gets me to the Component Definition but I need the Component Instance to find the layer name so I can change the dim layer to the Component Instance layer.

    Keith

    1 Reply Last reply Reply Quote 0
    • S Offline
      sdmitch
      last edited by 15 Nov 2014, 17:32

      @ktkoh said:

      I am starting with the following code (after selecting a dim in the model)

      mod = Sketchup.active_model # Open model
      > ent = mod.entities # All entities in model
      > sel = mod.selection # Current selection
      > 
      > s_ent = sel[0]
      > type = s_ent.typename
      > if type == "DimensionLinear"
      >   
      >    arr = s_ent.start
      > if arr[0].nil?
      >   UI.messagebox("No attached entity. Point is " + arr[1].to_s)
      >  else
      >    UI.messagebox("Attached entity; " + arr[0].to_s)
      >   type = arr[0].typename
      >  end 
      >   parent1 = arr[0].parent
      >   l_name = parent1.layer.name
      >  
      > end
      

      This gets me to the Component Definition but I need the Component Instance to find the layer name so I can change the dim layer to the Component Instance layer.

      Keith

      mod = Sketchup.active_model
      ent = mod.active_entities
      sel = mod.selection
      
      s_ent = sel[0]
      type = s_ent.typename
      if type == "DimensionLinear" 
       arr = s_ent.start
       if arr[0].parent.is_a?(Sketchup;;ComponentDefinition)
        parent1 = arr[0].parent;
        parent1.instances.each{|ci|
         if ci.bounds.contains?(arr[1])
          l_name = ci.layer.name;
          s_ent.layer=l_name;
          sel.add ci;break
         end
        }
       else
        puts "Dimension not associated with component"
       end
      end
      

      Nothing is worthless, it can always be used as a bad example.

      http://sdmitch.blogspot.com/

      1 Reply Last reply Reply Quote 0
      • T Offline
        TIG Moderator
        last edited by 15 Nov 2014, 17:37

        Couple of pointers.

        I think you really want:

        mod.**active_**entities # All entities in **active context**

        NOT the entities in the model itself: after all, the user might be inside an edit ?

        You might also like to check Sketchup.version and/or if a particular method is defined [ .respond?] because dim's were added to the API quite recently...

        ... arr = s_ent.start ### the array of data that the dim is attached to [at its start] ... parent1 = arr[0].parent ### the vertex inside the definition l_name = parent1.layer.name ### the definition itself won't have a layer ?

        You have no simple way of knowing the instance from the definition.
        if the parent1.instances.length==1
        instance = parent1.instances[0]
        BUT if not, then you need to find how to spot the instance...

        We have a point at the end of the dim...

        pt = arr[1]

        You can get its screen xy thus:

        view = mod.active_view xy = view.screen_coords(pt) x=xy[0] y=xy[1]

        Now set up a pickhelper to test it...

        ph = view.pick_helper ph.do_pick(x, y) array = ph.all_picked
        returns an array of all things at that point, including instance[s], so iterate through the array.uniq and test if its an instance AND it matches arr[0] == instance.definition
        If so then you have your instance !
        Get the layer from that etc...

        😕

        TIG

        1 Reply Last reply Reply Quote 0
        • K Offline
          ktkoh
          last edited by 15 Nov 2014, 20:34

          Thanks for the help. I'll try your suggestions.

          Keith

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

          Advertisement