• Login
sketchucation logo sketchucation
  • Login
🤑 30% Off | Artisan 2 on sale until April 30th Buy Now

Get Point3D from place_component

Scheduled Pinned Locked Moved Developers' Forum
6 Posts 3 Posters 188 Views
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.
  • F Offline
    Francis Mc Shane
    last edited by 10 Jan 2013, 14:31

    Is there a Ruby way to get the insertion point of a component. Unfortunately model.place_component does not return a Point3D object, just nil.

    Thank you for any help you can offer.

    Francis

    1 Reply Last reply Reply Quote 0
    • S Offline
      sdmitch
      last edited by 10 Jan 2013, 15:40

      Model.place_component seems to be the tool that is used when you select a component for placement from the Components Browswer. I guess the only way to find the insertion points would be to search for all instances of the component to get the .transformation.origin. of each like this

      mod = Sketchup.active_model
      ent = mod.active_entities
      sel = mod.selection
      cmp = mod.definitions["Component#1"]
      puts "--insertion points of Component#1--"
      cmp.instances.each{|ci| puts ci.transformation.origin}
      puts "-----------------------------------"
      
      
      

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

      http://sdmitch.blogspot.com/

      1 Reply Last reply Reply Quote 0
      • D Offline
        Dan Rathbun
        last edited by 10 Jan 2013, 20:30

        If we are talking about the native tool... then (regardless of what was selected beforehand,) the selection set will contain the newly placed Sketchup::ComponentInstance after the user clicks the insertion point.

        So:

        (1) You CAN use a ToolsObserver in combination with a SelectionObserver, to detect the placement of a component instance, and get it's origin (relative to the active entities object,) via sel[0].transformation.origin

        (2) Then determine if your at the model level via:

        Sketchup.active_model.active_entities == Sketchup.active_model.entities

        If true then the origin is in "world co-ordinates".

        If false, then you must determine the instance's nesting via Sketchup.active_model.active_path, and reverse iterate the returned array, combining the origin offsets together.

        The catch is that perhaps one of the nested parent objects may not be aligned with the world axis. So you would need to take that into account.

        This subject has been covered MANY times.
        See this recent post: find the position of a group in the active context
        Also use the forum search feature to search on terms "Group Origin" "Transformation" etc.

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 10 Jan 2013, 20:41

          Now... model#place_component()

          (1) The API dictionary is incorrect, it does NOT return nil, it returns a reference to the active_model which is basically frivolous, but does allow call chaining.

          We have logged an API doc typo on this, as well as asking for something more useable to be returned (especially in the case of multiple insertions when the 2nd argument is true.)

          (2) When the 2nd argument is false, and only 1 instance is inserted, model#place_component() works the same as the native tool, ie: the selection is cleared, and the newly placed component instance's reference is pushed into the selection (as it's only member.)

          So you can get it's insertion point as discussed in the post above.

          FYI ... When the 2nd argument to model#place_component() is true, and multiple instances MIGHT HAVE BEEN inserted, the model's selection object IS NOT CHANGED.
          If something was previously selected, it still is. If the selection was empty, it will still be empty.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • F Offline
            Francis Mc Shane
            last edited by 11 Jan 2013, 16:45

            @sdmitch said:

            Model.place_component seems to be the tool that is used when you select a component for placement from the Components Browswer. I guess the only way to find the insertion points would be to search for all instances of the component to get the .transformation.origin. of each like this

            mod = Sketchup.active_model
            > ent = mod.active_entities
            > sel = mod.selection
            > cmp = mod.definitions["Component#1"]
            > puts "--insertion points of Component#1--"
            > cmp.instances.each{|ci| puts ci.transformation.origin}
            > puts "-----------------------------------"
            > 
            > 
            

            Thank you for this it's very helpful. I will try it.

            1 Reply Last reply Reply Quote 0
            • F Offline
              Francis Mc Shane
              last edited by 11 Jan 2013, 16:49

              @dan rathbun said:

              Now... model#place_component()

              (1) The API dictionary is incorrect, it does NOT return nil, it returns a reference to the active_model which is basically frivolous, but does allow call chaining.

              We have logged an API doc typo on this, as well as asking for something more useable to be returned (especially in the case of multiple insertions when the 2nd argument is true.)

              (2) When the 2nd argument is false, and only 1 instance is inserted, model#place_component() works the same as the native tool, ie: the selection is cleared, and the newly placed component instance's reference is pushed into the selection (as it's only member.)

              So you can get it's insertion point as discussed in the post above.

              FYI ... When the 2nd argument to model#place_component() is true, and multiple instances MIGHT HAVE BEEN inserted, the model's selection object IS NOT CHANGED.
              If something was previously selected, it still is. If the selection was empty, it will still be empty.

              Dan,
              thank you for you for your two posts which are very helpful. I would have ticked them both as 'resolve' but I had already done this for a previous post and 'the system' wouldn't allow me to tick more than one post as a 'resolved' post. I will be trying your ideas soon. Thanks again. Francis

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

              Advertisement