sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Erase! gives an error: can't find parent

    Scheduled Pinned Locked Moved Developers' Forum
    13 Posts 5 Posters 1.4k Views 5 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.
    • daikuD Offline
      daiku
      last edited by

      Has anyone ever gotten an error like this:

      Error: #<TypeError: Cannot determine parent of entity>

      Here's the line of code:

      sel.each {|e| e.erase! }

      Even if sel is empty, I shouldn't get an error. As it happens, it's got the whole model in it, except for a few component instances.

      Here's a larger code snippet:

      sel.clear
      model.entities.each {|e| sel.add(e)}
      shop_dwg.each {|dwg| sel.remove(dwg)}
      sel.each {|e| e.erase! }

      I'm trying to erase everything but the shop_dwg comps. This code normally runs fine, but I've got a model that usually (but not always?) give me this error. CB.

      Clark Bremer
      http://www.northernlightstimberframing.com

      1 Reply Last reply Reply Quote 0
      • bradB Offline
        brad
        last edited by

        Hi Clark,

        I have seen similar error messages like this before.

        Try this:

        1. Start a new model
        2. Drag in a component from the component broswer
        3. Bring up the Ruby Console
        4. Type in: Sketchup.active_model.definitions[0].erase! > Enter

        Is this the same error message you are getting?

        Please let me know.

        Thank you.

        Brad
        SketchUp QA

        1 Reply Last reply Reply Quote 0
        • daikuD Offline
          daiku
          last edited by

          Brad:

          Yes, that's exactly the same error message I'm getting. But in my case, I'm not trying to erase a definition - I'm trying to erase entities. Your suggestion gave me an idea, though. I typed this in the console:

          Sketchup.active_model.entities.each {|e| e.erase!}

          This gave an odd behavior: some of my model gets erased, but not all. If I then execute the same line again, more disappears. After about 8 times, it's finally all gone. Is the list somehow getting rearranged out from under me as I erase things (thus hosing up the "each" method)?

          Clark Bremer
          http://www.northernlightstimberframing.com

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

            Clark,

            Does the error line correspond to the erase! command? Or is it pointing to a different portion of your code?

            RickW
            [www.smustard.com](http://www.smustard.com)

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

              Clark,

              Yes, as you erase edges that bound faces, the face is removed from the selection automatically (since it no longer exists), and the objects' indices are shifted accordingly.

              Given sel=model.selection followed by sel.each{|e| e.erase!}:

              The script erases sel[0], an edge. That edge bounded a face at sel[1]. This shifts the object at sel[2] to sel[0], which is then ignored because the .each loop has already finished working on sel[0] and moved on to sel[1].

              Furthermore, faces aren't the only culprits. Erasing anything could cause the next entity to be shifted to its index location, thus you can miss about 1/2 of the entities on any given pass of selection.each.

              Better to add the selection entities to an array and cycle through the array rather than rely on selection.each

              RickW
              [www.smustard.com](http://www.smustard.com)

              1 Reply Last reply Reply Quote 0
              • bradB Offline
                brad
                last edited by

                Thanks for jumping in Rick.

                That is a great suggestion about adding them to an array and cycling through the array indexes.

                Brad
                SketchUp QA

                1 Reply Last reply Reply Quote 0
                • daikuD Offline
                  daiku
                  last edited by

                  Thanks Guys.

                  Yes, I can see now why it's happening, and also why it happens on this model and not others. Most of my models are components only.

                  But if I add the entities to my own array, and then erase them all, won't I run into a similar problem? If I erase an edge before I get to the face, I'll still try to erase the face, but it will be gone from the model.

                  The reason I'm using the selection is that it's easy to add all the entities to it. There's a sel.add that doesn't care what kind of thing I'm adding. If I make my own entities array, I have to know what each thing is and call the right add, as there's no generic add. CB.

                  Clark Bremer
                  http://www.northernlightstimberframing.com

                  1 Reply Last reply Reply Quote 0
                  • tbdT Offline
                    tbd
                    last edited by

                    also check with entity.valid? before doing stuff on an entity like this:

                    all_ent = []
                    Sketchup.active_model.entities.each do |ent|
                      all_ent.push ent
                    end
                    all_ent.each {|ent| x.erase! if ent.valid?}
                    

                    SketchUp Ruby Consultant | Podium 1.x developer
                    http://plugins.ro

                    1 Reply Last reply Reply Quote 0
                    • daikuD Offline
                      daiku
                      last edited by

                      That's the stuff! Thanks, guys. All better now. CB.

                      Clark Bremer
                      http://www.northernlightstimberframing.com

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

                        Sketchup.active_model.entities.to_a.each{|e|e.erase! if e.valid?}
                        

                        is a bit shorter and does the same...
                        😄

                        TIG

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

                          Clark,

                          @unknownuser said:

                          The reason I'm using the selection is that it's easy to add all the entities to it. There's a sel.add that doesn't care what kind of thing I'm adding.

                          True, but .erase! does care what you're erasing (as you discovered), so you're still in the same boat with type checking. In fact, that's really the bottom line: check everything and assume nothing.

                          It's a hassle, but it's necessary, because inevitably someone will come along and provide unexpected input, and things will error out.

                          Regards, and happy coding!

                          RickW
                          [www.smustard.com](http://www.smustard.com)

                          1 Reply Last reply Reply Quote 0
                          • daikuD Offline
                            daiku
                            last edited by

                            TIG: to_a == Make into array? Undocumented or standard ruby?
                            Rick: I can add any kind of item to a generic array. I was stuck on using the entities container for some reason. Your cautionary point is well made.

                            Thanks, guys. CB.

                            Clark Bremer
                            http://www.northernlightstimberframing.com

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

                              Standard ruby conversion methods include (but are not limited to):
                              .to_a - convert to an array
                              .to_i - convert to an integer
                              .to_f - convert to a float
                              .to_s - convert to string

                              Though standard ruby, they may not be applicable to all data types. SU adds more .to_n methods:

                              .to_feet - convert a number of inches to feet
                              .to_yard - convert a number of inches to yards
                              .to_mile - convert a number of inches to miles
                              .to_m - convert a number of inches to meters
                              .to_cm - convert a number of inches to centimeters
                              .to_mm - convert a number of inches to millimeters
                              .to_km - convert a number of inches to kilometers

                              .feet - convert a number of feet to inches
                              .yard - convert a number of yards to inches
                              .mile - convert a number of miles to inches
                              .m - convert a number of meters to inches
                              .cm - convert a number of cm to inches
                              .mm - convert a number of mm to inches
                              .km - convert a number of km to inches

                              There are also .inch and .to_inch methods, but since SU internal units are inches, these are almost never used (but are included, I think, for consistency).

                              RickW
                              [www.smustard.com](http://www.smustard.com)

                              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