<UG.NX二次开发> 之 创建点/点集特征

0 阅读3分钟

<UG.NX二次开发> 之 创建点/点集特征

基于UFun可以创建自定义给定坐标的点集特征。 基于NXOpen无法实现,只能创建点特征

C++代码和C#代码的实现方式完全相同,仅语法不同。

C++

基于NXOpen

#include <NXOpen/Section.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Point.hxx>
#include <NXOpen/Body.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/Features_BaseFeatureCollection.hxx>
#include <NXOpen/Features_PointFeatureBuilder.hxx>
#include <NXOpen/DisplayModification.hxx>
#include <NXOpen/DisplayManager.hxx>

#include <Eigen/Dense>
#include <string>

namespace UseNXOpen {
    /// @brief 基于NXOpen构造单点特征
    /// @param color 特征上色
    /// @param name 特征命名
    /// @return 点特征对象
    inline NXOpen::NXObject* BuildPoint(double x, double y, double z,
        const int color = -1, const std::string& name = "") {

        NXOpen::Session* theSession = NXOpen::Session::GetSession();
        NXOpen::Part* workpart(theSession->Parts()->Work());

        NXOpen::Point3d point3d(x, y, z);
        NXOpen::Point* point = workpart->Points()->CreatePoint(point3d);

        NXOpen::Features::PointFeatureBuilder* pointFeatureBuilder;
        NXOpen::Features::Feature* nullNXOpen_Features_Feature(NULL);
        pointFeatureBuilder = workpart->BaseFeatures()->CreatePointFeatureBuilder(nullNXOpen_Features_Feature);
        pointFeatureBuilder->SetPoint(point);

        NXOpen::NXObject* nxObject = pointFeatureBuilder->Commit();
        pointFeatureBuilder->Destroy();

        tag_t tag = nxObject->Tag();

        if (!name.empty()) {
            nxObject->SetName(name.c_str());
        }

        if (color > 0) {
            NXOpen::DisplayModification* displayModification = theSession->DisplayManager()->NewDisplayModification();
            displayModification->SetNewColor(color);

            std::vector<NXOpen::DisplayableObject*> displayObjects(1);
            displayObjects[0] = point;
            displayModification->Apply(displayObjects);
            delete displayModification;
        }
    }
 
    /// @brief 基于NXOpen构造多个单点特征
    /// @param color 特征上色
    /// @param name 特征命名
    /// @return 点特征对象向量
    inline std::vector<NXOpen::NXObject*> BuildPoints(const Eigen::MatrixXd& coords,
        const int color = -1, const std::string& name = "") {

        NXOpen::Session* theSession = NXOpen::Session::GetSession();
        NXOpen::Part* workpart(theSession->Parts()->Work());

        std::vector<NXOpen::DisplayableObject*> displayObjects(coords.rows());
        std::vector<NXOpen::NXObject*> nxObjects(coords.rows());

        for (int i = 0; i < coords.rows(); ++i) {
            NXOpen::Point3d point3d(coords(i, 0), coords(i, 1), coords(i, 2));
            NXOpen::Point* point = workpart->Points()->CreatePoint(point3d);

            NXOpen::Features::PointFeatureBuilder* pointFeatureBuilder;
            NXOpen::Features::Feature* nullFeature(NULL);
            pointFeatureBuilder = workpart->BaseFeatures()->CreatePointFeatureBuilder(nullFeature);
            pointFeatureBuilder->SetPoint(point);

            NXOpen::NXObject* nxObject = pointFeatureBuilder->Commit();
            pointFeatureBuilder->Destroy();

            tag_t tag = nxObject->Tag();
            nxObjects[i] = nxObject;

            if (!name.empty()) {
                nxObject->SetName(name.c_str());
            }

            displayObjects[i] = point;
        }

        if (color > 0) {
            NXOpen::DisplayModification* displayModification = theSession->DisplayManager()->NewDisplayModification();
            displayModification->SetNewColor(color);
            displayModification->Apply(displayObjects);
            delete displayModification;
        }

        return nxObjects;
    }
} // namespace UseNXOpen

基于UFun

#include <uf_defs.h>
#include <uf.h>
#include <uf_defs.h>
#include <uf_curve.h>
#include <uf_obj.h>
#include <uf_modl.h>

#include <Eigen/Dense>
#include <string>

namespace UseUFun {
	/// @brief 基于UFun构造点特征
	/// @param color 特征上色
	/// @param name 特征命名
	/// @return 点特征tag
	inline tag_t BuildPoint(double x, double y, double z,
		const int color = -1, const std::string& name = "") {

        UF_initialize();

        double point_coord[3] = { x, y, z };
        tag_t point_tag = NULL_TAG;
        UF_CURVE_create_point(point_coord, &point_tag);

        if (color > 0) {
            UF_OBJ_set_color(point_tag, color);
        }

        if (!name.empty()) {
            UF_OBJ_set_name(point_tag, name.c_str());
        }

        UF_terminate();
        return point_tag;
	}
 
	/// @brief 基于UFun构造点集特征
    /// @param color 特征上色
    /// @param name 特征命名
    /// @return 点集特征tag
	inline tag_t BuildPoints(const Eigen::MatrixXd& coords,
		const int color = -1, const std::string& name = "") {

        UF_initialize();

        std::vector<tag_t> point_tags(coords.rows());

        for (int i = 0; i < coords.rows(); ++i) {
            double point_coord[3] = { coords(i, 0), coords(i, 1), coords(i, 2) };
            tag_t point_tag = NULL_TAG;
            UF_CURVE_create_point(point_coord, &point_tag);

            if (color > -1)
                UF_OBJ_set_color(point_tag, color);

            // 隐藏对象
            // UF_OBJ_set_blank_status(point_tag, UF_OBJ_BLANKED);

            point_tags[i] = point_tag;
        }
        tag_t points_tag = NULL_TAG;
        UF_MODL_create_points_feature(point_tags.size(), point_tags.data(), &points_tag);

        if (color > -1) {
            UF_OBJ_set_color(points_tag, color);
        }

        if (!name.empty()) {
            UF_OBJ_set_name(points_tag, name.c_str());
        }

        UF_terminate();

        return points_tag;
	}

} // namespace ByUFun

C#

基于NXOpen

using System.Collections.Generic;
using System.Linq;
using NXOpen;
using NXOpen.Features;

 public static class PointsBuilder_UseNXOpen
 {
     private static Session theSession { get => Session.GetSession(); }
     private static Part WorkPart { get => theSession.Parts.Work; }

     /// <summary>
     /// 基于NXOpen创建单点特征
     /// </summary>
     /// <param name="coord">坐标[x,y,z]</param>
     /// <param name="name">特征命名</param>
     /// <param name="color">特征颜色</param>
     /// <returns>点特征对象</returns>
     public static NXObject BuildPoint(this double[] coord, string name = null, int color = -1)
     {
         Point3d point3d = new Point3d(coord[0], coord[1], coord[2]);
         Point point = WorkPart.Points.CreatePoint(point3d);
         PointFeatureBuilder builder = WorkPart.BaseFeatures.CreatePointFeatureBuilder(null);
         builder.Point = point;
         NXObject nxObject = builder.Commit();
         builder.Destroy();

         if (name != null) 
             nxObject.SetName(name);
            
         if (color > 0) 
         {
             DisplayModification display = theSession.DisplayManager.NewDisplayModification();
             display.ApplyToAllFaces = true;
             display.ApplyToOwningParts = false;
             display.NewColor = color;

             DisplayableObject[] objects = new DisplayableObject[1];
             objects[0] = point;
             display.Apply(objects);

             theSession.UpdateManager.DoUpdate(0);

             display.Dispose();
         }

         return nxObject;
     }

     /// <summary>
     /// 基于NXOpen创建多个单点特征
     /// </summary>
     /// <param name="coord">坐标列表</param>
     /// <param name="name">特征命名</param>
     /// <param name="color">特征颜色</param>
     /// <returns>点特征对象列表</returns>
     public static NXObject[] BuildPoints(this IEnumerable<double[]> coords, string name = null, int color = -1)
     {
         DisplayableObject[] displayObjects = new DisplayableObject[coords.Count()];
         NXObject[] nxObjects = new NXObject[coords.Count()];

         for (int i = 0; i < coords.Count(); i++) 
         {
             double[] coord = coords.ElementAt(i);
             Point3d point3d = new Point3d(coord[0], coord[1], coord[2]);
             Point point = WorkPart.Points.CreatePoint(point3d);
             PointFeatureBuilder builder = WorkPart.BaseFeatures.CreatePointFeatureBuilder(null);
             builder.Point = point;

             NXObject nxObject = builder.Commit();
             builder.Destroy();

             if (name != null) 
                 nxObject.SetName(name);
             
             nxObjects[i] = nxObject;
             displayObjects[i] = point;
         }

         if (color > 0) 
         {
             DisplayModification display = theSession.DisplayManager.NewDisplayModification();
             display.ApplyToAllFaces = true;
             display.ApplyToOwningParts = false;
             display.NewColor = color;
             display.Apply(displayObjects);
             theSession.UpdateManager.DoUpdate(0);
             display.Dispose();
         }

         return nxObjects;
     }
 }

基于UFun

using System.Collections.Generic;
using System.Linq;
using NXOpen;

 public static class PointsBuilder_UseUFun
 {
     private static readonly NXOpen.UF.UFSession theUFSession = NXOpen.UF.UFSession.GetUFSession();

     /// <summary>
     /// 基于UFun创建单点特征
     /// </summary>
     /// <param name="coord">坐标[x,y,z]</param>
     /// <param name="name">特征命名</param>
     /// <param name="color">特征颜色</param>
     /// <returns>特征Tag</returns>
     public static Tag BuildPoint(this double[] coord, string name = null, int color = -1)
     {
         theUFSession.Curve.CreatePoint(coord, out Tag tag);

         if (name != null) 
             theUFSession.Obj.SetName(tag, name);
         if (color > 0) 
             theUFSession.Obj.SetColor(tag, color);

         return tag;
     }

     /// <summary>
     /// 基于UFun创建点集特征
     /// </summary>
     /// <param name="coord">坐标列表</param>
     /// <param name="name">特征命名</param>
     /// <param name="color">特征颜色</param>
     /// <returns>特征Tag</returns>
     public static Tag BuildPoints(this IEnumerable<double[]> coords, string name = null, int color = -1)
     {
         Tag[] pointTags = new Tag[coords.Count()];

         int i = 0;
         foreach (var coord in coords)
         {
             theUFSession.Curve.CreatePoint(coord, out Tag pointTag);

             if (color > 0) 
                 theUFSession.Obj.SetColor(pointTag, color);
             if (name != null) 
                 theUFSession.Obj.SetName(pointTag, name);

             pointTags[i++] = pointTag;
         }

         theUFSession.Modl.CreatePointsFeature(pointTags.Length, pointTags, out Tag pointsTag);
         if (color > 0) 
             theUFSession.Obj.SetColor(pointsTag, color);
         if (name != null) 
             theUFSession.Obj.SetName(pointsTag, name);

         return pointsTag;
     }
 }