Text to component
-
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. -
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#ifcopy/paste into a file in Plugins folder called
text2compo.rb, to use select the text and typetext2compoin 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 usecompo2text...

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

-
Yes Sir, works pretty well now.
Thanks a lot.
impressive, what the combination of Ruby with an expert can do, and quickly. -
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
-
I gave another shot at the file and succeeded in making it work on any text string of 1, 2 or 3 characters.

Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement