Drawing an array of boxes
-
I used Building.new and called the io_calcs function as well as the draw_slab_array function, what does that mean about my variables then, should I be able to use them within the functions that are in the class?
To my knowledge, within a class, Ruby makes all of it's variables and functions public unless specified otherwise, so it confuses me as to why I get this problem of the variable not being read
-
Each kind of variable is referenced differently to indicate it's scope. @@var is a class variable, @var is an instance variable, and var without a modifier is a method variable. method variables can only be referenced within that method, instance variables have unique values for each instance, and class variables are shared by all instances of a class including super/sub classes. Class instance variables exist as well, and use the @var syntax, but are declared outside of a method which can trip up the unaware coder. They act like class variables but without super/sub classes having access to them. This gives you granular control over the scope for the reasons mentioned above - variables with limited scope are easier to debug. Using that as a rule of thumb then a best -> worst listing would be method -> instance -> class instance -> class -> global. In your case I believe simply changing the $ to @ would give you the behavior you're looking for assuming you want distinct values every time you call Building.new
-
Ahh i see, thanks for that
Another error now, the code keeps looping and doesn't stop (doesn't draw either), the values seem to be wrong also... nothing seems to be working :s
-
I would suggest breaking the code out into logical steps, and give each step a method. Normally I'd start with a blank file and write out what I want to happen in natural language as comments. Once I get it into a step-by-step form that makes sense I will declare method names around each step. Once I've done that I figure out what the inputs and outputs of each method should be and document them in a comment. Once that's done I pick the easiest one to test, write a simple test for it, then implement the method and see if it passes the test. Repeat until finished. It's hard to say why it's failing here because of all the variable kicking around, so maybe just try and split the methods into more bit sized portions then put it together at the end.
-
I think i've found it with the help you gave there (just a snippet of the code where the error might be):
class Building def io_calcs #distprompts = [$exStrings.GetString("What is the distance between the start of one column to the start of the next one? (make it at least 1m more than the width of the foundation")] #distvalues = [5500] # Now display the inputbox #distresults = inputbox distprompts, distvalues, $exStrings.GetString("Distance between each column (mm)") #return if not distresults # This means that the user cancelled the operation #coldistance = distresults ##Enter values from C++ here slab = IO.readlines('C;\\Users\\Zane\\My Documents\\slabout.txt') @slabdepth = slab[0].to_f.mm @slabwidth = slab[1].to_f.mm @slabheight = slab[2].to_f.mm #UI.messagebox ("Sketchup will now take in the details and attempt to draw the building", MB_OK) end
It's either not taking in the values of slabheight, slapdepth nor slabwidth as i'm trying to take them in from an input file.
It's not even letting me "puts" the variables nor any other string I tell it to output like:puts 'test'
Ideally i want to input variables from a text file whose content is
250
5500
7700and assign each one of these values to a variable, then use those respective variables.
-
The namespace Examples::SU I really intended for the Google supplied examples.
You should choose your own Toplevel namespace (Zane is good for now.)
Beneath that you will have you various plugin namespaces (module blocks.)
like this or similar:
module Zane module BuildWizard # this plugin module Structure class Building end # class class Bridge end # class class Roadway end # class end # module Structure module Widget ebd # module Widget end # module BuildWizard end # module Zane
etc..
Once your namespace heirarchy is defined (in the first loaded file of your plugin,) you can break up the various code blocks into separate files (and save some indentation by using qualified blocks:
file2
module Zane;;BuildWizard;;Structure # define module vars and module methods end
file3
class Zane;;BuildWizard;;Structure;;Bridge # define instance vars and instance methods end
file4
class Zane;;BuildWizard;;Structure;;Building # define instance vars and instance methods end
file5
module Zane;;BuildWizard;;Widget # define module vars and module methods end
.. etc...
and use the require("filename") where needed to load the various files that make up your plugin project.
-
Sorry Dan, i'm a bit lost as to what you mean by your last post, why is it that I would split the file up into bits and what is the purpose of using modules, i've read they're similar to classes but nothing specified a distinct reason as to why they are as they are?
I'm also curious as to what you meant when you wrote module_function(:function) in the box.rb file, what does it do exactely? I presume it can only be used for external functions and not ones in classes?
-
@zane said:
I think i've found it with the help you gave there (just a snippet of the code where the error might be):
> #distprompts = [$exStrings.GetString("What is the distance between the start of one column to the start of the next one? (make it at least 1m more than the width of the foundation")]
$exStrings is not YOUR object. It is a hash of strings, created and used by the Google supplied code examples.
Create your own local string, or a LanguageHandler hash object within your plugin namespace (DO NOTuse a global like Google did, it is bad programming.)
Or just use a string literal.
distprompts = ["Column Spacing"]
Also with a prompt string that large, the UI.inputbox is likely to look crazy.
-
ok, thanks for the headsup on that, any idea about the crazy looping issue or why I can't see the puts anymore? Oh, and that module question, sorry there's so much to ask.
-
Another booboo:
coldistance = distresults
A UI.inputbox returns an Array, not a single value.
So to get the first value from an inputbox:coldistance = distresults[0]
or
coldistance = distresults.first
-
@zane said:
I think i've found it with the help you gave there (just a snippet of the code where the error might be):
It's either not taking in the values of slabheight, slapdepth nor slabwidth as i'm trying to take them in from an input file.
It's not even letting me "puts" the variables nor any other string I tell it to output like:try stripping each line of whitespsce and control characters:
slab = IO.readlines('C;\\Users\\Zane\\My Documents\\slabout.txt') @slabdepth = slab[0].strip.to_f.mm @slabwidth = slab[1].strip.to_f.mm @slabheight = slab[2].strip.to_f.mm
Eventually you'll want these values on one line separated by commas (likely with the tag "slab" as the first vaule.
lines = IO.readlines('C;\\Users\\Zane\\My Documents\\slabout.txt') lines.each {|line| data = line.strip.split(',') if data[0]=='slab' @slabdepth = data[1].to_f.mm @slabwidth = data[2].to_f.mm @slabheight = data[3].to_f.mm elsif data[0]=='wall' # do something else end }
-
@zane said:
Sorry Dan, i'm a bit lost as to what you mean by your last post, why is it that I would split the file up into bits ...
When you write complex plugins that have 1000s of lines of code, you WILL get tired of scrolling up and down. It is easier if you have a tabbed code editor to click another tab, find what you need in another file (a constant name, etc.,) and then click the tab of the file your working on. (I use Notepad++ which also allows splitting the editor into 2 panes.)
@zane said:
...and what is the purpose of using modules, ...
See my info topic: [info] Using Ruby Modules
@zane said:
...I've read they're similar to classes ...
Actually, class Class is the child subclass of class Module, and subclasses inherit objects (like methods,) from their superclass, so of course they are similar.
@zane said:
I'm also curious as to what you meant when you wrote module_function(:function) in the box.rbfile, what does it do exactly? I presume it can only be used for external functions and not ones in classes?
Well... you won't understand this issue until you do some reading... but basically the module_function() allows a coder to write a double-duty module, that can act as both a library module (it's methods are called from outside using name qualification,) and a Mixin-Module (that is mixed into classes, giving the class all it's instance methods.)
The box.rb is really a poor example of such a "double-duty" Module.see Ruby Pick-Axe book: module_function
Jim asked a similar question, and I answered in the box.rb Example topic, see:
Re: module_functionZane.. did you read my "Newbie's Guide" ? (If not click the Ruby Resource link in my signature.)
-
Thanks Dan, I have read the pragmatic programmers book but that was a while back and unfortunately I have a deadline submission I need to meet next week, i've managed to get the C++ side of the coding done but am struggling with the Ruby and don't have time to re-assess what i've learnt, just need to sift through the relevant information and collaborate it.
Advertisement