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

    Change the color of only one instance

    Scheduled Pinned Locked Moved Developers' Forum
    13 Posts 3 Posters 733 Views 3 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.
    • P Offline
      Pout
      last edited by

      i'm posting my progress.

      To change the component definition from an instance i have found.

      Leaves me with 2 questions:

      • Can i copy the 'in model' components in the library
      • Secondly: how do i set all those copied components to be red? (also the internal geometry of the component)

      thx

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

        Assuming you are not going to edit the component definition...

        Pick the component instance [let's callits definition Y].
        Use make unique on it [let's call this new version's definition X].
        Chnage the definition of X to make all of its materials the <default> (nil).
        Make the instance's colour say 'Red' - or whatever.
        At the end you simply swap the instances of X with the original definition Y and it looks the same.
        You will have a spare definition hanging around unused - could purge it or empty it's entity list and give it a 'scrap' name...

        .

        TIG

        1 Reply Last reply Reply Quote 0
        • P Offline
          Pout
          last edited by

          Thank you Tig,

          Sounds like the setup we thought about yesterday.

          This is the processflow we were thinking about:

          Change color depending on script result

          1. Get the component definition of the instance
          2. Write this component definition as an attribute in the instance attribute dictionary
          3. Make the instance unique (this results in a new component definition)and give it a name (can we use the auto-component name generator from SU itself?)
          4. Loop through all the entities in the component and set color as red

          Reset color

          1. Check to see if the current instance component definition equals the component definition that is available in the attribute dict of the instance
          2. If there is a difference: give the instance the component definition it has set in it's attribute dictionary.
          3. Delete the 'current' component definition from the component library

          Is this a good way? Thx again

          1 Reply Last reply Reply Quote 0
          • GaieusG Offline
            Gaieus
            last edited by

            And you were asking for help in the SU discussions πŸ˜„

            Gai...

            1 Reply Last reply Reply Quote 0
            • P Offline
              Pout
              last edited by

              lol Gaieus
              Well i thought i asked it in the component section πŸ˜„
              The thread we had going on there was very helpfull to see the posibilities and clear my mind of what exactly i wanted it to do.
              I now have the basics of it but still there are some major issues.

              Here is the code:

              def cinchange
              ########################
              #--Change the instance--
              ########################
              #--initialize--
                model=Sketchup.active_model
                ss=model.selection.first
              #--Get component definition name of instance--
                compdefname=ss.definition.name
              #--Write component def. to instance dictionary
                ss.set_attribute 'xdid_dict', 'compdefname', compdefname
              #--Make instance unique (creates new comp. definition)--
                ss.make_unique
              #--Edit the instance (make all entities red)--
                compdef=ss.definition
                ent=compdef.entities
                ent.each {|i|
                        if i.typename=="Face"
                          i.material="red"
                          i.back_material="red"
                          end
                        }
              end
              
              def cinrestore
              #########################
              #--Restore the instance--
              #########################
              #--initialize--
                model=Sketchup.active_model
                ss=model.selection.first
              #--Get current component definition of instance--
                current_compdefname=ss.definition.name
              #--Get the component definition defined in the attrib. dict.--
                orig_compdefname=ss.get_attribute 'xdid_dict', 'compdefname'
              #--If comp. def. set in attrib. dict. does not equal current comp. def., set the component definition from the attrib. dict. back--
                  if orig_compdefname
                    if orig_compdefname!= current_compdefname
                    definitionlist=model.definitions
                    ss.definition=definitionlist[orig_compdefname]
              #--Delete the current component definition from the library--
              #--Delete the attrib. dict. entry from the instance
                end
                end
              end
              
              

              The issues are:

              • what to do when there is only one instance of a component present in the model. make unique does not work then. (any thoughts on this?)
              • now for each instance of the same component it creates a new component definition while it could reuse the first one. (dito)
              • get the attribute information out of the instances dictionary

              I don't know if anyone could use the above script and if i need to put it in a plugin or something.

              1 Reply Last reply Reply Quote 0
              • P Offline
                Pout
                last edited by

                ok i have my script (almost working)

                def cinchange
                #--initialize--
                model=Sketchup.active_model
                ss=model.selection.first
                UI.messagebox('selection OK')
                ########################
                #--Change the instance--
                ########################
                #--Get component definition of instance--
                compdef=ss.definition.name
                UI.messagebox('definition OK')
                #--Write component def. to instance dictionary
                ss.set_attribute 'xdid_dict', 'compdef', compdef
                UI.messagebox('attribute OK')
                #--Make instance unique (creates new comp. definition)--
                ss.make_unique
                UI.messagebox('unique OK')
                #--Edit the instance (make all entities red)--
                compdef2=ss.definition
                ent=compdef2.entities
                ent.each {|i|
                        if i.typename=="Face"
                          i.material="red"
                          i.back_material="red"
                          end
                        }
                UI.messagebox('rood OK')
                UI.messagebox('total succes')
                end
                def cinrestore
                
                #########################
                #--Restore the instance--
                #########################
                #--Get current component definition of instance--
                model=Sketchup.active_model
                ss=model.selection.first
                UI.messagebox('selection OK')
                #--Get the component definition defined in the attrib. dict.--
                orig_compdef=ss.get_attribute 'xdid_dict', 'compdef'
                UI.messagebox(orig_compdef)
                #--If no comp. definition set in the attrib. dict., do nothing--
                #--If comp. def. set in attrib. dict. does not equal current comp. def., set the component definition from the attrib. dict. back--
                current_compdef=ss.definition.name
                UI.messagebox(current_compdef)
                if orig_compdef!= current_compdef
                UI.messagebox('niet gelijk')
                ss.definition=orig_compdef
                end
                

                The only problem left is how to set the definition of the instance back to the original one based upon the variable orig_compdef which is the name of the definition.
                I have saved the original 'name' of the component definition in the attrib. dictionary cause appaerently it is not possible to save the definition itself.
                Can someone help me on this? --> SOLVED

                Also, could you check if it is possible to make this script 'better'?

                Thx πŸ˜„

                1 Reply Last reply Reply Quote 0
                • GaieusG Offline
                  Gaieus
                  last edited by

                  @pout said:

                  what to do when there is only one instance of a component present in the model. make unique does not work then. (any thoughts on this?)

                  True this works that way but how to solve it with ruby, I don't know. Maybe make a temporary copy of it, make it unique then and delete the copy.

                  Gai...

                  1 Reply Last reply Reply Quote 0
                  • P Offline
                    Pout
                    last edited by

                    Does anyone know how i can give a component definition a certain name?
                    When making an instance unique it automatically adds #1 behind the current component definition name.

                    1 Reply Last reply Reply Quote 0
                    • GaieusG Offline
                      Gaieus
                      last edited by

                      You can always rename the component (and yes, there have been several feature requests for the Create Component window to pop up when making a component unique).

                      Now how you would do it in ruby, that's a different question πŸ˜„

                      Gai...

                      1 Reply Last reply Reply Quote 0
                      • P Offline
                        Pout
                        last edited by

                        Ok i have found that (jeez, i should stop posting when something pops up in my mind)
                        Almost finished.
                        I'll post my code soon.

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

                          Error 404 (Not Found)!!1

                          favicon

                          (groups.google.com)

                          .

                          TIG

                          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