Please help with this code to normalize size to (1,1,1)
-
I want to write a plugin that takes a selected group and resizes it so that it fits in a (1,1,1) box , while keeping the model proportions.
Someone advised me with this code, except that it is unfinished and i dont understand how to write a ruby plugin from juste these lines, please could you help me?
e # your entity target_size = 1.m s = [e.bounds.width, e.bounds.height, e.bounds.depth].max r = target_size / s t = Geom;;Transformation.scaling(r) e.transform!(t)
thankyou
-
Try something like this [works on the selected object]
require('sketchup.rb') # Usage; select an object, then TIG.scaleto(dimension) in Ruby Console module TIG def self.scaleto(d=1.m) e=Sketchup.active_model.selection[0] # your entity s = [e.bounds.width, e.bounds.height, e.bounds.depth].max r = d / s t = Geom;;Transformation.scaling(r,r,r) e.transform!(t) end end
You can pass any maximum dimension to tool
-
i had this message in ruby console:
TIG.scaleto(1) Error; #<NoMethodError; undefined method `bounds' for nil;NilClass> C;/Program Files/Google/Google SketchUp 8/Plugins/resize_mesh_111.rb;6 (eval);6 TIG.scaleto(1m) Error; #<SyntaxError; (eval);6; compile error (eval);6; syntax error, unexpected tIDENTIFIER, expecting ')' TIG.scaleto(1m) ^> (eval);6 TIG.scaleto(1.m) Error; #<NoMethodError; undefined method `bounds' for nil;NilClass> C;/Program Files/Google/Google SketchUp 8/Plugins/resize_mesh_111.rb;6 (eval);6
-
Thanks!
I was lost at this moment: then TIG.scaleto(dimension) in Ruby Console
i need a ruby console plugin?
i was hoping to add it for example into the end of this code, anything that runs is great.
# Author ; Todd Burch http://www.smustard.com require 'sketchup.rb' class Move2OriginAndCenter def initialize bb = Geom;;BoundingBox.new am = Sketchup.active_model # Sketchup Active Model. am.start_operation "Move To Origin" ; se = am.active_entities # Sketchup Active Entities. Allows working inside a component. ss = am.selection # Work with selection, if any... if (ss.length == 0) then ss = se ; end ; # ...else work with whole model. ss.each {|e| # add all geometry to our temporary bounding box bb.add(e.bounds) ; } trans = Geom;;Transformation.new(bb.center.vector_to(ORIGIN)) ; # move the temp bounding box status = Sketchup.active_model.entities.transform_entities(trans, ss.to_a ) ; #puts "status is #{status}" ; am.commit_operation ; end ; # initialize end ; # class UI.menu("Plugins").add_item("Move to Origin And Center") { Move2OriginAndCenter.new } if not file_loaded?(__FILE__) file_loaded(__FILE__)
-
You now want it to move to be centered on the origin, and work on a multiple selection or the whole model if no selection - you are moving the goalposts somewhat !
With my initial code...
You need to select a group or component-instance and then run the code - it only processes one object !
That error message is just telling you it can't find the bounds of 'nothing' !!Also the dimension passed to the method needs to use the Length format
1.m
NOT1m
...
There must be a '.' before your units suffix to show it's a Length.
A 'plain number' is taken by SketchUp to be in inches, adding a Length suffix -1.m 100.cm 1000.mm
etc - uses those units instead...Also regarding your coding attempt - you do not want to make a base level Class, make it a Module and a Method...
It's much simpler in the long run...
Here is some revised code...
This processes the selected objects OR the whole model's active_entities, if there is no selection.
It also accepts a Length as the argument when it's typed into the Ruby Console, if there's no argument passed you'll get a dialog asking for the Size***.
If you use the menu code the Plugins menu item has no argument, so it will run with a dialog to enter the Size - ***it can be typed as a number without a units suffix - and is then taken to be in your current model units, OR if you add a suffix to the number it's taken as if you were doing it in the VCB - and this time it's1.0m
etc NOT 1.0.m because it's auto-converted into a Length for you !
If you also want to skip the dialog from the menu item, then add a Length argument to pass a fixed Size to the menu code too, thus:
{self.scaleto(**1.0.m**)}
You can of course customize the module name, from my 'TIG' to say 'JamesJones' - note that a module's name must start with an uppercase letter [A-Z] and thereafter only include [A-Za-z0-9_] ...require('sketchup.rb') # Usage; select object[s], then TIG.scaleto(1.m) in Ruby Console # You are prompted for a size if none is given. module TIG @tit="Center on Origin and Scale" UI.menu("Plugins").add_item(@tit){self.scaleto()} unless file_loaded?(__FILE__) file_loaded(__FILE__) def self.scaleto(d=nil) model = Sketchup.active_model ss = model.selection ae = model.active_entities if ss[0] es = ss.to_a else # no selection use active_entities es = ae.to_a end unless d && (d.is_a?(Fixnum) || d.is_a?(Float) || d.is_a?(Length)) @dim = 1.m unless @dim # remembered across usage per session. results = inputbox(["Size; "], [@dim], @tit) return nil unless results @dim = results[0] d = @dim end ### model.start_operation(@tit) ### bb = Geom;;BoundingBox.new() es.each{|e| bb.add(e.bounds) } c = bb.center v = c.vector_to(ORIGIN) tv = Geom;;Transformation.translation(v) ae.transform_entities(tv, es) s = [bb.width, bb.height, bb.depth].max r = d / s ts = Geom;;Transformation.scaling(ORIGIN, r, r, r) ae.transform_entities(ts, es) ### model.commit_operation ### end end
-
Hi there, it's ok now i managed to run the TIG code ok.
There is only one piculiar thing happening, if i resize to TIG.scaleto(1) the resulting width is (0.24 meters), and when i resize to 200, the ruler measures (5.080m) bounds width.
Thanks alot! no no the move to origin is just a whimsy, the move to origin code is great i was just abit confused with the error message.
i tried to select it, i obviously didnt manage!
Thankyou so much for your help.
I added this line r = r*39.37007874015748; to make (1) scale to 1m. obviously i have to adjust some setting. thanks.
-
Using meters scales to meters...
1.m in code but 1m in dialog...
Advertisement