Skip to content

Commit

Permalink
Dogukan/cnx 790 version independent plugin icon (#386)
Browse files Browse the repository at this point in the history
* adds decimal agnostic grid conversion

* fixes versioning in icon

* formatting

* adds notes

---------

Co-authored-by: Oğuzhan Koral <45078678+oguzhankoral@users.noreply.github.com>
  • Loading branch information
dogukankaratas and oguzhankoral authored Nov 17, 2024
1 parent a5ff2ca commit b1ef5c7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ public SpeckleTeklaPanelHost()
this.Text = "Speckle (Beta)";
this.Name = "Speckle (Beta)";

// CNX-790: Needs to be solved
string version = GetVersion().ToString()[1..]; // removes the 'v' from version
string resourcePath = $"Speckle.Connector.Tekla{version}.Resources.et_element_Speckle.bmp";
using (
Bitmap bmp = new Bitmap(
GetType().Assembly.GetManifestResourceStream(resourcePath)
?? throw new InvalidOperationException($"Could not find resource: {resourcePath}")
)
)
string assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
string resourcePath = $"{assemblyName}.Resources.et_element_Speckle.bmp";
using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath))
{
if (stream == null)
{
throw new InvalidOperationException($"Could not find resource: {resourcePath}");
}

using var bmp = new Bitmap(stream);
this.Icon = Icon.FromHandle(bmp.GetHicon());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Speckle.Converters.Common;
using System.Globalization;
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Sdk.Models;

Expand All @@ -18,13 +19,23 @@ public GridToSpeckleConverter(
_lineConverter = lineConverter;
}

// this function is to check system global seperator
// helps us to avoid conflicts between "," and "."
private double GetScaleFactor(TG.CoordinateSystem coordinateSystem)
{
return coordinateSystem.AxisX.X / 1000.0;
}

private IEnumerable<double> ParseCoordinateString(string coordinateString)
{
if (string.IsNullOrEmpty(coordinateString))
{
yield break;
}

var numberStyles = NumberStyles.Float;
var culture = CultureInfo.InvariantCulture;

var parts = coordinateString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
double lastValue = 0;

Expand All @@ -35,8 +46,8 @@ private IEnumerable<double> ParseCoordinateString(string coordinateString)
var repetitionParts = part.Split(new[] { '*' }, StringSplitOptions.RemoveEmptyEntries);
if (
repetitionParts.Length == 2
&& int.TryParse(repetitionParts[0], out int count)
&& double.TryParse(repetitionParts[1], out double increment)
&& int.TryParse(repetitionParts[0], numberStyles, culture, out int count)
&& double.TryParse(repetitionParts[1], numberStyles, culture, out double increment)
)
{
double baseValue = lastValue;
Expand All @@ -50,7 +61,7 @@ private IEnumerable<double> ParseCoordinateString(string coordinateString)
}
else
{
if (double.TryParse(part, out double value))
if (double.TryParse(part, numberStyles, culture, out double value))
{
yield return value;
lastValue = value;
Expand All @@ -67,30 +78,34 @@ public IEnumerable<Base> Convert(TSM.Grid target)
yield break;
}

var xCoordinates = ParseCoordinateString(target.CoordinateX).ToList();
var yCoordinates = ParseCoordinateString(target.CoordinateY).ToList();
var scale = GetScaleFactor(coordinateSystem);

var xCoordinates = ParseCoordinateString(target.CoordinateX).Select(x => x / scale).ToList();
var yCoordinates = ParseCoordinateString(target.CoordinateY).Select(y => y / scale).ToList();

double minX = xCoordinates.Min();
double maxX = xCoordinates.Max();
double minY = yCoordinates.Min();
double maxY = yCoordinates.Max();

double extendedMinX = minX - target.ExtensionLeftX;
double extendedMaxX = maxX + target.ExtensionRightX;
double extendedMinY = minY - target.ExtensionLeftY;
double extendedMaxY = maxY + target.ExtensionRightY;
double extendedMinX = minX - (target.ExtensionLeftX / scale);
double extendedMaxX = maxX + (target.ExtensionRightX / scale);
double extendedMinY = minY - (target.ExtensionLeftY / scale);
double extendedMaxY = maxY + (target.ExtensionRightY / scale);

double scaledZ = coordinateSystem.Origin.Z / scale;

foreach (var x in xCoordinates)
{
var startPoint = new TG.Point(x, extendedMinY, coordinateSystem.Origin.Z);
var endPoint = new TG.Point(x, extendedMaxY, coordinateSystem.Origin.Z);
var startPoint = new TG.Point(x, extendedMinY, scaledZ);
var endPoint = new TG.Point(x, extendedMaxY, scaledZ);
yield return _lineConverter.Convert(new TG.LineSegment(startPoint, endPoint));
}

foreach (var y in yCoordinates)
{
var startPoint = new TG.Point(extendedMinX, y, coordinateSystem.Origin.Z);
var endPoint = new TG.Point(extendedMaxX, y, coordinateSystem.Origin.Z);
var startPoint = new TG.Point(extendedMinX, y, scaledZ);
var endPoint = new TG.Point(extendedMaxX, y, scaledZ);
yield return _lineConverter.Convert(new TG.LineSegment(startPoint, endPoint));
}
}
Expand Down

0 comments on commit b1ef5c7

Please sign in to comment.