sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Draw a new component with axes at original pick point?

    Scheduled Pinned Locked Moved Developers' Forum
    14 Posts 5 Posters 978 Views 5 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.
    • TIGT Offline
      TIG Moderator
      last edited by

      As Chris says...
      You can't relocate the axes of a component [or group] using the API.
      BUT you can transform all of its geometry and then make an inverse transformation of its instance so that it looks exactly the same in the model, as it dis but its axes appear to have changed: in face the axes have stayed [they are always at 0,0,0 inside the container's context], so in the same 'place', but everything else has changed around them, like the geometry and the instances transformation that mirror it...

      Look at this http://forums.sketchucation.com/viewtopic.php?p=398885#p398885 it relocates the group's origin at the ORIGIN... expand it with a rotation transformation too if needed...

      TIG

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

        component_instance.transformation.origin
        The component definition is based around [0,0,0].

        TIG

        1 Reply Last reply Reply Quote 0
        • J Offline
          johnwmcc
          last edited by

          Well, I'm ALMOST there, following the advice so generously and quickly given.

          My first two points are the initial picked point @ip1, and the current mouse position @ip. I pick on a corner of an existing component, NOT at its origin, drag the mouse to place the cross-section in the correct orientation, then click again.

          In the method onLButtonDown on the first click I set a transform @tf = @ip1.transformation.

          I pass the position of these points to the method 'draw_geometry(pt1, pt2, view)' where pt1 = @ip1.position and pt2 = @ip.position.

          I copy the points defining the cross-section into an array @pts0[] with one corner at the origin. I then transform these points to @pts[] using

          @pts0.each_index {|i| @pts[i] = @pts0[i].transform(@tf)}

          I can then draw the cross-section in the method 'draw_geometry(pt1, pt2, view)' in the right orientation using view.draw_polyline(@pts), and also draw the cross-section as a component @comp (initially just a face) at the origin in 'create_geometry(pt1, pt2, view)', then move and rotate it back to (almost) where I want it using

          @comp.move!(@tf)

          But in both cases, the cross-section is drawn NOT at the location @ip1 (which is where I expected it to go), but at the origin of the existing component on whose corner I clicked.

          If I click somewhere in the model not on a component, the cross-section is drawn correctly oriented at the World origin, not at @ip1.position.

          I thought that the @ip1.transformation would transform a point [0,0,0] to the location of the pick point @ip1, NOT to the origin of the component it is located in (or World origin if the pick point is not located in existing geometry). I seem to have misunderstood this.

          How can I retrieve the coordinates of this component origin, so as to define a vector from there to @ip1.position, and locate the cross-section correctly?

          Thanks again in advance if you can help further.

          John McC

          1 Reply Last reply Reply Quote 0
          • J Offline
            johnwmcc
            last edited by

            I don't think I have a handle for the component_instance that I click on - just the @ip1 pick point - so I'm not sure how I'd use your suggestion, TIG.

            But I think Martin Rinehart's Edges to Rubies might have the clue - the translate elements of the @tf.to_a array elements 12, 13, and 14 will contain the coordinates of the picked-on component's origin, I think. (Must look it up to be certain.) Will try this later today, and see if it works.

            John McC

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

              @johnwmcc said:

              I don't think I have a handle for the component_instance that I click on - just the @ip1 pick point - so I'm not sure how I'd use your suggestion, TIG.

              But I think Martin Rinehart's Edges to Rubies might have the clue - the translate elements of the @tf.to_a array elements 12, 13, and 14 will contain the coordinates of the picked-on component's origin, I think. (Must look it up to be certain.) Will try this later today, and see if it works.

              John McC
              If you get the object.transformation, make it an array [.to_a] and extract the elements [12][13][14] for the x/y/z and then assemble a point from those..., it's the same as using object.transformation.origin which return the point ready made.

              Why don't you have the transformation ?
              You can use a view pickhelper 'best_picked' or 'picked_element' method to get the object under the cursor and then from there its transformation...
              https://developers.google.com/sketchup/docs/ourdoc/pickhelper#best_picked
              https://developers.google.com/sketchup/docs/ourdoc/pickhelper#picked_element

              TIG

              1 Reply Last reply Reply Quote 0
              • J Offline
                johnwmcc
                last edited by

                I'll try the pick_helper, then. Thanks again.

                1 Reply Last reply Reply Quote 0
                • T Offline
                  timwarner
                  last edited by

                  I was hoping this discussion might resolve an issue I've been wrestling with. I created a group at some odd angle and position in a model, and then turned it into a component (group.to_component). When I created a new instance using an identity transformation it showed up in the same place (which is what I wanted).

                  brace = brace.to_component
                  braces = ents.add_group()
                  dummy = Geom::Transformation.new()
                  braces.entities.add_instance(brace.definition,dummy)
                  

                  In this case the component instance and the component definition seem to be in the same place

                  brace.bounds.corner(0)
                  Point3d(-94.3149, -12.7862, -48.5601)
                  brace.definition.bounds.corner(0)
                  Point3d(-94.3149, -12.7862, -48.5601)

                  But in another example I have a component created the same way, but the component origin (i.e., the corner of the boundingbox is relocated to [0,0,0]. In this case a new instance is created at the origin, using an identity transformation. How does SU decide, when it creates a component, where to place the origin of the component definition?

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

                    A hand built component always has its axes at the bottom left of the component's bounding box.
                    You can of course change it at the time of creation or later on using the context-menu.
                    A group is similarly axis-ed but there is no adjustment to the axes manually later...
                    To use code to change e.g. a group's axes you need to work out the transformation needed to move the group back to the model-axes, oriented as desired, and then apply the same transformation.inverse to the group.entities so the contents are moved back to where they appeared in the model before, but of course the group#s axes [aka its insertion-point] might well be in a different place and/or the group itself might be rotated in 3d too...

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      timwarner
                      last edited by

                      @tig said:

                      A hand built component always has its axes at the bottom left of the component's bounding box.
                      You can of course change it at the time of creation or later on using the context-menu.
                      A group is similarly axis-ed but there is no adjustment to the axes manually later...
                      To use code to change e.g. a group's axes you need to work out the transformation needed to move the group back to the model-axes, oriented as desired, and then apply the same transformation.inverse to the group.entities so the contents are moved back to where they appeared in the model before, but of course the group#s axes [aka its insertion-point] might well be in a different place and/or the group itself might be rotated in 3d too...

                      Thx, Tig -- this is helpful. But what does "hand built" mean? The example I cited was a component built in code. Contrast this example:

                      ` ents = model.entities

                      Build a parallelogram centred on the origin

                      a = ents.add_line([-75,-25,0],[25,-25,0])
                      b = ents.add_line([25,-25,0],[75,25,0])
                      c = ents.add_line([75,25,0],[-25,25,0])
                      d = ents.add_line([-25,25,0],[-75,-25,0])
                      d.find_faces
                      shape = ents.add_group(d.all_connected)
                      shape_as_component = shape.to_component
                      shapedef = shape_as_component.definition
                      dummy = Geom::Transformation.new()
                      ents.add_instance(shapedef,dummy)`

                      Here the new instance is located with its corner at the origin, not overlaying the original. Hence my confusion.

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

                        By 'hand built' I mean 'made manually', i.e. without using any code... that's all...

                        If you make a group that straddles the origin, then its origin [axes] are located at its bounds.min...
                        As I tried to explain... you can then move that group from its insertion_point back to the model ORIGIN, and then apply the same translation transformation BUT as an 'inverse', to the group's entities... so that it appears in the same place in the model, BUT its actual axes have been relocated to the ORIGIN...

                        TIG

                        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