问题描述: 

在.NET开发过程中经常会需要用到一些重要的接口,这些接口并未提供.NET接口,只有C++接口,本文介绍罗列一些C#调用C++接口的方法。


步骤指引:

[DllImport("zwcad.exe", EntryPoint = "zcedCommand")]
 private static extern int acedCommand(IntPtr vlist);
[DllImport("zwcad.exe", EntryPoint = "zcedCmd", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
private static extern int zcedCmd(System.IntPtr vlist);
[CommandMethod("Test")]
static public void HelloCS()
{
  ResultBuffer rb2 = new ResultBuffer
   {
      new TypedValue((int)LispDataType.Text, "layer"),
      new TypedValue((int)LispDataType.Text, "m"),
      new TypedValue((int)LispDataType.Text, "net7"),
      new TypedValue((int)LispDataType.Text, "")
     };
     zcedCmd(rb2.UnmanagedObject);
}
[DllImport("ZwDatabase.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?zcdbGetZdsName@@YA?AW4ErrorStatus@Zcad@@AEAY01_JVZcDbObjectId@@@Z")]
static extern int zcdbGetZdsName(out AdsName ename, ObjectId id);

[DllImport("zwcad.exe", CallingConvention = CallingConvention.Cdecl, EntryPoint = "zcdbEntGet")]
static extern IntPtr zcdbEntGet(AdsName ename);

[CommandMethod("UpdateDim")]
 public void UpdateDim()
 {
    var doc = Application.DocumentManager.MdiActiveDocument;
    var db = doc.Database;
    var ed = doc.Editor;

    var promptRes = ed.GetEntity("\nPick a Dim:");
     if (promptRes.Status != PromptStatus.OK)
     {
       return;
      }

      using(var tr = db.TransactionManager.StartTransaction())
      {
         var Ent = tr.GetObject(promptRes.ObjectId, OpenMode.ForRead) as Entity;
          if (Ent is Dimension dim)
           {
              var dict = tr.GetObject(dim.ExtensionDictionary, OpenMode.ForRead) as DBDictionary;
              var extDictId = dict.GetAt("ACAD_DIMASSOC");
              AdsName ename;
              zcdbGetZdsName(out ename, extDictId);
              var result = zcdbEntGet(ename);
                if (result != IntPtr.Zero)
                 {
                        var resBuffer = ResultBuffer.Create(result, true);
                        foreach (var item in resBuffer)
                        {
                            if (item.TypeCode == 331)
                            {
                                var assocEnt = tr.GetObject((ObjectId)item.Value, OpenMode.ForWrite) as Entity;
                                assocEnt.RecordGraphicsModified(true);
                                break;
                            }
                        }
                    }
                }
                //Ent.RecordGraphicsModified(true);
                tr.Commit();
            }
        }

    }
[DllImport("zwcad.exe", EntryPoint = "zcedPostCommand", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
extern static private int acedPostCommand(string strExpr);
 [DllImport("zwcad.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, EntryPoint = "zcedUsrBrk")]
   extern static private int UsrBrk();
[System.Security.SuppressUnmanagedCodeSecurity]
  [DllImport("zwcad.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, EntryPoint = "?zcedGetEnv@@YAHPEB_WPEA_W_K@Z")]//名称大小写不能换
   static extern int AcedGetEnv(string envName, StringBuilder ReturnValue);
    [CommandMethod("HelloCS")]
     static public void HelloCS()
     {
        Database db = HostApplicationServices.WorkingDatabase;
        Document doc = Application.DocumentManager.GetDocument(db);
        ZwSoft.ZwCAD.EditorInput.Editor ed = doc.Editor;
        StringBuilder ReturnValue = new StringBuilder(1024);  
        AcedGetEnv("PLOTCONFIGPATH", ReturnValue);
        ed.WriteMessage(ReturnValue.ToString());
       }
英文空间链接


★中望CAD中.NET开发调用常见C++函数的写法https://confluence.zwcad.com/pages/viewpage.action?pageId=262603608

  • No labels