Python sketch text rotation problem

Python sketch text rotation problem

jeremydde
Contributor Contributor
1,270 Views
4 Replies
Message 1 of 5

Python sketch text rotation problem

jeremydde
Contributor
Contributor

I would like to have the ability to rapidly edit a sketch text object, replace it's text value, and re-align the text to the center of an existing area. I have a number of labels to create, extrude and export to STL for 3D printing. I found the following link in a search which was a great first step.

 

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/script-to-select-sketchtext-and-have-a-dia...

 

Here's the problem: When I created the sketch text in Fusion, it came in upside down and backwards, despite being placed on a solid surface parallel to the x-y plane, with the viewcube correctly indicating "Top". No problem, I used the flip icons within the text window to orient it correctly.

 

When I activate the script, the text does get replaced just as I'd expect, but the sketchText ends up upside down and backwards again. This requires me to edit the sketch anyways simply to re-flip the text. Is there a way to access these flip parameters within the API?

 

So, I decided to rotate the sketch text 180 degrees instead of using the flip icons. Now the script crashes whenever I attempt to set the text value to the new one with an "Invalid input angle" error. This sent me down the rabbit hole where I've spent hours attempting to set this angle myself. The documentation says that the sketchText angle is a double value. It makes no mention if it's in radians, or if negatives are allowed. If I check the angle variable for a sketch text that is rotated -180 degrees, the value is -pi. So it would appear it is in radians. However, I cannot set my own value in radians either. I've tried setting negative and positive values in degrees and radians, and all fail with an "Invalid input angle" error. Is this an API bug?

 

It appears that I can set the text and angle with no problem as long as the text rotation is not negative to start with.

Perhaps a workaround is to simply replace the sketchText with a new one if it is checked and found to have a negative rotation value? I would prefer to figure this problem out rather than attempt to get the text object to always have a positive or zero rotation value, that way I don't have to worry about the script failing in the future on a different scenario/project.

 

I have attached a simple F3D with a single sketch text as well as the sample python code. Most of it is based on the script provided by other members in the link mentioned above. This rotation problem has taken so long I haven't gotten to the center re-alignment part yet.

 

import adsk.core, adsk.fusion, adsk.cam, traceback, math

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        design = app.activeProduct
        rootComp = design.rootComponent
        
        # Visual to prompt user to select a sketch from the Tree
        ui.messageBox('Select Sketch to Change (From Tree)')

        # This allows user to select a sketch
        selection = ui.selectEntity("Select sketch to change", "Sketches")   
        
        # Gets the name of the Sketch
        sketch = selection.entity.name

        # Prompts the user for the new Text   
        (returnValue, cancelled) = ui.inputBox('New Text:', 'Edit Sketch Text', )
        
        # Grab the sketch and first text entity 
        sk = rootComp.sketches.itemByName(sketch) 
        skText = sk.sketchTexts.item(0)
        
        # Get sketchText position and bounding box
        textPosition = skText.position
        textBounds = skText.boundingBox
        textHeight = skText.height
        textAngle = math.degrees(skText.angle)
#        textAngle = math.radians(20.0)
        
        print("Text sketch X: ",textPosition.x)
        print("Text sketch Y: ",textPosition.y)
        print("Text bounds minX: ",textBounds.minPoint.x)
        print("Text bounds minY: ",textBounds.minPoint.y)
        print("Text bounds maxX: ",textBounds.maxPoint.x)
        print("Text bounds maxY: ",textBounds.maxPoint.y)
        print("Bounding box length(x): ",abs(textBounds.maxPoint.x) + abs(textBounds.minPoint.x))
        print("Bounding box length(y): ",abs(textBounds.maxPoint.y) + abs(textBounds.minPoint.y))
        print("Text height: ",textHeight)
        print("Text angle: ",textAngle)

        # Set the angle
        if textAngle == 180 or textAngle == -180:
            skText.angle = 0.0
        else:
            skText.angle = textAngle
            
        # Change the text
        skText.text = returnValue

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes
1,271 Views
4 Replies
Replies (4)
Message 2 of 5

JesusFreke
Advocate
Advocate

Instead of rotating the sketch text, you might consider rotating the sketch itself first.

 

e.g. You could create a subcomponent of the component with the body you want to eventually extrude the labels on, create a sketch in that subcomponent, and then rotate the subcomponent itself to where the text is in the correct orientation upon creation. And then, when you replace the text, it should still be in that same orientation.

 

I believe rotating a subcomponent like that only works if you're in direct edit mode, so you'll either need to enable direct edit mode, or perform the operation within the context of a base feature.

0 Likes
Message 3 of 5

JesusFreke
Advocate
Advocate

Actually, you can just create the sketch in the same component, and rotate the sketch directly. No need for a subcomponent. Although it still requires direct edit mode/a base feature.

0 Likes
Message 4 of 5

JesusFreke
Advocate
Advocate

And nevermind again. Just played with it a bit again and you can directly rotate the subcomponent (but not a sketch -- afaict) in parametric mode.

0 Likes
Message 5 of 5

ebunn3
Advocate
Advocate

I was able to rotate the text object by using SketchTexts.createInput2 Method using the code below to rotate the text.  I tried rotating the sketch text using it's angle property and if would fail.  For whatever reason, setting the angle property using createInput2 worked for me.  Has to be in radians.

 

You can also use the isHorizontalFlip and isVerticalFlip properties to flip the text if need be.

 

Eric

 

# Get the SketchTexts collection object.
texts = sketch.sketchTexts
Text = 'Test Text'
input = texts.createInput2(Text, 1.0)
import math
input.angle = math.radians(90)