Number Formatting Macro from Chief Academy


 Share

Recommended Posts

Good Afternoon all,

 

Yesterday I shared a user defined macro with the Advanced 2 class at Chief Academy.  I promised that I would share it on ChiefTalk, so here it is.

 

I shared this example for two reasons,  The first was that I wanted to demonstrate how to use the NumberFormatter class that we have provided in Chief.  You can read more about NumberFormatter in the section title "Measurement and NumberFormatter Classes" in the Chief Architect Premier X16 Reference Manual.

 

The other purpose for sharing this macro was to demonstrate a technique for creating user defined macros that can accept input.  This technique makes use of a Ruby construct called a Proc.

 

I am attaching a file that you can import into chief.  The first macro in that file is format_ft_in:

Proc.new { |val|
    formatter = NumberFormatter.new
    formatter.unit = "'-\""
    formatter.use_fractions = true
    formatter.apply val
}

 

The other macro is format_sq_ft:

Proc.new { |val|
    formatter = NumberFormatter.new
    formatter.unit = 'ft²'
    formatter.decimal_places = 2
    formatter.apply val
}

 

These macros aren't very helpful if you use something like "%format_ft_in%" all by itself.  Here is an example of how to use it.  I included this text in the label for a slab:

Thickness: %macros.format_ft_in.call(thickness)%
Perimeter: %macros.format_ft_in.call(perimeter)%
Area: %macros.format_sq_ft.call(area)%

 

The attached image shows the results:

image.thumb.png.c7016b00f3266d429898fbdae117042c.png

 

The primary advantage of this technique is that with a single macro I can format any linear measurement.  I this case I am able to use "format_ft_in" for both the thickness and perimeter of the slab. Furthermore, I could apply it to the length of a wall, the width of a cabinet, the height of a window, etc.  Similarly "format_sq_ft" could be used for any area measurement.

 

I hope these macros are helpful to a few of you.  I know that some of you can improve on this technique further.  I would love to see what you come up with.

NumberFormatter and Proc.json

  • Like 3
  • Upvote 1
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share