UG二次开发-CAM-获取修改路径参数

项目中要获取路径参数,网上大多是C++的例子,而本项目是用C#写的,探索了下,记录下。

以获取某条路径的刀具号为例,其他参数依此类推。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NXOpen;
using NXOpen.Utilities;
using NXOpen.UF;
using NXOpen.CAM;
namespace NXTest
{
    public class NXCAM
    {
        public static Session theSession;
        public static UFSession theUfSession;
        public static UFUi theUFUi;
        public static NXOpen.Part workPart;

        static bool GetPathToolNumber(string pathName, out int toolNumber)
        {
            toolNumber = -1;
            NXOpen.CAM.CAMSetup camSetup = workPart.CAMSetup;
            NXOpen.CAM.NCGroup ncGroup = camSetup.GetRoot(NXOpen.CAM.CAMSetup.View.MachineTool);//获取机床视图下的所有刀具组
            NXOpen.CAM.CAMObject[] camObjArr = ncGroup.GetMembers();
            foreach (NXOpen.CAM.CAMObject camObj in camObjArr)//tool
            {
                if (camObj is NXOpen.CAM.NCGroup)
                {
                    NXOpen.CAM.NCGroup toolGroup = camObj as NXOpen.CAM.NCGroup;
                    NXOpen.CAM.CAMObject[] pathArr = toolGroup.GetMembers();
                    foreach (NXOpen.CAM.CAMObject path in pathArr)//path
                    {
                        if (path.Name == pathName)
                        {
                            Tool tool = toolGroup as Tool;
                            if (tool == null)
                                return false;

                            //方法一
                            NXOpen.UF.UFSession.GetUFSession().Param.AskIntValue(tool.Tag, NXOpen.UF.UFConstants.UF_PARAM_TL_NUMBER, out toolNumber);

                            //方法二
                            NXOpen.CAM.MillToolBuilder millToolBuilder1;
                            millToolBuilder1 = workPart.CAMSetup.CAMGroupCollection.CreateMillToolBuilder(tool);
                            toolNumber = millToolBuilder1.TlNumberBuilder.Value;
                            millToolBuilder1.Destroy();

                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public static int Main()
        {
            NXOpen.Session theSession = NXOpen.Session.GetSession();
            theUfSession = UFSession.GetUFSession();
            theUFUi = theUfSession.Ui;
            workPart = theSession.Parts.Work;
            NXOpen.Part displayPart = theSession.Parts.Display;
            UI theUI = UI.GetUI();
            TaggedObject obj = theUI.SelectionManager.GetSelectedTaggedObject(0);//获取路径树上被选中的路径
            NXObject nxobj = obj as NXObject;int num = 0;
            if (GetPathToolNumber(nxobj.Name, out num))
                theUFUi.DisplayMessage(nxobj.Name + " tool number is " + num, 1);
            else
                theUFUi.DisplayMessage("cannot get " + nxobj.Name + " tool number", 1);
            return 0;
        }

        public static int GetUnloadOption(string dummy)
        {
            return UFConstants.UF_UNLOAD_IMMEDIATELY;
        }
    }
}

 可以看到,有两种途径都可以获得:

NXOpen.UF.UFSession.GetUFSession().Param.AskIntValue
NXOpen.CAM.MillToolBuilder

值得注意的是,UG 的开发,引用的 dll 的版本要和实际使用的 UG 的版本严格对应,否则可能出错。

刚开始,我引用的是 UG12 的dll,在 UG11上调试,发现使用上述方法二总是报错,找了好久没找到原因,后来才发现是版本的原因。

完整代码:(修改3D圆角半径补偿的一键设置)

https://www.aliyundrive.com/s/ZrQg5KMhCYc

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注