Python sketch text rotation problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
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()))