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

    Control component with csv file

    Scheduled Pinned Locked Moved Newbie Forum
    sketchup
    10 Posts 5 Posters 1.0k Views 5 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.
    • R Offline
      rpetter
      last edited by

      I have a lot of components in a drawing and I would like to be able to change their colors based off an excel spread sheet. I would like to build the components in sketchup export them to a csv file, then in the csv file add a column for color, filter the components and add color, then import it back into sketchup. Just wondering if this is possible with either a regular component or a dynamic component or some kind of ruby script?

      Thanks

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

        @rpetter said:

        I have a lot of components in a drawing and I would like to be able to change their colors based off an excel spread sheet. I would like to build the components in sketchup export them to a csv file, then in the csv file add a column for color, filter the components and add color, then import it back into sketchup. Just wondering if this is possible with either a regular component or a dynamic component or some kind of ruby script?
        Thanks
        You can only do it in the same session if you use the instance's id in the csv because that changes across sessions.
        You could add a custom attribute to each one before exporting the csv - that way you'd access the instances by that later, even in another session.
        Filter a selection or entities context etc so you have an array of the required instances...
        Then iterate through that array and use e.g.
        tid=Time.now.to_f+rand
        to give you a unique reference to the instances a tid as they are exported [unless they already have one]
        instance.set_attribute("RPetter","tid",tid)unless instance.get_attribute("RPetter","tid",nil)
        Now export the instance's with their tid.to_s as the first column [A] and instance.material.color.red to the second column [B] and .green and .blue to columns C abd D respectively to compile the csv [there are many examples of doing that available].
        Then manipulate the csv adding color red/green/blue to columns B/C/D.
        Now read in the csv into the SKP as an array of lines using csv.IO.readlines.
        Then split(",") to get the row's column entries as strings.
        Finally find the matching instance instance.get_attribute("RPetter","tid",nil)==rowA.to_f
        and the matching instance's material.color becomes a new material with the RGB values .color[rowB.to_i,rowC.to_i,rowC.to_i]
        If you want to specify the material's name add a csv column for that... 🤓

        TIG

        1 Reply Last reply Reply Quote 0
        • R Offline
          rpetter
          last edited by

          Thanks I will give it a go.

          1 Reply Last reply Reply Quote 0
          • R Offline
            raineysky
            last edited by

            rpetter, please let us know how this turns out for you. Sounds very interesting. Thanks

            1 Reply Last reply Reply Quote 0
            • sdmitchS Offline
              sdmitch
              last edited by

              What am I missing here. I have been investigating this problem and thought I was making some progress until I noticed something rather peculiar. In my model I have 4 components and 5 instances. Two of #1 and 1 each of #2,#3 amd #4. I select anyone of them and change its' material color and the color of all change.

              mod = Sketchup.active_model
              ent = mod.entities
              sel = mod.selection
              
              c = sel.first
              c.material.color=[0,0,255]
              
              

              Nothing is worthless, it can always be used as a bad example.

              http://sdmitch.blogspot.com/

              1 Reply Last reply Reply Quote 0
              • J Offline
                Jim
                last edited by

                The material is applied to all instances. You have changed the color of the material, which is still applied to all instances. Drawingelements have Materials, but not color directly; Materials have color.

                Hi

                1 Reply Last reply Reply Quote 0
                • sdmitchS Offline
                  sdmitch
                  last edited by

                  I would certainly understand if I was editing a component with multiple instances and I changed something then it would change in all instances but in this case I'm not editing anything merely selecting a unique component and changing the color applied to the component. I'm not changing the definition of a color.

                  Nothing is worthless, it can always be used as a bad example.

                  http://sdmitch.blogspot.com/

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

                    You are changing the color of the instance's material...
                    So whatever that is its color changes...
                    You need to change the material of the instance [which perhaps will have a different color]...

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • J Offline
                      Jim
                      last edited by

                      What you did was the same as editing a Material in the Material Browser. You haven't added a new material, you haven't changed which components the material is applied to - you just changed the color of the Material.

                      Hi

                      1 Reply Last reply Reply Quote 0
                      • sdmitchS Offline
                        sdmitch
                        last edited by

                        Yes I see now. It should be instance.material=[r,g,b] not instance.material.color=[r,g,b] which I thought is what TIG meant in his original suggestions.

                        After making that change everything seems to be working. I have attached a copy if anyone else is interested in trying it out.


                        Exports/Imports component color info to/from .csv file

                        Nothing is worthless, it can always be used as a bad example.

                        http://sdmitch.blogspot.com/

                        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