Implementing Todds' Progress Bar?
-
I need some help installing a progress bar inside my Tile Ruby. Using a rectangle approach for selecting an area that creates tiles of varying thickness, and random colors can take some time for SU to generate. I have looked at a few examples that use the progress bar, but I don't grasp where my specific bits of code need to placed, to report the amount of time it will take to create the vpanes and hpanes.
if( $type == "Tile Random Thickness" ) 1.upto($vpanes) do |i| x = ($vtile*i)+(i*$space) 1.upto($hpanes) do |j| y = ($htile*j)+(j*$space) o = $pt3.offset($hVec, y - $space).offset($vVec, x - $space) pt1 = o.offset($hVec, -$htile) pt2 = o.offset($vVec, -$vtile) pt3 = pt1.offset($vVec, -$vtile) #make the geometry face = entities.add_face o, pt2, pt3, pt1 face.material= Sketchup;;Color.new( rand(255), rand(255), rand(255)) face.reverse! face.pushpull ($tthick+rand(20)) end end end #if
Note: the code within the 2 if statements represents just one option selected from a drop-down menu
-
Just a sidenote: I see you use a lot of global variables - a few with names that can quite likely cause conflicts if others use global variables. Any particular reason you are not using instance variables or class variables?
-
Kwok: thank you for your input. I could not agree with you more, Todd has been a valuable Ruby member.
thomthom: I will change from globals after I have finished the entire script. Since many of my Rubies rely heavily on exact xyz co-ordinates. I usually start with globals, so I can evaluate them manually in the Ruby Console as a dbl. check should I have made a math mistake in a particular expression.
-
@kyyu said:
Basically you just make a new progress bar and place a counter/update command in your loop of interest.
-KwokI was ing to soon! The point raised about the loop of interest is very compelling. There appear to be 3 loops of interest. the Vpanes theHpanes, and the most time consuming appears to be:
face.material= Sketchup;;Color.new( 79, rand(172), 200)
its interesting that:
face.material = Sketchup;;Color.new(255, 255, 255)
takes no time at all.
So going forward: Do I add the 3 loops in some fashion, or can I express the 3 variables into one
pb = ProgressBar.new (etc,etc,etc)
statement ?
-
If the 3 loops are nested, you can just use the outer loop and have one progress bar.
If you have 3 separate loops, you can have a unique instance of a progressbar in each one.
Thanks, Todd
-
Hi tomot,
The Progress Bar is an excellent plugin. Thanks Todd! I just recently added it to 2 of my plugins. Try the code below. I just added 4 lines. Basically you just make a new progress bar and place a counter/update command in your loop of interest. Oh, and I guess you need the require statement at the beginning: require 'progressbar.rb'
-Kwok
if( $type == "Tile Random Thickness" ) z = 0 pb = ProgressBar.new($vpanes,"Processing Panes") 1.upto($vpanes) do |i| x = ($vtile*i)+(i*$space) 1.upto($hpanes) do |j| y = ($htile*j)+(j*$space) o = $pt3.offset($hVec, y - $space).offset($vVec, x - $space) pt1 = o.offset($hVec, -$htile) pt2 = o.offset($vVec, -$vtile) pt3 = pt1.offset($vVec, -$vtile) #make the geometry face = entities.add_face o, pt2, pt3, pt1 face.material= Sketchup;;Color.new( rand(255), rand(255), rand(255)) face.reverse! face.pushpull ($tthick+rand(20)) end z+=1 pb.update(z) end end #if
-
Its really simple once I stopped making it difficult. I lost track of the fundamentals of the upto routine, which was actually multiplying a number of horiz elements by a number of vertical elements. Hence the revised code snippet makes the progress bar measure the correct variables
z=0 pb= ProgressBar.new($hpanes*$vpanes,"Processing panes") 1.upto($vpanes) do |i| x = ($vtile*i)+(i*$space) 1.upto($hpanes) do |j| y = ($htile*j)+(j*$space) ------------------------SNIP-------------------- z+=1 pb.update(z)
-
If you do it that way, then you would have to advance the counter and update for both loops. Like Todd said, you only have to do the outer loop, because it can't finish until all nested loops do.
-Kwok
-
@kyyu said:
If you do it that way, then you would have to advance the counter and update for both loops. Like Todd said, you only have to do the outer loop, because it can't finish until all nested loops do.
-Kwok
Kwok, I'm only using one outer loop. The -----SNIP----- was supposed to represent the rest of the code which I did not want to repeat. I'm trying to be more like Also the rand function has no bearing on the amount of time it take to generate the randomly colored tiles, since its within the nested loop.
-
The code looks good to me.
My comment about the "outer loop" should have really have been phrased as "once somewhere in the nesting of the loops". Where it was placed is where I would have placed it - in the meat of processing.
If, for an extreme example, two progress bars were made, one for horizontals and another for verticals, it would work, but the status bars would be bouncing between the two, making it overly complicated and confusing to the user.
-
Interesting! I see what you guys are doing now and learned something. -Kwok
-
Thanks guys, without your input, I would have been watching the Bachelor .............I'm kidding!
I will post this Ruby when I get it in STILL better shape.
Advertisement