Maxwell 3D v11版已经支持用户自己定义建立参数模型,比如基本模型中没有管状件,需要生成两个圆柱,然后布尔运算才可以实现。这里我们就给Maxwell添加参数化建立这个模型的功能。
在VC中建立一个普通Win32的动态连接库DLL项目Tube,在项目中添加UserDefinedPrimitiveStructures.h和UserDefinedPrimitiveDLLInclude.h两个文件,这两个文件放在X:/Program Files/Ansoft/Maxwell11/UserDefinedPrimitives/Examples/Headers目录中。
然后在Tube.cpp中添加如下代码:
#include "Headers/UserDefinedPrimitiveDLLInclude.h" //包含预定义
static struct UDPPrimitiveTypeInfo primitiveInfo =
{
"TubeModel", //模型标题
"Create a tube model with Z axis as axis. Written by Hottomson", //关于模型的说明
"CHINA", //单位信息
"08-22-2006", //开发日期
"1.0" //版本
};
extern "C" DLLEXPORT
struct UDPPrimitiveTypeInfo* GetPrimitiveTypeInfo()
{
return &primitiveInfo;
}
struct UDPPrimitiveParameterDefinition primParams[] =
{
{"Xpos", "X Position of center point", kUDPLengthUnit, 0},
{"Ypos", "Y Position of center point", kUDPLengthUnit, 0},
{"OutRad", "Outer Radius of tube", kUDPLengthUnit, 15},
{"inRad", "Inner Radius of tube", kUDPLengthUnit, 12},
{"Length", "Length of tube model", kUDPLengthUnit, 100},
//定义所有用到的参数名称、说明、单位、默认值
};
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";
}
// Incase of error this function should return 0
extern "C" DLLEXPORT
int AreParameterValuesValid(char ** error, double* paramValues)
{ //输入数值的有效性判断
double inRad = paramValues[3];
double OutRad = paramValues[2];
double length = paramValues[4];//数值的取出与上面参数表的定义对应
if (inRad <= 0)
{
*error = "Inner Radius of tube should be more than 0.";
return 0;
}
if (OutRad <= 0)
{
*error = "Outer Radius of tube should be more than 0.";
return 0;
}
if (length <= 0)
{
*error = "Length of tube model should be more than 0.";
return 0;
}
if (OutRad <= inRad)
{
*error = "Outer Diameter of tube should be more than the inner Diameter.";
return 0;
}
return 1;
}
extern "C" DLLEXPORT
long CreatePrimitive(struct UDPFunctionLib* functionLib, void* callbackData, double* paramValues)
{
//生成实体过程
functionLib->addMessage(kUDPInfoMessage, "Start to create tube model", callbackData);
//在消息栏添加开始生成模型的消息
double inRad = paramValues[3];
double outRad = paramValues[2];
double length = paramValues[4];
struct UDPPosition startPt = {0, 0, 0};
startPt.z=-length/2;
long idInCyl=functionLib->createCylinder(kUDPZAxis, &startPt, inRad, length, callbackData);
//生成较小的圆柱体
if (idInCyl == -1)
{
functionLib->addMessage(kUDPErrorMessage, "Could not create inner cylinder", callbackData);
return 0;
}
long idOutCyl=functionLib->createCylinder(kUDPZAxis, &startPt, outRad, length, callbackData);
//生成较大的圆柱体
if (idOutCyl == -1)
{
functionLib->addMessage(kUDPErrorMessage, "Could not create outer cylinder", callbackData);
return 0;
}
long success = functionLib->subtract(&idOutCyl, 1, &idInCyl, 1, callbackData);
//两个圆柱体相减
if (success == 0)
{
functionLib->addMessage(kUDPErrorMessage, "Could not sweep profile along path", callbackData);
return 0;
}
functionLib->addMessage(kUDPInfoMessage, "Create tube model end", callbackData);
//在消息栏添加生成模型结束的消息
return success;
}
编译输出,将生成的Tube.dll复制到目录X:/Program Files/Ansoft/Maxwell11/userlib/UserDefinedPrimitives中,此时再打开Maxwell V11,在菜单Draw>User Defined Primitive>UserLib中就出现了Tube这个命令,点击后就出现如同其它实体输入参数的对话框,确定后实体就生成了。
这里生成的实体比较简单,如果你的项目中有的一系列复杂实体,只是几个参数不同,就可以采用这种方法来实现,操作比适用VBScript还要方便,当然这要你有点编程的功底哦。