• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Control component with csv file

Scheduled Pinned Locked Moved Newbie Forum
sketchup
10 Posts 5 Posters 1.0k Views
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 17 Dec 2011, 17:06

    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
    • T Offline
      TIG Moderator
      last edited by 17 Dec 2011, 18:05

      @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 17 Dec 2011, 18:55

        Thanks I will give it a go.

        1 Reply Last reply Reply Quote 0
        • R Offline
          raineysky
          last edited by 19 Dec 2011, 01:04

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

          1 Reply Last reply Reply Quote 0
          • S Offline
            sdmitch
            last edited by 19 Dec 2011, 20:50

            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 19 Dec 2011, 21:14

              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
              • S Offline
                sdmitch
                last edited by 19 Dec 2011, 21:49

                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
                • T Offline
                  TIG Moderator
                  last edited by 19 Dec 2011, 21:56

                  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 19 Dec 2011, 22:05

                    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
                    • S Offline
                      sdmitch
                      last edited by 19 Dec 2011, 22:22

                      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
                      1 / 1
                      • First post
                        1/10
                        Last post
                      Buy SketchPlus
                      Buy SUbD
                      Buy WrapR
                      Buy eBook
                      Buy Modelur
                      Buy Vertex Tools
                      Buy SketchCuisine
                      Buy FormFonts

                      Advertisement