UG/NX二次开发(C#) 获取组件里面的部件实体

 部件一旦装配之后,UG会对部件进行封装,并生成新的tag,那我们如何去查找装配后的部件呢?

这里提供2种思路:

1、通过ufun函数获取;

2、通过NXOPEN遍历部件;

上代码:

        /// <summary>
        /// 通过ufun获取组件里的部件信息
        /// </summary>
        public static void GetBodyListFromComponet(ref List<Component> compList, ref List<Body> bodyList)
        {
            theSession = Session.GetSession();
            theUFSession = UFSession.GetUFSession();
            workPart = theSession.Parts.Work;
            compList = new List<Component>();
            bodyList = new List<Body>();
            ComponentTool.GetComponentList(workPart, compList);
            ComponentAssembly compAssembly = workPart.ComponentAssembly;
            Component rootComponent = compAssembly.RootComponent;
            foreach (Component c in compList)
            {
                SetWorkPart(c);
                workPart = theSession.Parts.Work;
                Tag objTag = Tag.Null;
                theUFSession.Obj.CycleObjsInPart(workPart.Tag, UFConstants.UF_solid_type, ref objTag);
                while (objTag != Tag.Null)
                {
                    int type, subtype;
                    theUFSession.Obj.AskTypeAndSubtype(objTag, out type, out subtype);
                    if (type == 70 && subtype == 0)
                    {
                        Body b = (Body)NXOpen.Utilities.NXObjectManager.Get(objTag);
                        bodyList.Add(b);
                    }
                    theUFSession.Obj.CycleObjsInPart(workPart.Tag, UFConstants.UF_solid_type, ref objTag);
                }
            }
            SetWorkPart(rootComponent);
        }

        /// <summary>
        /// 通过NXOPEN直接遍历
        /// </summary>
        public static void GetBodyListFromComponet1(ref List<Component> compList, ref List<Body> bodyList)
        {
            theSession = Session.GetSession();
            theUFSession = UFSession.GetUFSession();
            workPart = theSession.Parts.Work;
            compList = new List<Component>();
            bodyList = new List<Body>();
            ComponentTool.GetComponentList(workPart, compList);
            ComponentAssembly compAssembly = workPart.ComponentAssembly;
            Component rootComponent = compAssembly.RootComponent;
            foreach (Component c in compList)
            {           
                SetWorkPart(c);
                workPart = theSession.Parts.Work;
                foreach (Body bodyObj in workPart.Bodies)
                {
                    bodyList.Add(bodyObj);
                }
            }
            SetWorkPart(rootComponent);
        }

 调用方法:

            List<Component> compList = new List<Component>();
            List<Body> bodyList = new List<Body>();
            ComponentTool.GetBodyListFromComponet(ref compList, ref bodyList);

            List<Component> compList1 = new List<Component>();
            List<Body> bodyList1 = new List<Body>();
            ComponentTool.GetBodyListFromComponet1(ref compList1, ref bodyList1);

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
UG/NX二次开发中,可以使用以下方法获取曲线的类型: 1. 获取曲线对象,例如线、圆、椭圆等。 2. 使用UG/Curve_Type.hxx头文件中的枚举类型,通过曲线对象的CurveType()方法获取曲线类型。 以下是一个示例代码,演示如何获取曲线类型: ``` #include <NXOpen/NXException.hxx> #include <NXOpen/Part.hxx> #include <NXOpen/PartCollection.hxx> #include <NXOpen/Features_Feature.hxx> #include <NXOpen/Features_FeatureCollection.hxx> #include <NXOpen/Features_FeatureType.hxx> #include <NXOpen/Features_BodyFeature.hxx> #include <NXOpen/Features_BodyFeatureCollection.hxx> #include <NXOpen/Curve.hxx> #include <NXOpen/Curve_Type.hxx> using namespace NXOpen; void getCurveType(Curve* curve) { if (curve == NULL) { return; } Curve::Type type = curve->CurveType(); switch (type) { case Curve::Type_Line: // do something for line break; case Curve::Type_Circle: // do something for circle break; case Curve::Type_Ellipse: // do something for ellipse break; // add more cases for other curve types default: // do something for unknown curve type break; } } int main(int argc, char* argv[]) { Part* part = NULL; PartCollection* partCollection = NULL; Features::Feature* feature = NULL; Features::FeatureCollection* featureCollection = NULL; Features::BodyFeature* bodyFeature = NULL; Features::BodyFeatureCollection* bodyFeatureCollection = NULL; Curve* curve = NULL; try { partCollection = NXOpen::Session::GetSession()->Parts(); part = partCollection->ActivePart(); featureCollection = part->Features(); feature = featureCollection->FindObject("feature_name", Features::FeatureType_Body); bodyFeatureCollection = feature->BodyFeatures(); bodyFeature = bodyFeatureCollection->FindObject("body_feature_name"); curve = dynamic_cast<Curve*>(bodyFeature->GetGeometryObject()); getCurveType(curve); } catch (NXException& ex) { // handle exception } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MarcoPro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值