Doug_N

Macro returning "units"

Recommended Posts

Here is a little macro I made for displaying area in meters instead of sq ft.

 

a=area*0.092903

a.round(2)

 

As a habit, I use the three letters own or ref to tell if the macro is to be used in an item label or attached as a referenced macro.

 

If I put this into a macro such as "object_area_own_metric" in an imperial drawing, and insert the macro into an enclosed polyline it returns 112.52 SQ FT.   Why is it attaching the units to the output?  If I use a referenced macro "object_area_ref_metric" and connect a text box to it the output is 112.52 with no units.  Obviously I want sq m not sq ft after the output,  

 

Any idea how to stop the units from displaying if I use an owner macro in a polyline label?

Share this post


Link to post
Share on other sites

In X12 all numeric attributes are Measurements that have "UNITS".  Calculations return the same Units even though that isn't what you want.

 

Change your macro to:

 

a=(area*0.092903).round(2).to_f.sq_m.to_s

Share this post


Link to post
Share on other sites

It's also possible to leverage the unit system to handle the conversion for you, which is useful when you don't want to look up conversion constants:

area.convert_to("sq m").round(2)

 

Share this post


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

In X12 all numeric attributes are Measurements that have "UNITS".  Calculations return the same Units even though that isn't what you want.

 

Change your macro to:

 

a=(area*0.092903).round(2).to_f.sq_m.to_s

 

2 minutes ago, BenMerritt said:

It's also possible to leverage the unit system to handle the conversion for you, which is useful when you don't want to look up conversion constants:


area.convert_to("sq m").round(2)

 

Wow, thank you Joe and Ben. 

Ben your solution is very elegant, 

Share this post


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

 

Wow, thank you Joe and Ben. 

Ben your solution is very elegant, 

Yes, Ben's solution using the .convert_to() method works nicely but I'm not really up on the methods available with the Measurement Class.  I really need a pdf document that is akin to the Ruby.org documentation.

 

I think CA should provide that in their help files or a separate PDF for us.

  • Upvote 1

Share this post


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

Yes, Ben's solution using the .convert_to() method works nicely but I'm not really up on the methods available with the Measurement Class.  I really need a pdf document that is akin to the Ruby.org documentation.

 

I think CA should provide that in their help files or a separate PDF for us.

 

Yes Please.   That would be great. @BenMerritt

Share this post


Link to post
Share on other sites

I agree.  

Share this post


Link to post
Share on other sites
8 hours ago, BenMerritt said:

It's also possible to leverage the unit system to handle the conversion for you, which is useful when you don't want to look up conversion constants:


area.convert_to("sq m").round(2)

 

 

It can actually be even shorter.  I believe area.to_sq_m.round(2) does the same thing right?

Share this post


Link to post
Share on other sites

It's not a full list if what can be done, an explanation of exactly how the conversions work, or an explanation of the different types of Measurements (Linear vs. Area vs. Volume), but here's a list of a few of the basic methods you can use to convert measurements to floats based on other units:

.to_inch

.to_in

.to_foot

.to_ft

.to_yard

.to_yd

.to_mm

.to_cm

.to_dm

.to_m

.to_sq_inch

.to_sq_in

.to_sq_foot

.to_sq_ft

.to_sq_yard

.to_sq_yd

.to_sq_mm

.to_sq_cm

.to_sq_dm

.to_sq_m

.to_cu_inch

.to_cu_in

.to_cu_foot

.to_cu_ft

.to_cu_yard

.to_cu_yd

.to_cu_mm

.to_cu_cm

.to_cu_dm

.to_cu_m

 

And here's a list of what you can use to convert a float to a measurement...

.inch

.in

.foot

.ft

.yard

.yd

.mm

.cm

.dm

.m

.sq_inch

.sq_in

.sq_foot

.sq_ft

.sq_yard

.sq_yd

.sq_mm

.sq_cm

.sq_dm

.sq_m

.cu_inch

.cu_in

.cu_foot

.cu_ft

.cu_yard

.cu_yd

.cu_mm

.cu_cm

.cu_dm

.cu_m

 

...again, learning exactly how the conversion works is a bit more complicated but if you use .to_s you will get the newly created Measurement and its units. 

 

There are other similar methods as well such as .convert_to (as was mentioned above by Ben) as well as Measurement.new(value, optional unit).

 

In addition Chief also has a built in NumberFormatter functionality that you can use to format various measurements.  It basically works exactly like the Dimension formatting options we have.  I don't have the time or inclination to go into all of it in this post, but it's pretty cool. 

Share this post


Link to post
Share on other sites
15 hours ago, Joe_Carrick said:

I think CA should provide that in their help files or a separate PDF for us.

See "Measurement and NumberFormatter Classes" under "Ruby in Chief Architect" in Help.

Share this post


Link to post
Share on other sites

Well here it is, the links to usefull resources for Ruby in Chief Architect

 

1860803171_RubyhelpinChiefArchitect.thumb.jpg.ad1f2ca336fb65be3844a0c0bd778d78.jpg

Share this post


Link to post
Share on other sites

Notably missing from the Help is an example of using the NumberFormatter class. Basically it has to be created first.  There's no reason to create it for every time you want to format a number - particularly if you are always going to be using the same parameters.

 

Here's the way I create a persistent instance of the class and how it can be used:

  • $NF = NumberFormatter.new
  • $NF.unit = "'-\""
  • $NF.use_fractions = true
  • $NF.denominator = 8

This sets the parameters to what I want.  Then I can use the following whenever I need to format a number:

  • $NF.apply( 123.5.in )   ---> 10' 3-1/2"

Note the inclusion of .in so the formatter knows the value is in inches.  If passing a Measurement value then that wouldn't need to be included.

 

  • Upvote 1

Share this post


Link to post
Share on other sites

I’m curious if there’s a way to access and modify the initial settings in the NumberFormatter class.  

Share this post


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

I’m curious if there’s a way to access and modify the initial settings in the NumberFormatter class.  

Not that I'm aware of.  They are initialized automatically by the .new method.  That's why I set up a global as my NumberFormatter.  You could actually have several globals, each with different settings - or just use 1 and change the parameters on the fly.

  • Upvote 1

Share this post


Link to post
Share on other sites

You should be able to create your own "derived class" with whatever settings you want but I'm not 100% sure it would work since the NumberFormatter class is one that Chief created and it may not be registered.

  • Upvote 1

Share this post


Link to post
Share on other sites

The .inspect method will give you some that information, depending on the units being used.

  • Upvote 1

Share this post


Link to post
Share on other sites

Ya, I know how to use it and I currently have a base macro set up that I can modify and simply copy as necessary for various formatting.  I was just thinking aloud and wondering whether we could access the settings for the class itself or not.  Not a big deal at all.  I might search the program files to see if it’s hidden in there somewhere.  It’s just a personal curiosity really and not too important. 

Share this post


Link to post
Share on other sites

It would be nice if Chief's Help included what the "defaults" are for each parameter.  The only one they mention is:

  • reduce_fractions()

I think that all of the True | False settings are defaulted to False

  • Upvote 1

Share this post


Link to post
Share on other sites
On 3/11/2020 at 9:47 AM, Joe_Carrick said:

It would be nice if Chief's Help included what the "defaults" are for each parameter.  The only one they mention is:

  • reduce_fractions()

I think that all of the True | False settings are defaulted to False

 

These are the apparent OOB settings...

unit = Not set

show_unit = true

show_leading_zero = true

show_trailing_zeros = false

use_fractions = false

decimal_places = 6

denominator = 16

thousands_separator = ,

show_denominator = true

reduce_fractions = gcd (greatest common divisor)

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

    27860
    Total Members
    6254
    Most Online
    Emileen
    Newest Member
    Emileen
    Joined