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

0 Members and 1 Guest are viewing this topic.

gile

  • Water Moccasin
  • Posts: 2191
  • 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