• Login
sketchucation logo sketchucation
  • Login
⚠️ Attention | Having issues with Sketchucation Tools 5? Report Here

Deleting Component Instance

Scheduled Pinned Locked Moved Developers' Forum
5 Posts 4 Posters 746 Views 4 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.
  • B Offline
    bomastudio
    last edited by 5 Aug 2015, 16:18

    Hi guys. I'm inserting into my model two Component

    
    pants_path1 = Sketchup.find_support_file("myCOMPO1.skp" ,"Plugins")
    pants_def1 = Skecthup.active_model.definitions.load(pants_path1)
    pG1 = Geom;;Ponit3d.new(0,0,5)
    transform1 = Geom;;Transformation.new(pG1)
    compo1 = @entities.add_instance(pants_def1, transform1)
    compo1.name="Name1"
    compo1.locked = true
    
    

    and similarly for the 2nd Component

    
    pants_path2 = Sketchup.find_support_file("myCOMPO2.skp" ,"Plugins")
    pants_def2 = Skecthup.active_model.definitions.load(pants_path2)
    pG2 = Geom;;Ponit3d.new(0,0,10)
    transform2 = Geom;;Transformation.new(pG2)
    compo2 = @entities.add_instance(pants_def2, transform2)
    compo2.name="Name2"
    compo2.locked = true
    
    

    If, in a separate function, I try to delete those Components through an iteration I get a Error: #<TypeError: reference to deleted Entity>.

    def eraseCompo()
    Sketchup.active_model.active_entities.each{|e|
     if e.typename=="ComponentInstance"
      if e.name=="Name1" or e.name=="Name2"
       Sketchup.active_model.erase_entities(e)
      end
     end
    }
    
    

    What I'm missing?

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 5 Aug 2015, 17:12

      Try this...

      Sketchup.active_model.active_entities.grep(Sketchup;;ComponentInstance).select{|e|
        e.name=="Name1" || e.name=="Name2" 
      }.each{|e| e.erase! if e.valid? }
      

      TIG

      1 Reply Last reply Reply Quote 0
      • S Offline
        sdmitch
        last edited by 5 Aug 2015, 17:30

        @bomastudio said:

        Hi guys. I'm inserting into my model two Component

        
        > pants_path1 = Sketchup.find_support_file("myCOMPO1.skp" ,"Plugins")
        > pants_def1 = Skecthup.active_model.definitions.load(pants_path1)
        > pG1 = Geom;;Ponit3d.new(0,0,5)
        > transform1 = Geom;;Transformation.new(pG1)
        > compo1 = @entities.add_instance(pants_def1, transform1)
        > compo1.name="Name1"
        > compo1.locked = true
        > 
        

        and similarly for the 2nd Component

        
        > pants_path2 = Sketchup.find_support_file("myCOMPO2.skp" ,"Plugins")
        > pants_def2 = Skecthup.active_model.definitions.load(pants_path2)
        > pG2 = Geom;;Ponit3d.new(0,0,10)
        > transform2 = Geom;;Transformation.new(pG2)
        > compo2 = @entities.add_instance(pants_def2, transform2)
        > compo2.name="Name2"
        > compo2.locked = true
        > 
        

        If, in a separate function, I try to delete those Components through an iteration I get a Error: #<TypeError: reference to deleted Entity>.

        def eraseCompo()
        > Sketchup.active_model.active_entities.each{|e|
        >  if e.typename=="ComponentInstance"
        >   if e.name=="Name1" or e.name=="Name2"
        >    Sketchup.active_model.erase_entities(e)
        >   end
        >  end
        > }
        > 
        

        What I'm missing?

        The mis-spelling of Sketchup in the .load statements, and Point3d in the Geom::Transformation statements?

        Sketchup.active_model.erase_entities(e) should be e.erase!

        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
          Dan Rathbun
          last edited by 5 Aug 2015, 21:28

          Your missing the number one rule of iterating collections (in any programming language.)

          Do not delete collection members while iterating the collection. The loop will lose it's place, and strange results occur. In that case, always iterate an array copy of the collection, if your loop must remove or add collection members. This is most often seen while modifying Entities collections.

          In TIG's example, he used the select method, which produces a separate new array subset of the original collection.

          The simplest way to get an array copy, is using the to_a method.


          The second good thing TIG showed was to always check your API Entity sub-class objects for validity.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • B Offline
            bomastudio
            last edited by 10 Aug 2015, 17:53

            Guys you are and invaluable support for SU developers.... a beer to all of you!!!

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            1 / 1
            • First post
              5/5
              Last post
            Buy SketchPlus
            Buy SUbD
            Buy WrapR
            Buy eBook
            Buy Modelur
            Buy Vertex Tools
            Buy SketchCuisine
            Buy FormFonts

            Advertisement