From 0d0a396c9359ca42198fe6110209978ae72f6461 Mon Sep 17 00:00:00 2001 From: Davide Bazzi Date: Wed, 28 Feb 2024 09:40:23 +0100 Subject: [PATCH 1/4] Fixed step color import. I round the value before truncating to int, otherwise the color could be wrong by one. For example in file Dies_15.stp, there is a red value of 0.313725490196078, that became 79 (79.999999999999886), instead of 80. --- CADability/ImportStep.cs | 240 +++++++++++++++++++-------------------- 1 file changed, 120 insertions(+), 120 deletions(-) diff --git a/CADability/ImportStep.cs b/CADability/ImportStep.cs index 86862519..86337442 100644 --- a/CADability/ImportStep.cs +++ b/CADability/ImportStep.cs @@ -4350,9 +4350,9 @@ private object CreateEntity(Item item) case Item.ItemType.colourRgb: { string name = item.parameter["name"].sval; - int red = (int)(item.parameter["red"].fval * 255); - int green = (int)(item.parameter["green"].fval * 255); - int blue = (int)(item.parameter["blue"].fval * 255); + int red = (int)Math.Round(item.parameter["red"].fval * 255); + int green = (int)Math.Round(item.parameter["green"].fval * 255); + int blue = (int)Math.Round(item.parameter["blue"].fval * 255); if (string.IsNullOrWhiteSpace(name)) name = "rgb:" + red.ToString() + "_" + green.ToString() + "_" + blue.ToString(); item.val = new ColorDef(name, Color.FromArgb(red, green, blue)); AvoidDuplicateColorNames(item.val as ColorDef); @@ -4370,123 +4370,123 @@ private object CreateEntity(Item item) { string nm = item.SubString(0); object oo = CreateEntity(item.SubItem(1)); - if (oo is ICurve basisCurve) - { - basisCurve = basisCurve.Clone(); - double startParameter = double.MinValue; - double endParameter = double.MinValue; - GeoPoint startPoint = GeoPoint.Invalid; - GeoPoint endPoint = GeoPoint.Invalid; - if (item.SubItem(2).type == Item.ItemType.list) - { - List lst = (item.SubItem(2).val as List); - for (int i = 0; i < lst.Count; i++) - { - object o = CreateEntity(lst[i]); - if (o is List) o = (o as List)[0].val; - if (o is double) startParameter = (double)o; - if (o is GeoPoint) startPoint = (GeoPoint)o; - } - } - if (item.SubItem(3).type == Item.ItemType.list) - { - List lst = (item.SubItem(3).val as List); - for (int i = 0; i < lst.Count; i++) - { - object o = CreateEntity(lst[i]); - if (o is List) o = (o as List)[0].val; - if (o is double) endParameter = (double)o; - if (o is GeoPoint) endPoint = (GeoPoint)o; - } - } - bool sense = item.SubBool(4); - string masterRepresentation = item.SubString(5); - if (startParameter != double.MinValue && masterRepresentation != "CARTESIAN") - { - // trimmed curve defined by start- and endParameter - if (basisCurve is Ellipse) - { - if (context != null) - { - startParameter = context.toRadian * startParameter; - endParameter = context.toRadian * endParameter; - } - } - if (!sense) basisCurve.Reverse(); - if (startParameter != endParameter) basisCurve.Trim(basisCurve.ParameterToPosition(startParameter), basisCurve.ParameterToPosition(endParameter)); - item.val = basisCurve; - } - else if (startPoint.IsValid && endPoint.IsValid) - { - // trimmed curve defined by start- and endpoint - if (!sense) basisCurve.Reverse(); - double startPos = basisCurve.PositionOf(startPoint); - double endPos = basisCurve.PositionOf(endPoint); - basisCurve.Trim(startPos, endPos); - item.val = basisCurve; - } - } - else if (oo is ICurve2D basisCurve2d) - { - basisCurve2d = basisCurve2d.Clone(); - double startParameter = double.MinValue; - double endParameter = double.MinValue; - GeoPoint2D startPoint = GeoPoint2D.Invalid; - GeoPoint2D endPoint = GeoPoint2D.Invalid; - if (item.SubItem(2).type == Item.ItemType.list) - { - List lst = (item.SubItem(2).val as List); - for (int i = 0; i < lst.Count; i++) - { - object o = CreateEntity(lst[i]); - if (o is List) o = (o as List)[0].val; - if (o is double) startParameter = (double)o; - if (o is GeoPoint2D) startPoint = (GeoPoint2D)o; - } - } - if (item.SubItem(3).type == Item.ItemType.list) - { - List lst = (item.SubItem(3).val as List); - for (int i = 0; i < lst.Count; i++) - { - object o = CreateEntity(lst[i]); - if (o is List) o = (o as List)[0].val; - if (o is double) endParameter = (double)o; - if (o is GeoPoint2D) endPoint = (GeoPoint2D)o; - } - } - bool sense = item.SubBool(4); - string masterRepresentation = item.SubString(5); - if (startParameter != double.MinValue && masterRepresentation != "CARTESIAN") - { - // trimmed curve defined by start- and endParameter - if (basisCurve2d is Ellipse2D elli2d) - { - if (context != null) - { - startParameter = context.toRadian * startParameter; - endParameter = context.toRadian * endParameter; - } - elli2d.Trim(startParameter / (2 * Math.PI), endParameter / (2 * Math.PI)); - } - else - { // are all other curves from 0 to 1 ??? there is no "ParameterToPosition" for 2d curves - // basisCurve2d.Trim(basisCurve2d.ParameterToPosition(startParameter), basisCurve.ParameterToPosition(endParameter)); - basisCurve2d.Trim(startParameter, endParameter); - } - if (!sense) basisCurve2d.Reverse(); - item.val = basisCurve2d; - } - else if (startPoint.IsValid && endPoint.IsValid) - { - // trimmed curve defined by start- and endpoint - double startPos = basisCurve2d.PositionOf(startPoint); - double endPos = basisCurve2d.PositionOf(endPoint); - basisCurve2d.Trim(startPos, endPos); - if (!sense) basisCurve2d.Reverse(); - item.val = basisCurve2d; - } - } + if (oo is ICurve basisCurve) + { + basisCurve = basisCurve.Clone(); + double startParameter = double.MinValue; + double endParameter = double.MinValue; + GeoPoint startPoint = GeoPoint.Invalid; + GeoPoint endPoint = GeoPoint.Invalid; + if (item.SubItem(2).type == Item.ItemType.list) + { + List lst = (item.SubItem(2).val as List); + for (int i = 0; i < lst.Count; i++) + { + object o = CreateEntity(lst[i]); + if (o is List) o = (o as List)[0].val; + if (o is double) startParameter = (double)o; + if (o is GeoPoint) startPoint = (GeoPoint)o; + } + } + if (item.SubItem(3).type == Item.ItemType.list) + { + List lst = (item.SubItem(3).val as List); + for (int i = 0; i < lst.Count; i++) + { + object o = CreateEntity(lst[i]); + if (o is List) o = (o as List)[0].val; + if (o is double) endParameter = (double)o; + if (o is GeoPoint) endPoint = (GeoPoint)o; + } + } + bool sense = item.SubBool(4); + string masterRepresentation = item.SubString(5); + if (startParameter != double.MinValue && masterRepresentation != "CARTESIAN") + { + // trimmed curve defined by start- and endParameter + if (basisCurve is Ellipse) + { + if (context != null) + { + startParameter = context.toRadian * startParameter; + endParameter = context.toRadian * endParameter; + } + } + if (!sense) basisCurve.Reverse(); + if (startParameter != endParameter) basisCurve.Trim(basisCurve.ParameterToPosition(startParameter), basisCurve.ParameterToPosition(endParameter)); + item.val = basisCurve; + } + else if (startPoint.IsValid && endPoint.IsValid) + { + // trimmed curve defined by start- and endpoint + if (!sense) basisCurve.Reverse(); + double startPos = basisCurve.PositionOf(startPoint); + double endPos = basisCurve.PositionOf(endPoint); + basisCurve.Trim(startPos, endPos); + item.val = basisCurve; + } + } + else if (oo is ICurve2D basisCurve2d) + { + basisCurve2d = basisCurve2d.Clone(); + double startParameter = double.MinValue; + double endParameter = double.MinValue; + GeoPoint2D startPoint = GeoPoint2D.Invalid; + GeoPoint2D endPoint = GeoPoint2D.Invalid; + if (item.SubItem(2).type == Item.ItemType.list) + { + List lst = (item.SubItem(2).val as List); + for (int i = 0; i < lst.Count; i++) + { + object o = CreateEntity(lst[i]); + if (o is List) o = (o as List)[0].val; + if (o is double) startParameter = (double)o; + if (o is GeoPoint2D) startPoint = (GeoPoint2D)o; + } + } + if (item.SubItem(3).type == Item.ItemType.list) + { + List lst = (item.SubItem(3).val as List); + for (int i = 0; i < lst.Count; i++) + { + object o = CreateEntity(lst[i]); + if (o is List) o = (o as List)[0].val; + if (o is double) endParameter = (double)o; + if (o is GeoPoint2D) endPoint = (GeoPoint2D)o; + } + } + bool sense = item.SubBool(4); + string masterRepresentation = item.SubString(5); + if (startParameter != double.MinValue && masterRepresentation != "CARTESIAN") + { + // trimmed curve defined by start- and endParameter + if (basisCurve2d is Ellipse2D elli2d) + { + if (context != null) + { + startParameter = context.toRadian * startParameter; + endParameter = context.toRadian * endParameter; + } + elli2d.Trim(startParameter / (2 * Math.PI), endParameter / (2 * Math.PI)); + } + else + { // are all other curves from 0 to 1 ??? there is no "ParameterToPosition" for 2d curves + // basisCurve2d.Trim(basisCurve2d.ParameterToPosition(startParameter), basisCurve.ParameterToPosition(endParameter)); + basisCurve2d.Trim(startParameter, endParameter); + } + if (!sense) basisCurve2d.Reverse(); + item.val = basisCurve2d; + } + else if (startPoint.IsValid && endPoint.IsValid) + { + // trimmed curve defined by start- and endpoint + double startPos = basisCurve2d.PositionOf(startPoint); + double endPos = basisCurve2d.PositionOf(endPoint); + basisCurve2d.Trim(startPos, endPos); + if (!sense) basisCurve2d.Reverse(); + item.val = basisCurve2d; + } + } } break; case Item.ItemType.mappedItem: From 65bc025e375c609b8d6347c90a04e5b2481dc26e Mon Sep 17 00:00:00 2001 From: Davide Bazzi Date: Tue, 5 Mar 2024 14:22:56 +0100 Subject: [PATCH 2/4] Fixed crash in BSpline.PointAtParam, plane could have a value also in the case of 3d nurbs; nubs2d and nurbs2d not null should be tested (like in PointDirAtParam). It could append after the call of TrimParam that calls getPlane to make the new spline that will cause the creation of a plane (maybe also arbitrary). --- CADability/BSpline.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CADability/BSpline.cs b/CADability/BSpline.cs index 2ea18234..2e77e814 100644 --- a/CADability/BSpline.cs +++ b/CADability/BSpline.cs @@ -2040,7 +2040,7 @@ public GeoPoint PointAtParam(double param) lock (this) { if (!nurbsHelper) MakeNurbsHelper(); - if (plane.HasValue) + if (plane.HasValue && (nubs2d != null | nurbs2d != null)) { if (nubs2d != null) { @@ -2069,7 +2069,6 @@ private void PointDirAtParam(double param, out GeoPoint point, out GeoVector dir lock (this) { if (!nurbsHelper) MakeNurbsHelper(); - if (plane.HasValue && (nubs2d != null | nurbs2d != null)) { if (nubs2d != null) From 1e4146e025fcb357f09dcd32b1ee7afb3beace7f Mon Sep 17 00:00:00 2001 From: Davide Bazzi Date: Thu, 6 Jun 2024 11:06:41 +0200 Subject: [PATCH 3/4] Points imported from step file didn't have the layer assigned. --- CADability/ImportStep.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/CADability/ImportStep.cs b/CADability/ImportStep.cs index 86337442..5c9a3ab2 100644 --- a/CADability/ImportStep.cs +++ b/CADability/ImportStep.cs @@ -2387,6 +2387,7 @@ private object CreateEntity(Item item) pnt.Symbol = PointSymbol.Cross; pnt.Name = subItem.parameter["name"].sval; val.Add(pnt); + subItem.val = pnt; //For this CartesianPoint a Cadability Point is generated. I set it to the item instead of GeoPoint to make it work layer assignment. } else if (o is IGeoObject) val.Add(o as IGeoObject); else if (o is GeoObjectList) val.AddRange(o as GeoObjectList); From f72b441c0a1fd9c127111e27b460c35e54a5a625 Mon Sep 17 00:00:00 2001 From: Davide Bazzi Date: Fri, 14 Jun 2024 10:14:32 +0200 Subject: [PATCH 4/4] Fixed crash if for some reason there is a shell without faces. In my case it was a file with only shell containing a single face, and some of them are not imported for other problems. --- CADability/Shell.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CADability/Shell.cs b/CADability/Shell.cs index b013d0e8..b3149444 100644 --- a/CADability/Shell.cs +++ b/CADability/Shell.cs @@ -5779,7 +5779,7 @@ public Face[][] FindSameForm(Face[] form, double precision, FindSameFormSearchMo internal static Shell MakeShell(Face[] faces, bool tryToConnectOpenEdges = false) { Shell res = Shell.Construct(); - res.colorDef = faces[0].ColorDef; + if (faces.Length > 0) res.colorDef = faces[0].ColorDef; res.SetFaces(faces); if (tryToConnectOpenEdges) {