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

    Script to bring all components from library into SU model

    Scheduled Pinned Locked Moved Developers' Forum
    23 Posts 3 Posters 640 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.
    • TIGT Offline
      TIG Moderator
      last edited by

      You can't have multi-line blocks in the Ruby Console [expect perhaps on a MAC?].
      Making it as one line you need ' ;' where the newline would be... so

      newdefs.each{|defn|tr=Geom;;transformation.new(pt)model.entities.add_instance(defn, tr)pt.x=pt.x+1.m}
      

      should become

      newdefs.each{|defn|tr=Geom;;transformation.new(pt);model.entities.add_instance(defn, tr);pt.x=pt.x+1.m}
      

      NOTE this is all in one line but the code-pane 'wraps' it...
      Then it should work...

      TIG

      1 Reply Last reply Reply Quote 0
      • S Offline
        samyell77
        last edited by

        Hi TIG,

        Still no dice Im afraid!

        %(#FF0000)[skpfolderpath='C:\Users\sam.morris\Documents\TEST COMPS'
        C:\Users\sam.morris\Documents\TEST COMPS
        model=Sketchup.active_model
        #Sketchup::Model:0xca07ab0
        ents=model.active_entities
        #Sketchup::Entities:0xe967bbc
        defs=model.definitions
        #Sketchup::DefinitionList:0xe84a5a4
        skps=[]
        []
        Dir.foreach(skpfolderpath){|f|skps << File.join(skpfolderpath, f) if File.extname(f).downcase==".skp"}
        nil
        skps.each{|skp|defn=defs.load(skp)}
        ["C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal A.skp", "C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal B.skp", "C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal C.skp", "C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal D.skp"]
        pt = Geom::Point3d.new(0,0,0)
        Point3d(0, 0, 0)
        newdefs = defs.to_a[(defs.length - skps.length)..-1]
        [#Sketchup::ComponentDefinition:0xe6e0704, #Sketchup::ComponentDefinition:0xe6e0614, #Sketchup::ComponentDefinition:0xe6e05d8, #Sketchup::ComponentDefinition:0xe6d9634]
        newdefs.each{|defn|tr=Geom::transformation.new(pt);model.entities.add_instance(defn, tr)pt.x=pt.x+1.m}
        Error: #<SyntaxError: (eval):178: compile error
        (eval):178: syntax error, unexpected tIDENTIFIER, expecting '}'
        ...ities.add_instance(defn, tr)pt.x=pt.x+1.m}
        ^>
        (eval):178]

        Am I missing a step - am working on PC and am pasting each line into ruby console one at a time... but still get this unexpected tIDENTIFIER error? Im afraid I can't troubleshoot this at all with my very limited ruby knowledge. Any ideas?

        SU Pro 2016
        Dell Precision M4800
        PC Windows 10
        Intel Core i7-4900MQ @ 2.80ghz
        Nvidia Quadro K2100M
        16gb RAM

        1 Reply Last reply Reply Quote 0
        • S Offline
          samyell77
          last edited by

          [hr]just spotted that I was missing the ';' in this line just before 'pt.x' but have added it in, re-run and am now getting a different error (have highlighted the code in red this time and the result from the ruby console in blue...)

          skpfolderpath='C:\Users\sam.morris\Documents\TEST COMPS';model=Sketchup.active_model;ents=model.active_entities;defs=model.definitions;skps=[;Dir.foreach(skpfolderpath){|f|skps << File.join(skpfolderpath, f) if File.extname(f).downcase==".skp"};skps.each{|skp|defn=defs.load(skp)}]
          ["C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal A.skp", "C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal B.skp", "C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal C.skp", "C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal D.skp"]

          pt = Geom::Point3d.new(0,0,0)
          Point3d(0, 0, 0)

          newdefs = defs.to_a[(defs.length - skps.length)..-1]
          [#Sketchup::ComponentDefinition:0xeb0e344, #Sketchup::ComponentDefinition:0xeb0e254, #Sketchup::ComponentDefinition:0xeb0e218, #Sketchup::ComponentDefinition:0xeb06270]

          newdefs.each{|defn|tr=Geom::transformation.new(pt);model.entities.add_instance(defn, tr);pt.x=pt.x+1.m}
          %(#0000BF)[Error: #<NoMethodError: undefined method transformation' for Geom:Module> (eval):181 (eval):181:in each'
          (eval):181]

          SU Pro 2016
          Dell Precision M4800
          PC Windows 10
          Intel Core i7-4900MQ @ 2.80ghz
          Nvidia Quadro K2100M
          16gb RAM

          1 Reply Last reply Reply Quote 0
          • S Offline
            samyell77
            last edited by

            Also added ';' to all the lines of code to make complete blocks so I could paste a whole block in in one go rather than line by line - hope this is ok...

            SU Pro 2016
            Dell Precision M4800
            PC Windows 10
            Intel Core i7-4900MQ @ 2.80ghz
            Nvidia Quadro K2100M
            16gb RAM

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

              Another cap error on the same line:

              newdefs.each{|defn|tr=Geom;;transformation.new(pt);model.entities.add_instance(defn, tr);pt.x=pt.x+1.m}
              

              should be:

              newdefs.each{|defn|tr=Geom;;Transformation.new(pt);model.entities.add_instance(defn, tr);pt.x=pt.x+1.m}
              

              class (and module,) identifiers are actually constants so start with a capital character.

              I'm not here much anymore.

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

                Thanks Dan, I missed the [tT]ransformation typo in his code I copy/pasted..... πŸ˜’
                The error messages are pretty good at telling you what is wrong πŸ˜‰

                TIG

                1 Reply Last reply Reply Quote 0
                • S Offline
                  samyell77
                  last edited by

                  TIG, Dan,
                  Yee haaa! It works! Thank you both so much - TIG, thanks for being patient with a ruby luddite and Dan, thanks for finding the final missing piece of the puzzle. You have both been a huge help.

                  SU Pro 2016
                  Dell Precision M4800
                  PC Windows 10
                  Intel Core i7-4900MQ @ 2.80ghz
                  Nvidia Quadro K2100M
                  16gb RAM

                  1 Reply Last reply Reply Quote 0
                  • S Offline
                    samyell77
                    last edited by

                    Grrr - curses, they load in but something is going wrong Im afraid.

                    Copied and pasted code below - no error messages appearing but the components aren't all there and some of them are broken up / exploded it seems. I have 4 components that Im loading in this case (PLAT Carpet Amber FULL Diagonal A, B, C & D) but when I run the code I only get C and D as complete components and then I get two groups of objects that are broken up?

                    Ive attached a screen grab - the bottom row is the components as they appear after running the lines of code. The top row is the components loaded from the components pallet.
                    ScreenCapture.JPG

                    Ive also pasted the code from the console below...

                    skpfolderpath='C:\Users\sam.morris\Documents\TEST COMPS';model=Sketchup.active_model;ents=model.active_entities;defs=model.definitions;skps=[];Dir.foreach(skpfolderpath){|f|skps << File.join(skpfolderpath, f) if File.extname(f).downcase==".skp"};skps.each{|skp|defn=defs.load(skp)}
                    ["C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal A.skp", "C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal B.skp", "C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal C.skp", "C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal D.skp"]
                    pt = Geom::Point3d.new(0,0,0)
                    Point3d(0, 0, 0)
                    newdefs = defs.to_a[(defs.length - skps.length)..-1]
                    [#Sketchup::ComponentDefinition:0xe60a9d8, #Sketchup::ComponentDefinition:0xe60a8e8, #Sketchup::ComponentDefinition:0xe60a8ac, #Sketchup::ComponentDefinition:0xe603908]
                    newdefs.each{|defn|tr=Geom::Transformation.new(pt);model.entities.add_instance(defn, tr);pt.x=pt.x+1.m}
                    [#Sketchup::ComponentDefinition:0xe60a9d8, #Sketchup::ComponentDefinition:0xe60a8e8, #Sketchup::ComponentDefinition:0xe60a8ac, #Sketchup::ComponentDefinition:0xe603908]

                    Any further thoughts or help with this much appreciated.
                    Thanks πŸ‘

                    SU Pro 2016
                    Dell Precision M4800
                    PC Windows 10
                    Intel Core i7-4900MQ @ 2.80ghz
                    Nvidia Quadro K2100M
                    16gb RAM

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

                      Your imported components contain subcomponents or groups - these have definitions.
                      The current code simply takes the definitions list, and because definitions are added onto the end it it assumes the last 4 [say] are the 4 components you loaded, but of course there are more than that because the subcomponent/groups are all extra definitions too - so you only get the first few and not all.
                      We can recode so that we make an array of the definitions before the import and then an array after an find the difference, skipping groups... that way you only get new components added as instances... I've also added a sorting routine to add them in order...

                      model=Sketchup.active_model;
                      defs=model.definitions;
                      olddefs=defs.to_a;
                      skpfolderpath='C;\Users\sam.morris\Documents\TEST COMPS';model=Sketchup.active_model;ents=model.active_entities;
                      skps=[];
                      Dir.foreach(skpfolderpath){|f|skps << File.join(skpfolderpath, f) if File.extname(f).downcase==".skp"};
                      skps.each{|skp|defn=defs.load(skp)};
                      newdefs = defs.to_a - olddefs;
                      newdefsnames=[];
                      newdefs.each{|defn|next if defn.group? or defn.image?; newdefsnames << defn.name};
                      newdefsnames.sort!;
                      pt = Geom;;Point3d.new(0,0,0);
                      newdefsnames.each{|name|tr=Geom;;Transformation.new(pt); model.entities.add_instance(defs[name], tr); pt.x=pt.x+1.m};
                      

                      Hope this helps [untested...] πŸ€“

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • S Offline
                        samyell77
                        last edited by

                        As we say in the west country "Thats the badger mi babber!!"

                        Works a treat now TIG - brilliant!

                        Thanks again for ALL your help with this and you patience πŸ˜„

                        SU Pro 2016
                        Dell Precision M4800
                        PC Windows 10
                        Intel Core i7-4900MQ @ 2.80ghz
                        Nvidia Quadro K2100M
                        16gb RAM

                        1 Reply Last reply Reply Quote 0
                        • 1
                        • 2
                        • 2 / 2
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement