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

    [Plugin] ReGlue

    Scheduled Pinned Locked Moved Plugins
    35 Posts 11 Posters 11.9k Views 11 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

      There is a method:
      glued = face.get_glued_instances
      And it'd be easy to grep then iterate all of the faces and their glued components etc...
      BUT there's no API ' .could_be_glued_instances' method...

      BUT here's some possible [pretty much untested] code which might just do it...

      
      ctr=0
      model=Sketchup.active_model
      faces=model.active_entities.grep(Sketchup;;Face)
      comps=model.active_entities.grep(Sketchup;;ComponentInstance).find_all{|e|
        e.definition.behavior.is2d? && ! e.glued_to
      }
      ### collect if comp can glue, but not glued !
      cct=comps.length
      faces.each{|f|
        plane=f.plane
        norm =f.normal
        comps.dup.each{|e|
          pt=e.transformation.origin
          nm=e.transformation.zaxis
          if pt.distance_to_plane(plane)==0 && norm==nm
            ### glue-able
            cp=f.classify_point(pt)
            if cp==Sketchup;;Face;;PointInside || cp==Sketchup;;Face;;PointOnEdge || cp==Sketchup;;Face;;PointOnVertex
              ### on face so can glue
              e.glued_to=f
              comps.delete(e)
              ctr+=1
              ### reduce tested comps collection for speed
            end
          end
        }
      }
      puts "\n\nReglued #{ctr} of #{cct}" ### print how many reglued
      
      

      TIG

      1 Reply Last reply Reply Quote 0
      • R Offline
        rv1974
        last edited by rv1974

        @TIG This plugin reglues multiple components in SU 2022. In v2023 and newer it reglues only one component.
        What is the reason for this degradation? Thanks
        Screenshot 2024-05-08 112951.png

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

          @rv1974

          This plugin is over a decade old !
          As far as I recall, and tell from the code and version, it has always needed a selection of one face and one component to work. for 19 years.
          Although I do see from my posts do speaks of one or more components being processed...

          As far as I can see this old code is very simplistic... e.g.

          It does not check for gluing properties, or face-normal, or Z-axis, or position on the face.plane, or transformation.origin point on the face at all.

          If it checked for several components being on the face and sharing the face.normal as their Z-axis etc, then it would be possible the get it to process several gluing components. However, this would require a complete rewrite...

          I'll back check...

          TIG

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

            Here's v2.0 in the PluginStore...
            https://sketchucation.com/pluginstore?pln=Re_Glue
            It now allows multiple pre selected component-instances, but still only one preselected face.
            These are glued to the face [is possible], any errors or unsuitable instances are reported in the Ruby Console...

            TIG

            R 1 Reply Last reply Reply Quote 1
            • R Offline
              rv1974 @TIG
              last edited by

              @TIG Many thanks!

              designerbursaD 1 Reply Last reply Reply Quote 0
              • designerbursaD Offline
                designerbursa @rv1974
                last edited by Rich O Brien

                @rv1974

                def self.glue_components_to_faces
                  model = Sketchup.active_model
                  selection = model.selection
                
                  return UI.messagebox("Error: No selection!") if selection.empty?
                
                  faces = selection.grep(Sketchup::Face)
                  components = selection.grep(Sketchup::ComponentInstance)
                
                  return UI.messagebox("Error: Select faces and components!") if faces.empty? || components.empty?
                  
                  model.start_operation("Glue Components to Faces", true)
                
                  begin
                    components.each do |component|
                      closest_face = find_closest_aligned_face(component, faces)
                      component.glued_to = closest_face if closest_face
                    end
                  ensure
                    model.commit_operation
                  end
                end
                
                def self.find_closest_aligned_face(component, faces)
                
                  component_normal = component.transformation.zaxis
                  closest_face = faces.min_by { |face| face.normal.angle_between(component_normal) }
                  closest_face
                
                end
                
                R 1 Reply Last reply Reply Quote 0
                • R Offline
                  rv1974 @designerbursa
                  last edited by

                  @designerbursa what do you mean?

                  designerbursaD 1 Reply Last reply Reply Quote 0
                  • designerbursaD Offline
                    designerbursa @rv1974
                    last edited by

                    @rv1974 I developed the code for pasting on all selected surfaces at once, it would be useful for everyone if it is updated in this way.

                    R 1 Reply Last reply Reply Quote 0
                    • R Offline
                      rv1974 @designerbursa
                      last edited by

                      @designerbursa
                      I might be missing something but currently I can do it without any plugin

                      designerbursaD 1 Reply Last reply Reply Quote 0
                      • designerbursaD Offline
                        designerbursa @rv1974
                        last edited by designerbursa

                        @rv1974 If you can paste without extensions, I really congratulate you...

                        R 1 Reply Last reply Reply Quote 0
                        • R Offline
                          rv1974 @designerbursa
                          last edited by

                          @designerbursa
                          ?
                          85dfe1a8-ee4c-4834-b2ed-f896caf03ad8-SketchUp_ozyex36IzG.gif

                          designerbursaD 1 Reply Last reply Reply Quote 0
                          • designerbursaD Offline
                            designerbursa @rv1974
                            last edited by

                            @rv1974 I am using Sketchup 2021 version
                            Kayıt 2024-06-10 000123.gif

                            designerbursaD 1 Reply Last reply Reply Quote 0
                            • designerbursaD Offline
                              designerbursa @designerbursa
                              last edited by designerbursa

                              @designerbursa ```

                              module GlueComponentsToFaces
                                extend self
                              
                                def glue_components_to_faces
                                  model = Sketchup.active_model
                                  selection = model.selection
                              
                                  if selection.empty?
                                    return UI.messagebox("Error: No selection! Please select faces and components.")
                                  end
                              
                                  faces = selection.grep(Sketchup::Face)
                                  components = selection.grep(Sketchup::ComponentInstance)
                              
                                  if faces.empty? || components.empty?
                                    return UI.messagebox("Error: Select faces and components!")
                                  end
                              
                                  model.start_operation("Glue Components to Faces", true)
                              
                                  begin
                                    components.each do |component|
                                      closest_face = find_closest_aligned_face(component, faces)
                                      component.glued_to = closest_face if closest_face
                                    end
                                  rescue => e
                                    UI.messagebox("An error occurred: #{e.message}")
                                  ensure
                                    model.commit_operation
                                  end
                                end
                              
                                def find_closest_aligned_face(component, faces)
                                  component_normal = component.transformation.zaxis
                                  closest_face = faces.min_by { |face| face.normal.angle_between(component_normal) }
                                  closest_face
                                end
                              end
                              
                              # Adding a menu item for ease of use in SketchUp
                              if not file_loaded?(__FILE__)
                                UI.menu("Plugins").add_item("Glue Components to Faces") {
                                  GlueComponentsToFaces.glue_components_to_faces
                                }
                                file_loaded(__FILE__)
                              end
                              
                              
                              ### I haven't tested it on the 2024 version but you can try this, maybe it will work. It works smoothly on my 2021 version
                              R 1 Reply Last reply Reply Quote 0
                              • R Offline
                                rv1974 @designerbursa
                                last edited by

                                @designerbursa I took a closer look.. your version is indeed more advanced and user friendly, many thanks!
                                P.S. you can cut and paste in place multiple and un-glued components in any SU version to get the same positive result, but your script is less laborious indeed.

                                1 Reply Last reply Reply Quote 0
                                • designerbursaD Offline
                                  designerbursa
                                  last edited by designerbursa

                                  module Reglue
                                      extend self
                                  
                                      # Menü öğesinin eklenme konumunu belirleyen değişken
                                      SUPPORT_MENU_POSITION ||= Sketchup::Menu.instance_method(:add_item).arity != 1 ? 8 : nil
                                  
                                      def glue_components_to_faces
                                        model = Sketchup.active_model
                                        selection = model.selection
                                  
                                        if selection.empty?
                                          UI.messagebox("Hata: Hiçbir seçim yapılmadı!")
                                          return
                                        end
                                  
                                        faces = selection.grep(Sketchup::Face)
                                        components = selection.grep(Sketchup::ComponentInstance).select { |comp| comp.glued_to.nil? }
                                  
                                        if faces.empty? || components.empty?
                                          UI.messagebox("Hata: Lütfen hem yüzeyleri hem de yapıştırılabilir bileşenleri seçin!")
                                          return
                                        end
                                  
                                        model.start_operation("Bileşenleri Yüzeylere Yeniden Yapıştır", true)
                                  
                                        components.each do |component|
                                          closest_face = find_closest_aligned_face(component, faces)
                                          component.glued_to = closest_face if closest_face
                                        end
                                  
                                        model.commit_operation
                                      end
                                  
                                      def find_closest_aligned_face(component, faces)
                                        component_normal = component.transformation.zaxis
                                        faces.min_by { |face| face.normal.angle_between(component_normal) }
                                      end
                                  
                                      unless @loaded
                                        UI.add_context_menu_handler do |menu|
                                          selection = Sketchup.active_model.selection
                                          next if selection.empty?
                                  
                                          has_faces = selection.any? { |entity| entity.is_a?(Sketchup::Face) }
                                          has_glueable_components = selection.any? { |entity| entity.is_a?(Sketchup::ComponentInstance) && entity.glued_to.nil? }
                                  
                                          if has_faces && has_glueable_components
                                            position = SUPPORT_MENU_POSITION
                                            menu.add_item("Reglue", position) { glue_components_to_faces }
                                          end
                                        end
                                  
                                        @loaded = true
                                      end
                                  end
                                  
                                  
                                  1 Reply Last reply Reply Quote 0
                                  • 1
                                  • 2
                                  • 2 / 2
                                  • First post
                                    Last post
                                  Buy SketchPlus
                                  Buy SUbD
                                  Buy WrapR
                                  Buy eBook
                                  Buy Modelur
                                  Buy Vertex Tools
                                  Buy SketchCuisine
                                  Buy FormFonts

                                  Advertisement