newer 发表于 2021-1-31 03:00:42

C#下是否有等同fromAcDbTextStyle()的实现方法

Equivalent to fromAcDbTextStyle() under C# (.NET)

Issue
Is there any way to use the fromAcDbTextStyle() function in my .NET application?

解答:
One workaround for your issue is to use standard .NET P/Invoke technique. The sample below shows code for importing “fromAcDbTextStyle” in .NET.

    [DllImport("acdb18.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl,
    EntryPoint = ?fromAcDbTextStyle@@YA?AW4ErrorStatus@Acad@@AAVAcGiTextStyle@@PB_W@Z)]
    private static extern ErrorStatus fromAcDbTextStyle(System.IntPtr style, string styleName);
   
    public void getTextStrWidth()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Database db = doc.Database;
      Editor ed = doc.Editor;
      PromptResult pr = ed.GetString("\nEnter a string: ");
      if (pr.Status != PromptStatus.OK) return;
      using (Transaction Tx = db.TransactionManager.StartTransaction())
      {
      TextStyleTable TxtStlTbl = Tx.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
      string styleName = "STANDARD";
      if (!TxtStlTbl.Has(styleName))
      {
          ed.WriteMessage("\nStyle " + styleName + " doesn't exist :(");
          return;
      }
      Autodesk.AutoCAD.GraphicsInterface.TextStyle iStyle = new Autodesk.AutoCAD.GraphicsInterface.TextStyle();
      if (fromAcDbTextStyle(iStyle.UnmanagedObject, styleName) == ErrorStatus.OK)
      {
          Extents2d ex = iStyle.ExtentsBox(pr.StringResult, true, true, null);
          double width = ex.MaxPoint.X - ex.MinPoint.X;
          double height = ex.MaxPoint.Y - ex.MinPoint.Y;
          ed.WriteMessage("\n-Width = " + width.ToString() + "\n-Height = " + height.ToString());
      }
      }
    }


页: [1]
查看完整版本: C#下是否有等同fromAcDbTextStyle()的实现方法