Author Topic: .NET GEOMETRY Routines  (Read 63763 times)

0 Members and 1 Guest are viewing this topic.

pkohut

  • Bull Frog
  • Posts: 483
Re: .NET GEOMETRY Routines
« Reply #45 on: February 09, 2022, 02:37:34 AM »
Yes, Thanks Gile!
New tread (not retired) - public repo at https://github.com/pkohut

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: .NET GEOMETRY Routines
« Reply #46 on: November 14, 2023, 02:59:56 AM »
Looking to answer a question on the Autodesk forum, I thought I'd try to do something more generic to get data that can be used to create a Hatch from a Region. I updated the GeometryExtensions library on Github.
Code - C#: [Select]
  1.         /// <summary>
  2.         /// Gets the hatch loops data for the supplied region.
  3.         /// </summary>
  4.         /// <param name="region">The instance to which this method applies.</param>
  5.         /// <returns>A collection of tuples containing the loop data.</returns>
  6.         public static IEnumerable<(HatchLoopTypes, Curve2dCollection, IntegerCollection)> GetHatchLoops(this Region region)
  7.         {
  8.             var plane = new Plane(Point3d.Origin, region.Normal);
  9.  
  10.             using (var brep = new Brep(region))
  11.             {
  12.                 foreach (var complex in brep.Complexes)
  13.                 {
  14.                     foreach (var loop in complex.Shells.First().Faces.First().Loops)
  15.                     {
  16.                         var edgePtrCollection = new Curve2dCollection();
  17.                         var edgeTypeCollection = new IntegerCollection();
  18.                         foreach (var edge in loop.Edges.Select(e => ((ExternalCurve3d)e.Curve).NativeCurve).ToOrderedArray())
  19.                         {
  20.                             switch (edge)
  21.                             {
  22.                                 case LineSegment3d lineSegment3D:
  23.                                     edgePtrCollection.Add(
  24.                                         new LineSegment2d(
  25.                                             lineSegment3D.StartPoint.Convert2d(plane),
  26.                                             lineSegment3D.EndPoint.Convert2d(plane)));
  27.                                     edgeTypeCollection.Add(1);
  28.                                     break;
  29.                                 case CircularArc3d circularArc3D:
  30.                                     edgePtrCollection.Add(
  31.                                         new CircularArc2d(
  32.                                             circularArc3D.Center.Convert2d(plane),
  33.                                             circularArc3D.Radius,
  34.                                             circularArc3D.StartAngle,
  35.                                             circularArc3D.EndAngle,
  36.                                             circularArc3D.ReferenceVector.Convert2d(plane),
  37.                                             false));
  38.                                     edgeTypeCollection.Add(2);
  39.                                     break;
  40.                                 case EllipticalArc3d ellipticalArc3D:
  41.                                     edgePtrCollection.Add(
  42.                                         new EllipticalArc2d(
  43.                                             ellipticalArc3D.Center.Convert2d(plane),
  44.                                             ellipticalArc3D.MajorAxis.Convert2d(plane),
  45.                                             ellipticalArc3D.MinorAxis.Convert2d(plane),
  46.                                             ellipticalArc3D.MajorRadius,
  47.                                             ellipticalArc3D.MinorRadius,
  48.                                             ellipticalArc3D.StartAngle,
  49.                                             ellipticalArc3D.EndAngle));
  50.                                     edgeTypeCollection.Add(3);
  51.                                     break;
  52.                                 case NurbCurve3d nurbCurve3D:
  53.                                     var ctrlPts = new Point2dCollection();
  54.                                     for (int i = 0; i < nurbCurve3D.NumberOfControlPoints; i++)
  55.                                     {
  56.                                         ctrlPts.Add(nurbCurve3D.ControlPointAt(i).Convert2d(plane));
  57.                                     }
  58.                                     edgePtrCollection.Add(
  59.                                         new NurbCurve2d(
  60.                                             nurbCurve3D.Degree,
  61.                                             nurbCurve3D.Knots,
  62.                                             ctrlPts,
  63.                                             nurbCurve3D.IsPeriodic(out double _)));
  64.                                     edgeTypeCollection.Add(4);
  65.                                     break;
  66.                                 default:
  67.                                     break;
  68.                             }
  69.                         }
  70.                         if (loop.LoopType == LoopType.LoopExterior)
  71.                             yield return (HatchLoopTypes.External, edgePtrCollection, edgeTypeCollection);
  72.                         else
  73.                             yield return (HatchLoopTypes.Default, edgePtrCollection, edgeTypeCollection);
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.  
Speaking English as a French Frog

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: .NET GEOMETRY Routines
« Reply #47 on: December 06, 2023, 07:23:19 PM »
Thanks Gilles for this work, it has come in handy many times :)

I've forked your project to update the files and solution to build for both AutoCAD and BricsCAD

https://github.com/MickDuprez/GeometryExtensions/tree/master

Let me know if you want me to make a pull request if you would like to merge the updates.

cheers,
Mick
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: .NET GEOMETRY Routines
« Reply #48 on: December 07, 2023, 04:19:48 AM »
Hi Mick,

I think it's a good idea but a very next update is going to be compatibility with AutoCAD 2025 and .NET 8 which I'm thinking of doing in another project (from the same solution) to be able to use new C# 12 features without having to multiply conditional compilation expressions in the code.
I don't know about the Bricscad API (which I use very occasionally) and .NET 8.
Speaking English as a French Frog

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: .NET GEOMETRY Routines
« Reply #49 on: December 07, 2023, 05:06:57 AM »
Not a problem Gilles, and thanks again for all of your great work!
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: .NET GEOMETRY Routines
« Reply #50 on: December 07, 2023, 06:53:10 AM »
We can talk more about this after the update.
Speaking English as a French Frog