NX二次开发-C#使用DllImport调用libugui.dll里的内部函数自动将NX标题设置为prt路径例子(三部曲3)

三部曲:

NX二次开发-C#使用DllImport调用libufun.dll里的UF函数(反编译.net.dll)调用loop等UF函数(三部曲1)

NX二次开发-C#使用DllImport调用libufun.dll里的UF函数学习方法及tag转handle例子(三部曲2)

NX二次开发-C#使用DllImport调用libugui.dll里的内部函数自动将NX标题设置为prt路径例子(三部曲3)

在看完三部曲1和三部曲2后,我们来个更有意思的玩法,去调内部函数。

做NX二次开发,西门子官方开放给了我们API,但也有一些是他们内部用的函数,并没有开放给我们(比如关于NX窗口界面相关的等等)。

这个时候我们如果有相关的需求了怎么办?怎么去查找有哪些内部函数?怎么去调用函数?

这一讲就会介绍。

1.又是有需求

以前经常看到别人问怎么 自动将NX标题设置为prt路径,最开始我不会,后来七少兄弟很早之前教过我,他是用获取窗口NX标题句柄,然后修改标题内容,在加上计时器去做的。

我感觉好像别人也应该是这样做的。

今天我写一下调内部函数的方法,去设置NX标题。

2.反编译libugui.dll

首先用Depends.exe去反编译libugui.dll找到内部函数的入口。

内部函数具体用哪个得慢慢去找。找到了还得去猜函数的输入输出类型怎么定义。蛮难使用的。能搞清楚怎么用得,应该也就那么几个。

因为不像UF有明确的帮助说明,参数定义介绍。这里只介绍方法,具体有兴趣和需求的,你们可以自己去慢慢研究。

找到这个内部函数"?MT_set_ug_title@@YAXPEADI@Z",我猜测只有一个输入参数,结果被我猜中了。给一个输入参数是可以用的。

 3.我们的项目

 去项目中调用

    [DllImport("libugui.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "?MT_set_ug_title@@YAXPEADI@Z")]
    internal unsafe static extern int _SetUgTitle(string title);
    public unsafe static void SetUgTitle(string title)
    {
        NXOpen.Utilities.JAM.StartUFCall();
        OpenAPI._SetUgTitle(title);
        NXOpen.Utilities.JAM.EndUFCall();
    }

要测试这个,我们还需要做三步操作。

1.加一个计时器,不断去刷新NX标题。

2.更改卸载方式,不要立即卸载,改完关闭NX卸载。

3.想要每次打开NX就默认加载,就把dll程序放到startup里(这一步好像有问题,用C+做,用ufsta入口没问题.C#的main入口,好像不能默认加载进来)。

以下为项目代码

using System;
using NXOpen;
using NXOpen.UF;
using NXOpen.Utilities;
using System.Runtime.InteropServices;//DllImport的命名空间
using System.Timers;//计时器

public class OpenAPI
{

    [DllImport("libugui.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "?MT_set_ug_title@@YAXPEADI@Z")]
    internal unsafe static extern int _SetUgTitle(string title);
    public unsafe static void SetUgTitle(string title)
    {
        NXOpen.Utilities.JAM.StartUFCall();
        OpenAPI._SetUgTitle(title);
        NXOpen.Utilities.JAM.EndUFCall();
    }


}



public class Program
{
    // class members
    private static Session theSession;
    private static UI theUI;
    private static UFSession theUfSession;
    public static Program theProgram;
    public static bool isDisposeCalled;

    //------------------------------------------------------------------------------
    // Constructor
    //------------------------------------------------------------------------------
    public Program()
    {
        try
        {
            theSession = Session.GetSession();
            theUI = UI.GetUI();
            theUfSession = UFSession.GetUFSession();
            isDisposeCalled = false;
        }
        catch (NXOpen.NXException ex)
        {
            // ---- Enter your exception handling code here -----
            // UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
        }
    }


    static void TimerUp(object sender, System.Timers.ElapsedEventArgs e)
    {
        //获得prt路径
        string PrtPath = "";
        theUfSession.Part.AskPartName(theSession.Parts.Display.Tag, out PrtPath);

        //设置NX标题
        OpenAPI.SetUgTitle(PrtPath);
    }


    //------------------------------------------------------------------------------
    //  Explicit Activation
    //      This entry point is used to activate the application explicitly
    //------------------------------------------------------------------------------
    public static int Main(string[] args)
    {
        int retValue = 0;
        try
        {
            theProgram = new Program();

            //TODO: Add your application code here 

   
            Timer timer1 = new Timer(500);
            //设置执行一次(false)还是一直执行(true)
            timer1.AutoReset = true;
            //设置是否执行System.Timers.Timer.Elapsed事件
            timer1.Enabled = true;
            //绑定Elapsed事件
            timer1.Elapsed += new System.Timers.ElapsedEventHandler(TimerUp);


            theProgram.Dispose();
        }
        catch (NXOpen.NXException ex)
        {
            // ---- Enter your exception handling code here -----

        }
        return retValue;
    }

    //------------------------------------------------------------------------------
    // Following method disposes all the class members
    //------------------------------------------------------------------------------
    public void Dispose()
    {
        try
        {
            if (isDisposeCalled == false)
            {
                //TODO: Add your application code here 
            }
            isDisposeCalled = true;
        }
        catch (NXOpen.NXException ex)
        {
            // ---- Enter your exception handling code here -----

        }
    }

    public static int GetUnloadOption(string arg)
    {
        //Unloads the image explicitly, via an unload dialog
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);

        //Unloads the image immediately after execution within NX
        //return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);

        //Unloads the image when the NX session terminates
        return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);//使用这种卸载方式
    }

}


Caesar卢尚宇
2020年9月30日

 4.附件参考

在附加一个调宏的内部函数

    [DllImport("libugui.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "?MACRO_playback_from_usertool@@YAXPEBD@Z")]
    internal unsafe static extern int _CallMacro(string path);
    public unsafe static void CallMacro(string path)
    {
        NXOpen.Utilities.JAM.StartUFCall();
        OpenAPI._CallMacro(path);
        NXOpen.Utilities.JAM.EndUFCall();
    }

到这里三部曲就正式结束,写完了。

Caesar卢尚宇

2020年9月30日

原文地址:https://www.cnblogs.com/nxopen2018/p/13756592.html