Replace components
-
Hi,
I have this ruby script to replace a component with an other:
` require 'sketchup'
module Swapi
def Swapi.main model = Sketchup.active_model entities = model.active_entities selection = model.selection
newDef = model.definitions.load("c:\new.skp")
oldDef = model.definitions['old']
oldDef.instances.each { |old_inst|
t = old_inst.transformation
ents = old_inst.parent.entities
ents.add_instance(newDef, t)
old_inst.erase!
}end unless file_loaded?( __FILE__ ) UI.menu("Plugins").add_item("swap") {Swapi.main} end
end # module
file_loaded( __FILE__ )`
But it doesn't work, can someone help me out?.
And an other question: Is it possible to insert a component from a file wich contains several
different components?Thnx
-
Assuming that you have the 'old' compo etc...
###... newDef = model.definitions.load("c;\\new.skp") oldDef = model.definitions['old'] oldDef.instances.each{|old_inst|old_inst.definition=newDef} ###...
is much easier...
Also note that you must use \ for a \ in the file path inside "" otherwise 'newDef' is returned as 'nil'.
In the Ruby Console setmodel=Sketchup.active_model
and then copy paste that one linemodel.definitions.load("c:\new.skp")
and thenmodel.definitions.load("c:\\new.skp")
to see what each returns... -
Thank you TIG
still learning... Now it works
Is it also possible to do this:
newDef = model.definitions.load("c;\\new.skp(componentX)")
To load 'componentX' from c:\new.skp assuming that new.skp contains several
components among which there is a component called componentX?Or do I have to do something with the model.definitions belonging to new.skp?
Making a list and choose from there? I really don't have a clue..Please help
-
@liquid98 said:
Thank you TIG
still learning... Now it works
Is it also possible to do this:
newDef = model.definitions.load("c;\\new.skp(componentX)")
To load 'componentX' from c:\new.skp assuming that new.skp contains several
components among which there is a component called componentX?Or do I have to do something with the model.definitions belonging to new.skp?
Making a list and choose from there? I really don't have a clue..Please help
The whole 'new.skp' will load with whatever subcomponents it might have.
If you only want componentX you have to either have split it out by hand earlier and use that, OR import all of 'new.skp' and then manipulate that. For example, after importing new.skp you have a component definition called 'new' and a definition object pointing at it called 'newDef' [as you coded], you can now iterate through the entities inside newDef thus:<span class="syntaxdefault"></span><span class="syntaxcomment">###...<br /></span><span class="syntaxdefault">defn</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">nil<br />newDef</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|</span><span class="syntaxdefault">defn</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">e if e</span><span class="syntaxkeyword">.class==</span><span class="syntaxdefault">ComponententInstance and e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">==</span><span class="syntaxstring">"componentX"</span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">if defn<br /> </span><span class="syntaxcomment">### do something with 'componentX' or 'newDef'### e.g.<br /></span><span class="syntaxdefault"> inst</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_instance</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">defn</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Transformation</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">ORIGIN</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault"> xdef</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">inst</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definition<br /> inst</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">erase</span><span class="syntaxkeyword">!<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">### moves xdef into mainstream defs list outside of nested 'new' entities [name will probably adjust too?]<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">###...<br /></span><span class="syntaxdefault">end</span><span class="syntaxcomment">#if<br />###...<br /></span><span class="syntaxdefault"> </span>
-
Hi Tig,
Thanks
I'll do some more study until I get what you suggested and come back
here
Advertisement