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

    Change face color of component

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 4 Posters 2.8k Views 4 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.
    • M Offline
      mjones
      last edited by

      I have a script that creates cylinders turns them into components and displays them, I then export an picture of the view. I would like to then change the color of the components faces and export it again to an image like so: File1 and File 2 attached

      Any help is appreciated, thanks,
      Mike

      The script is as follows:

      def createComp(compColor, yaxis)
        normal_vector = Geom;;Vector3d.new(0,0,1)
        radius = 0.5
        center_point = Geom;;Point3d.new(yaxis,1,0)
      
        mygroup = Sketchup.active_model.entities.add_group 
      
        myedgearray = mygroup.entities.add_circle center_point, normal_vector, radius
        myfirst_edge = myedgearray[0]
        
        myface = mygroup.entities.add_face(myedgearray) 
        myface.material = compColor 
        myface.pushpull(15)
        
        return mygroup.to_component
      
      end
      
      # Get "handles" to our model and the Entities collection it contains. 
      model = Sketchup.active_model 
      entities = model.entities 
      
      cylinder1 = createComp("red", 1)
      cylinder2 = createComp("white", 1)
      cylinder3 = createComp("blue", 1)
      
      for i in 1..10 do
        transformation = Geom;;Transformation.new([0,i,0])
        entities.add_instance(cylinder1.definition, transformation)
        transformation2 = Geom;;Transformation.new([1,i,0])
        entities.add_instance(cylinder2.definition, transformation2)
        transformation3 = Geom;;Transformation.new([2,i,0])
        entities.add_instance(cylinder3.definition, transformation3)
      end
      
      view = model.active_view 
      status = view.write_image "redwhiteblue.jpg"
      
      

      File1


      File2

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        What would be better here it to apply the materials to the component instances. That way you don't have to colour every face, and you don't have to make unique components for each colour variance.

        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          cylinder1, cylinder2, and cylinder3 would be references to your 3 component object instances.

          Sketchup::ComponentInstance is a subclass of Sketchup::DrawingElement (just as Sketchup::Face is also a subclass of Sketchup::DrawingElement.)

          Sketchup::DrawingElement class has a material= method so Sketchup::ComponentInstance (as it's subclass,) will inherit a material= method.

          You could try assigning the color to the ComponentInstance objects as a whole, instead of the nested Face(s).

          Make sure ColorByLayer is OFF, so you see the material colors displayed and not the Layer colors displayed for Faces.

          I'm not here much anymore.

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

            Make one cylinder component (referenced as definition defn).
            It's faces' material=nil by default.
            Place the required instances in three goes...
            ` cyls1=[]
            0.upto(10) do |i| ### 10 OR how many you want ?

            set incrementing transformation here

            inst=entities.add_instance(defn, transformation)
            cyls1<< inst
            end#doRepeat for cyls2 and cyls3... Next: Make groups for each set... g1=entities.add_group(cyls1)Repeat for g2/g3... Now give the groups some colors - the color will trickle down and color any face within the group/instances etc that has material=nil g1.material="red"
            g2.material="white"
            g3.material="blue"Move the groups around by 'transforming' them... Change the three groups' colors as desired... *Alternatively* you can color the instances within a group rather than the group itself - you have made a list of these as cyls1etc which you can step through, do partial blocks, or even give individual colors as desired... cyls1.each{|e|e.material="pink"}OR cyls1[0..5].each..........or cyls1[0].material="black"`
            .........................

            TIG

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              refering to TIG's example:

              Sketchup::Group is also subclass of Sketchup::DrawingElement (so Sketchup::Group instances will inherit a material= method from it's superclass Sketchup::DrawingElement.)

              I'm not here much anymore.

              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