Use of Dimension Options (Python)

Use of Dimension Options (Python)
  1# this script demonstrates the use of the new DimensionOptions, DimensionDistanceFormat and DimensionAngleFormat objects
  2# along with their use in the new dimensioning methods
  3
  4if impact.active_drawing.isNone():
  5    impact.gui.output_toolbox.add('Unable to continue: no active drawing')
  6else:
  7
  8    # Create the DimensionOptions
  9    DimensionOptions = impact.creator.dimension_options()
 10
 11    # We can also create our own formats for display distances and angles
 12    PrimaryDistanceFormat = DimensionOptions.create_dimension_distance_format()
 13    PrimaryAngleFormat = DimensionOptions.create_dimension_angle_format()
 14
 15    # We can then set their values manually
 16    PrimaryDistanceFormat.units = ipDistanceFormat.dfMillimetres
 17    PrimaryDistanceFormat.suffix = "mm"
 18    PrimaryDistanceFormat.decimal_places = 2
 19    PrimaryDistanceFormat.remove_trailing_zeros = True
 20    PrimaryDistanceFormat.display_fractions = False
 21    PrimaryDistanceFormat.reduce_denominator = False
 22    PrimaryDistanceFormat.denominator = 64
 23
 24    PrimaryAngleFormat.units = ipAngleFormat.afDegrees
 25    PrimaryAngleFormat.suffix = "deg"
 26    PrimaryAngleFormat.decimal_places = 1
 27    PrimaryAngleFormat.remove_trailing_zeros = True
 28    PrimaryAngleFormat.display_fractions = False
 29    PrimaryAngleFormat.reduce_denominator = False
 30    PrimaryAngleFormat.denominator = 64
 31
 32    # Then set the formats in the DimensionOptions
 33    DimensionOptions.primary_distance_format = PrimaryDistanceFormat
 34    DimensionOptions.primary_angle_format = PrimaryAngleFormat
 35
 36    # Get a text style
 37    textStyleSheetName = "Palette Default"
 38    textStyleSheet = None
 39    try:
 40        textStyleSheet = impact.active_drawing.text_style_sheets.item( textStyleSheetName )
 41    except Exception as exc:
 42        pass
 43    impact.gui.output_toolbox.add(f"Failed to create object via impact.active_drawing.text_style_sheets.item(): {exc}")
 44
 45    DimensionOptions.text_style = textStyleSheet.text_style
 46
 47    # Setup the rest of DimensionOptions
 48    DimensionOptions.add_lines = True
 49    DimensionOptions.terminator_style = ipTerminatorStyle.tsClosedFilledArrow
 50    DimensionOptions.terminator_size = 2.0
 51    DimensionOptions.leader_length = 2.0
 52    DimensionOptions.show_radial_lines = True
 53    DimensionOptions.parallel_offset = 10.0
 54    DimensionOptions.dimension_gap = 10.0
 55    DimensionOptions.projection_ascent = 10.0
 56    DimensionOptions.force_palette_colour = False
 57    DimensionOptions.format_specifier = "%1 (%2)"
 58    DimensionOptions.dim_text_parallel = False
 59    DimensionOptions.text_position = ipDimensionTextPosition.dtAboveLine
 60    DimensionOptions.radius_prefix = "Radius"
 61    DimensionOptions.diameter_prefix = "Diameter"
 62    DimensionOptions.display_tolerances = True
 63    DimensionOptions.display_tolerances_plus_minus = True
 64    DimensionOptions.tolerance_upper_limit = 2.0
 65    DimensionOptions.tolerance_lower_limit = 2.0
 66    DimensionOptions.auto_size = False
 67
 68    # Instead of that you can apply a master tool setting instead
 69    db = impact.active_database
 70    ms = db.find_master_tool_setting( "Standard Dimensions", ipMasterSettingType.mstDimension )
 71    if not ms.isNone():
 72        impact.gui.output_toolbox.add( "Applying MTS" )
 73        DimensionOptions.apply_master_setting( ms )
 74
 75    # DimensionHorizontal2 as an example
 76    # The new dimensioning methods take the same arguments as the old ones
 77    # plus the addition of the new DimensionOptions object
 78
 79    # create a local variable for the active block
 80    ab = impact.active_drawing.active_layer.active_block
 81
 82    # create an array of points to dimension
 83    Points = [None] * 4
 84    Points[0] = impact.creator.vector(0,0)
 85    Points[1] = impact.creator.vector(21,0)
 86    Points[2] = impact.creator.vector(40,0)
 87    Points[3] = impact.creator.vector(60,0)
 88
 89    # create some local variables to store the dimensions returned,
 90    # projection length, projection side, etc.
 91    Dimensions = None
 92    ProjectionLength = 10
 93    ProjectionSide = ipProjectionSide.psLeft
 94    DimensionMode = ipDimensionMode.dmSingle
 95
 96    # create the horizontal dimensions
 97    Dimensions = ab.dimension_horizontal2( Points, ProjectionLength, ProjectionSide, DimensionMode, DimensionOptions )
 98
 99    # check that some dimensions were returned
100    if isinstance(Dimensions):
101        for dimEnt in Dimensions:
102            impact.gui.output_toolbox.add("Dimension value is " + dimEnt.expression_value)