• Login
sketchucation logo sketchucation
  • Login
πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Find the location of an object using Ruby

Scheduled Pinned Locked Moved Developers' Forum
20 Posts 6 Posters 2.7k Views 6 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.
  • J Offline
    jblively
    last edited by 30 Jun 2010, 20:54

    Hello,

    Simple task, but I think i'm over thinking it. I have a box in sketch up and i'm trying to use Ruby to determine the location. it could be the center point of the box or the bottom right corner, I just need a consistent way to read the object location in X,Y,Z coordinates.

    any help would be much appreciated!

    Thanks,

    Joe

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 30 Jun 2010, 21:17

      hmm... need more info.

      Is the box in a component or group? If so - is it nested? Do you know the nesting?

      You need the absolute co-ordinates in world space, or relative to its parent context?

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

      1 Reply Last reply Reply Quote 0
      • T Offline
        TIG Moderator
        last edited by 30 Jun 2010, 21:47

        bounds transformation
        etc

        TIG

        1 Reply Last reply Reply Quote 0
        • J Offline
          jblively
          last edited by 30 Jun 2010, 22:16

          @thomthom said:

          hmm... need more info.

          Is the box in a component or group? If so - is it nested? Do you know the nesting?

          You need the absolute co-ordinates in world space, or relative to its parent context?

          It's a group, non nested and i'd like to be able to find the co-ordinates in world space. I've attached a simple example of what i'm looking at. It's just a grouped box. I'm trying to find the real world co-ordinates of the bottom left corner or center point...doesn't really matter as log as I can check the same spot consistently.

          Thanks again!

          Joe

          1 Reply Last reply Reply Quote 0
          • T Offline
            thomthom
            last edited by 30 Jun 2010, 22:24

            Assuming that the grouped box is selected:

            group = Sketchup.active_model.selection[0] bb = group.bounds

            Now you can get it's center co-ordinates by:
            bb.center

            Or one of the corners: http://code.google.com/apis/sketchup/docs/ourdoc/boundingbox.html#corner
            bb.corner(0)

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

            1 Reply Last reply Reply Quote 0
            • J Offline
              jblively
              last edited by 1 Jul 2010, 18:19

              @thomthom said:

              Assuming that the grouped box is selected:

              group = Sketchup.active_model.selection[0] bb = group.bounds

              Now you can get it's center co-ordinates by:
              bb.center

              Or one of the corners: http://code.google.com/apis/sketchup/docs/ourdoc/boundingbox.html#corner
              bb.corner(0)

              That works! Now, though, what if it's not selected. Is there a way to reference the entity name so that at any given time (when it's not being touched) I could look up where it's location is?

              1 Reply Last reply Reply Quote 0
              • C Offline
                Chris Fullmer
                last edited by 1 Jul 2010, 18:25

                Well, how is it being made? Is your script going to make the object? Does it need to be remembered every time the model is opened? Or does this only need to work while your script is running? There are ways to do all of this, but some are considerably harder than others.

                If your script makes it, then you can just keep the group referenced with a variable. And find it easily while your script is running, or even until SU closes.

                But if you need to find it again when SU is re-opened, then its a little tougher. You will need to add an attribute to your group that marks it as yours. And your script will need to parse through the model each time a model is opened and it will need to search the groups and components for any that are marked with your attibute.

                So which is closer to what you think needs to happen?

                Chris

                Lately you've been tan, suspicious for the winter.
                All my Plugins I've written

                1 Reply Last reply Reply Quote 0
                • T Offline
                  thomthom
                  last edited by 1 Jul 2010, 18:29

                  @jblively said:

                  That works! Now, though, what if it's not selected. Is there a way to reference the entity name so that at any given time (when it's not being touched) I could look up where it's location is?

                  Yes - but how really depend on the scenario...

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

                  1 Reply Last reply Reply Quote 0
                  • J Offline
                    jblively
                    last edited by 2 Jul 2010, 16:31

                    @chris fullmer said:

                    Well, how is it being made? Is your script going to make the object? Does it need to be remembered every time the model is opened? Or does this only need to work while your script is running? There are ways to do all of this, but some are considerably harder than others.

                    If your script makes it, then you can just keep the group referenced with a variable. And find it easily while your script is running, or even until SU closes.

                    But if you need to find it again when SU is re-opened, then its a little tougher. You will need to add an attribute to your group that marks it as yours. And your script will need to parse through the model each time a model is opened and it will need to search the groups and components for any that are marked with your attibute.

                    So which is closer to what you think needs to happen?

                    Chris

                    It will be an existing object that i'll need to find each time the model is opened. The group will eventually be used with sketchyphysics and will be moved by the user. I'll need to be able to get the groups x,y,z position at any given time.

                    In the 'entity info' box, can you give the group a name, such as 'mybox' and then be able to reference the 'mybox' group name somehow to locate it's x,y,z position from ruby?

                    Thanks guys...this is really helping!

                    1 Reply Last reply Reply Quote 0
                    • C Offline
                      Chris Fullmer
                      last edited by 2 Jul 2010, 16:42

                      Sure, I'm not exactly sure what people are doing to make plugins find objects as soon as a model is opened, but I think the options are to use an observer that checks for a new model being opened. Then your script would now to parse the components in the model and look for the one you want.

                      Another option could be to just have an initialize process in your script. Perhaps when you start your script, or maybe an actual menu item called "initialize". Or any number of ways to make you script find the object when the user loads your script for the first time.

                      I think the way you would want to parse the model for your component would be to parse the component definitions in the model, then look for the one you want. I would guess that adding an attribute to your component object would be the best thing to do. If it just realized on the name, then someone might change the name and your script would lose the ability to track the component. So whatever method you choose to "tag" or identify your component, just parse the definitions list for that. Then once you find the definition, get all its instances that are in the model (I think you're suggesting there will be just one). so find it, then you can work its co-ordinates.

                      Sounds like a fun project.

                      Chris

                      Lately you've been tan, suspicious for the winter.
                      All my Plugins I've written

                      1 Reply Last reply Reply Quote 0
                      • T Offline
                        TIG Moderator
                        last edited by 2 Jul 2010, 22:52

                        Give the "box" [group] an attribute to identify it.
                        box.set_attribute("jbl","this_is_the_one",true)
                        Then your early loading 'observer' can find it on the SKP's first opening and on any 'entities change' events... if an entity's a group and it's moved then use
                        if entity.get_attribute("jbl","this_is_the_one",false).... then ...........
                        That way if a user renames it you still know it's there...
                        Copying it so you have multiple instances with the attribute is more tricky ???

                        TIG

                        1 Reply Last reply Reply Quote 0
                        • artmusicstudioA Offline
                          artmusicstudio
                          last edited by 13 Oct 2013, 16:42

                          hello gentlemen,
                          now the question is:

                          how can i place my own object per ruby on

                          user defined coordinates (means: where the user definies per axis-position)

                          so the object's X-coordinate would be the AXIS's X and the 0,0,0 of the object
                          would be 0,0,0 of the USER's axis (temp.)

                          i cannot find anything about axis-positioning in the api....

                          thanx & regards stan
                          thanx stan

                          1 Reply Last reply Reply Quote 0
                          • T Offline
                            TIG Moderator
                            last edited by 13 Oct 2013, 17:23

                            There is no [or at least very scant] API access to custom coordinates.
                            Do you really mean how do you move an object/group to a location ?
                            In that case you made a transformation and apply that to the object...
                            So let's say you want a group relocating at [1,2,3]...
                            tr=Geom::Transformation.new(Geom::Point3d.new(1,2,3)) group.transform!(tr)
                            It you have a group and you want a copy somewhere else, then you need its definition - use:
                            defn=group.entities.parent
                            If it's a component and you have an instance then:
                            defn=instance.definition
                            Then you can add_an instance to an entities context...
                            some_entities.add_instance(defn, tr)
                            If you are moving from one entities context into another and don't want the original use:
                            defn=group.entities.parent some_entities.add_instance(defn, group.transformation) group.erase!

                            you might need to adjust the transformation if the target context differs from the original ?

                            TIG

                            1 Reply Last reply Reply Quote 0
                            • artmusicstudioA Offline
                              artmusicstudio
                              last edited by 13 Oct 2013, 20:52

                              hi tig,
                              the situation is the following.

                              when i calculate coordinates for the parts of my geometry to be created by the ruby, i have all measures related to 0,0,0.

                              but this is always the global zero.

                              what i would like to achieve is, that the user sets the axis at the point, where the new geometry is to be generated, with x-axis defining the orientation (the ruby would only take the temporary 0,0,0 , the direction of the horizontal compoment of the x-axis and place the object - always horizontal at X).

                              so if there are global variables showing the offset from global zero to temp-zero, i could place the object at global 0
                              and transform it (move).

                              πŸ˜„
                              stan

                              1 Reply Last reply Reply Quote 0
                              • T Offline
                                TIG Moderator
                                last edited by 13 Oct 2013, 21:21

                                So... if the parts are inside a group [with [0,0,0] ( ORIGIN) as its origin] then simply transform! the group to the new point, as explain before.
                                OR transform all of the entities inside the group using an 'offset' [in this case the group's origin stays where it was] - this time make a 'translation' transformation...
                                ` vector=ORIGIN.vector_to(Geom::Point3d.new(1, 2, 3))

                                or whatever 'offset' you want...

                                tr=Geom::Transformation.translation(vector)then use: group.entities.transform_entities(tr, group.entities.to_a)`
                                Now the group's contents move along that vector...

                                TIG

                                1 Reply Last reply Reply Quote 0
                                • artmusicstudioA Offline
                                  artmusicstudio
                                  last edited by 13 Oct 2013, 22:08

                                  hi tig,
                                  exactly that is my problem.

                                  if i was defining the position for the object placement myself, i of course could transformate myself to a desired x,y,z-position.

                                  but

                                  what do i do, when the desired x,y,z is defined by the user (set coordinates axis) before he starts the script?

                                  so the question is, is it possible to 'read' the user-defined axis-offset from some global variable?

                                  stan

                                  1 Reply Last reply Reply Quote 0
                                  • Dan RathbunD Offline
                                    Dan Rathbun
                                    last edited by 14 Oct 2013, 00:58

                                    @artmusicstudio said:

                                    hello gentlemen,
                                    now the question is:

                                    how can i place my own object per ruby on

                                    user defined coordinates (means: where the user definies per axis-position)

                                    It is easier than you think.. using SketchUp's built-in place component tool.

                                    But you must save your object as a component.

                                    see my answer in another topic: http://sketchucation.com/forums/viewtopic.php?f=180&t=51853&p=469352&hilit=move+rotate+component#p469352

                                    πŸ’­

                                    I'm not here much anymore.

                                    1 Reply Last reply Reply Quote 0
                                    • T Offline
                                      TIG Moderator
                                      last edited by 14 Oct 2013, 09:04

                                      You can even do it with a group, BUT you need to write a Tool class...
                                      Then after making the group you transform it to the cursor position and keep transforming it as the mouse moves. So it looks like it's stuck on the cursor, then when the user left-clicks the transforming stops and the group is where the user wishes...

                                      Probably easier, as Dan said, if you have made it as a group use instance=group.to_component then defn=instance.definition [use defn.name='MyThing' etc].
                                      Then instance.erase! to remove the original and place the new component on the cursor for the user to choose a location with model.place_component(defn, false)

                                      TIG

                                      1 Reply Last reply Reply Quote 0
                                      • artmusicstudioA Offline
                                        artmusicstudio
                                        last edited by 14 Oct 2013, 21:32

                                        hi dan & tig,
                                        thanx a lot, though this is a different approach than mine, but very useful. i will try to implement thiscurslr functionality , too, if i manage it.
                                        for my idea i will let the user draw 1 line, which can maybe be selected for the ruby call and give the ruby the vector to transform the object.
                                        so the ruby-fight continues...
                                        stan

                                        1 Reply Last reply Reply Quote 0
                                        • Dan RathbunD Offline
                                          Dan Rathbun
                                          last edited by 15 Oct 2013, 02:12

                                          You should study the drawline.rb in the "Examples" sub-dir.

                                          I'm not here much anymore.

                                          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