Autodesk / PowerShapeAndPowerMillAPI

An API for Autodesk PowerShape and PowerMill
MIT License
31 stars 39 forks source link

With coordinates near zero, scientific notation is used but PS doesn't understand it #38

Closed mdelanno closed 2 years ago

mdelanno commented 2 years ago

Some code to reproduce:

var automation = new PSAutomation(InstanceReuse.UseExistingInstance);
var line = automation.ActiveModel.Lines.CreateLine(new Point(0, 0, 0), new Point(100, 0.00001, 0));
Console.WriteLine($"{line.StartPoint.X}, {line.StartPoint.Y}, {line.StartPoint.Z}"); // Print 0,0,0
Console.WriteLine($"{line.EndPoint.X}, {line.EndPoint.Y:F5}, {line.EndPoint.Z}"); // 100, 1.00000, 0 !!

The CreateLine() call is translated as this:

CREATE LINE SINGLE
ABS 0 0 0
ABS 100 1E-5 0

PowerSHAPE interprets the 1E-5 as 1 because the E is in upper case. The end point is set to 100, 1, 0. If the call was:

CREATE LINE SINGLE
ABS 0 0 0
ABS 100 1e-5 0

It will work as expected (end point at 100, 0.00001, 0) because PowerSHAPE will translate 1e-5 to 0.00001

I just found this bug on one of my client projects. If it is confirmed, it is very serious because it can lead to a lot of calculation errors.

I have not tested it, but perhaps the problem can occur with very large values as well.

mdelanno commented 2 years ago

This bug affects all methods that use real numbers, points, vectors...

lukeedw commented 2 years ago

I've just created v1.1.59 pre-release which should go a long way to resolving this issue. Let me know your feedback.

mdelanno commented 2 years ago

Thank you for your quick response.

The class Point should be updated as well because for example, when the PSWorkplane constructor is called, there is a command which is issued like this:

_powerSHAPE.DoCommand("CREATE WORKPLANE SINGLE " + inputWorkplane.Origin);

Point.ToString() is called, the x, y and z coordinates are formatted and the scientific notation can appear there.

And don't you think that using only 5 decimal places will result in a loss of precision? I would have used .ToString("G17").ToLower() instead. G17 is the roundtrip format recommended by MS and the ToLower() ensure that the E is in lower case, which is correctly interpreted by PowerSHAPE.

lukeedw commented 2 years ago

I believe PowerShape uses 6 decimal places for values. So I used a ToString("0.######") function to provide it with the amount of precision it will take.