making a hole in a group with multiple subgroups

making a hole in a group with multiple subgroups

Postby biervat » Fri Mar 03, 2017 5:06 pm

I'm writing a script to make a hole in a group with multiple subgroups.
I iterate through the group and subtract a box if they intersect.
The boxes are created correctly but it gives the following error:
#<Deleted Entity:0x65d174a8>
Error: #<TypeError: reference to deleted Group>


Code: Select all
def addhole(beginx,beginz,breedte,hoogte)
    #beginpunt x = afstand tot begincoordinaat muur; y = hoogte tov nulpas
    dikte = @wandgroep.local_bounds.depth
    openingbound = Geom::BoundingBox.new
    openingbound.add([beginx,0,beginz],[beginx + breedte,dikte,beginz + hoogte])
    points = [
        [beginx,0,beginz],
        [beginx,0,beginz + hoogte],
        [beginx + breedte,0,beginz + hoogte],
        [beginx + breedte,0,beginz]
    ]
    @wandgroep.entities.to_a.each do |element|
      if not openingbound.intersect(element.bounds).valid?
        if element.typename == 'Group'
          if element.manifold?
            opening = @wandgroep.entities.add_group
            opening.entities.add_face(points).pushpull(dikte)
            result = opening.subtract(element)
          else
            element.entities.to_a.each do |subelement|
              opening = subelement.entities.add_group
              opening.entities.add_face(points).pushpull(dikte)
              result = opening.subtract(subelement)
            end
          end
        end
      end
    end
  end
0

biervat 
 

Re: making a hole in a group with multiple subgroups

Postby TIG » Fri Mar 03, 2017 7:48 pm

The error-message usually includes a line number.
That helps you find where things are going wrong...

It seems to relate to a missing Group ??

A few ideas...
@wandgroep
what is it ?
Is @wandgroep.valid? when the code runs ?
If not return ??

@wandgroep.entities.to_a.each do |element|...
perhaps add an extra line at the start of that block...
next unless element.valid?
so only valid elements get processed...

PS: I swapped your [ruby] tags for [code] to make it indent and be easier to read...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: making a hole in a group with multiple subgroups

Postby Dan Rathbun » Sat Mar 04, 2017 12:05 am

For faster speed, change:
if element.typename == 'Group'
with:
if element.is_a?(Sketchup::Group)

Reason: Ruby string comparison is slow, but object identity is fast.
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: making a hole in a group with multiple subgroups

    Postby biervat » Tue Mar 07, 2017 3:11 pm

    Yes @wandgroep is a group containing several groups which contain several manifold groups.

    the error occurs in the line "opening = subelement.entities.add_group" after the first subtraction is done. When I delete the subtract line, I don't get the error and every cube is drawn correctly in the correct group.
    It looks like all subelement are deleted after I subtract the first subelement.
    0

    biervat 
     

    Re: making a hole in a group with multiple subgroups

    Postby biervat » Tue Mar 07, 2017 4:07 pm

    I've found the problem. It was in line "opening = subelement.entities.add_group"
    I created a group inside the group I wanted to subtract.
    0

    biervat 
     

    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 5 guests

    Visit our sponsors: