WilsonBlr
Aerospace
- Jun 11, 2014
- 19
I am working on an External NX application using C and C++ API with NX 8.5.
I am trying to fit curves and surfaces through some point data that I have. I am adding the points to an array and trying to fit a curve through those using StudioSplineBuilder. I need a NURBS curve so I use studioSplineBuilder. But the issue that I get is the functions SetKnots() and SetContents() show error "Access Violation reading location 0x0000000 ". The other set methods for studioSplineBuilder1 above SetKnots seem to work.
I am not able to figure out what I am missing here. Is there something with the settings that I needto add ?
I have added following lib references in the project settings.
libnxopencpp.lib
libnxopencpp_features.lib
libnxopencpp_geometricutilities.lib
libnxopencpp_preferences.lib
libugopenint.lib
libufun.lib
I am using Visual Studio 2005 for development. I have tried the same with Visual Studio 2010 also but no success. Any help would be highly appreciated.
Code:
// These includes are in different Header file
#include<NXOpen\Face.hxx>
#include<NXOpen\Edge.hxx>
#include<NXOpen\EdgeTangentRule.hxx>
#include <FaceTangentRule.hxx>
#include <SelectionIntentRule.hxx>
#include <DisplayModification.hxx>
#include <DisplayManager.hxx>
#include <GeometricAnalysis_DeviationGaugeBuilder.hxx>
#include <GeometricAnalysis_AnalysisManager.hxx>
#include <GeometricAnalysis_AnalysisObjectCollection.hxx>
#include <Plane.hxx>
#include <PlaneCollection.hxx>
#include <Features_SelectFeature.hxx>
#include <Features_SelectFeatureList.hxx>
#include <Unit.hxx>
#include <UnitCollection.hxx>
#include <ModelingView.hxx>
#include <ModelingViewCollection.hxx>
#include <NXObject.hxx>
#include <Layout.hxx>
#include <LayoutCollection.hxx>
#include <ExpressionCollection.hxx>
#include <SelectNXObjectList.hxx>
#include <Point.hxx>
#include <Xform.hxx>
#include <PartCollection.hxx>
#include <Part.hxx>
#include <NXObjectManager.hxx>
#include <PointCollection.hxx>
#include <Features_StudioSpline.hxx>
#include <Features_StudioSplineBuilder.hxx>
#include <Features_StudioSplineBuilderEx.hxx>
#include <Features_FeatureCollection.hxx>
#include <Features_FeatureGroup.hxx>
#include <Features_PointFeature.hxx>
#include <Features_ThroughCurves.hxx>
#include <Features_ThroughCurvesBuilder.hxx>
#include <Features_ThroughCurveMesh.hxx>
#include <Features_ThroughCurveMeshBuilder.hxx>
#include <Preferences_PartPreferences.hxx>
#include <Preferences_PartModeling.hxx>
#include <Preferences_SessionModeling.hxx>
#include <Preferences_SessionPreferences.hxx>
#include <Spline.hxx>
#include <ScalarCollection.hxx>
#include <XformCollection.hxx>
#include <SectionCollection.hxx>
#include <SectionList.hxx>
#include <CurveDumbRule.hxx>
#include <ScRuleFactory.hxx>
//NXOpen header files
#include <NXOpen/Session.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Line.hxx>
#include <NXOpen/CurveCollection.hxx>
#include <NXOpen/Arc.hxx>
#include <NXOpen/NXObjectManager.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/ListingWindow.hxx>
//UFunc Headers
#include <uf_curve.h>
#include <uf.h>
#include <uf_csys.h>
#include <uf_part.h>
// UGOpen headers
#include <ug_session.hxx>
#include <ug_exception.hxx>
#include <ug_part.hxx>
#include <uf_obj.h>
#include <uf_vec.h>
#include <uf_attr.h>
#include <uf_mtx.h>
#include <NXOpen/FileNew.hxx>
///////////////////////////////////
//NX Open C++ header files
#include <NXOpen/Session.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Point.hxx>
#include <NXOpen/Line.hxx>
#include <NXOpen/CurveCollection.hxx>
#include <NXOpen/Arc.hxx>
#include <NXOpen/NXObjectManager.hxx>
#include <NXOpen/NXException.hxx>
//Open C Headers
//UFunc Headers
#include <uf.h>
#include <uf_ui.h>
#include <uf_exit.h>
#include <uf_defs.h>
#include <uf_modl_curves.h>
#include <uf_curve.h>
#include <uf.h>
#include <uf_csys.h>
#include <uf_part.h>
#include <uf_eval.h>
#include <uf_forgeo.h>
#include <uf_point.h>
#include <uf_curve.h>
#include <uf_modl_utilities.h>
#include <uf_modl_freeform.h>
#include <uf_modl.h>
#include <uf_evalsf.h>
#include <uf_defs.h>
#include <uf_modl_types.h>
// standard headers
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
using namespace std;
using namespace NXOpen;
NXOpen::Spline* createSpline(vector<Point3d> points, NXOpen::Session *theSession)
{
//We grab the current part before we can work on it.
Part *workPart(theSession->Parts()->Work());
//Now we need to create a studiosplinebuilder object. This is what we will use to build our spline,
//it contains the necessary functions. We also set some necessary parameters for the studiosplinebuilder.
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(5);
studioSplineBuilder1->SetPeriodic(false);
studioSplineBuilder1->SetMatchKnots(Features::StudioSplineBuilder::MatchKnotsTypeNone);
//A few more required objects that we don't need to elaborate on
std::vector<double> knots1(0);
studioSplineBuilder1->SetKnots(knots1);
std::vector<double> parameters1(0);
studioSplineBuilder1->SetParameters(parameters1);
//These objects are required for the loop that adds the points to the spline
Features::GeometricConstraintData *geometricConstraintData1;
Direction *nullDirection(NULL);
Scalar *nullScalar(NULL);
Offset *nullOffset(NULL);
std::vector<Features::GeometricConstraintData*> constraints1(points.size());
//This loop is where the points are actually inserted into the spline
for(unsigned int j = 0; j < points.size(); j++)
{
geometricConstraintData1 = studioSplineBuilder1->ConstraintManager()->CreateGeometricConstraintData();
//The code contained in SetPoint() creates a point from the current index of point objects
geometricConstraintData1->SetPoint(workPart->Points()->CreatePoint(points.at(j)));
geometricConstraintData1->SetAutomaticConstraintDirection(Features::GeometricConstraintData::ParameterDirectionIso);
geometricConstraintData1->SetAutomaticConstraintType(Features::GeometricConstraintData::AutoConstraintTypeNone);
geometricConstraintData1->SetTangentDirection(nullDirection);
geometricConstraintData1->SetTangentMagnitude(nullScalar);
geometricConstraintData1->SetCurvature(nullOffset);
geometricConstraintData1->SetCurvatureDerivative(nullOffset);
geometricConstraintData1->SetHasSymmetricModelingConstraint(false);
constraints1[j] = geometricConstraintData1;
}
studioSplineBuilder1->ConstraintManager()->SetContents(constraints1);
//Here is where we actually create the object and insert it into the workview. We also extract the spline from the feature.
Features::Feature *feature1;
feature1 = studioSplineBuilder1->CommitFeature();
Spline* returnSpline = studioSplineBuilder1->Curve();
return returnSpline;
}
int testSpline()
{
cout << "Creating point array" << endl;
vector<Point3d> points(0);
points.push_back(NXOpen::Point3d(0.551833582E+01, -.751787484E+00, -.164708939E+02));
points.push_back(NXOpen::Point3d(0.553420258E+01, -0.722469866E+00, -0.164536076E+02));
points.push_back(NXOpen::Point3d(0.555618429E+01, -0.690917611E+00, -0.164276180E+02));
points.push_back(NXOpen::Point3d(0.557990170E+01, -0.661347866E+00, -0.163984585E+02 ));
points.push_back(NXOpen::Point3d(0.559568548E+01, -0.642132103E+00, -0.163788280E+02 ));
points.push_back(NXOpen::Point3d(0.562709475E+01, -0.603930891E+00, -0.163393974E+02 ));
points.push_back(NXOpen::Point3d(0.567364693E+01, -0.547425508E+00, -0.162801094E+02 ));
points.push_back(NXOpen::Point3d(0.571962833E+01, -0.491778433E+00, -0.162205620E+02 ));
points.push_back(NXOpen::Point3d(0.576506901E+01, -0.437105417E+00, -0.161607571E+02 ));
points.push_back(NXOpen::Point3d(0.581000900E+01, -0.383398294E+00, -0.161007004E+02 ));
points.push_back(NXOpen::Point3d(0.585456467E+01, -0.330540240E+00, -0.160402966E+02 ));
points.push_back(NXOpen::Point3d(0.589891195E+01, -0.278634399E+00, -0.159793291E+02 ));
points.push_back(NXOpen::Point3d(0.594296455E+01, -0.228052020E+00, -0.159179573E+02 ));
points.push_back(NXOpen::Point3d(0.598668861E+01, -0.178970233E+00, -0.158562908E+02 ));
points.push_back(NXOpen::Point3d(0.603012180E+01, -0.131551802E+00, -0.157943335E+02 ));
points.push_back(NXOpen::Point3d(0.607330799E+01, -0.855702534E-01, -0.157320976E+02 ));
points.push_back(NXOpen::Point3d(0.611628342E+01, -0.410230793E-01, -0.156695929E+02 ));
points.push_back(NXOpen::Point3d(0.615908098E+01, 0.209187460E-02, -0.156068258E+02 ));
cout << "Initializing the Open C API environment - UF_initialize()" << endl;
/* Initialize the Open C API environment */
int errorCode = UF_initialize();
if ( 0 != errorCode )
throw NXOpen::NXException::Create(errorCode);
cout << "Initializing NX Open C++ API environment - NXOpen::Session::GetSession()" << endl;
/* Initialize the NX Open C++ API environment */
NXOpen::Session *theSession = NXOpen::Session::GetSession();
/* Create a new part.
To create new part one can use Open C API as well as NX Open C++ API.
*/
/* To create new part using Open C API. */
char* partName = "testSpline.prt";
tag_t partTag;
cout << "Creating UG Part using C API - UF_PART_new " << endl;
UF_PART_new(partName,2,&partTag);
NXOpen::Part *part1 = NULL;
cout << "Casting part to C++ object from part tag " << endl;
part1 = theSession->Parts()->Work(); // Session::GetSession()->Parts()->Work();
part1 = theSession->Parts()->Work();
partTag = part1->GetTag();
if(partTag == NULL)
{
std::cerr << "Could not create Part. Does it already exist from a from previous run?" << std::endl;
return 1;
}
cout << "Calling CreateSpline() ....." << endl;
NXOpen::Spline* l_spline = createSpline( points, theSession);
/* To get the part from the part tag */
part1 = (NXOpen::Part*)NXOpen::NXObjectManager::Get(partTag);
cout << "Save part " << endl;
/* To save work part using NX Open C++ APIs */
NXOpen::PartSaveStatus *partSaveStatus = part1->Save(NXOpen::Part::SaveComponentsTrue, NXOpen::Part::CloseAfterSaveTrue);
/* To close all parts using NX Open C++ APIs */
theSession->Parts()->CloseAll(NXOpen::BasePart::CloseModifiedCloseModified, NULL);
cout << "Terminate session - UF_terminate() " << endl;
/* Terminate the Open C API environment */
errorCode = UF_terminate();
return 0;
}