开发实战
第五章:组件开发实战
5.1 标准开发流程
一个完整的组件开发过程通常遵循以下模式:
1. 创建 Windows 窗体应用 (.NET Framework 4.8),输出类型改为类库
2. 引用 SDK DLL
3. 定义参数数据类 ([AttributeFields])
4. 创建 WinForms 窗口 (拖控件)
5. 实现组件主类(继承对应 Plugin 基类)
6. 写 CalcParams() → 算坐标系 + 算尺寸
7. 写 CreateXxx() → 创建场景对象
8. 配调试参数 → F5 启动调试
9. 反复测试、修正
5.2 节点组件标准结构
节点组件是所有类型中应用最广泛的。以下是其标准的代码骨架:
[Plugin("MyJoint")]
[PluginUserInterface("MyForm")]
[PluginNumber(10001)]
[SecondaryType(ConnectionPlugin.SecondaryType.SECONDARYTYPE_ONE)]
public class MyJoint : ConnectionPlugin
{
Connection comp;
Beam mainPart; // PrimaryObject
Beam subPart; // SecondaryObjects[0]
public MyData data; // 窗口传入的参数
Matrix refMat; // 坐标系
public MyJoint(MyData data) { this.data = data; }
public override bool Run(Connection componentObject)
{
comp = componentObject;
mainPart = comp.PrimaryObject as Beam;
subPart = comp.SecondaryObjects[0] as Beam;
if (mainPart == null || subPart == null) return false;
CalcParams();
comp.Scene.SetCurrentTransformationPlane(new TransformationPlane(refMat));
CreateFitting();
CreatePlate();
CreateBolts();
CreateWeld();
refMat.Inverse();
comp.Scene.SetCurrentTransformationPlane(new TransformationPlane(refMat));
return true;
}
}
这个骨架在所有节点组件中几乎一模一样,差异只在于 CalcParams() 里的几何计算和 CreateXxx() 里的具体建模逻辑。
5.3 自定义组件标准结构
与节点组件不同,自定义组件需要实现 DefineInput() 来采集用户场景输入:
[Plugin("MyCustom")]
[PluginUserInterface("MyForm")]
[PluginNumber(10002)]
public class MyCustom : CustomPlugin
{
Component comp;
public MyData data;
List<Point> inputPoints;
Matrix refMat;
public MyCustom(MyData data) { this.data = data; }
// 采集用户输入(自定义组件必须实现)
public override List<InputDefinition> DefineInput()
{
try
{
var picker = new Picker();
var pts = picker.PickPoints(
Picker.PickPointEnum.PICK_TWO_POINTS, "请选择起点和终点");
if (pts == null || pts.Count < 2)
return new List<InputDefinition>();
var input = new List<InputDefinition>();
input.Add(new InputDefinition(pts));
return input;
}
catch (UserInterruptException)
{
return null; // 用户按 ESC 取消
}
}
public override bool Run(Component componentObject,
List<InputDefinition> input)
{
comp = componentObject;
if (input == null || input.Count == 0) return false;
inputPoints = input[0].GetInput() as List<Point>;
if (inputPoints == null || inputPoints.Count < 2) return false;
CalcParams();
comp.Scene.SetCurrentTransformationPlane(new TransformationPlane(refMat));
CreateParts();
refMat.Inverse();
comp.Scene.SetCurrentTransformationPlane(new TransformationPlane(refMat));
return true;
}
}
5.4 细部组件要点
细部组件与节点组件类似,但有两点不同:
- 只有 一个主体零件 (
comp.PrimaryObject) - 可选的辅助参考点 (
comp.ReferancePoint),通过[PluginRefPoint(true)]开启
[Plugin("MyDetail")]
[PluginUserInterface("MyForm")]
[PluginNumber(10003)]
[PluginRefPoint(true)] // ← 细部专属
public class MyDetail : DetailPlugin
{
Detail comp;
Beam mainPart;
public MyData data;
Matrix refMat;
public override bool Run(Detail componentObject)
{
comp = componentObject;
comp.Select(); // 细部先 Select
mainPart = comp.PrimaryObject as Beam;
Point refPt = comp.ReferancePoint; // 用户选的参考点
// ...
}
}