Author Topic: NET P/INVOKE Routines  (Read 35083 times)

0 Members and 1 Guest are viewing this topic.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: NET P/INVOKE Routines
« Reply #30 on: March 08, 2019, 11:22:41 AM »
Mimic the 'textbox' LISP function by P/Invoking acedTextBox but avoiding to P/Invoke acdbGetAdsName32/64 and acdbEntGet by building the ResultBuffer from the DBText properties.
The minimum required data is DBText.TextString, the default for other data is got from the Database current settings (TestSize, TextStyle).

Code - C#: [Select]
  1.         [DllImport("accore.dll", CharSet = CharSet.Unicode,
  2.             CallingConvention = CallingConvention.Cdecl, EntryPoint = "acedTextBox")]
  3.         static extern System.IntPtr acedTextBox(IntPtr rb, double[] point1, double[] point2);
  4.  
  5.         /// <summary>
  6.         /// Gets the extents of a DBText (mimics the 'textbox' LISP function)
  7.         /// </summary>
  8.         /// <param name="rb">DXF data decribing the DBText</param>
  9.         /// <returns>The diagonal of the non rotated text with insertion point at (0, 0, 0).</returns>
  10.         public static Extents3d TextBox(ResultBuffer rb)
  11.         {
  12.             var point1 = new double[3];
  13.             var point2 = new double[3];
  14.             acedTextBox(rb.UnmanagedObject, point1, point2);
  15.             return new Extents3d(new Point3d(point1), new Point3d(point2));
  16.         }
  17.  
  18.         /// <summary>
  19.         /// Gets the WCS points of the text bounding box.
  20.         /// </summary>
  21.         /// <param name="dbText">Instance of DBText the method applies to.</param>
  22.         /// <returns>The bounding box points in counterclockwise sense.</returns>
  23.         public static Point3d[] GetTextBoxCorners(this DBText dbText)
  24.         {
  25.             if (dbText == null)
  26.                 throw new ArgumentNullException("dbText");
  27.  
  28.             int mirrored = dbText.IsMirroredInX ? 2 : 0;
  29.             mirrored |= dbText.IsMirroredInY ? 4 : 0;
  30.             var rb = new ResultBuffer(
  31.                     new TypedValue(1, dbText.TextString),
  32.                     new TypedValue(40, dbText.Height),
  33.                     new TypedValue(41, dbText.WidthFactor),
  34.                     new TypedValue(51, dbText.Oblique),
  35.                     new TypedValue(7, dbText.TextStyleName),
  36.                     new TypedValue(71, mirrored),
  37.                     new TypedValue(72, (int)dbText.HorizontalMode),
  38.                     new TypedValue(73, (int)dbText.VerticalMode));
  39.  
  40.             var extents = TextBox(rb);
  41.             var xform =
  42.                 Matrix3d.Displacement(dbText.Position.GetAsVector()) *
  43.                 Matrix3d.Rotation(dbText.Rotation, dbText.Normal, Point3d.Origin) *
  44.                 Matrix3d.WorldToPlane(new Plane(Point3d.Origin, dbText.Normal));
  45.  
  46.             return new[]
  47.             {
  48.                 extents.MinPoint.TransformBy(xform),
  49.                 new Point3d(extents.MaxPoint.X, extents.MinPoint.Y, 0.0).TransformBy(xform),
  50.                 extents.MaxPoint.TransformBy(xform),
  51.                 new Point3d(extents.MinPoint.X, extents.MaxPoint.Y, 0.0).TransformBy(xform)
  52.             };
  53.         }
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: NET P/INVOKE Routines
« Reply #31 on: November 14, 2023, 04:07:42 AM »
Mimic the 'entget' AutoLISP function.
Code - C#: [Select]
  1.         // Replace the DLL name according to the AutoCAD targeted version
  2.         // 2013-2014:   "acdb19.dll"
  3.         // 2015-2016:   "acdb20.dll"
  4.         // 2017:        "acdb21.dll"
  5.         // 2018:        "acdb22.dll"
  6.         // 2019-2020:   "acdb23.dll"
  7.         // 2021-2024:   "acdb24.dll"
  8.         // Replace the EntryPoint according to AutoCAD plateform
  9.         // 32 bits:     "?acdbGetAdsName@@YA?AW4ErrorStatus@Acad@@AAY01JVAcDbObjectId@@@Z"
  10.         // 64 bits:     "?acdbGetAdsName@@YA?AW4ErrorStatus@Acad@@AEAY01_JVAcDbObjectId@@@Z"
  11.         [System.Security.SuppressUnmanagedCodeSecurity]
  12.         [DllImport("acdb24.dll", CallingConvention = CallingConvention.Cdecl,
  13.             EntryPoint = "?acdbGetAdsName@@YA?AW4ErrorStatus@Acad@@AEAY01_JVAcDbObjectId@@@Z")]
  14.         static extern AcRx.ErrorStatus acdbGetAdsName(out AdsName ename, ObjectId id);
  15.  
  16.         [System.Security.SuppressUnmanagedCodeSecurity]
  17.         [DllImport("accore.dll", CallingConvention = CallingConvention.Cdecl,
  18.             EntryPoint = "acdbEntGet")]
  19.         static extern IntPtr acdbEntGet(AdsName ename);
  20.  
  21.         public static ResultBuffer EntGet(ObjectId id)
  22.         {
  23.             var errorStatus = acdbGetAdsName(out AdsName ename, id);
  24.             if (errorStatus != AcRx.ErrorStatus.OK)
  25.                 throw new AcRx.Exception(errorStatus);
  26.             var result = acdbEntGet(ename);
  27.             if (result != IntPtr.Zero)
  28.                 return ResultBuffer.Create(result, true);
  29.             return null;
  30.         }
Speaking English as a French Frog