nx mcd运行时行为(2):运行时api的使用,vs环境下的联调



(一)nx端的使用方式





(二)使用vs来调试运行时代码


创建.net framework类库程序。

image.png


nx也提供了vs2019的一个开发环境插件,安装后也可以使用nxopen的模板来创建项目。

这个插件在本文的下载里提供了。

image.png


C#代码:

using System;
using NXOpen;
public class UserBehavior : BehaviorDef
{
    MCDSignal m_Trigger; // m_Trigger is the trigger value MCDSignal m_Times; 
    MCDSignal m_Times;                      // m_Timers is the counter for trigger
    bool bFlag = false; public override void Define(IDefinitionContext access)
    {
        // Insert definition code here
        // connect the m_Trigger and m_Timers to MCD object access.Connect("Trigger", out m_Trigger); access.Connect("Times", out m_Times);
        access.Connect("Trigger", out m_Trigger);
        access.Connect("Times", out m_Times);

    }
    public override void Start(IRuntimeContext context)
    {
        // Insert start up code here
    }
    public override void Stop(IRuntimeContext context)
    {
        // Insert stopping code here
    }
    public override void Step(IRuntimeContext context, double dt)
    {
        // NXOPEN APIs can not be called in Step function since Step is running in the worker thread. 
        // only MCD runtime APIs can be called in the Step function
        // Insert simulation step code here
        if (!bFlag & m_Trigger.BoolValue)
        {
            m_Times.IntValue++;
            bFlag = true;
        }
        if (m_Trigger.BoolValue == false)
        {
            bFlag = false;
        }
    }
    public override void Refresh(IRuntimeContext context)
    {
        // Insert simulation refreshing code here. This is called when it is safe // to read and change runtime variables outside of the main control loop. } public override void Repaint()
        {
            // Insert simulation repainting code here. This is where you use the data
            // taken from the runtime to update the display.
        }
    }

    public override void Repaint()
    {
        // Insert simulation repainting code here. This is where you use the data
        // taken from the runtime to update the display.
    }

}


引用dll
在勇哥的电脑上,dll的位置在:C:\Program Files\Siemens\NX2412\NXBIN\managed
实在找不到的人,可以装个everything搜索一下位置。
image.png

接下来很重要一点,要对编译后的dll做nx的签名,否则你无法在nx中应用这个dll。
在项目->属性中,添加下面的签名事件。
image.png
注意把路径替换成你自己的。

"C:\Program Files\Siemens\NX2412\NXBIN\SignDotNet.exe" $(TargetPath)
"C:\Program Files\Siemens\NX2412\NXBIN\SignDotNet.exe" -verify $(TargetPath)


编译吧,如果成功了,最好看下签名是否成功了。
使用下面的命令。
看如下的信息则表示成功了。
image.png

然后在nx这边使用这个dll。

image.png

如果C#脚本没问题,就可以看到两个值,它是由下面代码里指定的:

  access.Connect("Trigger", out m_Trigger);

        access.Connect("Times", out m_Times);


但是名称这个还是需要手工指定的。

image.png

你现在就可以运行仿真了。通过观察面板里查看结果。

你也可以VS下断点联调,需要附加到进程。

选择ugraf这个进程。

image.png



运行仿真,就可以看到断点会断下来了。



本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:
本帖最后由 勇哥,很想停止 于 2025-07-31 14:30:37 编辑

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2025年7月    »
123456
78910111213
14151617181920
21222324252627
28293031
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864