sketchucation logo sketchucation
    • Login
    โ„น๏ธ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    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.
    • Chris FullmerC Offline
      Chris Fullmer
      last edited by

      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
      • TIGT Offline
        TIG Moderator
        last edited by

        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

          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
          • TIGT Offline
            TIG Moderator
            last edited by

            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

              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
              • TIGT Offline
                TIG Moderator
                last edited by

                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

                  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

                    @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
                    • TIGT Offline
                      TIG Moderator
                      last edited by

                      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

                        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

                          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