sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ›£οΈ Road Profile Builder | Generate roads, curbs and pavements easily Download

    Text to component

    Scheduled Pinned Locked Moved Plugins
    7 Posts 2 Posters 1.2k Views 2 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.
    • mariochaM Offline
      mariocha
      last edited by

      Hi all,
      Is there a .rb that can substitute a component for a text?
      I mean can a text, let's say "A", be replaced by a component named "A".
      That would be helpfull for me.
      Or even better a selection of several texts replaced by the name-equivalent components.

      %(#008000)[Mario C.
      Every rule has exceptions, but some.]

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

        
        
        def text2compo()### replace text with a compo
              model=Sketchup.active_model
              ss=model.selection.to_a
              defs=model.definitions
              ss.each{|e|
                if e.class==Sketchup;;Text
                  txt=e.text
                  pt=e.point
                  next if not pt
                  ### text must have a location == i.e. have a leader
                  defn=defs[txt]
                  ### text must match compo name exactly including Case
                  if defn
                    tr=Geom;;Transformation.new(pt)
                    model.active_entities.add_instance(defn,tr)
                    e.erase!
                  end#if
                end#if
              }
        end#if
        def compo2text()### replace a compo with text
              model=Sketchup.active_model
              ss=model.selection.to_a
              ss.each{|e|
                if e.class==Sketchup;;ComponentInstance
                  txt=e.definition.name
                  ### text will == compo defn's name
                  pt=e.transformation.origin
                  model.active_entities.add_text(txt,pt,[10,10,10])
                  e.erase!
                end#if
              }
        end#if
        

        copy/paste into a file in Plugins folder called text2compo.rb, to use select the text and type text2compo in the Ruby Console, and if there's a component with that exact same name loaded into the Model then it's replaced...
        To replace selected components with text use compo2text...
        πŸ€“

        TIG

        1 Reply Last reply Reply Quote 0
        • mariochaM Offline
          mariocha
          last edited by

          Hey, Thanks for the quick answer TIG.
          You really know your stuff.

          One small bug thought.
          Upon SU launch
          *%(#0000BF)[Error Loading File text2compo.rb
          /Users/xxxxxx/Library/Application Support/Google SketchUp 7/SketchUp/Plugins/text2compo.rb:13: parse error, unexpected kIF, expecting ']'
          if defn
          ^
          ]*
          and Ruby console
          %(#0000BF)[> text2compo
          Error: #<NameError: undefined local variable or method `text2compo' for #Object:0x378f9e4>
          (eval):1034
          ]
          I did make sure I had everything copied, twice. πŸ˜›

          %(#008000)[Mario C.
          Every rule has exceptions, but some.]

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

            There was a typo in the original code - I quickly fixed it and it should now be working.
            I suspect you managed to copy it before I corrected it !
            Try recopying the code and try again - it works for me.
            You must match the text and the defn name exactly...
            πŸ€“

            TIG

            1 Reply Last reply Reply Quote 0
            • mariochaM Offline
              mariocha
              last edited by

              Yes Sir, works pretty well now.
              Thanks a lot.
              impressive, what the combination of Ruby with an expert can do, and quickly.

              %(#008000)[Mario C.
              Every rule has exceptions, but some.]

              1 Reply Last reply Reply Quote 0
              • mariochaM Offline
                mariocha
                last edited by

                Ah... one more thing, if I may.
                Could the script be activated with right-click ?
                updt: I managed to get it in the Tools menu. πŸ˜„
                updt: and the contextual also.
                I'm learning a bit of Ruby just by looking at .rb files πŸ˜›

                %(#008000)[Mario C.
                Every rule has exceptions, but some.]

                1 Reply Last reply Reply Quote 0
                • mariochaM Offline
                  mariocha
                  last edited by

                  I gave another shot at the file and succeeded in making it work on any text string of 1, 2 or 3 characters. πŸ˜„

                  %(#008000)[Mario C.
                  Every rule has exceptions, but some.]

                  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