• Login
sketchucation logo sketchucation
  • Login
πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Replace components

Scheduled Pinned Locked Moved Developers' Forum
5 Posts 2 Posters 578 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.
  • L Offline
    liquid98
    last edited by 12 Feb 2011, 11:42

    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

    Things that flourish fall into decay. This is not-Tao, And what is not-Tao soon ends ~ Lao tse

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 12 Feb 2011, 12:41

      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 set model=Sketchup.active_model and then copy paste that one line model.definitions.load("c:\new.skp") and then model.definitions.load("c:\\new.skp") to see what each returns... πŸ€“

      TIG

      1 Reply Last reply Reply Quote 0
      • L Offline
        liquid98
        last edited by 12 Feb 2011, 15:46

        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

        Things that flourish fall into decay. This is not-Tao, And what is not-Tao soon ends ~ Lao tse

        1 Reply Last reply Reply Quote 0
        • T Offline
          TIG Moderator
          last edited by 12 Feb 2011, 18:00

          @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>
          

          TIG

          1 Reply Last reply Reply Quote 0
          • L Offline
            liquid98
            last edited by 15 Feb 2011, 22:39

            Hi Tig,

            Thanks

            I'll do some more study until I get what you suggested and come back
            here πŸ˜’

            Things that flourish fall into decay. This is not-Tao, And what is not-Tao soon ends ~ Lao tse

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

            Advertisement