ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

NX二次开发-远程开发模式(C/S架构-客户端和服务端)

2021-10-10 14:01:36  阅读:329  来源: 互联网

标签:theSession dll NX Session NXOpen 二次开发 using 服务端


 

  • 版本

NX11+VS2013

 

  •  API帮助

远程开发模式在API帮助中的出处

首先打开NX11的API帮助手册,点击 NX开放程序员指南

进来后如下

 英文不好,自己翻译一下。

 点击执行方法,进入如下

 

 

 选择远程进程,点击去,记住这个单词 Remote Processes

与远程开发模式的相关内容,如下所示

通过上面的内容介绍,我们可以初步了解到

1.它是通过操作进程来操作NX的,可以是本机电脑的NX,也可以是局域网内服务器上的NX。

2.只能用.NET和JAVA框架,C++和Python不能用。

3.需要写两份程序,一份客户端程序,一份服务端程序。

4.Java和.Net的远程进程示例在ugopen\SampleNXOpenApplications中。

5.远程开发模式可以操作服务器上的NX进行实时GUI交互操作,也可以做批处理外部开发(不打开NX,不带界面交互)

......

 

  • UGOPEN中的例子

好,下面我们去找远程开发模式.NET的例子。

例子就是下面的这个,通过英文单词RemotingExample我们可以直接的找到这就是远程的例子

 

 txt里是例子的相关说明,和执行方法

 翻译过后,如果开发过C/S架构的软件,大致看看也就能理解怎么用了。

-------------------------------------------------------------------------------


版权所有:西门子产品生命周期管理软件有限公司
保留所有权利。

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
. net Remoting的例子
-------------------------------------------------------------------------------

描述:
------------
这个例子演示了一种为。net设置Remoting的方法。这
示例由两个c#项目组成。一个是服务器,一个是
客户端。

注意示例代码:
这个例子展示了一个使用HTTP协议的。net服务器程序。这
示例程序使用硬编码的值。在实践中,这将被指定
命令行或使用。net配置文件。

请注意服务器代码:Run方法(它被放在infinite中
休眠状态)只应执行以下操作:
—设置远程连接
—获取会话
—导出会话

如果您进行其他调用,特别是涉及到UI的NXOpen调用(例如
ListingWindow)。这可能会使NX处于无限循环状态。调用
但LogFile API是可以接受的。

此外,实现了GetUnloadOption和UnloadLibrary方法。这个服务器
是设置,以便可以使用文件->实用程序->卸载共享图像
功能。属性的显式卸载服务器可以停止服务器
卸载共享图像功能。这将停止无限循环并断开连接
会话。


--------------
文件要求
--------------
1. Client.csproj
2. NXOpenRemotingClient.cs
上面列出的文件位于$UGII_BASE_DIR\ugopen\SampleNXOpenApplications\。净\ RemotingExample \客户机

3.NXOpenRemotingService.cs
4. Server.csproj
上面列出的文件位于$UGII_BASE_DIR\ugopen\SampleNXOpenApplications\。净\ RemotingExample \服务器


-------------------------
构建步骤
-------------------------

窗户
-------
服务器:
1. 在Visual Studio中打开Server c#项目(Server\Server.csproj)。

2. 添加对NXOpen.dll、NXOpen.UF.dll、NXOpen.UI.dll和NXOpen.Utilities.dll的引用。
为此,右键单击解决方案资源管理器中服务器项目下的“References”。
选择“Add Reference…”,然后导航到dll的位置。
这些dll可以在%UGII_BASE_DIR%\nxbin\managed中找到。

3.使用“生成-生成解决方案”生成项目并单击“确定”。

4. 新创建的Server.dll的默认位置位于名为Server\obj\Debug的子目录中。

客户:
1. 在Visual Studio中打开Client c#项目(Client\Client.csproj)。

2. 添加对NXOpen.dll、NXOpen.UF.dll、NXOpen.UI.dll和NXOpen.Utilities.dll的引用。
为此,右键单击解决方案资源管理器中的Client项目下的“References”。
选择“Add Reference…”,然后导航到dll的位置。
这些dll可以在%UGII_BASE_DIR%\nxbin\managed中找到。

3.使用“生成-生成解决方案”生成项目并单击“确定”。

4. 新创建的Client.exe的默认位置位于名为Client\obj\Debug的子目录中。

Linux
-------
- n / A -


----------------------------
启动NX前的设置
----------------------------
na -

----------------------------
启动NX后的设置
----------------------------
1. 修改“角色”为“高级”。

--------------------------
示例执行步骤
--------------------------

注意:您需要一个。net创作许可证来运行这个示例。看到
有关许可的详细信息的在线文档。

1. 通过NX命令提示符启动NX。

2. 输入Ctrl-U打开“Execute NXOpen”对话框。

3.导航到Server.dll(Server\obj\Debug\Server.dll)并按OK。

4. 使用相同的NX命令提示符,运行Client.exe(Client\obj\Debug\Client.exe)。
这将创建一个带有一些简单曲线的新部分。您可能需要缩小以查看创建的曲线。

------
笔记
------
1. NXOpen应用程序在发布之前必须进行签名。如果没有签名,应用程序可能无法执行。
有关签署应用程序的更多细节,请参阅NXOpen程序员指南。”

2. 通过使用File->Utilities->Unload Shared Images功能可以关闭服务器。

3.服务器关闭后,可以使用“Execute NXOpen”对话框重新启动服务器。

 

  • 搭建开发环境

下面我们把例子单独考出来到一个空目录下

客户端程序

启动VS,打开项目,编译代码

 打开后,自动会搭建好开发环境,

 我们直接编译代码,去bin目录里找到我们编译生成的exe,这就是客户端的应用程序。.exe单独执行的,不需要打开ug去里面调用。需要引用的.dll动态链接库都已经自动打包过来了。

在这里我们其实就可以直接编写一个winform.exe带界面的桌面应用程序,具有各种功能,而其中一个功能就是发送消息远程操作服务器上的NX进行操作画图,后台处理数据,在返回给客户端前端界面显示。

甚至我在想,如果我要是会asp.net搭建网页web程序的话,我都想做一个web网页前端和服务器后台NX,这种B/S架构,想做一个前端网页显示三维模型和二维图纸,后台NX进行数据计算和生成。

  • 服务端程序

打开服务端的程序

 

 编译代码,我们看到它生成的是.dll文件,这个就是我们服务端要执行的程序了,打开服务端的NX,在里面ctrl+u去执行这个dll

  • 操作演示

要注意一点问题,在客户端的例子上有段代码是新建prt,这个我测试之后发现用中文版的NX,执行新建prt会报错,因为它找不到那个模板位置,换成全路径就可以了。

或者你就直接用英文版的NX去执行,它就能找到。这个模板应该是UGII目录里的,如果是中文版的NX调的不是UGII,而且LOCALIZATION目录下得,所以找不到。这是我猜测的。

 中文版NX执行的错误问题截图

 先加载服务端dll,在运行客户端exe

1.本机演示(客户端和服务端都在本机电脑上)

2.C/S架构-客户端+服务器演示(我专门装了一台电脑做服务器,安装的windows server2012r2系统)

这里需要先更改一下客户端项目代码中的ip地址

 这里的localhost意思是代表本机(做过数据库项目的应该都知道),localhost和127.0.0.1都是代表本机的意思。

现在我们要操作服务器上的NX,那么要把这里改成服务器的ip地址

把我们的服务端程序考到服务器上,打开NX去执行dll。

  • 代码内容介绍

Client

1.这是一个控制台程序,并不是带界面的winform程序。

2.从Main函数入口进来后,通过下面这两句代码来获取服务器上NX的Session会话。(http协议,服务器ip地址,端口号)

        Session theSession = (Session)Activator.GetObject(typeof(Session), "http://192.168.1.101:4567/NXOpenSession");
        UFSession theUFSession = (UFSession)Activator.GetObject(typeof(UFSession), "http://192.168.1.101:4567/UFSession");

3.打印log,开始新建prt,画直线,画圆弧

客户端的代码就这么几句话功能,代码里也有英文注释,自己看看就好了。

 

完整代码

/*=============================================================================

                    Copyright (c) 2008 Siemens PLM Solutions
                    Unpublished - All rights reserved

===============================================================================
File description: Sample NX/Open Application
===============================================================================

=============================================================================
*/


using System;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Diagnostics;

using System.Collections;

using NXOpen;
using NXOpen.Utilities;
using NXOpen.UF;


public class NXOpenRemotingClient
{
    public static void DoLog(String s)
    {
        Session.GetSession().LogFile.WriteLine(s);
        Console.WriteLine(s);
    }

    static void Main(string[] args)
    {
        Session theSession = (Session)Activator.GetObject(typeof(Session), "http://192.168.1.101:4567/NXOpenSession");
        UFSession theUFSession = (UFSession)Activator.GetObject(typeof(UFSession), "http://192.168.1.101:4567/UFSession");

        try
        {
            DoLog("working");
            theSession.LogFile.WriteLine("\nITS WORKING\n");

            // ----------------------------------------------
            //   Menu: File->New...
            // ----------------------------------------------
            NXOpen.Session.UndoMarkId markId1;
            markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "'Start");

            NXOpen.FileNew fileNew1;
            fileNew1 = theSession.Parts.FileNew();

            theSession.SetUndoMarkName(markId1, "''New Dialog");

            NXOpen.Session.UndoMarkId markId2;
            markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "'New");

            theSession.DeleteUndoMark(markId2, null);

            NXOpen.Session.UndoMarkId markId3;
            markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "'New");

            fileNew1.TemplateFileName = "model-plain-1-mm-template.prt";

            fileNew1.UseBlankTemplate = false;

            fileNew1.ApplicationName = "ModelTemplate";

            fileNew1.Units = NXOpen.Part.Units.Millimeters;

            fileNew1.RelationType = "";

            fileNew1.UsesMasterModel = "No";

            fileNew1.TemplateType = NXOpen.FileNewTemplateType.Item;

            fileNew1.TemplatePresentationName = "Model";

            fileNew1.ItemType = "";

            fileNew1.Specialization = "";

            fileNew1.SetCanCreateAltrep(false);

            fileNew1.NewFileName = "Remoting.prt";

            fileNew1.MasterFileName = "";

            fileNew1.MakeDisplayedPart = true;

            NXOpen.NXObject nXObject1;
            nXObject1 = fileNew1.Commit();

            NXOpen.Part workPart = theSession.Parts.Work;
            NXOpen.Part displayPart = theSession.Parts.Display;
            theSession.DeleteUndoMark(markId3, null);

            fileNew1.Destroy();

            NXOpen.Session.UndoMarkId markId4;
            markId4 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Enter Gateway");

            theSession.ApplicationSwitchImmediate("UG_APP_GATEWAY");

            theSession.ApplicationSwitchImmediate("UG_APP_MODELING");

            NXOpen.Session.UndoMarkId markId5;
            markId5 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Enter Modeling");

            // Create Line using NXOpen Jam APIs
            Point3d point3d1 = new Point3d(-2.45197396411307, 3.58206319143819, 0);
            Point3d point3d2 = new Point3d(5.32514590979158, -1.0012853802839, 0);
            Line line1 = theSession.Parts.Work.Curves.CreateLine(point3d1, point3d2);

            //Ask line data using wrapped NXOpen.UF API
            UFCurve.Line line_coords;
            theUFSession.Curve.AskLineData(line1.Tag, out line_coords);

            // Create Arc using wrapped NXOpen.UF APIs
            Tag arc, wcs;
            UFCurve.Arc arc_coords = new UFCurve.Arc();

            // Fill out the data structure
            arc_coords.start_angle = 0.0;
            arc_coords.end_angle = 3.0;
            arc_coords.arc_center = new double[3];
            arc_coords.arc_center[0] = 0.0;
            arc_coords.arc_center[1] = 0.0;
            arc_coords.arc_center[2] = 1.0;
            arc_coords.radius = 2.0;

            // Create a ARC using wrapped NXOpen.UF API
            theUFSession.Csys.AskWcs(out wcs);
            theUFSession.Csys.AskMatrixOfObject(wcs, out arc_coords.matrix_tag);
            theUFSession.Curve.CreateArc(ref arc_coords, out arc);

            // Create an NX Open Arc object from the UF arc tag using the NXObjectManager.
            // You need to get the NXObjectManager from the remote session object.
            NXObjectManager theNXOM = theSession.GetObjectManager();
            Arc theArc = (Arc)theNXOM.GetTaggedObject(arc);

            theSession.Information.DisplayObjectsDetails(new NXObject[] { theArc });
        }
        catch (NXException e)
        {
            DoLog("NX Exception is: {0} " + e.Message);
        }
        catch (Exception e)
        {
            DoLog("Exception is: {0} " + e.Message);
        }
        finally
        {
            DoLog("Done");
            theSession.LogFile.WriteLine("DONE\n");
        }

    }
}

 

Server

1.这是一个dll项目,要从NX中ctrl+u进行执行

2.也是从Main函数入口进来后,它创建了一个新的线程。

    public static void Main(String[] args)
    {
        Start();
    }

    public static void Start()
    {
        Thread serverThread = new Thread(new ThreadStart(Run));
        serverThread.Start();
    }

3.新的线程执行Run函数里的内容

通过heet协议,端口号,设置远程连接  

            DoLog("Starting NX Service\n");

            LifetimeServices.LeaseTime = System.TimeSpan.FromDays(10000);

            SoapServerFormatterSinkProvider provider = new SoapServerFormatterSinkProvider();
            provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

            // Create the IDictionary to set the port on the channel instance.

            IDictionary props = new Hashtable();
            props["port"] = port;

            // Create a new http channel with the given provider and properties
            ChannelServices.RegisterChannel(new HttpChannel(props, null, provider), false);

 

获取会话,导出会话 

            DoLog("\n\n");
            DoLog("Exporting Session object");
            RemotingServices.Marshal(theSession, "NXOpenSession");

            DoLog("Exporting UFSession Object");
            RemotingServices.Marshal(theUFSession, "UFSession");


            DoLog("NX Service started on port " + port + "\n");

代码里都有注释,自己看看吧。

 

完整代码

/*=============================================================================

                    Copyright (c) 2008 Siemens PLM Solutions
                    Unpublished - All rights reserved

===============================================================================
File description: Sample NX/Open Application
===============================================================================

=============================================================================
*/
using System;
using System.IO;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Lifetime;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Serialization.Formatters;
using System.Diagnostics;

using System.Collections;

using NXOpen;
using NXOpen.Utilities;
using NXOpen.UF;

public class NXOpenRemotingService
{

    public static int port = 4567;
    public static Session theSession;
    public static UFSession theUFSession;
    
    //The first flag indicates that we are unloading the dll.  The second flag indicates that the service has completed.  This is needed as the
    //Run and the unload will be run in in two different threads.  We indicate that we are unloading so that we can end the while loop
    //and then output a message to the syslog that the service has ended.  However, since we have two threads, we do not want the unload method to 
    //proceed until the service loop is done processing.
    public static bool isUnloaded = false;
    public static bool serviceEnded = false; 


    public static void DoLog(String s)
    {
        Session.GetSession().LogFile.WriteLine(s);
    }

    public static void Main(String[] args)
    {
        Start();
    }

    public static void Start()
    {
        Thread serverThread = new Thread(new ThreadStart(Run));
        serverThread.Start();
    }

    // This method should only peform the following operations:
    // - Setting up Remoting 
    // - Obtaining the session
    // - Exporting the session
    // 
    // If you make other calls especially NXOpen calls that may involve the UI
    // (such as ListingWindow).  This may put NX in state of an infinite loop.
    // Calls to the LogFile API are acceptable though.
    public static void Run()
    {
        DoLog("In NXOpenRemotingService.Main - getting session\n");
        theSession = Session.GetSession();
        theUFSession = UFSession.GetUFSession();
        try
        {
            DoLog("Starting NX Service\n");

            LifetimeServices.LeaseTime = System.TimeSpan.FromDays(10000);

            SoapServerFormatterSinkProvider provider = new SoapServerFormatterSinkProvider();
            provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

            // Create the IDictionary to set the port on the channel instance.

            IDictionary props = new Hashtable();
            props["port"] = port;

            // Create a new http channel with the given provider and properties
            ChannelServices.RegisterChannel(new HttpChannel(props, null, provider), false);

            DoLog("\n\n");
            DoLog("Exporting Session object");
            RemotingServices.Marshal(theSession, "NXOpenSession");

            DoLog("Exporting UFSession Object");
            RemotingServices.Marshal(theUFSession, "UFSession");


            DoLog("NX Service started on port " + port + "\n");

        }
        catch (Exception e)
        {
            DoLog(e.ToString());
        }
        while (!isUnloaded)
        {
            Thread.Sleep(1000);
        }
        
        DoLog("\n\nSERVICE ENDED!!!\n\n");
        serviceEnded = true;
    }


    public static int GetUnloadOption(string dummy) 
    { 
        return (int)Session.LibraryUnloadOption.Explicitly; 
    }


    public static void UnloadLibrary(string arg)
    {
        isUnloaded = true;
        while (!serviceEnded)
        {

        }

        DoLog("Disconnecting Session object");
        RemotingServices.Disconnect(theSession);

        DoLog("Disconnecting UFSession Object");
        RemotingServices.Disconnect(theUFSession);
    }

}

 

 

更多的玩法,以后在慢慢往上更新吧。。。

 

 

阿飞

2021年10月10日

 

标签:theSession,dll,NX,Session,NXOpen,二次开发,using,服务端
来源: https://www.cnblogs.com/nxopen2018/p/15389255.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有