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

    Redraw a dynamic component

    Scheduled Pinned Locked Moved Developers' Forum
    19 Posts 2 Posters 4.4k Views 2 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.
    • TNTDAVIDT Offline
      TNTDAVID
      last edited by

      **I found with some tests that it is not possible to add dynamic attributes to neutral components.

      The component must have at least an existing attribute for it to be possible to add attributes in ruby.

      If not, we have this error message:

      @unknownuser said:

      Error: #<NoMethodError: undefined method []' for nil:NilClass> <main>:1:in block in <main>'
      <main>:in each' <main>:in grep'
      <main>:in <main>' -e:1:in eval'
      nil

      The goal is certainly to protect the creation of dynamic components, which is only available in Pro version of SketchUp?**

      [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

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

        Without us seeing your code it's impossible to say what raises your error message.
        Perhaps adding an extra test before testing for the DC attribute dictionary [] entry
        next unless i.definition.attribute_dictionaries
        as the first test will stop it failing, since the DC dictionary can't be tested for without the existence of the attribute dictionaries entry.

        TIG

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

          You can create a new dictionary thus

          some_definition.set_attribute("dynamic_attributes", "test", "1")
          p some_definition.attribute_dictionaries["dynamic_attributes"]
          

          TIG

          1 Reply Last reply Reply Quote 0
          • TNTDAVIDT Offline
            TNTDAVID
            last edited by

            **Sorry TIG for not having specified the method used.

            I tried these 2 methods:

            Sketchup.active_model.selection.grep(Sketchup;;ComponentInstance){|i|
            next unless i.definition.attribute_dictionaries["dynamic_attributes"]
            i.set_attribute 'dynamic_attributes','teste', '1'
            $dc_observers.get_latest_class.redraw_with_undo(i)
            }
            

            Sketchup.active_model.selection.grep(Sketchup;;ComponentInstance){|i|
            next unless i.definition.attribute_dictionaries["dynamic_attributes"]
            i.set_attribute("dynamic_attributes", "test", "1")
            i.attribute_dictionaries["dynamic_attributes"]
            $dc_observers.get_latest_class.redraw_with_undo(i)
            }
            

            None of the 2 solutions work on a component without dynamic attributes.

            @unknownuser said:

            Error: #<NoMethodError: undefined method []' for nil:NilClass> <main>:1:in block in <main>'
            <main>:in each' <main>:in grep'
            <main>:in <main>' -e:1:in eval'
            nil

            But these 2 solutions work on components already have dynamic attributes.**

            [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

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

              The attributes belong to the instance's defintion NOT the instance.
              You mess up trying to set things to the instance, NOT the definition !
              Try substituting something like...

              i.definition.set_attribute('dynamic_attributes', 'teste', '1')
              $dc_observers.get_latest_class.redraw_with_undo(i)
              

              Please learn the difference between a definition and its instance !!!!!

              TIG

              1 Reply Last reply Reply Quote 0
              • TNTDAVIDT Offline
                TNTDAVID
                last edited by

                This still does not work for components without attributes:

                Sketchup.active_model.selection.grep(Sketchup;;ComponentInstance){|i|
                next unless i.definition.attribute_dictionaries["dynamic_attributes"]
                i.definition.set_attribute('dynamic_attributes', 'teste', '1')
                $dc_observers.get_latest_class.redraw_with_undo(i)
                }
                

                [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

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

                  WTF !
                  You really must learn basic principles...
                  consider this:

                  
                  Sketchup.active_model.selection.grep(Sketchup;;ComponentInstance){|i|
                  ###next unless i.definition.attribute_dictionaries["dynamic_attributes"]
                  i.definition.set_attribute('dynamic_attributes', 'teste', '1')
                  $dc_observers.get_latest_class.redraw_with_undo(i)
                  }
                  

                  The added ### skips the check for the dictionary [ which you do NOT need ! ]... so IF a definition does NOT have that dictionary set then it makes it anyway !!!!!!

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • TNTDAVIDT Offline
                    TNTDAVID
                    last edited by

                    **OK TIG, I understand!

                    The Ruby is very wide and has many instructions such as:

                    !,&&,||,and,box,else,elsif,eql?,equal,if,then,not,gold,require,unless,wheen,true, false...

                    I had not yet read "unless".

                    I realize that it takes many years of Ruby to totally understand him master it.

                    As I learn according to my needs, this undoubtedly slows down my general understanding of ruby.

                    Promised, next time I would make more effort with your examples before publishing it does not work. 😉

                    Ps: Your solution works wonderfully TIG

                    Thanks a lot for your help.

                    David**

                    [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

                    1 Reply Last reply Reply Quote 0
                    • TNTDAVIDT Offline
                      TNTDAVID
                      last edited by

                      **Hello, ☀

                      I now want to refresh the visible dynamic components.

                      If not, the answer must be "nil".

                      The methde below works for components that are not nested:

                      m=Sketchup.active_model;
                      s=m.selection;
                      m.definitions.each{|d|s.add d.instances if d.name=~/#{"BOUTON"}/}
                      s.grep(Sketchup;;ComponentInstance){|i|
                      next if i.hidden?
                      $dc_observers.get_latest_class.redraw_with_undo(i)
                      }
                      

                      Oddly if the components with the "BOUTON" definitions, are nested in another component, they will be considered as visible even if they are hidden.

                      Do you know what is the problem?

                      Thank you**

                      [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

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

                        Please stop adding entities into the selection.
                        What happens if it already contains some selected entities ?
                        You haven't cleared it ??
                        Also these additions might not be in the current context and cause various issue down the line...

                        Simply create an empty array and add entities to that if they meet a certain test.
                        e.g.

                        def handlesCI(ents)
                          array = []
                          ents.grep(Sketchup;;ComponentInstance).each{|e| ### collect only visible instances
                            next unless e.visible?
                            array << e if e.definition.name =~ /#{"HANDLES"}/
                            handlesCI(e.definition.entities) ### repeat for nested entities in visibles
                          }
                          array.each{|i| ### process this array of instances
                            $dc_observers.get_latest_class.redraw_with_undo(i)
                          }
                        end
                        

                        TIG

                        1 Reply Last reply Reply Quote 0
                        • TNTDAVIDT Offline
                          TNTDAVID
                          last edited by

                          **Your method works TIG, but it does not keep the "Handles" definitions selected.

                          The goal is to update all Invisible Handles, then select any definition "Handles" without visibility restrictions.**

                          [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

                          1 Reply Last reply Reply Quote 0
                          • TNTDAVIDT Offline
                            TNTDAVID
                            last edited by

                            **Here I am in a paradox!

                            If I apply your TIG method, the handles selected in Method 2 will also be redrawn.

                             
                            def methodeTIG(ents)
                                sel = Sketchup.active_model.selection
                                array = []
                                ents.grep(Sketchup;;ComponentInstance).each{|e| ### collect only visible instances
                                next unless e.visible?
                                array << e if e.definition.name =~ /#{"HANDLES"}/
                                methodeTIG(e.definition.entities) ### repeat for nested entities in visibles
                                }
                                array.each{|i| ### process this array of instances
                                $dc_observers.get_latest_class.redraw_with_undo(i)
                                }
                                end  	  
                            
                            def methode2(ents)
                                sel = Sketchup.active_model.selection
                                ents.each do |e|
                                if e.is_a? Sketchup;;ComponentInstance
                                sel.add e if e.definition.name=~/#{"HANDLES"}/
                                methode2(e.definition.entities)
                                end
                                end
                                end
                            
                            def methode_end
                                methodeTIG(instances)
                                methode2(instances) ### The handles of this method are drawn by the methodTIG! 
                                end
                            
                            
                            

                            How can I avoid this?

                            Thank you**

                            [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

                            1 Reply Last reply Reply Quote 0
                            • TNTDAVIDT Offline
                              TNTDAVID
                              last edited by

                              **I solved the problem of my previous post!

                              Now, I'm facing another problem that is much more difficult to solve.

                              The code below adds an attribute to all the "Handles" definitions that are visible in the components being selected.

                              
                              def handlesCI(ents)
                                  sel = Sketchup.active_model.selection
                                  ents.each do |e|
                                  if e.is_a? Sketchup;;ComponentInstance
                                  sel.add e if e.definition.name=~/#{"HANDLES"}/
                                  handlesCI(e.definition.entities)
                                  sel.grep(Sketchup;;ComponentInstance){|i|
                                  next unless i.visible?
                                  i.set_attribute 'dynamic_attributes','attribut', '1'
                                  }
                                  end
                                  end
                                  end
                              

                              This method does not work to refresh only visible components.

                              
                              def handlesCI(ents)
                                  sel = Sketchup.active_model.selection
                                  ents.each do |e|
                                  if e.is_a? Sketchup;;ComponentInstance
                                  sel.add e if e.definition.name=~/#{"HANDLES"}/
                                  handlesCI(e.definition.entities)
                                  sel.grep(Sketchup;;ComponentInstance){|i|
                                  next unless i.visible?
                                  $dc_observers.get_latest_class.redraw_with_undo(i)
                                  }
                                  end
                                  end
                                  end
                              

                              I need your help to understand the problem.

                              Thank you

                              David**

                              [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

                              1 Reply Last reply Reply Quote 0
                              • TNTDAVIDT Offline
                                TNTDAVID
                                last edited by

                                **I ended up bypassing the problem by selecting a different definition than "handles" in method 2.

                                I asked to select the IKEA definition, which corresponds to the furniture that contains the facades and the handle inside.

                                
                                def ikeaCI(ents)
                                    sel = Sketchup.active_model.selection
                                    ents.each do |e|
                                 if e.is_a? Sketchup;;ComponentInstance
                                    sel.add e if e.definition.name=~/#{"IKEA"}/
                                    ikeaCI(e.definition.entities)
                                    end
                                    end
                                    end	
                                
                                

                                This is possible because I specified the definition of the highest components of the hierachie.

                                Is it possible to select the highest components of the hierarchy without specifying the name of that definition?

                                Thank you TIG for your method that allows me to redraw the visible handles. 👍**

                                [b:8wt9py2i]* Nouveau !!![/b:8wt9py2i] Découvrez notre nouveau Plugin [url=http://www.composant-dynamique.com/:8wt9py2i][color=#40FF00:8wt9py2i]C[/color:8wt9py2i][color=#77FF00:8wt9py2i]l[/color:8wt9py2i][color=#ADFF00:8wt9py2i]i[/color:8wt9py2i][color=#E4FF00:8wt9py2i]c[/color:8wt9py2i][color=#FFDB09:8wt9py2i]k[/color:8wt9py2i][color=#FF921B:8wt9py2i]-[/color:8wt9py2i][color=#FF492E:8wt9py2i]C[/color:8wt9py2i][color=#FF0040:8wt9py2i]u[/color:8wt9py2i][color=#ED2577:8wt9py2i]i[/color:8wt9py2i][color=#DA49AD:8wt9py2i]s[/color:8wt9py2i][color=#C86EE4:8wt9py2i]i[/color:8wt9py2i][color=#AD77FF:8wt9py2i]n[/color:8wt9py2i][color=#8965FF:8wt9py2i]e[/color:8wt9py2i] [color=#4040FF:8wt9py2i]2[/color:8wt9py2i][/url:8wt9py2i], pour créer vos cuisines 3D !

                                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