Author Topic: acedTrans to .NET  (Read 4509 times)

0 Members and 1 Guest are viewing this topic.

gile

  • Water Moccasin
  • Posts: 2204
  • Marseille, France
Re: acedTrans to .NET
« Reply #15 on: January 26, 2019, 03:35:17 PM »
This topic is quite old, but here's my new attempt for a Trans method working as the LISP one (it accepts either integer, ObjectId or Vector3d as argument).
The extension method uses some C# 7 functional programming features (local function and pattern matching).

Code - C#: [Select]
  1.    static class Extension
  2.    {
  3.        [DllImport("accore.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "acedTrans")]
  4.        static extern int acedTrans(double[] point, IntPtr fromRb, IntPtr toRb, int disp, double[] result);
  5.  
  6.        public static Point3d Trans(this Point3d source, object from, object to, bool disp = false)
  7.        {
  8.            ResultBuffer toResBuf(object obj)
  9.            {
  10.                switch (obj)
  11.                {
  12.                    case int i:
  13.                        return new ResultBuffer(new TypedValue(5003, i));
  14.                    case ObjectId id:
  15.                        return new ResultBuffer(new TypedValue(5006, id));
  16.                    case Vector3d v:
  17.                        return new ResultBuffer(new TypedValue(5009, new Point3d(v.X, v.Y, v.Z)));
  18.                    default:
  19.                        throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.InvalidInput);
  20.                }
  21.            }
  22.            double[] result = new double[3];
  23.            int status = acedTrans(source.ToArray(), toResBuf(from).UnmanagedObject, toResBuf(to).UnmanagedObject, disp ? 1 : 0, result);
  24.            if (status == 5100)
  25.                return new Point3d(result);
  26.            else
  27.                throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.InvalidInput);
  28.        }
  29.    }
Speaking English as a French Frog

kdub

  • Mesozoic relic
  • SuperMod
  • Swamp Rat
  • Posts: 1267
  • class keyThumper<T>:ILazy<T>
Re: acedTrans to .NET
« Reply #16 on: January 26, 2019, 04:03:54 PM »

Nice update Gilles.
called Kerry in my other life

Sometimes the question is more important than the answer.