Band.cpp

#include "../Headers/Band.h"

 

const double PI = 3.1415926535897932;

 

static struct UDPPrimitiveTypeInfo primitiveInfo =

{

"Band",

"Create a band, shaft or outer region",

"Ansys Incorporation",

"06-01-2012",

"2.0"

};

 

extern "C" DLLEXPORT

struct UDPPrimitiveTypeInfo* GetPrimitiveTypeInfo()

{

return &primitiveInfo;

}

 

struct UDPPrimitiveParameterDefinition primParams[] =

{

{"DiaGap", "Band diameter in gap center, DiaGap<DiaYoke for outer band",

kUDPLengthUnit, 101},

{"DiaYoke", "Band diameter on yoke side, DiaYoke<DiaGap for inner band",

kUDPLengthUnit, 40},

{"Length", "Band length", kUDPLengthUnit, 200},

 

{"SegAngle", "Deviation angle for band (0.1~5 degrees).", kUDPAngleUnit, 3},

{"Fractions", "Number of circumferential fractions, 1 for circular region.",

kUDPNoUnit, 1},

{"HalfAxial", "0: full model; 1: half model in axial direction.", kUDPNoUnit, 0},

{"InfoCore", "0: band; 1: tool; 2: independent; 3: dependent; 100: region.", kUDPNoUnit, 0}

};

 

static int numOfParameters = sizeof(primParams)/sizeof(primParams[0]);

 

extern "C" DLLEXPORT

int GetPrimitiveParametersDefinition(struct UDPPrimitiveParameterDefinition**

paramDefinition)

{

*paramDefinition = primParams;

return numOfParameters;

}

 

extern "C" DLLEXPORT

char* GetLengthParameterUnits()

{

return "mm";

}

 

static CBand obj;

 

// In case of error this function should return 0

extern "C" DLLEXPORT

int AreParameterValuesValid(char ** error, double* paramValues)

{

obj.IniParameters(paramValues, numOfParameters);

return obj.AreParametersValid(error);

}

 

extern "C" DLLEXPORT

int MapParametersDefinitionVersions(double** newParamValues, char* oldVersion,

int oldNumberOfParams, struct UDPPrimitiveParameterDefinition* oldParamDefinition)

{

obj.IniParameters(*newParamValues, numOfParameters);

obj.MapParameters(oldParamDefinition, oldNumberOfParams);// map para by keywords

 

int verN = getVerNum(oldVersion);

if (verN < 200) {

obj.SetDefaultParameter(*newParamValues, "HalfAxial", 0);

}

 

return 1;

}

 

extern "C" DLLEXPORT

long CreatePrimitive(struct UDPFunctionLib* functionLib, void* callbackData, double* paramValues)

{

obj.SetParameterPointer(paramValues);

return obj.CreatePrimitive(functionLib, callbackData);

}

 

//***************************************************

// Member function definition for class CBand

//***************************************************

void CBand::GetParameters()

{

diaGap = diaYoke = 0;

length = 0;

infoCore = 0;

infoOut = -1;

 

segAng = 3;

numFras = 1;

infoHalf = 0;

 

for (int i=0; i<numOfParams; i++) {

if (GetValue(i, "DiaGap", diaGap)) continue;

if (GetValue(i, "DiaYoke", diaYoke)) continue;

if (GetValue(i, "Length", length)) continue;

 

if (GetValue(i, "SegAngle", segAng)) continue;

if (GetValue(i, "Fractions", numFras)) continue;

if (GetValue(i, "HalfAxial", infoHalf)) continue;

if (GetValue(i, "InfoCore", infoCore)) continue;

}

rRegion = diaYoke / 2;

}

 

void CBand::PutParameters()

{

for (int i=0; i<numOfParams; i++) {

if (PutValue(i, "DiaGap", diaGap)) continue;

if (PutValue(i, "DiaYoke", diaYoke)) continue;

if (PutValue(i, "Length", length)) continue;

 

if (PutValue(i, "SegAngle", segAng)) continue;

if (PutValue(i, "Fractions", numFras)) continue;

if (PutValue(i, "InfoCore", infoCore)) continue;

}

}

 

char* CBand::ValidateParameters()

{

if (!paramValues) return 0;

 

if (diaYoke < 0) diaYoke = 0;

if (diaGap <= 0) return "DiaGap must be larger than 0.";

if (length < 0) length = 0; // length==0 for 2D geometry

infoOut = diaYoke>diaGap ? 1 : -1;

 

if (numFras < 1) numFras = 1;

if (segAng > 5) segAng = 5;

if (segAng <= 0) segAng = 0;

else if (segAng < 0.1) segAng = 0.1;

if (segAng > 0) {

numSegs = (int)(360/segAng + 0.5);

int remain = numSegs % numFras;

if (remain > 0) numSegs += numFras - remain;

segAng = 360.0 / numSegs;

}

else numSegs = 0;

 

if (infoCore < 100) {

if (infoCore < 0) infoCore = 0;

if (infoCore > 3) infoCore = 3;

}

 

if (infoCore >= 100) {

if (diaYoke <= 0) return "DiaYoke must be larger than 0.";

}

else if (infoCore >= 1) { // for tool; independent & dependent

if (diaYoke <= diaGap) return "DiaYoke must be larger than DiaGap.";

}

 

return 0;

}

 

long CBand::SweepAlongZAxis(long obj)

{

if (obj == -1) return obj;

if (functionLib==0 || callbackData==0) return obj;

if (length == 0) return obj;

 

double z0 = infoHalf==0 ? -length/2 : 0;

UDPSweepOptions sweepOptions = {kUDPRoundDraft, 0.0, 0.0};

long sweepPath = CreateZAxisPath(z0, length/2);

if (sweepPath == -1) return obj;

functionLib->sweepAlongPath(obj, sweepPath, &sweepOptions, callbackData);

 

return obj;

}

 

long CBand::CreateBand()

{

long band = -1;

double r0 = diaGap / 2;

if (numSegs == 0) band = CreateRegion(r0);

else {

double angle = 2*PI / numSegs;

int n = numSegs / numFras;

double x0 = r0 * cos(angle);

double y0 = r0 * sin(angle);

 

UDPPosition pos[] = { 0, 0, 0,

r0, 0, 0,

x0, y0, 0};

band = CreateSheet(pos, 3);

band = SweepAlongZAxis(band);

band = DuplicateAroundAxis(band, kUDPZAxis, n, angle*180/PI);

}

if (diaYoke <= diaGap) return band;

 

long region = CreateRegion();

return SubObject(region, band);

}

 

long CBand::CreateTool()

{

UDPPosition p0 = {0, 0, -length/2};

double r0 = 0.55 * diaYoke;

long tool = functionLib->createCylinder(kUDPZAxis, &p0, r0, length, callbackData);

long region = CreateRegion();

return SubObject(tool, region);

}

 

// Create independent or dependent

long CBand::CreateIndependent(bool isIndependent)

{

double r0 = 0.5 * diaYoke;

UDPPosition startPos = {0, 0, 0};

UDPPosition endPos = {r0, 0, 0};

long independent = CreateLine(startPos, endPos);

if (!isIndependent) RotateAroundZVector(independent, 360.0/numFras);

return SweepAlongZAxis(independent);

}

 

long CBand::CreateRegion(double r0, int fras)

{

if (r0 == 0) r0 = diaYoke/2;

if (fras == 0) fras = numFras;

UDPPosition startPos = {0, 0, 0};

UDPPosition endPos = {r0, 0, 0};

long region = CreateLine(startPos, endPos);

region = SweepAlongZAxis(region);

return SweepAroundAxis(region, kUDPZAxis, 360.0/fras);

}

 

long CBand::CreatePrimitive(UDPFunctionLib* functionLib, void* callbackData)

{

if (!checkOK) return 0;

 

// display information of modified parameters

SetFunctionLib(functionLib, callbackData);

DisplayInfo();

 

// create region

if (infoCore == 100) {

long region = CreateRegion();

return region==-1 ? 0 : 1;

}

 

// create tool

if (infoCore == 1) {

long tool = CreateTool();

return tool==-1 ? 0 : 1;

}

 

// create independent

if (infoCore == 2) {

long independent = CreateIndependent();

return independent==-1 ? 0 : 1;

}

 

// create dependent

if (infoCore == 3) {

long dependent = CreateIndependent(false);

return dependent==-1 ? 0 : 1;

}

 

// create band

long band = CreateBand();

return band==-1 ? 0 : 1;

}