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

    Move a Transformation.rotation object to x, y and z position

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 3 Posters 529 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.
    • kimpastroK Offline
      kimpastro
      last edited by

      Hi everyone,

      just need to move a Transformation.rotation, to another X, Y point.

      My Code:

      
      vector = Geom;;Vector3d.new 0,0,1
      
      #rot_z came from jSON, here he's an Integer 45
      t_rotation = Geom;;Transformation.rotation ORIGIN, vector, rot_z * Math;;PI / 180
      
      

      Alright, have this code, but now I need to move this guy above to a new position:

      
      #pos_x, y and z came from an imported jSON.
      point = Geom;;Point3d.new pos_x, pos_y, pos_z
      t_position = Geom;;Transformation.new point
      
      

      I've tried:

      
      t_rotation = t_rotation.set! t_position
      
      

      And after all that, I put entities into model, like that:

      
      entities = Sketchup.active_model.active_entities
      instance = entities.add_instance loaded_module, t_position
      $dc_observers.get_latest_class.redraw_with_undo instance
      Sketchup.active_model.materials.purge_unused
      
      

      *loaded_module is:

      
      loaded_module = Sketchup.active_model.definitions.load_from_url uri
      loaded_module.set_attribute 'dynamic_attributes', 'front', 'black_color'
      etc...
      
      

      And tried a lot of other combination with Transformation...

      This structure of classes and instances in Sketchup is just so confuse to me. Don't know who is parent of who, the Ruby API is very poor and misterious... I work with web app dev, not sketchup.. and this thing is hurting my brain. 😍 lol.

      Well... that's it. Thank you all.

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

        Sorry, but your code is full of holes...
        Try

        #rot_z came from jSON, here he's an Integer 45
        t_rotation = Geom;;Transformation.rotation(ORIGIN, Z_AXIS, rot_z.degrees)
        
        

        Now

        #pos_x, y and z came from an imported jSON.
        point = Geom;;Point3d.new(pos_x, pos_y, pos_z)
        t_position = Geom;;Transformation.new point
        

        So far so good...

        tr = t_position * t_rotation
        

        to combine them into one Transformation...

        entities = Sketchup.active_model.active_entities
        instance = entities.add_instance(loaded_module, tr)
        

        Assuming loaded_module is a valid definition...
        It is placed with the rotation and point as expected...
        Alternatively do it in two steps - at as point, then rotate it...

        instance = entities.add_instance(loaded_module, t_position)
        instance.transform!(t_rotation)
        

        You are actually making this more convoluted and awkward than it really is πŸ˜•
        No need to involve DC code at this stage...

        TIG

        1 Reply Last reply Reply Quote 0
        • kimpastroK Offline
          kimpastro
          last edited by

          @TIG, thanks a lot man.. that clarify a lot of things now. 😎

          but, still having some issues.
          All that code is inside of an 'each' block.. so actually he'll place N entities on the module and

          Well.. while I was writing that above, I've tried another thing, and this works:

          
          instance = entities.add_instance(loaded_module, t_rotation)
          instance.transform!(t_position)
          
          

          but this don't:

          
          instance = entities.add_instance(loaded_module, t_position)
          instance.transform!(t_rotation)
          
          

          The both generate modules and put they in the model, but the second option rotate the entity like... earth and sun, not in your own axis.

          and i need $dc_observers because i'm using texture in these guys, and before setting dynamic_attributes i'm loading textures and setting up to them, and need to redraw to apply (at least, that's a solution I've founded).

          PS: don't know if i'm using the correct terms for elements like: model, entity, modules, etc. Sorry about that.

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

            If you rotate it then position it ... the object is assumed to be placed at the ORIGIN and then rotated about the specified point - originally set as the ORIGIN, then it is positioned where your specify it to be.
            BUT...
            If you position it then rotate it ... the object is positioned where you specify, and it is then rotated about the given point - and in the original transformation that was set as the ORIGIN, so the rotation might appear weird as the object is NOT rotated about its insertion point.
            But if you choose that order of operation, and you were to recast the rotation transformation to specify the actual insertion point used in the positioning transformation it will work.

            If you think about doing this manually...
            1: Place at ORIGIN, rotate about ORIGIN, move to new position...
            gives a different result from...
            2: Place at ORIGIN, move to new-position, rotate about ORIGIN.
            BUT with...
            3: Place at ORIGIN, move to new-position, rotate about new-position.
            the result is the same as 1: !

            If you combine the transformations into one used in the add_instance -- using * then
            trans = trans_p * trans_r
            -- should work as the combination order is read right-to-left...

            TIG

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              @kimpastro said:

              PS: don't know if i'm using the correct terms for elements like: model, entity, modules, etc. Sorry about that.

              Yes you are misusing the term module.

              In your examples, "loaded_module" is a "loaded_component_definition".

              module is a ruby keyword, and a block of code that forms a namespace.

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • kimpastroK Offline
                kimpastro
                last edited by

                Alright, understood.
                Thanks a lot TIG and Dan for all information. you guys are awesome!!!

                1 Reply Last reply Reply Quote 0
                • kimpastroK Offline
                  kimpastro
                  last edited by

                  @dan rathbun said:

                  @kimpastro said:

                  PS: don't know if i'm using the correct terms for elements like: model, entity, modules, etc. Sorry about that.

                  Yes you are misusing the term module.

                  In your examples, "loaded_module" is a "loaded_component_definition".

                  module is a ruby keyword, and a block of code that forms a namespace.

                  Thanks, I'll be careful next time. πŸ˜„

                  I'm confusing about module because we use the same term to determine the "master entity" of our projects to our clients.

                  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