mildcat 发表于 2013-12-11 11:14:40

NX二次开发源码:通过选择点创建样条曲线


NX二次开发源码:通过选择点创建样条曲线



#include <uf_defs.h>
#include <uf.h>
#include <uf_modl.h>
#include <uf_object_types.h>
#include <uf_ui.h>
#include <uf_modl.h>
#include <<em>NXOpen</em>/NXException.hxx>
#include <<em>NXOpen</em>/Session.hxx>
#include <<em>NXOpen</em>/Selection.hxx>
#include <<em>NXOpen</em>/Builder.hxx>
#include <<em>NXOpen</em>/Features_Feature.hxx>
#include <<em>NXOpen</em>/Features_FeatureBuilder.hxx>
#include <<em>NXOpen</em>/Features_FeatureCollection.hxx>
#include <<em>NXOpen</em>/Features_GeometricConstraintData.hxx>
#include <<em>NXOpen</em>/Features_GeometricConstraintDataManager.hxx>
#include <<em>NXOpen</em>/Features_StudioSplineBuilder.hxx>
#include <<em>NXOpen</em>/Part.hxx>
#include <<em>NXOpen</em>/PartCollection.hxx>
#include <<em>NXOpen</em>/Point.hxx>
#include <<em>NXOpen</em>/PointCollection.hxx>
#include <<em>NXOpen</em>/NXString.hxx>
#include <<em>NXOpen</em>/UI.hxx>
#include <<em>NXOpen</em>/Spline.hxx>
#include <<em>NXOpen</em>/NXObjectManager.hxx>

using namespace <em>NXOpen</em>;
using namespace std;

extern "<em>C</em>" DllExport int ufusr_ask_unload()
{
    return (int)Session::LibraryUnloadOptionImmediately;
}

static vector<Point *> selectPoints(NXString prompt)
{
    UI *ui = UI::GetUI();
    Selection *sm = ui->SelectionManager();
    std::vector<Selection::MaskTriple> mask(1);
    mask = Selection::MaskTriple(UF_point_type, 0, 0);
    std::vector<NXObject *> objects;

    sm->SelectObjects("Select Points", prompt,
      Selection::SelectionScopeAnyInAssembly,
      Selection::SelectionActionClearAndEnableSpecific,
      false, false, mask, objects);

    vector<Point *>selPoints(objects.size());
    for (unsigned int ii = 0; ii < objects.size(); ii++)
      selPoints = dynamic_cast<Point *>(objects);

    return selPoints;
}

static Spline *createStudioSplineThruPoints(vector<Point *> thePoints)
{
    Session *theSession = Session::GetSession();
    Part *workPart(theSession->Parts()->Work());

    Session::UndoMarkId markId1;
    markId1 = theSession->SetUndoMark(Session::MarkVisibilityVisible,
      "Studio Spline Thru Points");

    Features::StudioSpline *nullFeatures_StudioSpline(NULL);

    Features::StudioSplineBuilder *studioSplineBuilder1;
    studioSplineBuilder1 = workPart->Features()->
      CreateStudioSplineBuilder(nullFeatures_StudioSpline);
    studioSplineBuilder1->SetAssociative(true);
    studioSplineBuilder1->
      SetInputCurveOption(Features::StudioSplineBuilder::CurveOptionRetain);
    studioSplineBuilder1->
      SetSplineMethod(Features::StudioSplineBuilder::MethodThroughPoints);
    studioSplineBuilder1->SetDegree((int)thePoints.size() - 1);
    studioSplineBuilder1->SetPeriodic(false);
    studioSplineBuilder1->
      SetMatchKnots(Features::StudioSplineBuilder::MatchKnotsTypeNone);
    std::vector<double> knots1(0);
    studioSplineBuilder1->SetKnots(knots1);
    std::vector<double> parameters1(0);
    studioSplineBuilder1->SetParameters(parameters1);
    Direction *nullDirection(NULL);
    Scalar *nullScalar(NULL);
    Offset *nullOffset(NULL);

    std::vector<Features::GeometricConstraintData *>
      geometricConstraintData(thePoints.size());

    for (unsigned int ii = 0; ii < thePoints.size(); ii++)
    {
      geometricConstraintData = studioSplineBuilder1->
            ConstraintManager()->CreateGeometricConstraintData();
      geometricConstraintData->SetPoint(thePoints);
      geometricConstraintData->SetAutomaticConstraintDirection(
            Features::GeometricConstraintData::ParameterDirectionIso);
      geometricConstraintData->SetAutomaticConstraintType(
            Features::GeometricConstraintData::AutoConstraintTypeNone);
      geometricConstraintData->SetTangentDirection(nullDirection);
      geometricConstraintData->SetTangentMagnitude(nullScalar);
      geometricConstraintData->SetCurvature(nullOffset);
      geometricConstraintData->SetCurvatureDerivative(nullOffset);
      geometricConstraintData->SetHasSymmetricModelingConstraint(false);
    }

    studioSplineBuilder1->ConstraintManager()->SetContents(
      geometricConstraintData);

    Features::Feature *feature1 = studioSplineBuilder1->CommitFeature();
    Spline *theSpline = studioSplineBuilder1-><em>Curve</em>();

    studioSplineBuilder1->Destroy();

    return theSpline;
}

extern "<em>C</em>" DllExport void ufusr(char *param, int *retcod, int param_len)
{
    std::vector<Point *>thePoints = selectPoints("Studio Spline Thru Points");
    if (!thePoints.size()) return;

    Spline *theSpline = createStudioSplineThruPoints(thePoints);

    if (theSpline)
    {
      theSpline->Highlight();
      uc1601("Studio Spline Created Thru Points", TRUE);
      theSpline->Unhighlight();
    }
}
<p> </p>
页: [1]
查看完整版本: NX二次开发源码:通过选择点创建样条曲线