基于.NET三維控件的個性化管道軟件開發
1 簡介
管道廣泛用于化工、工廠、建筑、市政等方面,關系到國計民生。雖然管道設計軟件種類繁多,有的也非常強大(然而也非常昂貴),但也并不能完全滿足個性化需要。 如何快速開發一款滿足自己需求的三維管道設計軟件?本文提供一種基于AnyCAD .NET SDK的開發解決方案,不妨一試。
2 方案
《.NET6: 開發基于WPF的摩登三維工業軟件》系列提供了一種插件式的開發工業軟件的框架(源碼詳見:https://gitee.com/anycad/RapidCAX )。本案例基于該框架開發。
整體上采用MVVM模式,以直管為例,整體架構如下:
注:上述架構在《.NET6: 開發基于WPF的摩登三維工業軟件》系列有詳細說明。
3 實現
本案例中,管道由直管和彎管組成,兩種的參數不一樣,因此需要按照不同的類型來處理。由于篇幅所限,此處以直管為例的MVVM架構實現。
3.1 Model實現
// 定義管子的參數
public class PipeModel : ElementModel
{
public GPnt Position = new GPnt();
public GDir Direction = new GDir();
public double Thickness = 2;
public double InnerRadius = 5;
public double Length = 100;
public PipeModel()
{
}
// 根據參數創建幾何
public TopoShape CreateShape()
{
return ShapeBuilder.MakeTube(Position, Direction, InnerRadius, Thickness, Length);
}
}
//參數化機制實現
class PipeSchema : ElementSchema
{
public PipeSchema()
: base(nameof(PipeModel))
{
this.SetTitle("管");
}
public override ElementModel CreateModel()
{
return new PipeModel();
}
public override Element OnCreateInstance()
{
return new ShapeInstance();
}
public override bool OnParameterChanged(Document document, Element instance, ParameterDict parameters)
{
var element = ShapeInstance.Cast(instance);
if (element == null)
return false;
PipeModel model = new PipeModel();
model.Load(parameters);
var shape = model.CreateShape();
element.SetShape(shape);
return true;
}
}
3.2 ViewModel實現
internal class PipeViewModel : ElementViewModel
{
public PipeModel Model { get { return (PipeModel)_Model; } }
public PipeViewModel(Element model, Document doc)
: base(new PipeModel(), model, doc)
{
SetPickFilter(EnumShapeFilter.VertexEdgeFace);
}
public GPnt Position
{
get => Model.Position;
set => SetProperty(nameof(Position), ref Model.Position, value, nameof(X), nameof(Y), nameof(Z));
}
public double X
{
get => Model.Position.x;
set
{
if(X != value)
{
Position = new GPnt(value, Model.Position.y, Model.Position.z);
}
}
}
public double Y
{
get => Model.Position.y;
set
{
if (Y != value)
{
Position = new GPnt(Model.Position.x, value, Model.Position.z);
}
}
}
public double Z
{
get => Model.Position.z;
set
{
if (Z != value)
{
Position = new GPnt(Model.Position.x, Model.Position.y, value);
}
}
}
public GDir Direction
{
get => Model.Direction;
set => SetProperty(nameof(Direction), ref Model.Direction, value);
}
public double InnerRadius
{
get => Model.InnerRadius;
set => SetProperty(nameof(InnerRadius), ref Model.InnerRadius, value,
() => { return value > 0; }, nameof(OutterRadius));
}
public double Thickness
{
get => Model.Thickness;
set => SetProperty(nameof(Thickness

本文從技術的角度提供一種管道建模的方案,能夠快速驗證想法,但距離實際應用還有一段距離,達到生產應用還需要精雕細琢。基于AnyCAD Rapid .NET框架提供的建模、顯示、數據管理等基礎設施,可以快速驗證產品原型,大大地縮短產品研發周期,使產品能夠更快的產生價值。