Message 1 of 6
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
In the following script, I'm trying to create text in numerical order. I have two issues.
1. the text height is not accurate, if I enter .06 I get .0236.
2. the text location is not accurate. It comes out like this:
as opposed to this:
Any help would be appreciated.
import adsk.core, adsk.fusion, adsk.cam, traceback
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
design = app.activeProduct
rootComp = design.rootComponent
# Create a new sketch on the selected face
def create_sketch(face, name):
sketches = rootComp.sketches
sketch = sketches.add(face)
sketch.name = name
return sketch
# Create text on the sketch
def create_text(sketch, text, height, position, angle, flip):
textInput = sketch.sketchTexts.createInput(str(text), height, position)
textInput.angle = angle
textInput.fontName = 'Romans.shx'
textInput.isHorizontalFlip = flip
sketchText = sketch.sketchTexts.add(textInput)
return sketchText
# Get user inputs
range_input = ui.inputBox('Enter the range of numbers (e.g., 1-10):', 'Range Input', '1-10')
if isinstance(range_input, list):
range_input = range_input[0]
start, end = map(int, range_input.split('-'))
numbers = range(start, end + 1)
height_input = ui.inputBox('Enter the height:', 'Height Input', '0.060')
height = float(height_input[0]) if isinstance(height_input, list) else float(height_input)
angle_input = ui.inputBox('Enter the orientation angle (in degrees):', 'Orientation Input', '0.0')
angle = float(angle_input[0]) if isinstance(angle_input, list) else float(angle_input)
flip = ui.messageBox('Flip the text horizontally?', 'Flip Input', adsk.core.MessageBoxButtonTypes.YesNoButtonType) == adsk.core.DialogResults.DialogYes
# Select the face
face_selection = ui.selectEntity('Select the face to place the numbers on:', 'Faces')
if not face_selection:
ui.messageBox('No face selected. Please select a face to place the numbers on.')
return
face = face_selection.entity
# Select the joint origin to place the sketches
joint_origin_selection = ui.selectEntity('Select the joint origin to place the numbers:', 'JointOrigins')
if not joint_origin_selection:
ui.messageBox('No joint origin selected. Please select a joint origin to place the numbers.')
return
joint_origin = joint_origin_selection.entity
base_position = joint_origin.geometry.origin
# Create separate sketches and add text
for number in numbers:
sketch = create_sketch(face, str(number))
position = adsk.core.Point3D.create(base_position.x, base_position.y, base_position.z)
sketchText = create_text(sketch, number, height, position, angle, flip)
# Calculate the width and height of the text
textWidth = sketchText.boundingBox.maxPoint.x - sketchText.boundingBox.minPoint.x
textHeight = sketchText.boundingBox.maxPoint.y - sketchText.boundingBox.minPoint.y
# Adjust the position to center the text
centered_position = adsk.core.Point3D.create(base_position.x - textWidth / 2, base_position.y - height / 2, base_position.z)
sketchText.position = centered_position
ui.messageBox('Script completed successfully.')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Solved! Go to Solution.