• NX二次开发-将草图直线约束到基准轴上SketchConstraintBuilder


    有人问我,怎么做草图直线约束到基准轴上。其实录一下就可以了。

    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;//忽略,除非点类型是SplineDefiningPoint
        //创建一个水平约束
        sketch1->CreateHorizontalConstraint(geom_line1);
    
        //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
    
        //创建点到Y轴约束
        SketchConstraintBuilder *sketchConstraintBuilder1;
        sketchConstraintBuilder1 = workPart->Sketches()->CreateConstraintBuilder();
        Point3d point1_1(0, 0, 0);
        View *nullView(NULL);
        sketchConstraintBuilder1->GeometryToConstrain()->Add(InferSnapType::SnapTypeStart, line1, workPart->ModelingViews()->WorkView(), point1_1, NULL, nullView, point1_1);
        DatumAxis *datumAxis1(dynamic_cast<DatumAxis *>(workPart->Datums()->FindObject("DATUM_CSYS(0) Y axis")));
        sketchConstraintBuilder1->GeometryToConstrainTo()->SetValue(datumAxis1, workPart->ModelingViews()->WorkView(), point1_1);sketchConstraintBuilder1->Commit();
        sketchConstraintBuilder1->Destroy();
    
        //完成草图
        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月31日

  • 相关阅读:
    17岁韩寒在CCTV《对话》舌战群吊的视频
    在线LaTex编辑器
    PowerShell 点滴记录
    程序阅读理解题目(高中语文版,附答案)
    jQuery Validate 应用
    ASP.net MVC 向子视图传递数据
    分库分表(sharding)后主键全局唯一性的解决方案
    Ajax局部刷新例子
    限制 Flash 在指定域名/网址中播放 (Flash 防盗链)
    javascript实现二级联动下拉框
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/13593671.html
Copyright © 2020-2023  润新知