Remove and purge hidden components.

Remove and purge hidden components.

Postby TNTDAVID » Mon Aug 21, 2017 5:50 pm

Hello, :enlight:

I'm currently finalizing Click-Cuisine 2.

In order to offer a complete tool, it is essential for me to offer Click-Cuisine 2 specific functions.

Thanks to the many subjects opened in this formum and your help, Click-Cuisine allows to:


  • Remove the included dynamic thumbnails of each furniture through their definitions and purge their materials.

  • Purge materials not used by Click-Cuisine furniture 2.

To finish the functions of cleaning, I have to write 2 methods:

  • Purge the hidden dynamic components in all components with the "IKEA" definition.

  • Remove all dynamic attributes of components with the "IKEA" definition.

I want to target the components with the IKEA definition, so as not to involuntarily affect other dynamic components.

Any help from you will be greatly appreciated.

Cordially

David Barros
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Remove and purge hidden components.

Postby TNTDAVID » Wed Oct 18, 2017 10:43 am

Hello, :enlight:

After some research, I found this sample code:

Code: Select all
Sketchup.active_model.definitions.each {| d | d.entities.to_a.each {| e | e.erase! if e.hidden?}}


The problem with this example is that hidden lines are deleted.

I only want to remove he hidden components.

thank you in advance for your help

David
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Remove and purge hidden components.

Postby slbaumgartner » Fri Oct 20, 2017 2:18 pm

Perhaps add && e.is_a?(SketchUp::ComponentInstance) to the if test? Also note that it would be good to check whether e.respond_to?(:hidden?) before doing e.hidden? since not all Entities support hidden, and if your code ever encounters one of these it will throw an exception.
0

slbaumgartner 
PluginStore Author
PluginStore Author
 

Re: Remove and purge hidden components.

Postby TIG » Fri Oct 20, 2017 4:42 pm

Consider this with 'grep'...
Code: Select all
Sketchup.active_model.definitions.each{|d|d.entities.grep(Sketchup::ComponentInstance).each{|e|e.erase! if e.hidden?}}
This will include only nested instances found inside definition's and then erase them, if they are hidden.
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Remove and purge hidden components.

Postby TNTDAVID » Fri Oct 20, 2017 7:35 pm

Thank you TIG and slbaumgartner :enlight:

The TIG example works perfectly. :thumb:
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Remove and purge hidden components.

Postby TNTDAVID » Sat Oct 21, 2017 6:10 pm

Is it possible to purge hidden components inside components with the IKEA definition only?

This will avoid affecting all the file to target only the 3D models proposed by Click-Kitchen 2.

For example:

Method A selects all components with the IKEA definition.

Code: Select all
# Method A
m=Sketchup.active_model;s=m.selection;m.definitions.each{|d|s.add d.instances if d.name=~/#{"IKEA"}/}


Method B searches for hidden components inside the selection and deletes them.

To write Method B, I need your help.

Thank you

David
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Remove and purge hidden components.

Postby Dan Rathbun » Sat Oct 21, 2017 6:20 pm

(1) Why are you putting quotes around IKEA ? (The regular expressions are themselves specially treated double quoted strings.)
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: Remove and purge hidden components.

    Postby Dan Rathbun » Sat Oct 21, 2017 6:27 pm

    (2) You are wasting time pushing objects into the selection set.
    This is not necessary in order to modify them.

    (3) Instances do not "own" any nested components. Definitions own nested objects.
    So you should be collecting a set of IKEA definitions, and then searching the definitions entities collection(s) (in that set,) for hidden objects.
    When a definition has a child object that is hidden, it will be hidden in ALL of the definition's instances.
    When you delete a hidden object from a definition, ALL of it's instances will have that child object deleted.
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: Remove and purge hidden components.

      Postby TNTDAVID » Sat Oct 21, 2017 7:12 pm

      I'm not sure I understand all of your logical reasoning Dan. :?

      I want to delete hidden definitions and not instances.

      For example:

      Dynamic Cabinet consists of a Drawer and a Door.

      The Drawer is hidden by its "Hidden" attribute.

      The Gate is visible by its "Hidden" attribute.

      Both have instances "Handles" in "sub-component".

      The method must delete the "Drawer" definition because it is hidden.

      Thus, only the "Handles" instance of the drawer will be deleted without affecting the same instances of the door.

      Cordially

      David
      0
      * Nouveau !!!
      Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
      User avatar
      TNTDAVID 
      PluginStore Author
      PluginStore Author
       

      Re: Remove and purge hidden components.

      Postby Dan Rathbun » Sat Oct 21, 2017 7:56 pm

      TNTDAVID wrote:I want to delete hidden definitions and not instances.

      Definitions are not hidden in the model, because they do not exist in the model entities collection (only instances do.)

      When a definition is "hidden", it does not appear in the Components Browser list, but still is a member of the model's DefinitionList collection.
      0
        I'm not here much anymore. But a PM will fire email notifications.
        User avatar
        Dan Rathbun 
        PluginStore Author
        PluginStore Author
         

        Re: Remove and purge hidden components.

        Postby TIG » Sat Oct 21, 2017 8:57 pm

        As Dan says don't mess on with the selection-set...
        IF you actually need to collect things for later use, consider using an array [] or a hash {}...
        But that is NOT needed here...
        How about this ?
        Code: Select all
        Sketchup.active_model.definitions.each{|d|next unless d.name=~/IKEA/; d.entities.grep(Sketchup::ComponentInstance).each{|e|e.erase! if e.hidden?}}
        This deletes all nested entities within any definition named /IKEA/, if they are hidden...
        0
        TIG
        User avatar
        TIG 
        Global Moderator
         

        Re: Remove and purge hidden components.

        Postby TNTDAVID » Sat Oct 21, 2017 10:37 pm

        Sorry Dan, I mixed brushes between definitions and instances ...

        It's wonderful TIG, your code meets 100% to my needs. :thumb:

        I want to be very careful with the selections or deletes, for this reason your example is perfect.

        -

        The code below removes all attributes of all dynamic components present in a SketchUp file.

        Code: Select all
        m=Sketchup.active_model;m.start_operation("unDC");m.definitions.each{|d|d.attribute_dictionaries.delete("dynamic_attributes") if d.attribute_dictionaries and d.attribute_dictionaries["dynamic_attributes"];d.instances.each{|i|i.attribute_dictionaries.delete("dynamic_attributes") if i.attribute_dictionaries and i.attribute_dictionaries["dynamic_attributes"]}};m.commit_operation


        How to adapt this code so that it only deletes the dynamic attributes of all nested entities in the named definitions / IKEA / ?

        Thank you
        0
        * Nouveau !!!
        Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
        User avatar
        TNTDAVID 
        PluginStore Author
        PluginStore Author
         

        Re: Remove and purge hidden components.

        Postby TIG » Sun Oct 22, 2017 3:47 pm

        Adjust ...;m.definitions.each{|d|d.attribute_dictionaries...
        to something like this:
        ...;m.definitions.each{|d|next unless d.name=~/IKEA/; d.attribute_dictionaries...
        To limit the action to /IKEA/ named definitions...
        1
        TIG
        User avatar
        TIG 
        Global Moderator
         

        Re: Remove and purge hidden components.

        Postby TNTDAVID » Sun Oct 22, 2017 4:24 pm


        Wonderful! Your example works TIG, for instances with IKEA definitions:


        Code: Select all
        m=Sketchup.active_model;m.start_operation("unDC");m.definitions.each{|d|next unless d.name=~/IKEA/; d.attribute_dictionaries.delete("dynamic_attributes") if d.attribute_dictionaries and d.attribute_dictionaries["dynamic_attributes"];d.instances.each{|i|i.attribute_dictionaries.delete("dynamic_attributes") if i.attribute_dictionaries and i.attribute_dictionaries["dynamic_attributes"]}};m.commit_operation


        Is it possible to purge dynamic attributes of nested components in instances with IKEA definitions?

        Thank you
        0
        * Nouveau !!!
        Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
        User avatar
        TNTDAVID 
        PluginStore Author
        PluginStore Author
         

        Re: Remove and purge hidden components.

        Postby TNTDAVID » Mon Oct 30, 2017 6:58 pm

        Bonjour,

        Votre solution TIG pour supprimer les composants masqués à l'intérieur des définition IKEA, fonctionne très bien! ;)

        Code: Select all
        Sketchup.active_model.definitions.each{|d|next unless d.name=~/IKEA/; d.entities.grep(Sketchup::ComponentInstance).each{|e|e.erase! if e.hidden?}}


        A l'intérieur de la définition IKEA, il y a beaucoup d'autres définitions qui ont des sous composants masqués.

        Est-il possible de supprimer tous les composants masqués pour toutes les définitions présentes dans les meubles IKEA ?

        L'idéal serait de na pas avoir à citer toutes les définitions existant dans les meubles IKEA.

        Merci
        0
        * Nouveau !!!
        Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
        User avatar
        TNTDAVID 
        PluginStore Author
        PluginStore Author
         

        Re: Remove and purge hidden components.

        Postby TNTDAVID » Wed Nov 01, 2017 1:52 pm

        Little reminder:

        It is possible to remove all hidden components of a SketchUp file, with this method:

        Code: Select all
        Sketchup.active_model.definitions.each{|d|d.entities.grep(Sketchup::ComponentInstance).each{|e|e.erase! if e.hidden?}}


        It is possible to remove all hidden components with the name of a definition, with this method:

        Code: Select all
        Sketchup.active_model.definitions.each{|d|next unless d.name=~/IKEA/; d.entities.grep(Sketchup::ComponentInstance).each{|e|e.erase! if e.hidden?}}


        How to remove all hidden components and sub-components in a selection?

        Thank you

        David
        0
        * Nouveau !!!
        Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
        User avatar
        TNTDAVID 
        PluginStore Author
        PluginStore Author
         

        Re: Remove and purge hidden components.

        Postby TNTDAVID » Wed Nov 01, 2017 5:13 pm

        I finally found how to remove hidden sub-components from a selection:

        Code: Select all
        mod = Sketchup.active_model
        sel = mod.selection
        sel.grep(Sketchup::ComponentInstance).each{|s|;
        s.definition.entities.grep(Sketchup::ComponentInstance).each{|e|e.erase! if e.hidden?}}


        The problem is that only the first level of nesting is removed by the method.

        I will continue to search in esperente found the solution.

        See you

        David
        0
        * Nouveau !!!
        Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
        User avatar
        TNTDAVID 
        PluginStore Author
        PluginStore Author
         

        Re: Remove and purge hidden components.

        Postby TNTDAVID » Wed Nov 01, 2017 8:16 pm

        I can select all the definitions by their names and apply a cleaning of hidden components:

        Code: Select all
        def methode1 ### select components and sub-components with the name of the definitions.   
            ["IKEA","DOOR","DRAWER","HANDLES"].each{|name|
            match = /#{name}/i   
            m=Sketchup.active_model;
            s=m.selection;
            m.definitions.each{|d|s.add d.instances if d.name =~ match }}
            end   
           
        def methode2 ### remove hidden components in the selection      
            Sketchup.active_model.selection.grep(Sketchup::ComponentInstance).each{|s|
            s.definition.entities.grep(Sketchup::ComponentInstance).each{|e|e.erase! if e.hidden?}}
            end
              
        def methode_end
            methode1
            methode2   
            end


        I'm progressing but this solution is not great. :?

        Any help would be highly appreciated.
        0
        * Nouveau !!!
        Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
        User avatar
        TNTDAVID 
        PluginStore Author
        PluginStore Author
         

        Re: Remove and purge hidden components.

        Postby TNTDAVID » Sun Nov 05, 2017 7:08 pm

        They always told me to be persistent and I thank all those who gave me this advice. :-)

        Here is the method to remove all hidden sub-components in a parent component:

        Code: Select all
        def supprimer_tous_les_sous_composants_selection(instance)
            children = instance.definition.entities.select { |e| e.respond_to?(:definition) }
            children.each { |c| supprimer_tous_les_sous_composants_selection(c);
            c.erase! if c.hidden?
            }
            end
           
            supprimer_tous_les_sous_composants_selection(Sketchup.active_model.selection.first)


        Here is the method to remove all the dynamic attributes of all the components present in a selection:

        Code: Select all
        def selectionner_tous_les_sous_composants_selection(instance)
            children = instance.definition.entities.select { |e| e.respond_to?(:definition) }
            instance.model.selection.add(children)
            children.each { |c| selectionner_tous_les_sous_composants_selection(c);
            }
            end   
           
        def supprimer_tous_les_attributs_dynamiques_selection
            mod = Sketchup.active_model
            sel = mod.selection
            sel.each{|s|s.definition.attribute_dictionaries.delete("dynamic_attributes");
          sel.grep(Sketchup::ComponentInstance).each{|i|i.attribute_dictionaries.delete("dynamic_attributes")}}
            end   
           
        def desactiver_toutes_les_selections
            mod = Sketchup.active_model
            ent = mod.active_entities
            sel = mod.selection
            instances = sel.grep(Sketchup::ComponentInstance)
            sel.clear
            end      
           
        selectionner_tous_les_sous_composants_selection(Sketchup.active_model.selection.first)
        supprimer_tous_les_attributs_dynamiques_selection
        desactiver_toutes_les_selections


        You can test these methods directly with the ruby console, after selecting a dynamic component.

        You will notice that all hidden components and dynamic attributes will be removed, even if they are nested at multiple levels.

        Thank you to everyone who helped me. ;)

        See you

        David
        0
        * Nouveau !!!
        Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
        User avatar
        TNTDAVID 
        PluginStore Author
        PluginStore Author
         

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

        Visit our sponsors: