TimSchrock

Ruby Macro Cabinet width help

Recommended Posts

Hey all,

I have an idea that I want to create a macro to help my cabinet code. For this specific cabinet (from my manufacturer), if the cabinet is 42" or less, the code is "VTC30 *width*-1B" and greater than 42", the code is "VTC30 *width*-2B".

First, I'm starting with the macro and trying to get the width of the referenced object, but I keep getting an evaluation error that there is an undefined local variable or method. I've tried "width", "referenced.width", "owner.width" and all the same thing. 

Can you point me to the right direction?

Share this post


Link to post
Share on other sites

So, I can enter a macro "referenced.width" and I receive the correct width of the cabinet, but then I cannot figure how to get it to do the logic of *if referenced.width <= 42 then cablabel = "1B" else cablable = "2B""

Share this post


Link to post
Share on other sites

Are you using X11 like your signature says or X12?

Share this post


Link to post
Share on other sites

With X12 you can place this in the Label of the cabinet, and I'm sure there is better Ruby code to do it.

 

VTC30 %width.to_s.to_i%-%width.to_f > 42 ? '2' : '1'%B

 

Slightly different but same result. This uses chomp to remove the " in" that chief adds when converting to a string. The one above converted to an integer which has the same outcome.

 

VTC30 %width.to_s.chomp(' in')%-%width.to_f > 42 ? '2' : '1'%B

 

 

This code works in a user macro.

 

 

'VTC30 ' + width.to_s.chomp(' in') + '-' + (width.to_f > 42 ? '2' : '1') + 'B'

 

 

 

  • Like 1

Share this post


Link to post
Share on other sites
11 hours ago, Alaskan_Son said:

Are you using X11 like your signature says or X12?

Whoops. No. Signature is out of date. Using X12. 

Share this post


Link to post
Share on other sites
10 hours ago, solver said:

With X12 you can place this in the Label of the cabinet, and I'm sure there is better Ruby code to do it.

 

VTC30 %width.to_s.to_i%-%width.to_f > 42 ? '2' : '1'%B

 

 

 

 

 

 

 

Thanks! I’ll try that

Share this post


Link to post
Share on other sites

Here is the same code to be used in a macro.

 

'VTC30 ' + width.to_s.chomp(' in') + '-' + (width.to_f > 42 ? '2' : '1') + 'B'

Share this post


Link to post
Share on other sites
16 minutes ago, TimSchrock said:

Whoops. No. Signature is out of date. Using X12. 

 

Update by clicking on your user name at the top right of the page, click Account Settings, then Signature on the left.
 

Share this post


Link to post
Share on other sites
17 minutes ago, solver said:

 

Update by clicking on your user name at the top right of the page, click Account Settings, then Signature on the left.
 

I updated my signature after OP, but it still shows X11 here. odd.....

 

Anyway, the real reason for replying is that the code worked! Can I beg another question? What does it mean? I had the %width% working - it showed the accurate width. What is the "width.to_s.to_i" and "width.to_f" doing? convert "to string" and "to integer"? Not sure what to guess on "to_f".

Share this post


Link to post
Share on other sites
Just now, TimSchrock said:

Not sure what to guess on "to_f".

 

"to_float"  and good guesses on everything else.

Share this post


Link to post
Share on other sites
1 minute ago, Chopsaw said:

 

"to_float"  and good guesses on everything else.

what does that do?

Share this post


Link to post
Share on other sites
4 minutes ago, TimSchrock said:

what does that do?

 

I might leave that to Eric because I am not even sure it needs to be there in X12 as the width should already be a float value.

 

You could try removing it and see what changes.  I might be wrong though.

 

Float

Float objects represent inexact real numbers using the native architecture's double-precision floating point representation.

Share this post


Link to post
Share on other sites

Oh I forgot that will remove the punctuation generated by the new measurement class so it is necessary for this purpose.

Share this post


Link to post
Share on other sites
28 minutes ago, TimSchrock said:

What does it mean?

 

Lots of Ruby info via Google ...

 

11 minutes ago, Chopsaw said:

Oh I forgot that will remove the punctuation generated by the new measurement class so it is necessary for this purpose.

 

I believe that is correct.

Share this post


Link to post
Share on other sites

@solver or @Alaskan_Son,

I'm on a mission to get some cabinetry codes working with Ruby. I see a property that is "has_stile_between_double_doors" (TrueClass) and a property that is "num_doors" (Integer). If num_doors = 2 and has_style_between_double_doors is false, I want to add "-BT" to the code.

Share this post


Link to post
Share on other sites
13 minutes ago, TimSchrock said:

@solver or @Alaskan_Son,

I'm on a mission to get some cabinetry codes working with Ruby. I see a property that is "has_stile_between_double_doors" (TrueClass) and a property that is "num_doors" (Integer). If num_doors = 2 and has_style_between_double_doors is false, I want to add "-BT" to the code.

I've tried this in a macro I call %ButtDoors% -  has_stile_between_double_doors.to_s = 'true' ? (num_doors = 2 ? '' : '-BT') 

It only returns that very same text when I place %ButtDoors% in the text box

Share this post


Link to post
Share on other sites
1 hour ago, TimSchrock said:

Whoops. No. Signature is out of date. Using X12. 

 

The reason I asked is because Chief introduced the Measurement class in X12.  Prior to X12, object attributes like owner.width were stored and reported as Floats which are numeric.  They are no longer stored and reported as Floats but as Measurements with specified units.  In Imperial plans, the units are inches, and in Metric plans, the units are millimeters.  The reason your initial code wouldn't work is because  "if referenced.width <= 42..." didn't make any sense.  Since referenced.width is being stored as inches you needed to specify 42 of what.  Eric's code got around this by converting width to a float (decimal) or an integer (whole) number with no unit.  He could then compare the 2 values apples to apples.  My recommended approach would be different and I would suggest taking advantage of Chief's Measurement class by just adding a unit to 42... "if referenced.width <= 42.in..."  or "if referenced.width <= 3.5.ft..."

 

As a parting tip, I would also strongly recommend against coercing a value into an integer unless it really makes sense for the application.  It will always round down to the nearest whole number and values below 1 will always result in 0.  It's just bad practice in my opinion.  A 41.75" cabinet would report as 41 and any values multiplied by a value less than one would result in zero.  The better approach is .to_f.round with or with an argument.

Share this post


Link to post
Share on other sites

Lots of ways to do this. Suggest you either spend some time learning the ins and outs of macros and Ruby, or hire someone to set things up for you.

 

This isn't something easily done via the forum.

 

ct1.thumb.png.e969b81beaf93f40396ab07c55992294.png

Share this post


Link to post
Share on other sites
Posted (edited)
5 minutes ago, solver said:

Lots of ways to do this. Suggest you either spend some time learning the ins and outs of macros and Ruby, or hire someone to set things up for you.

 

This isn't something easily done via the forum.

 

ct1.thumb.png.e969b81beaf93f40396ab07c55992294.png

I'm actually decent at programming languages, but the Ruby tutorials I've found aren't geared toward Chief and talk about local, class, instance, and global variables, of which I can't figure out how that connects to Chief.

Can you point to tutorials of Ruby within Chief?

Edited by TimSchrock
clarification

Share this post


Link to post
Share on other sites
11 minutes ago, TimSchrock said:

Can you point to tutorials of Ruby within Chief?

 

Don't know of any.

 

11 minutes ago, TimSchrock said:

talk about local, class, instance, and global variables

 

Don't worry so much about these things. For most simple scripts, variables are local.

 

Here is an example of using global variables.

 

 

Searching the forum and trying things is about the only way to learn.

 

https://chieftalk.chiefarchitect.com/search/?&amp;q=ruby&amp;type=forums_topic&amp;nodes=24&amp;search_and_or=and&amp;sortby=relevancy

 

 

Share this post


Link to post
Share on other sites
Posted (edited)

I solved it!

Enter this into the label or code (under the "Object Information) box: %automatic_label%%has_stile_between_double_doors == false ? (num_doors.to_f > 1 ? '-BT' : '') : ''%

Edited by TimSchrock
Corrected code

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

  • Member Statistics

    29130
    Total Members
    9156
    Most Online
    da3020
    Newest Member
    da3020
    Joined