• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

[Code] intersect_with?() Group test method

Scheduled Pinned Locked Moved Developers' Forum
2 Posts 2 Posters 1.1k Views
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.
  • D Offline
    Dan Rathbun
    last edited by 25 Mar 2012, 18:27


    *Author*::*SomeLib*.intersect_with?( grp1, grp2 [, precision] )

    Test whether two groups have a boolean intersection. Returns true || false.

    ( Method is non-destructive, and does not change either reference. )

    module Author;;SomeLib
    
      class << self # PROXY CLASS
    
        #{ intersect_with?( grp1, grp2 [, precision] )
        #{
        #  Test whether two groups have a boolean intersection.
        #
        #  (Method is non-destructive, and does not change either reference.)
        #
        #  grp1  ; a Sketchup;;Group or Sketchup;;ComponentInstance reference.
        #  grp2  ; a Sketchup;;Group or Sketchup;;ComponentInstance reference.
        #  precision ; number of decimal places for precision (default = 3.)
        #
        #  Returns; true,  if "grp1" intersects with "grp2" argument.
        #           false, if they do not intersect.
        #           nil,   if either are not manifold, Sketchup is not Pro,
        #                    or if the groups are in different contexts.
        #}
        def intersect_with?( grp1, grp2, prec=3 )
          #{
          unless grp1.is_a?(Sketchup;;Group) || grp1.is_a?(Sketchup;;ComponentInstance)
            raise(TypeError,"(arg 0) Sketchup;;Group or Sketchup;;ComponentInstance expected.",caller)
          end
          unless grp2.is_a?(Sketchup;;Group) || grp2.is_a?(Sketchup;;ComponentInstance)
            raise(TypeError,"(arg 1) Sketchup;;Group or Sketchup;;ComponentInstance expected.",caller)
          end
          return nil unless Sketchup.is_pro? && Sketchup.version.to_i>7
          return nil unless (grp1.manifold? && grp2.manifold?)
          return nil unless (grp1.model == grp2.model)
          return nil unless (grp1.parent == grp2.parent)
          #}
          tgrp = nil
          retval = false
          mdl = grp1.model
          vola = volb = grp1.volume.to_l
          #
          begin
            #
            mdl.start_operation('Intersect Test')
            ###
            #
            if grp1.is_a?(Sketchup;;Group)
              tgrp = grp1.copy
            else grp1.is_a?(Sketchup;;ComponentInstance)
              tgrp = grp1.parent.entities.add_instance(grp1.definition,grp1.transformation)
            end
            # 
            vola = grp2.trim(tgrp).volume.to_l
            #
            ###
            mdl.commit_operation
            #
          rescue Exception => e
            mdl.abort_operation
            puts(e.message)
            puts(e.backtrace)
            return nil
          else
            #
            Sketchup.undo
            #
            if prec==3
              return puts( vola == volb ? false ; true )
            else
              aft = ((vola*(10**prec)).to_i)/((10**prec).to_f)
              bef = ((volb*(10**prec)).to_i)/((10**prec).to_f)
              return puts( aft == bef ? false ; true )
            end
          end
          #
        end #}
    
      end # PROXY CLASS
    
    end # module
    
    

    Tested OK with 2 cuboids that intersected at their corners by 0.001" x 0.001" x 0.001"

    💭

    I'm not here much anymore.

    1 Reply Last reply Reply Quote 0
    • B Offline
      buidoi3003
      last edited by 12 Apr 2013, 09:29

      Hi,
      May I know after determining if the 2 groups/ components/ faces are intersected. How do I get the area of the intersection?

      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