I'm having trouble getting a Paintable Canvas to work perfectly.
I've got a template with a Paintable Canvas that's drawing a polygon to represent the shape of a part, based on information stored in a dataset tag. Everything works, except that when I update the dataset tag, the Paintable Canvas doesn't refresh as I'd like it to. It'll partially draw the new shape, but the old one is still there. It seems like it needs to be cleared or refreshed in some way. I have a Numeric Entry field on the template as well, and if I click in there the Paintable Canvas updates like it should. But obviously I want it to repaint and refresh or whatever it has to do, without having to click somewhere else.
Here are screenshots to show what's happening. The first image shows what it looks like after I've clicked the "Flip" button (which swaps data points in the dataset tag to flip the part vertically):
And here's what the Paintable Canvas should be showing:
Here's my code:
from java.awt import Color
from java.awt.geom import Path2D
# Generate the polygon points
sorted_point_list = generatePartPoints.calculate(
event.source.parent.Nominal_Width,
event.source.parent.Saw1_EN, event.source.parent.Saw2_EN, event.source.parent.Saw3_EN, event.source.parent.Saw4_EN, event.source.parent.Saw5_EN,
event.source.parent.Saw1_Angle, event.source.parent.Saw2_Angle, event.source.parent.Saw3_Angle, event.source.parent.Saw4_Angle, event.source.parent.Saw5_Angle,
event.source.parent.Saw1_CL, event.source.parent.Saw2_CL, event.source.parent.Saw3_CL, event.source.parent.Saw4_CL, event.source.parent.Saw5_CL,
event.height-2, event.width-2
)
# Initialize max variables
max_y = float('-inf')
max_x = float('-inf')
# Iterate through the points to find the maximum X & Y coordinates
for point in sorted_point_list:
max_y = max(max_y, point[1])
max_x = max(max_x, point[0])
# Flip the points so that 0,0 is in the top left corner
sorted_point_list = [[point[0], max_y - point[1]] for point in sorted_point_list]
# Create a Path2D object to represent the polygon
polygon_path = Path2D.Double()
polygon_path.moveTo(sorted_point_list[0][0], sorted_point_list[0][1])
# Iterate through the points and add them to the path
for point in sorted_point_list[1:]:
polygon_path.lineTo(point[0], point[1])
# Close the polygon
polygon_path.closePath()
# Create a graphics object
graphics = event.graphics
# Set the color for the polygon outline
outline_color = Color(0, 0, 0)
graphics.setColor(outline_color)
# Draw the polygon
graphics.draw(polygon_path)
# Dispose of the graphics object to free up resources
graphics.dispose()