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

    Detect whether all elements of the model intersect

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 3 Posters 747 Views 3 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
      CEIT81729
      last edited by

      I want to detect whether all elements of the model intersect
      Like this, but I have no solution, please give me advice, Thank you very much!

      p = mod.entities.find_all {| ent | ent.is_a (Sketchup :: ComponentInstance)?}

      bb = p.bounds.intersect (p.bounds)
      if bb.valid?
      if p.description == "one"
      p.get_attribute ("dynamic_attributes", "name")
      else
      puts "B"
      end
      else
      puts "no"
      end

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

        @ceit81729 said:

        I want to detect whether all elements of the model intersect
        Like this, but I have no solution, please give me advice, Thank you very much!

        p = mod.entities.find_all {| ent | ent.is_a (Sketchup :: ComponentInstance)?}

        bb = p.bounds.intersect (p.bounds)
        if bb.valid?
        if p.description == "one"
        p.get_attribute ("dynamic_attributes", "name")
        else
        puts "B"
        end
        else
        puts "no"
        end

        bounds.intersect results are unreliable and can return a valid boundingbox even when bounds do not overlap as demonstrated with this code. In these cases when bb is valid, the x,y or z coordinate of a bb corner will be 1.0e30 in magnitude.

        mod = Sketchup.active_model
        ent = mod.active_entities
        sel = mod.selection
        grp = sel.grep(Sketchup;;Group)
        for i in 1...grp.length
         bb=grp[0].bounds.intersect(grp[i].bounds)
         if bb.valid?
          puts "#{grp[0].name} and #{grp[i].name} intersect"
         else
          puts "#{grp[0].name} and #{grp[1].name} do not intersect"
         end
        end
          
        

        bbi1.jpgbbi2.jpg

        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
          CEIT81729
          last edited by

          Hello sdmitch:
          First of all, thank you for teaching, I changed your code to meet my needs.
          Please look at this picture, ruby console return
          "one and two intersect
          one and three do not intersect"
          so I also want to know how to return
          "two and three intersect"
          Need to write a loop(for...end)?

          Because I want to deduct the intersection surface of the overlapping area,
          so I need to detect whether all elements of the model intersect.

          Thank you for teaching again.


          test.JPG

          1 Reply Last reply Reply Quote 0
          • D Offline
            driven
            last edited by

            without needing a pre-selection, something like this should return all combinations...
            But it will return two for each pair i.e. a=b and b=a...

            mod = Sketchup.active_model
            ent = mod.entities
            grp = ent.grep(Sketchup;;ComponentInstance)
            for i in 0...grp.length - 1
             grp.each{ |g| next if g == grp[i] # skip comparing to self
                           bb = Geom;;BoundingBox.new.add(g.bounds.intersect(grp[i].bounds)) # @Sam, does this avoid that error?
                           if bb.valid?
                              puts "#{g.name} and #{grp[i].name} intersect"
                            else
                              puts "#{g.name} and #{grp[i].name} do not intersect"
                            end
                          bb.clear
                         }
            end
             
             
            

            john

            learn from the mistakes of others, you may not live long enough to make them all yourself...

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

              @ceit81729 said:

              Hello sdmitch:
              so I also want to know how to return
              "two and three intersect"
              Need to write a loop(for...end)?

              Because I want to deduct the intersection surface of the overlapping area,
              so I need to detect whether all elements of the model intersect.

              Thank you for teaching again.

              Yes, a second For loop is needed.

              mod = Sketchup.active_model
              ent = mod.active_entities
              sel = mod.selection
              grp = sel.grep(Sketchup;;Group)
              for i in 0...grp.length-1
                for j in i+1...grp.length
                  bb=grp[i].bounds.intersect(grp[j].
                  if bb.valid?
                    puts "#{grp[i].name} and #{grp[j].name} intersect"
                  else
                    puts "#{grp[i].name} and #{grp[j].name} do not intersect"
                  end
                end
              end
              
              

              In your example with the groups aligned with the X axis, the code works perfectly but returns a valid bb even if the groups are only touching and not overlapping. Perhaps this is desired. If you rotate them so that they are on the Y or Z axis, a valid bb is returned in all cases. So a different or additional test may be needed.

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

              http://sdmitch.blogspot.com/

              1 Reply Last reply Reply Quote 0
              • D Offline
                driven
                last edited by

                @Sam...

                it was working for me so I checked the docs...

                Your still on v14?

                Note: Prior to SU2015 this method would return incorrect result in some cases. For correct result in these versions you must first check if the boundingboxes actually overlap - then call this to get the resulting boundingbox.

                john

                learn from the mistakes of others, you may not live long enough to make them all yourself...

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

                  @driven said:

                  @Sam...

                  it was working for me so I checked the docs...

                  Your still on v14?

                  Note: Prior to SU2015 this method would return incorrect result in some cases. For correct result in these versions you must first check if the boundingboxes actually overlap - then call this to get the resulting boundingbox.

                  john

                  Yes I am unfortunately.

                  Some how I missed that note!

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

                  http://sdmitch.blogspot.com/

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    driven
                    last edited by

                    this seems to offer solution...
                    http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=29156#p254014

                    learn from the mistakes of others, you may not live long enough to make them all yourself...

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

                      @driven said:

                      this seems to offer solution...
                      http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=29156#p254014

                      Yes, that seems to work.

                      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
                        CEIT81729
                        last edited by

                        Hello everyone
                        I reference everyone's suggestion
                        The successful implementation of the following code:

                        mod = Sketchup.active_model
                        ent = mod.entities
                        grp = ent.grep(Sketchup::ComponentInstance)
                        for i in 0...grp.length - 1
                        grp.each{ |g| next if g == grp[i]
                        bb = Geom::BoundingBox.new.add(g.bounds.intersect(grp[i].bounds))
                        if bb.valid?
                        puts "#{g.name.to_s} and #{grp[i].name.to_s} intersect"
                        else
                        puts "#{g.name.to_s} and #{grp[i].name.to_s} do not intersect"
                        end
                        bb.clear
                        }
                        end

                        Next, I want to know how to calculate the model of dynamic properties of all objects (such as "Lenx", "Leny" ,"Lenz"...etc) after the object intersected the other object.

                        The case description like this:

                        First, detect all objects intersect situations like the above code
                        Second, if name = "column" objects intersect name = "beam" objects(just a simple example)
                        then, "column" objects dynamic properties("Lenx") deduction "beam" objects dynamic properties("Leny")

                        Thank you for your advice and help 👍


                        Collision detection


                        Objects dynamic properties

                        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