sketchucation logo sketchucation
    • Login
    ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

    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.
    • liquid98L Offline
      liquid98
      last edited by

      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
      • TIGT Offline
        TIG Moderator
        last edited by

        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
        • liquid98L Offline
          liquid98
          last edited by

          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
          • TIGT Offline
            TIG Moderator
            last edited by

            @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
            • liquid98L Offline
              liquid98
              last edited by

              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
              • First post
                Last post
              Buy SketchPlus
              Buy SUbD
              Buy WrapR
              Buy eBook
              Buy Modelur
              Buy Vertex Tools
              Buy SketchCuisine
              Buy FormFonts

              Advertisement