SketchUcation Plugin Store

 

 

Script to bring all components from library into SU model

Script to bring all components from library into SU model

Postby samyell77 » Mon Mar 05, 2012 10:24 am

Hi,
Wonder if anyone can help me to find or write a script that will allow me to bring all of the components from a component library into an SU file. Ideally I'd like to be able to place them next to each other in a line or on a grid (say 1000mm grid).
Is this possible to do?
Thanks
Sam
1. PC Windows Ultimate
Intel Xeon Quad 2.26
6gb RAM
2. Mac OSX 10.7.2
2x2.8 Quad - 4gb RAM
samyell77
 
Posts: 126
Joined: Fri Feb 04, 2011 11:39 am
Name: samyell77
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: interior design
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby TIG » Sat Mar 10, 2012 7:35 pm

It's doable.
Set up some initial references etc
skpfolderpath='C:/temp/my_compos'
# OR wherever the SKPs are...
model=Sketchup.active_model
ents=model.active_entities
defs=model.definitions
pt=Geom::Point3d.new(0,0,0)

You iterate through the contents of that folder and every SKP is loaded in turn thus.
skps=[]
Dir.foreach(skpfolderpath){|f|skps << File.join(skpfolderpath, f) if File.extname(f).downcase==".skp"}

then load from the list of SKPs...
skps.each{|skp|
defn=defs.load(skp)
###***
}

You have a reference [defn] to the definition each time... so at ###*** add the code to place an instance after it's loaded thus [checking that 'defn' successfully loaded before trying to pace an instance at ###!]:
next if not defn ###!
tr=Geom::Transformation.new(pt)
inst=defs.add_instance(defn, tr)

and immediately afterwards relocate the point 'pt' thus:
pt.x=pt.x+1.m
The next instance will then be put 1m to the right, because the transformation 'tr' will use the changed values for 'pt' each time etc etc...
TIG
User avatar
TIG
Global Moderator
 
Posts: 14012
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby samyell77 » Fri Mar 30, 2012 11:52 am

Hi TIG,
Thanks for looking at this - have been busy with other projects so haven't had time to take another look at this until now. Ive run through the first part of your script
skpfolderpath='C:/temp/my_compos'
# OR wherever the SKPs are...
model=Sketchup.active_model
ents=model.active_entities
defs=model.definitions
pt=Geom::Point3d.new(0,0,0)

and get this message from the console
Running the code...Done. Feedback from Ruby: (0mm, 0mm, 0mm)

Then I iterate through the contents of the folder which loads the components into the models component list.
Then I pasted the next part of the script into the ruby console
skps=[]
Dir.foreach(skpfolderpath){|f|skps << File.join(skpfolderpath, f) if File.extname(f).downcase==.".skp"}

and get this message
Running the code...Done. Feedback from Ruby: Nil result (no result returned or run failed)

Not quite sure what is supposed to happen at this point and wonder if you could clarify for me.

Also on the next part of the script where you refer to definitions Im not sure what I need to put in in place of ###***
Sorry if this is all totally obvious - ive re-read your post many times over and Im afraid I cant work out what to do next.

Thanks
1. PC Windows Ultimate
Intel Xeon Quad 2.26
6gb RAM
2. Mac OSX 10.7.2
2x2.8 Quad - 4gb RAM
samyell77
 
Posts: 126
Joined: Fri Feb 04, 2011 11:39 am
Name: samyell77
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: interior design
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby TIG » Fri Mar 30, 2012 12:00 pm

There was a typo in the code, it should be
if File.extname(f).downcase==".skp"
There was a rogue '.' :roll:
So 'skps' is empty !
Try with the adjusted version [I edited the post!]
:?
I assume you are using a MAC because a PC won't accept multiple lines in the Console...
TIG
User avatar
TIG
Global Moderator
 
Posts: 14012
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby samyell77 » Fri Mar 30, 2012 12:07 pm

Thanks TIG
Copied and pasted from the edited post and now get this message from the console
Running the code...Done. Feedback from Ruby: Run aborted (error has occurred)

:?:
Do I need to start over and re-load the components into the model?
1. PC Windows Ultimate
Intel Xeon Quad 2.26
6gb RAM
2. Mac OSX 10.7.2
2x2.8 Quad - 4gb RAM
samyell77
 
Posts: 126
Joined: Fri Feb 04, 2011 11:39 am
Name: samyell77
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: interior design
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby TIG » Fri Mar 30, 2012 12:17 pm

skpfolderpath='C:/temp/my_compos'
# OR wherever the SKPs are...
model=Sketchup.active_model
ents=model.active_entities
defs=model.definitions
pt=Geom::Point3d.new(0,0,0)
skps=[]
Dir.foreach(skpfolderpath){|f|skps << File.join(skpfolderpath, f) if File.extname(f).downcase==".skp"}
then load from the list of SKPs...
skps.each{|skp|defn=defs.load(skp);###}

This should load the defns from the folder.
Does it ?
If you want to place instances use the extra code on 'defn', inserted into the {} block at '###'
TIG
User avatar
TIG
Global Moderator
 
Posts: 14012
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby samyell77 » Fri Mar 30, 2012 12:34 pm

Thanks again TIG
skpfolderpath='C:\Users\sam.morris\Documents\TEST COMPS'
# OR wherever the SKPs are...
model=Sketchup.active_model
ents=model.active_entities
defs=model.definitions
pt=Geom::Point3d.new(0,0,0)
skps=[]
Dir.foreach(skpfolderpath){|f|skps << File.join(skpfolderpath, f) if File.extname(f).downcase==".skp"}
then load from the list of SKPs...
skps.each{|skp|defn=defs.load(skp);###}

Pasted all this in in one go and still get a Nil result - . Thought I'd try with a small, local component folder so copied some SKPs onto into a folder in my documents folder and pasted the path into your script. Have tried this with '/' and '\' separators cos I remember there was an issue with that previously on another script you looked at for me.
I had been working through the script in sections previously but this time pasted it all in - is this correct or do I still need to iterate down (manually) through the components in the components library before running
skps=[]
Dir.foreach(skpfolderpath){|f|skps << File.join(skpfolderpath, f) if File.extname(f).downcase==".skp"}
then load from the list of SKPs...
skps.each{|skp|defn=defs.load(skp);###}

Sorry to keep coming back . . . with a nil result :oops:
Hope this makes sense
1. PC Windows Ultimate
Intel Xeon Quad 2.26
6gb RAM
2. Mac OSX 10.7.2
2x2.8 Quad - 4gb RAM
samyell77
 
Posts: 126
Joined: Fri Feb 04, 2011 11:39 am
Name: samyell77
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: interior design
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby TIG » Fri Mar 30, 2012 1:00 pm

Try a simple test first
skpfolderpath='C:/Users/sam.morris/Documents/TEST COMPS'
Dir.foreach(skpfolderpath){|f|puts f}

Do you get a list of the folder's contents printed, include '.', '..' and all files ?
If so try this
skps=[];Dir.foreach(skpfolderpath){|f|skps << File.join(skpfolderpath, f) if File.extname(f).downcase==".skp"}
Do you get a list of just the SKP files when you then type skps?
If so we have got the list and just need to 'load' the SKPs...
:?
TIG
User avatar
TIG
Global Moderator
 
Posts: 14012
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby samyell77 » Fri Mar 30, 2012 3:06 pm

Hi TIG,
Getting somewhere now - Im getting the list! Just to let you know Im using the plugin Ruby Console Editor to run your scripts and am now viewing the results in the (built in) Ruby Console - I hope this isn't causing any of the issues.
I ran the first part of your test and got the following
.
..
PLAT Carpet Amber FULL Diagonal A.skp
PLAT Carpet Amber FULL Diagonal B.skp
PLAT Carpet Amber FULL Diagonal C.skp
PLAT Carpet Amber FULL Diagonal D.skp
"Nil result (no result returned or run failed)"

Then I ran the second part and got
.
..
PLAT Carpet Amber FULL Diagonal A.skp
PLAT Carpet Amber FULL Diagonal B.skp
PLAT Carpet Amber FULL Diagonal C.skp
PLAT Carpet Amber FULL Diagonal D.skp
"Nil result (no result returned or run failed)"

When I type 'skps' i get this
.
..
PLAT Carpet Amber FULL Diagonal A.skp
PLAT Carpet Amber FULL Diagonal B.skp
PLAT Carpet Amber FULL Diagonal C.skp
PLAT Carpet Amber FULL Diagonal D.skp
"C:/Users/sam.morris/Documents/TEST COMPS/PLAT Carpet Amber FULL Diagonal A.skpC:/Users/sam.morris/Documents/TEST COMPS/PLAT Carpet Amber FULL Diagonal B.skpC:/Users/sam.morris/Documents/TEST COMPS/PLAT Carpet Amber FULL Diagonal C.skpC:/Users/sam.morris/Documents/TEST COMPS/PLAT Carpet Amber FULL Diagonal D.skp"

So Im getting somewhere - the (plugin) Ruby Console Editor wasn't showing me the list so didn't look like anything was happening.
I tried copying and pasting some of your code to load the skps but Im a serious Ruby novice so keep getting syntax error messages! Im guessing what I need to do next is run this piece of code
skps.each{|skp|defn=defs.load(skp)

But couldn't work out whether in needs to be in any kind of container '{}' or '[]'?
Once I have loaded the skps, do I need to past the following into the console at ###
tr=Geom::Transformation.new(pt)
inst=defs.add_instance(defn, tr)
pt.x=pt.x+1.m

Thanks again
1. PC Windows Ultimate
Intel Xeon Quad 2.26
6gb RAM
2. Mac OSX 10.7.2
2x2.8 Quad - 4gb RAM
samyell77
 
Posts: 126
Joined: Fri Feb 04, 2011 11:39 am
Name: samyell77
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: interior design
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby TIG » Fri Mar 30, 2012 4:33 pm

skps.each{ |skp|defn=defs.load(skp) }
Loads the SKPs into the model.
The other part is only if you want to add an instance to the model's entities too...
TIG
User avatar
TIG
Global Moderator
 
Posts: 14012
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby samyell77 » Wed Apr 11, 2012 4:27 pm

Hi TIG,
Managed to have another look at this today - afraid Im falling at the last hurdle tho. Have run the code an managed to get them to load into the components browser but can't seem to get the components to place into the model. Once I have run this part of code;

skpfolderpath='C:\Users\sam.morris\Documents\TEST COMPS'
model=Sketchup.active_model
ents=model.active_entities
defs=model.definitions
pt=Geom::Point3d.new(0,0,0)
skps=[]
Dir.foreach(skpfolderpath){|f|skps << File.join(skpfolderpath, f) if File.extname(f).downcase==".skp"}
skps.each{|skp|defn=defs.load(skp);}

the components load into the component browser. Can I just check that nothing should be loading into the model window at this point. Not sure whether

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

should actually put each newly loaded component into the model at 0,0,0?

I have tried pasting the next piece of code in so that it reads as follows;

skpfolderpath='C:\Users\sam.morris\Documents\TEST COMPS'
model=Sketchup.active_model
ents=model.active_entities
defs=model.definitions
pt=Geom::Point3d.new(0,0,0)
skps=[]
Dir.foreach(skpfolderpath){|f|skps << File.join(skpfolderpath, f) if File.extname(f).downcase==".skp"}
skps.each{|skp|defn=defs.load(skp);tr=Geom::Transformation.new
(pt)inst=defs.add_instance(defn, tr)pt.x=pt.x+1.m}

When I run this in the (plugin) Ruby Code Editor I get the following from the Ruby Console;

"Nil result (no result returned or run failed)"
Error: #<SyntaxError: (eval):9:in `initialize': compile error
(eval):9: syntax error, unexpected tIDENTIFIER, expecting '}'
(pt)inst=defs.add_instance(defn, tr)pt.x=pt.x+1.m}
^
(eval):9: syntax error, unexpected tIDENTIFIER, expecting '}'
(pt)inst=defs.add_instance(defn, tr)pt.x=pt.x+1.m}
^>
C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/as_plugins/as_rubyeditor/as_rubyeditor.rb:178:in `initialize'
C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/as_plugins/as_rubyeditor/as_rubyeditor.rb:199:in `eval'
C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/as_plugins/as_rubyeditor/as_rubyeditor.rb:178:in `initialize'
C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/as_plugins/as_rubyeditor/as_rubyeditor.rb:199:in `call'
C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/as_plugins/as_rubyeditor/as_rubyeditor.rb:199

:(

Ive tried moving '}' to various different points and have put various bits of code on new lines but still cant seem to find out why this isn't working. Really sorry to trouble you with this again but Im sooo close to getting this working - any chance you can help me get the last bit sorted? Ive attached a screen grab showing the components in the browser but not in the model and also to show how the code is formatted in the Ruby Code Editor. Just want to get the loaded components from the browser into the model starting at 0,0,0 and spaced at 1m increments along the x plane - and am tantalizingly close!

Thanks again for all your help ...

Sam
Please, register (free) to access all the attachments on the forums.
1. PC Windows Ultimate
Intel Xeon Quad 2.26
6gb RAM
2. Mac OSX 10.7.2
2x2.8 Quad - 4gb RAM
samyell77
 
Posts: 126
Joined: Fri Feb 04, 2011 11:39 am
Name: samyell77
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: interior design
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby TIG » Wed Apr 11, 2012 5:02 pm

Use a simple
skps.each{|skp|defs.load(skp)}
which loads the skps into the definitions-list.
If you want to place instances of the definitions into the model you need to decide where the go.
Piling them all at the origin seems a bit silly?
How will you decide where they go ?
Let's assume they are added in a line from the origin incrementing in the x by 1m...
pt = Geom::Point3d.new(0,0,0)
You've just added the definitions and we know how many were added (skps.length), therefore we can get the new ones thus:
newdefs = defs.to_a[(defs.length - skps.length)..-1]
Now we add the instances to the model.
newdefs.each{|defn|
tr=Geom::Transformation.new(pt)
model.entities.add_instance(defn, tr)
pt.x=pt.x+1.m
}

This shifts 'pt' to the right 1m each time...
TIG
User avatar
TIG
Global Moderator
 
Posts: 14012
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby samyell77 » Tue May 29, 2012 12:59 pm

Hi TIG
Im still getting a syntax error on this Im afraid. Below is everything that is listed in the console when i copy and paste the lines of code.

skpfolderpath='C:\Users\sam.morris\Documents\TEST COMPS'
C:\Users\sam.morris\Documents\TEST COMPS
model=Sketchup.active_model
#<Sketchup::Model:0xcec7dfc>
ents=model.active_entities
#<Sketchup::Entities:0xecc7c58>
defs=model.definitions
#<Sketchup::DefinitionList:0xebaa640>
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:0xed92f20>, #<Sketchup::ComponentDefinition:0xed92e1c>, #<Sketchup::ComponentDefinition:0xed92dcc>, #<Sketchup::ComponentDefinition:0xed8aab4>]
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 '}'
...m::transformation.new(pt)model.entities.add_instance(defn, t...
^
(eval):178: syntax error, unexpected tIDENTIFIER, expecting '}'
...ities.add_instance(defn, tr)pt.x=pt.x+1.m}
^>
(eval):178


Ive copied and pasted every line of code in one by one in the ruby console but keep getting a syntax error when i put this line in

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

All the definitions are have been added, just cant seem to get any instances into the model?
Can you shed any light for me - sorry this keeps coming back to you.
Thanks
Sam
1. PC Windows Ultimate
Intel Xeon Quad 2.26
6gb RAM
2. Mac OSX 10.7.2
2x2.8 Quad - 4gb RAM
samyell77
 
Posts: 126
Joined: Fri Feb 04, 2011 11:39 am
Name: samyell77
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: interior design
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby TIG » Tue May 29, 2012 3:11 pm

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
Code: Select all
newdefs.each{|defn|tr=Geom::transformation.new(pt)model.entities.add_instance(defn, tr)pt.x=pt.x+1.m}

should become
Code: Select all
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
User avatar
TIG
Global Moderator
 
Posts: 14012
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby samyell77 » Tue May 29, 2012 5:02 pm

Hi TIG,

Still no dice Im afraid!

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?
1. PC Windows Ultimate
Intel Xeon Quad 2.26
6gb RAM
2. Mac OSX 10.7.2
2x2.8 Quad - 4gb RAM
samyell77
 
Posts: 126
Joined: Fri Feb 04, 2011 11:39 am
Name: samyell77
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: interior design
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby samyell77 » Tue May 29, 2012 5:40 pm

[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}
Error: #<NoMethodError: undefined method `transformation' for Geom:Module>
(eval):181
(eval):181:in `each'
(eval):181
1. PC Windows Ultimate
Intel Xeon Quad 2.26
6gb RAM
2. Mac OSX 10.7.2
2x2.8 Quad - 4gb RAM
samyell77
 
Posts: 126
Joined: Fri Feb 04, 2011 11:39 am
Name: samyell77
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: interior design
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby samyell77 » Tue May 29, 2012 5:45 pm

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...
1. PC Windows Ultimate
Intel Xeon Quad 2.26
6gb RAM
2. Mac OSX 10.7.2
2x2.8 Quad - 4gb RAM
samyell77
 
Posts: 126
Joined: Fri Feb 04, 2011 11:39 am
Name: samyell77
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: interior design
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby Dan Rathbun » Tue May 29, 2012 7:23 pm

Another cap error on the same line:
Code: Select all
newdefs.each{|defn|tr=Geom::transformation.new(pt);model.entities.add_instance(defn, tr);pt.x=pt.x+1.m}


should be:
Code: Select all
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.
User avatar
Dan Rathbun
Top SketchUcator
 
Posts: 4102
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: education
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby TIG » Tue May 29, 2012 10:58 pm

Thanks Dan, I missed the [tT]ransformation typo in his code I copy/pasted..... :roll:
The error messages are pretty good at telling you what is wrong ;)
TIG
User avatar
TIG
Global Moderator
 
Posts: 14012
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby samyell77 » Wed May 30, 2012 8:40 am

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.
:sketchstatic:
1. PC Windows Ultimate
Intel Xeon Quad 2.26
6gb RAM
2. Mac OSX 10.7.2
2x2.8 Quad - 4gb RAM
samyell77
 
Posts: 126
Joined: Fri Feb 04, 2011 11:39 am
Name: samyell77
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: interior design
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby samyell77 » Wed May 30, 2012 9:45 am

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 :thumb:
Please, register (free) to access all the attachments on the forums.
1. PC Windows Ultimate
Intel Xeon Quad 2.26
6gb RAM
2. Mac OSX 10.7.2
2x2.8 Quad - 4gb RAM
samyell77
 
Posts: 126
Joined: Fri Feb 04, 2011 11:39 am
Name: samyell77
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: interior design
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby TIG » Wed May 30, 2012 10:17 am

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...
Code: Select all
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...] :geek:
TIG
User avatar
TIG
Global Moderator
 
Posts: 14012
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Script to bring all components from library into SU mode

Postby samyell77 » Wed May 30, 2012 11:07 am

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 :D
1. PC Windows Ultimate
Intel Xeon Quad 2.26
6gb RAM
2. Mac OSX 10.7.2
2x2.8 Quad - 4gb RAM
samyell77
 
Posts: 126
Joined: Fri Feb 04, 2011 11:39 am
Name: samyell77
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: interior design
Level of SketchUp: Advanced

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago

Keyframe Animation plugin - animate your SketchUp model by adding movement to any object.

Premium Members get 20% discount!

Ad Machine
Robot
 
Posts: 2012


Return to Developers' Forum

Who is online

Users browsing this forum: mtjnien and 5 guests