sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Help please. How to get transformation for the face

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 6 Posters 1.6k 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.
    • C Offline
      cesaro36
      last edited by cesaro36

      Hi everyone
      I need help to get the transformation of the current face under the cursor.
      I'm using the input point method but my problem emerges when the cursor is Inferenced.

      It is weird because my @ip.face is correct but @ip.transformation doesnt match.

      I dont use a pick helper because I need to get faces outside p.e. when editing inside a component.

      The only plugin that I've seen it managing well is AngleInpector from Freddo tools.

      Any ideas?

      Thanks


      get transformation

      1 Reply Last reply Reply Quote 0
      • sdmitchS Offline
        sdmitch
        last edited by

        @ip.transformation would be the transformation of the parent of the face not the face itself.

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

        http://sdmitch.blogspot.com/

        1 Reply Last reply Reply Quote 0
        • C Offline
          cesaro36
          last edited by

          thanks, but... what I mean is how to get the correct transformation for the face bellow when the cursor is inferenced. Is there something I'm missing?

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

            A face doesn't have a "transformation" per se.
            The "input point" your identify can give the transformation of where it was picked - which you can then use to modify entities' transformations later...
            You can "transform" a group/instance, face, edge or vertex...

            You can gather various info about any picked vertex/edge/face [e.g. position/line/center/normal ] - and devise transformations to modify these [bearing in mind the parent's transformation you have also collected...]
            BUT what do you want to do ?

            Please explain what you want to 'find' and what you want to do with it...

            TIG

            1 Reply Last reply Reply Quote 0
            • C Offline
              cesaro36
              last edited by

              Thanks TIG
              My intention is to highlight the face that the cursor is passing over.
              In the Animation GIF you can see that when the interfering is happening the highlighted face is been drawn where it should'n, meaning that the transformation given by @ip.transformation is not the same as it was before the iterferencing while the @ip.face is still the same. (is that make sense)
              I'm doing my best trying to explain this in english.

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

                @cesaro36 said:

                Thanks TIG
                My intention is to highlight the face that the cursor is passing over.

                You can do this easily by pushing the face's reference into the model selection set.

                old_set = model.selection.to_a model.selection.clear model.selection.add(@ip.face)

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • C Offline
                  cesaro36
                  last edited by

                  Thanks Anton
                  I'll try it as soon as I get to my laptop.

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

                    Anton_S,
                    Do you have similar code to highlight local bounding boxes (including those inside groups and components)?

                    1 Reply Last reply Reply Quote 0
                    • A Offline
                      Anton_S
                      last edited by

                      cesaro36,

                      Like TIG and others mentioned a face doesn't have a transformation, but its point positions are bound to the transformation of a group/component instance containing the face. In that case, you will need to transform all points of the face into global space before highlighting the face.

                      Here is a sample face highligher tool:

                      class FaceHighlighterTool
                      
                        def initialize
                          @ip = Sketchup;;InputPoint.new
                          @triplets = []
                          @edge_points = []
                          @hovered_face = nil
                          @hovered_face_tra = nil
                          @face_color = Sketchup;;Color.new(0,40,255,80)
                          @edge_color = Sketchup;;Color.new(0,0,255,255)
                          @edge_width = 3
                        end
                      
                        def deactivate(view)
                          @hovered_face = nil
                          view.invalidate
                        end
                      
                        def onMouseMove(flags, x, y, view)
                          @ip.pick(view, x, y)
                          face = @ip.face
                          if face.nil?
                            if @hovered_face
                              @hovered_face = nil
                              view.invalidate
                            end
                            return
                          end
                          if face != @hovered_face
                            @hovered_face = face
                            @hovered_face_tra = @ip.transformation
                            # In order to draw a face with holes, we must draw its mesh
                            mesh = face.mesh
                            polygons_size = mesh.count_polygons
                            @triplets = Array.new(polygons_size)
                            for i in 0...polygons_size
                              # Obtain one of the triangles making up the face
                              triplet = mesh.polygon_points_at(i+1)
                              # Transform to global space
                              triplet.each { |pt| pt.transform!(@hovered_face_tra) }
                              # Store for drawing
                              @triplets[i] = triplet
                            end
                            # Get all edges for drawing a border
                            @edge_points.clear
                            face.edges.each { |edge|
                              @edge_points << edge.start.position
                              @edge_points << edge.end.position
                            }
                            # Transform all edge points to global space
                            @edge_points.each { |point| point.transform!(@hovered_face_tra) }
                            # Trigger the drawing
                            view.invalidate
                          end
                        end
                      
                        def draw(view)
                          return unless @hovered_face
                          view.drawing_color = @face_color
                          @triplets.each { |triplet|
                            view.draw(GL_POLYGON, triplet)
                          }
                          view.drawing_color = @edge_color
                          view.line_width = @edge_width
                          view.line_stipple = ''
                          view.draw(GL_LINES, @edge_points)
                        end
                      
                      end # class FaceHighlighterTool
                      
                      Sketchup.active_model.select_tool(FaceHighlighterTool.new)
                      

                      If you paste it all into Ruby Console you will see the effect.

                      Anton

                      1 Reply Last reply Reply Quote 0
                      • A Offline
                        Anton_S
                        last edited by

                        There it is, TBoy:

                        class BoundsHighlighterTool
                        
                          def initialize
                            @ip = Sketchup;;InputPoint.new
                            @hovered_inst = nil
                            @global_parent_tra = nil
                            @global_bb = nil
                            @labb_global_faces = [] # Faces for the local axes aligned bounding box.
                            @gabb_global_faces = [] # Faces for the global axes aligned bounding box.
                            @labb_global_edges = []
                            @gabb_global_edges = []
                            @labb_face_color = Sketchup;;Color.new(255,40,0,80)
                            @gabb_face_color = Sketchup;;Color.new(0,40,255,80)
                            @labb_edge_color = Sketchup;;Color.new(255,0,0,255)
                            @gabb_edge_color = Sketchup;;Color.new(0,0,255,255)
                            @edge_width = 3
                          end
                        
                          def deactivate(view)
                            reset(view)
                          end
                        
                          def onMouseMove(flags, x, y, view)
                            @ip.pick(view, x, y)
                            ip_path = @ip.instance_path
                            if ip_path.empty?
                              reset(view)
                              return
                            end
                            inst = nil
                            gptra = nil
                            ip_path.each { |ent|
                              break if !ent.is_a?(Sketchup;;Group) && !ent.is_a?(Sketchup;;ComponentInstance)
                              if gptra
                                gptra = gptra * inst.transformation
                              elsif inst
                                gptra = inst.transformation
                              end
                              inst = ent
                            }
                            unless inst
                              reset(view)
                              return
                            end
                            return if inst == @hovered_inst
                            @hovered_inst = inst
                            @global_parent_tra = gptra
                            local_bb = @hovered_inst.bounds
                            # Obtain corners of local axes aligned bounding box in global space
                            lagc = []
                            for i in 0..7
                              lagc << local_bb.corner(i)
                            end
                            if @global_parent_tra
                              lagc.each { |point| point.transform!(@global_parent_tra) }
                            end
                            @labb_global_faces = [
                              [lagc[0], lagc[2], lagc[3], lagc[1]],
                              [lagc[4], lagc[6], lagc[7], lagc[5]],
                              [lagc[1], lagc[0], lagc[4], lagc[5]],
                              [lagc[2], lagc[3], lagc[7], lagc[6]],
                              [lagc[0], lagc[2], lagc[6], lagc[4]],
                              [lagc[3], lagc[1], lagc[5], lagc[7]]
                            ]
                            @labb_global_edges = [
                              lagc[0], lagc[2],
                              lagc[2], lagc[6],
                              lagc[6], lagc[4],
                              lagc[4], lagc[0],
                              lagc[3], lagc[1],
                              lagc[1], lagc[5],
                              lagc[5], lagc[7],
                              lagc[7], lagc[3],
                              lagc[0], lagc[1],
                              lagc[2], lagc[3],
                              lagc[4], lagc[5],
                              lagc[6], lagc[7]
                            ]
                            # Create global axes aligned bounding box
                            @global_bb = Geom;;BoundingBox.new()
                            @global_bb.add(lagc)
                            # Obtain corners of global axes aligned bounding box in global space
                            gagc = []
                            for i in 0..7
                              gagc << @global_bb.corner(i)
                            end
                            @gabb_global_faces = [
                              [gagc[0], gagc[2], gagc[3], gagc[1]],
                              [gagc[4], gagc[6], gagc[7], gagc[5]],
                              [gagc[1], gagc[0], gagc[4], gagc[5]],
                              [gagc[2], gagc[3], gagc[7], gagc[6]],
                              [gagc[0], gagc[2], gagc[6], gagc[4]],
                              [gagc[3], gagc[1], gagc[5], gagc[7]]
                            ]
                            @gabb_global_edges = [
                              gagc[0], gagc[2],
                              gagc[2], gagc[6],
                              gagc[6], gagc[4],
                              gagc[4], gagc[0],
                              gagc[3], gagc[1],
                              gagc[1], gagc[5],
                              gagc[5], gagc[7],
                              gagc[7], gagc[3],
                              gagc[0], gagc[1],
                              gagc[2], gagc[3],
                              gagc[4], gagc[5],
                              gagc[6], gagc[7]
                            ]
                            view.invalidate
                          end
                        
                          def draw(view)
                            return unless @hovered_inst
                            # Draw local axes aligned global bounding box
                            view.drawing_color = @labb_face_color
                            @labb_global_faces.each { |face|
                              view.draw(GL_POLYGON, face)
                            }
                            view.drawing_color = @labb_edge_color
                            view.line_width = @edge_width
                            view.line_stipple = ''
                            view.draw(GL_LINES, @labb_global_edges)
                            # Draw global axes aligned global bounding box
                            view.drawing_color = @gabb_face_color
                            @gabb_global_faces.each { |face|
                              view.draw(GL_POLYGON, face)
                            }
                            view.drawing_color = @gabb_edge_color
                            view.line_width = @edge_width
                            view.line_stipple = ''
                            view.draw(GL_LINES, @gabb_global_edges)
                          end
                        
                          def reset(view)
                            return false unless @hovered_inst
                            @hovered_inst = nil
                            @global_parent_tra = nil
                            @global_bb = nil
                            @labb_global_faces.clear
                            @gabb_global_faces.clear
                            @labb_global_edges.clear
                            @gabb_global_edges.clear
                            view.invalidate
                            return true
                          end
                        
                        end # class BoundsHighlighterTool
                        
                        Sketchup.active_model.select_tool(BoundsHighlighterTool.new)
                        

                        This time we iterate through the InputPoint's instance_path and transform the bounding box of the deepest instance across all the the parent groups/component instances in the path.

                        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