NX二次开发-NXOPEN C++方式创建草图,添加约束,标注尺寸

NX9+VS2012


//CreateSketch

// Mandatory UF Includes
#include <uf.h>
#include <uf_object_types.h>

// Internal Includes
#include <NXOpen/ListingWindow.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/UI.hxx>

// Internal+External Includes
#include <NXOpen/Annotations.hxx>
#include <NXOpen/Assemblies_Component.hxx>
#include <NXOpen/Assemblies_ComponentAssembly.hxx>
#include <NXOpen/Body.hxx>
#include <NXOpen/BodyCollection.hxx>
#include <NXOpen/Face.hxx>
#include <NXOpen/Line.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Session.hxx>


//头文件
#include <NXOpen/Sketch.hxx>
#include <NXOpen/SketchCollection.hxx>
#include <NXOpen/SketchInPlaceBuilder.hxx>
#include <NXOpen/Plane.hxx>
#include <NXOpen/PlaneCollection.hxx>
#include <NXOpen/Features_Feature.hxx>
#include <NXOpen/Features_FeatureBuilder.hxx>
#include <NXOpen/Features_FeatureCollection.hxx>
#include <NXOpen/Curve.hxx>
#include <NXOpen/CurveCollection.hxx>
#include <NXOpen/Line.hxx>
#include <NXOpen/LineCollection.hxx>
#include <NXOpen/Preferences_SessionPreferences.hxx>
#include <NXOpen/Preferences_SessionSketch.hxx>
#include <NXOpen/Preferences_SketchPreferences.hxx>
#include <NXOpen/Point.hxx>
#include <NXOpen/PointCollection.hxx>
#include <NXOpen/Expression.hxx>
#include <NXOpen/ExpressionCollection.hxx>



// Std C++ Includes
#include <iostream>
#include <sstream>

using namespace NXOpen;
using std::string;
using std::exception;
using std::stringstream;
using std::endl;
using std::cout;
using std::cerr;


//------------------------------------------------------------------------------
// NXOpen c++ test class 
//------------------------------------------------------------------------------
class MyClass
{
    // class members
public:
    static Session *theSession;
    static UI *theUI;

    MyClass();
    ~MyClass();

    void do_it();
    void print(const NXString &);
    void print(const string &);
    void print(const char*);

private:
    Part *workPart, *displayPart;
    NXMessageBox *mb;
    ListingWindow *lw;
    LogFile *lf;
};

//------------------------------------------------------------------------------
// Initialize static variables
//------------------------------------------------------------------------------
Session *(MyClass::theSession) = NULL;
UI *(MyClass::theUI) = NULL;

//------------------------------------------------------------------------------
// Constructor 
//------------------------------------------------------------------------------
MyClass::MyClass()
{

    // Initialize the NX Open C++ API environment
    MyClass::theSession = NXOpen::Session::GetSession();
    MyClass::theUI = UI::GetUI();
    mb = theUI->NXMessageBox();
    lw = theSession->ListingWindow();
    lf = theSession->LogFile();

    workPart = theSession->Parts()->Work();
    displayPart = theSession->Parts()->Display();

}

//------------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------------
MyClass::~MyClass()
{
}

//------------------------------------------------------------------------------
// Print string to listing window or stdout
//------------------------------------------------------------------------------
void MyClass::print(const NXString &msg)
{
    if(! lw->IsOpen() ) lw->Open();
    lw->WriteLine(msg);
}
void MyClass::print(const string &msg)
{
    if(! lw->IsOpen() ) lw->Open();
    lw->WriteLine(msg);
}
void MyClass::print(const char * msg)
{
    if(! lw->IsOpen() ) lw->Open();
    lw->WriteLine(msg);
}




//------------------------------------------------------------------------------
// Do something
//------------------------------------------------------------------------------
void MyClass::do_it()
{

    // TODO: add your code here

    //在任务环境中绘制草图,不加就是直接草图
    theSession->BeginTaskEnvironment();

    NXOpen::Sketch *nullNXOpen_Sketch(NULL);
    //按平面方式创建草图
    NXOpen::SketchInPlaceBuilder *sketchInPlaceBuilder1;
    sketchInPlaceBuilder1 = workPart->Sketches()->CreateNewSketchInPlaceBuilder(nullNXOpen_Sketch);

    //设置平面选项
    sketchInPlaceBuilder1->SetPlaneOption(Sketch::PlaneOptionNewPlane);

    //创建平面(Z平面)
    sketchInPlaceBuilder1->Plane()->SetMethod(PlaneTypes::MethodTypeFixedZ);

    //连续自动标注尺寸
    theSession->Preferences()->Sketch()->SetContinuousAutoDimensioning(false);

    //生成
    NXOpen::NXObject *nXObject1;
    nXObject1 = sketchInPlaceBuilder1->Commit();

    //设置对象属性的名字
    nXObject1->SetName("ObjectName");

    //转换成Feature
    NXOpen::Sketch *sketch1(dynamic_cast<NXOpen::Sketch *>(nXObject1));
    NXOpen::Features::Feature *feature1;
    feature1 = sketch1->Feature();

    //设置草图特征的名字
    feature1->SetName("SketchFeatureName");

    //销毁
    sketchInPlaceBuilder1->Destroy();

    //退出任务环境草图,不加就是直接草图
    theSession->EndTaskEnvironment();

    //激活草图
    sketch1->Activate(NXOpen::Sketch::ViewReorientTrue);//参数是否将视图定向到草图

    //创建四条直线(做矩形)
    Point3d startPoint1(0, 0, 0);
    Point3d endPoint1(100, 0, 0);
    Line *line1 = workPart->Curves()->CreateLine(startPoint1, endPoint1);

    Point3d startPoint2(100, 0, 0);
    Point3d endPoint2(100, -100, 0);
    Line *line2 = workPart->Curves()->CreateLine(startPoint2, endPoint2);

    Point3d startPoint3(100, -100, 0);
    Point3d endPoint3(0, -100, 0);
    Line *line3 = workPart->Curves()->CreateLine(startPoint3, endPoint3);

    Point3d startPoint4(0, -100, 0);
    Point3d endPoint4(0, 0, 0);
    Line *line4 = workPart->Curves()->CreateLine(startPoint4, endPoint4);

    //添加到草图里
    sketch1->AddGeometry(line1, Sketch::InferConstraintsOptionInferCoincidentConstraints);//参数二,自动推断出约束
    sketch1->AddGeometry(line2, Sketch::InferConstraintsOptionInferCoincidentConstraints);
    sketch1->AddGeometry(line3, Sketch::InferConstraintsOptionInferCoincidentConstraints);
    sketch1->AddGeometry(line4, Sketch::InferConstraintsOptionInferCoincidentConstraints);

    //1.由创建几何约束方法使用,以指示约束应该应用于什么几何
    Sketch::ConstraintGeometry geom_line1;
    geom_line1.Geometry = line1;//几何对象
    geom_line1.PointType = Sketch::ConstraintPointTypeNone;//点的类型
    geom_line1.SplineDefiningPointIndex = 0;//忽略,除非点类型是SplineDefiningPoint
    //创建一个水平约束
    sketch1->CreateHorizontalConstraint(geom_line1);

    //2.
    Sketch::ConstraintGeometry geom_line2;
    geom_line2.Geometry = line2;
    geom_line2.PointType = Sketch::ConstraintPointTypeNone;
    geom_line2.SplineDefiningPointIndex = 0;
    //创建一个垂直约束
    sketch1->CreateVerticalConstraint(geom_line2);

    //3.
    Sketch::ConstraintGeometry geom_line3;
    geom_line3.Geometry = line3;
    geom_line3.PointType = Sketch::ConstraintPointTypeNone;
    geom_line3.SplineDefiningPointIndex = 0;
    //创建一个水平约束
    sketch1->CreateHorizontalConstraint(geom_line3);

    //4.
    Sketch::ConstraintGeometry geom_line4;
    geom_line4.Geometry = line4;
    geom_line4.PointType = Sketch::ConstraintPointTypeNone;
    geom_line4.SplineDefiningPointIndex = 0;
    //创建一个垂直约束
    sketch1->CreateVerticalConstraint(geom_line4);

    //1.
    Sketch::ConstraintGeometry geom_line1_startPoint;
    geom_line1_startPoint.Geometry = line1;//几何对象(直线)
    geom_line1_startPoint.PointType = Sketch::ConstraintPointTypeStartVertex;//通过这条线找到它的起始端点
    geom_line1_startPoint.SplineDefiningPointIndex = 0;//忽略,除非点类型是SplineDefiningPoint

    //得到草图原点坐标
    Point3d SketchOri = sketch1->Origin();

    //创建一个点
    Point *OriPoint = workPart->Points()->CreatePoint(SketchOri);

    //2.
    Sketch::ConstraintGeometry geom_OriPoint;
    geom_OriPoint.Geometry = OriPoint;//几何对象(点)
    geom_OriPoint.PointType = Sketch::ConstraintPointTypeNone;//点的类型为空
    geom_OriPoint.SplineDefiningPointIndex = 0;//忽略,除非点类型是SplineDefiningPoint

    //创建点到点约束
    sketch1->CreateCoincidentConstraint(geom_line1_startPoint, geom_OriPoint);

    //标注尺寸约束(直线1)
    NXObject *nullNXObject(NULL);
    Sketch::DimensionGeometry dimLine1_startPoint;
    dimLine1_startPoint.Geometry = line1;
    dimLine1_startPoint.AssocType = Sketch::AssocTypeStartPoint;//起点
    dimLine1_startPoint.AssocValue = 0;
    dimLine1_startPoint.HelpPoint.X = 0.0;
    dimLine1_startPoint.HelpPoint.Y = 0.0;
    dimLine1_startPoint.HelpPoint.Z = 0.0;
    dimLine1_startPoint.View = nullNXObject;

    Sketch::DimensionGeometry dimLine1_endPoint;
    dimLine1_endPoint.Geometry = line1;
    dimLine1_endPoint.AssocType = Sketch::AssocTypeEndPoint;//终点
    dimLine1_endPoint.AssocValue = 0;
    dimLine1_endPoint.HelpPoint.X = 0.0;
    dimLine1_endPoint.HelpPoint.Y = 0.0;
    dimLine1_endPoint.HelpPoint.Z = 0.0;
    dimLine1_endPoint.View = nullNXObject;

    Point3d dimOri1(100,15,0);//尺寸位置放置的点
    Expression *dimExp1 = workPart->Expressions()->CreateSystemExpression("A1=200");//创建表达式
    sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine1_startPoint, dimLine1_endPoint, dimOri1, dimExp1, Sketch::DimensionOptionCreateAsDriving);

    //标注尺寸约束(直线2)
    Sketch::DimensionGeometry dimLine2_startPoint;
    dimLine2_startPoint.Geometry = line2;
    dimLine2_startPoint.AssocType = Sketch::AssocTypeStartPoint;//起点
    dimLine2_startPoint.AssocValue = 0;
    dimLine2_startPoint.HelpPoint.X = 0.0;
    dimLine2_startPoint.HelpPoint.Y = 0.0;
    dimLine2_startPoint.HelpPoint.Z = 0.0;
    dimLine2_startPoint.View = nullNXObject;

    Sketch::DimensionGeometry dimLine2_endPoint;
    dimLine2_endPoint.Geometry = line2;
    dimLine2_endPoint.AssocType = Sketch::AssocTypeEndPoint;//终点
    dimLine2_endPoint.AssocValue = 0;
    dimLine2_endPoint.HelpPoint.X = 0.0;
    dimLine2_endPoint.HelpPoint.Y = 0.0;
    dimLine2_endPoint.HelpPoint.Z = 0.0;
    dimLine2_endPoint.View = nullNXObject;

    Point3d dimOri2(210,-100,0);//尺寸位置放置的点
    Expression *dimExp2 = workPart->Expressions()->CreateSystemExpression("A2=200");//创建表达式
    sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine2_startPoint, dimLine2_endPoint, dimOri2, dimExp2, Sketch::DimensionOptionCreateAsDriving);

    //完成草图
    sketch1->Deactivate(Sketch::ViewReorientTrue, Sketch::UpdateLevelModel);//参数一,不重新定位视图到草图.参数二,更新完整的模型和草图

}

//------------------------------------------------------------------------------
// Entry point(s) for unmanaged internal NXOpen C/C++ programs
//------------------------------------------------------------------------------
//  Explicit Execution
extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen )
{
    try
    {
        // Create NXOpen C++ class instance
        MyClass *theMyClass;
        theMyClass = new MyClass();
        theMyClass->do_it();
        delete theMyClass;
    }
    catch (const NXException& e1)
    {
        UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message());
    }
    catch (const exception& e2)
    {
        UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());
    }
    catch (...)
    {
        UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception.");
    }
}


//------------------------------------------------------------------------------
// Unload Handler
//------------------------------------------------------------------------------
extern "C" DllExport int ufusr_ask_unload()
{
    return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
}


Caesar卢尚宇
2020年8月15日

代码步骤解析

1.在任务环境中绘制草图(去Session类里)

//在任务环境中绘制草图,不加就是直接草图
theSession->BeginTaskEnvironment();


//退出任务环境草图,不加就是直接草图
theSession->EndTaskEnvironment();

 2.找到SketchCollection类,创建Builder,创建草图

NXOpen::Sketch *nullNXOpen_Sketch(NULL);
//按平面方式创建草图
NXOpen::SketchInPlaceBuilder *sketchInPlaceBuilder1;
sketchInPlaceBuilder1 = workPart->Sketches()->CreateNewSketchInPlaceBuilder(nullNXOpen_Sketch);

只介绍在平面和基于路径两种方式(类里的其他创建方法不做介绍)

在平面上创建草图

 基于路径创建草图

 3.设置草图参数(定义Builder里的参数)

//设置平面选项
sketchInPlaceBuilder1->SetPlaneOption(Sketch::PlaneOptionNewPlane);

//创建平面(Z平面)
sketchInPlaceBuilder1->Plane()->SetMethod(PlaneTypes::MethodTypeFixedZ);

按需定义需要的参数

这里我定义了两个参数

设置平面选项为新建基准平面

 这里是个枚举类型

返回指定的基准面

 

 定义枚举(Z平面)

 4.定义草图首选项参数(去Session类里)

//连续自动标注尺寸
theSession->Preferences()->Sketch()->SetContinuousAutoDimensioning(false);

 

 5.生成草图(调用Builder->Commit)

//生成
NXOpen::NXObject *nXObject1;
nXObject1 = sketchInPlaceBuilder1->Commit();

SketchInPlaceBuilder下没有找到->Commit()方法

去公共成员函数里继承->SetName方法

 

 6.设置对象属性的名字

//设置对象属性的名字
nXObject1->SetName("ObjectName");

 7.将NXObject对象强制转换成Feature特征类型

//转换成Feature
NXOpen::Sketch *sketch1(dynamic_cast<NXOpen::Sketch *>(nXObject1));
NXOpen::Features::Feature *feature1;
feature1 = sketch1->Feature();

 8.设置草图特征的名字

//设置草图特征的名字
feature1->SetName("SketchFeatureName");

去公共成员函数里继承->SetName方法

 9.销毁Builder

//销毁
sketchInPlaceBuilder1->Destroy();

去公共成员函数里继承->Destroy方法

 10.激活草图

//激活草图
sketch1->Activate(NXOpen::Sketch::ViewReorientTrue);//参数是否将视图定向到草图

 定义枚举

 11.创建直线

//创建四条直线(做矩形)
Point3d startPoint1(0, 0, 0);
Point3d endPoint1(100, 0, 0);
Line *line1 = workPart->Curves()->CreateLine(startPoint1, endPoint1);

Point3d startPoint2(100, 0, 0);
Point3d endPoint2(100, -100, 0);
Line *line2 = workPart->Curves()->CreateLine(startPoint2, endPoint2);

Point3d startPoint3(100, -100, 0);
Point3d endPoint3(0, -100, 0);
Line *line3 = workPart->Curves()->CreateLine(startPoint3, endPoint3);

Point3d startPoint4(0, -100, 0);
Point3d endPoint4(0, 0, 0);
Line *line4 = workPart->Curves()->CreateLine(startPoint4, endPoint4);

 12.将曲线或点添加到草图里

//添加到草图里
sketch1->AddGeometry(line1, Sketch::InferConstraintsOptionInferCoincidentConstraints);//参数二,自动推断出约束
sketch1->AddGeometry(line2, Sketch::InferConstraintsOptionInferCoincidentConstraints);
sketch1->AddGeometry(line3, Sketch::InferConstraintsOptionInferCoincidentConstraints);
sketch1->AddGeometry(line4, Sketch::InferConstraintsOptionInferCoincidentConstraints);

 定义枚举

 13.创建几何约束(竖直,水平,点到点重合等)

//1.由创建几何约束方法使用,以指示约束应该应用于什么几何
Sketch::ConstraintGeometry geom_line1;
geom_line1.Geometry = line1;//几何对象
geom_line1.PointType = Sketch::ConstraintPointTypeNone;//点的类型
geom_line1.SplineDefiningPointIndex = 0;//忽略,除非点类型是SplineDefiningPoint
//创建一个水平约束
sketch1->CreateHorizontalConstraint(geom_line1);

//2.
Sketch::ConstraintGeometry geom_line2;
geom_line2.Geometry = line2;
geom_line2.PointType = Sketch::ConstraintPointTypeNone;
geom_line2.SplineDefiningPointIndex = 0;
//创建一个垂直约束
sketch1->CreateVerticalConstraint(geom_line2);

//3.
Sketch::ConstraintGeometry geom_line3;
geom_line3.Geometry = line3;
geom_line3.PointType = Sketch::ConstraintPointTypeNone;
geom_line3.SplineDefiningPointIndex = 0;
//创建一个水平约束
sketch1->CreateHorizontalConstraint(geom_line3);

//4.
Sketch::ConstraintGeometry geom_line4;
geom_line4.Geometry = line4;
geom_line4.PointType = Sketch::ConstraintPointTypeNone;
geom_line4.SplineDefiningPointIndex = 0;
//创建一个垂直约束
sketch1->CreateVerticalConstraint(geom_line4);
//1.
Sketch::ConstraintGeometry geom_line1_startPoint;
geom_line1_startPoint.Geometry = line1;//几何对象(直线)
geom_line1_startPoint.PointType = Sketch::ConstraintPointTypeStartVertex;//通过这条线找到它的起始端点
geom_line1_startPoint.SplineDefiningPointIndex = 0;//忽略,除非点类型是SplineDefiningPoint

//得到草图原点坐标
Point3d SketchOri = sketch1->Origin();

//创建一个点
Point *OriPoint = workPart->Points()->CreatePoint(SketchOri);

//2.
Sketch::ConstraintGeometry geom_OriPoint;
geom_OriPoint.Geometry = OriPoint;//几何对象(点)
geom_OriPoint.PointType = Sketch::ConstraintPointTypeNone;//点的类型为空
geom_OriPoint.SplineDefiningPointIndex = 0;//忽略,除非点类型是SplineDefiningPoint

//创建点到点约束
sketch1->CreateCoincidentConstraint(geom_line1_startPoint, geom_OriPoint);

 点进去是个结构体,里面有例子说明(按照说明去定义)

 创建水平约束

 创建竖直约束

 创建点到点约束

 如果需要创建其他几何约束,去找对于方法就行了。

14.创建尺寸约束

//标注尺寸约束(直线1)
NXObject *nullNXObject(NULL);
Sketch::DimensionGeometry dimLine1_startPoint;
dimLine1_startPoint.Geometry = line1;
dimLine1_startPoint.AssocType = Sketch::AssocTypeStartPoint;//起点
dimLine1_startPoint.AssocValue = 0;
dimLine1_startPoint.HelpPoint.X = 0.0;
dimLine1_startPoint.HelpPoint.Y = 0.0;
dimLine1_startPoint.HelpPoint.Z = 0.0;
dimLine1_startPoint.View = nullNXObject;

Sketch::DimensionGeometry dimLine1_endPoint;
dimLine1_endPoint.Geometry = line1;
dimLine1_endPoint.AssocType = Sketch::AssocTypeEndPoint;//终点
dimLine1_endPoint.AssocValue = 0;
dimLine1_endPoint.HelpPoint.X = 0.0;
dimLine1_endPoint.HelpPoint.Y = 0.0;
dimLine1_endPoint.HelpPoint.Z = 0.0;
dimLine1_endPoint.View = nullNXObject;

Point3d dimOri1(100,15,0);//尺寸位置放置的点
Expression *dimExp1 = workPart->Expressions()->CreateSystemExpression("A1=200");//创建表达式
sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine1_startPoint, dimLine1_endPoint, dimOri1, dimExp1, Sketch::DimensionOptionCreateAsDriving);

//标注尺寸约束(直线2)
Sketch::DimensionGeometry dimLine2_startPoint;
dimLine2_startPoint.Geometry = line2;
dimLine2_startPoint.AssocType = Sketch::AssocTypeStartPoint;//起点
dimLine2_startPoint.AssocValue = 0;
dimLine2_startPoint.HelpPoint.X = 0.0;
dimLine2_startPoint.HelpPoint.Y = 0.0;
dimLine2_startPoint.HelpPoint.Z = 0.0;
dimLine2_startPoint.View = nullNXObject;

Sketch::DimensionGeometry dimLine2_endPoint;
dimLine2_endPoint.Geometry = line2;
dimLine2_endPoint.AssocType = Sketch::AssocTypeEndPoint;//终点
dimLine2_endPoint.AssocValue = 0;
dimLine2_endPoint.HelpPoint.X = 0.0;
dimLine2_endPoint.HelpPoint.Y = 0.0;
dimLine2_endPoint.HelpPoint.Z = 0.0;
dimLine2_endPoint.View = nullNXObject;

Point3d dimOri2(210,-100,0);//尺寸位置放置的点
Expression *dimExp2 = workPart->Expressions()->CreateSystemExpression("A2=200");//创建表达式
sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine2_startPoint, dimLine2_endPoint, dimOri2, dimExp2, Sketch::DimensionOptionCreateAsDriving);

 定义枚举(尺寸的类型)

 定义结构体里的参数,里面有例子参考

创建表达式给尺寸用

 设置枚举定义尺寸类型(驱动尺寸还是参考尺寸)

 15.完成草图

//完成草图
sketch1->Deactivate(Sketch::ViewReorientTrue, Sketch::UpdateLevelModel);//参数一,不重新定位视图到草图.参数二,更新完整的模型和草图

 定义枚举

 

 补充 除了上述方法添加几何约束和尺寸标注外,也可以用单独的Buider方法去创建

    SketchConstraintBuilder *sketchConstraintBuilder1;
    sketchConstraintBuilder1 = workPart->Sketches()->CreateConstraintBuilder();
    SketchRapidDimensionBuilder *sketchRapidDimensionBuilder1;
    sketchRapidDimensionBuilder1 = workPart->Sketches()->CreateRapidDimensionBuilder();

NX二次开发-UFUN C方式创建草图,添加约束,标注尺寸 https://www.cnblogs.com/nxopen2018/p/13511058.html

Caesar卢尚宇

2020年8月15日

原文地址:https://www.cnblogs.com/nxopen2018/p/13510239.html