有人问我,怎么做草图直线约束到基准轴上。其实录一下就可以了。
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>
#include <NXOpen/DatumAxis.hxx>
#include <NXOpen/DatumCollection.hxx>
#include <NXOpen/SketchConstraintBuilder.hxx>
#include <NXOpen/ModelingView.hxx>
#include <NXOpen/ModelingViewCollection.hxx>
#include <NXOpen/SelectNXObject.hxx>
#include <NXOpen/SelectNXObjectList.hxx>
#include <NXOpen/SelectObject.hxx>
#include <NXOpen/SelectObjectList.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(50, 10, 0);
Point3d endPoint1(100, 10, 0);
Line *line1 = workPart->Curves()->CreateLine(startPoint1, endPoint1);
//添加到草图里
sketch1->AddGeometry(line1, Sketch::InferConstraintsOptionInferCoincidentConstraints);//参数二,自动推断出约束
//1.由创建几何约束方法使用,以指示约束应该应用于什么几何
Sketch::ConstraintGeometry geom_line1;
geom_line1.Geometry = line1;//几何对象
geom_line1.PointType = Sketch::ConstraintPointTypeNone;//点的类型
geom_line1.SplineDefiningPointIndex = 0;