[Code] intersect_with?() Group test method

[Code] intersect_with?() Group test method

Postby Dan Rathbun » Sun Mar 25, 2012 7:27 pm



    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. )

    Code: Select all
    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"

    :idea:
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: [Code] intersect_with?() Group test method

      Postby buidoi3003 » Fri Apr 12, 2013 10:29 am

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

      buidoi3003 
       

      SketchUcation One-Liner Adverts

      by Ad Machine » 5 minutes ago



      Ad Machine 
      Robot
       



       

      Return to Developers' Forum

      Who is online

      Users browsing this forum: No registered users and 11 guests