Created
November 28, 2018 16:38
-
-
Save pgolay/07a33ace8a534004c581fff997ba4d14 to your computer and use it in GitHub Desktop.
Set secondary DimArea units
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rhinoscriptsyntax as rs | |
import scriptcontext as sc | |
import Rhino | |
import System.Enum as enum | |
def AreaUnits(): | |
crntUnits = sc.doc.ModelUnitSystem | |
unitsList = enum.GetValues(Rhino.UnitSystem) | |
units = [item.ToString() for item in unitsList][1:10] | |
def filter_dim_area(rhino_object, geometry, component_index ): | |
if geometry.TextFormula.startswith("%<Area"): | |
return True | |
return False | |
while True: | |
unitIdx = 1 | |
if sc.sticky.has_key('UNIT_IDX'): | |
unitIdx = sc.sticky['UNIT_IDX'] | |
go = Rhino.Input.Custom.GetObject() | |
intPrecision = 2 | |
if sc.sticky.has_key('AREA_PRECISION'): | |
intPrecision = sc.sticky['AREA_PRECISION'] | |
go.GeometryFilter = Rhino.DocObjects.ObjectType.Annotation | |
go.SetCommandPrompt("Select a DimArea") | |
go.SetCustomGeometryFilter(filter_dim_area) | |
opUnits = go.AddOptionList("Units", units, unitIdx) | |
opPrecision = Rhino.Input.Custom.OptionInteger(intPrecision) | |
go.AddOptionInteger("Precision", opPrecision) | |
rc = go.Get() | |
if go.CommandResult()!=Rhino.Commands.Result.Success: | |
return go.CommandResult() | |
if rc==Rhino.Input.GetResult.Object: | |
id = go.Object(0).ObjectId | |
break | |
elif rc==Rhino.Input.GetResult.Option: | |
idx = go.OptionIndex() | |
if idx == 1: | |
unitIdx = go.Option().CurrentListOptionIndex | |
sc.sticky['UNIT_IDX'] = unitIdx | |
continue | |
else: | |
intPrecision = opPrecision.CurrentValue | |
sc.sticky['AREA_PRECISION']= intPrecision | |
continue | |
geo = rs.coercegeometry(id) | |
t = geo.PlainText.split() | |
value = float(t[0]) | |
f = geo.TextFormula | |
targUnits = unitsList[unitIdx+1] | |
factor = Rhino.RhinoMath.UnitScale(crntUnits, targUnits)**2 | |
outputUnits = Rhino.UI.Localization.UnitSystemName(targUnits, False, True, True) | |
indices = [i for i, a in enumerate(f) if a == '%'] | |
strFormat = "{:." + str(intPrecision) + "f}" | |
f = f[indices[0]:indices[1]+1] | |
newF = f + " (" + strFormat.format(value*factor) + ' ' +outputUnits + '2' ')' | |
geo.TextFormula = newF | |
sc.doc.Objects.Replace(id, geo) | |
pass | |
if __name__ == '__main__':AreaUnits() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment