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

    Deleting Component Instance

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 4 Posters 720 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.
    • bomastudioB Offline
      bomastudio
      last edited by

      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
      • TIGT Offline
        TIG Moderator
        last edited by

        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
        • sdmitchS Offline
          sdmitch
          last edited by

          @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
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            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
            • bomastudioB Offline
              bomastudio
              last edited by

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

              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