BimBase的Schema+C#的方式实现参数化建组件功能
·
目前这套流程只实现过基座系统(2025年11月14日的版本)
Schema编辑器这套

apikey需要申请

在软件安装目录找到












C#代码
引用

整体代码结构(注意要用x64编译)
ApplicationLoad.cs
using BIMBaseCS.Attributes;
using BIMBaseCS.Data;
using BIMBaseCS.UI;
using NeXus;
using NeXus.BIMBase;
using NeXus.BIMBase.Data;
using NeXus.Tracer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HelloBIMBase2
{
public class ApplicationLoad : BIMBase.Plugin.IExternalApplication
{
public void onStartup()
{
MessageBox.Show("Hello BIMBase from C#");
registerObject();
registerTools();
}
static public void registerObject()
{
BPObjectExtensionManager.getInstance().registerBPObjectExtension("NewSCharpDemo_sDgkgM", "MyCube", MyCube.ObjectExtension);
}
static public void registerTools()
{
PBBimToolsInterfaceManager.RegisterFactory("MyCube", PBBIM.IToolNameProperty, MyCubePropertyFactory.PropertyFactory);
ToolsManager.inst().regTool("ExLayoutCubeDemo", ExLayoutCubeDemoTool.Creator);
}
}
}
ExLayoutCubeDemoTool.cs
using HelloBIMBase2;
using NeXus;
using NeXus.BIMBase;
using NeXus.BIMBase.Core;
using NeXus.BIMBase.Data;
using NeXus.p3d;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Contexts;
//二进制存储加的
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HelloBIMBase2
{
//序列化
public class SerializeObjectToString
{
public byte[] SerializeObject(Object obj)
{
IFormatter formatter = new BinaryFormatter();
byte[] result = null;
// byte
using (MemoryStream stream = new MemoryStream())
{
formatter.Serialize(stream, obj);
byte[] byt = new byte[stream.Length];
byt = stream.ToArray();
result = byt;
stream.Flush();
}
return result;
}
//反序列化
public Object DerializeObject(byte[] bt)
{
IFormatter formatter = new BinaryFormatter();
Object obj = null;
using (Stream stream = new MemoryStream(bt, 0, bt.Length))
{
obj = formatter.Deserialize(stream);
}
return obj;
}
}
}
namespace HelloBIMBase2
{
public class BIMBaseWindow : System.Windows.Forms.IWin32Window
{
public IntPtr Handle
{
get { return TRAFFIC.getMainFrame(); }
}
}
public class ExLayoutCubeDemoTool : PrimitiveTool<ExLayoutCubeDemoTool>
{
GePoint3d m_ptS;
GePoint3d m_ptE;
int m_nFlag = 0;
int mLayoutType = 0;
FormLayoutCube form = null;
public ExLayoutCubeDemoTool()
{
mLayoutType = 0;
}
protected override void onPostInstall()
{
form = new FormLayoutCube();
form.Show(new BIMBaseWindow());
}
protected override void onCleanup()
{
if (form != null) { form.Close(); }
//base.onCleanup();
}
protected override void onExitTool()
{
base.onExitTool();
}
protected override bool onLeftClick(BPBaseButtonEvent ev)
{
P3DStatus status;
BPProject project = ev.getViewport().getTargetModel().getBPProject();
BPModel model = ev.getViewport().getTargetModel();
if (mLayoutType == 0)
{
MyCube cube = new MyCube();
BPPlacement placement = cube.placement;
placement.setOrigin(ev.getPoint());
cube.setPlacement(placement);
HelloBIMBase2.SerializeObjectToString trans = new HelloBIMBase2.SerializeObjectToString();
//把GePoint3d的数据存二进制
GePoint3d pt = cube.placement.getOrigin();
List<double> points = new List<double>();
points.Add(pt.x);
points.Add(pt.y);
points.Add(pt.z);
byte[] data = trans.SerializeObject(points);
status = cube.addToProject(project, model.getModelId());
}
else
{
if (m_nFlag == 0)
{
m_nFlag = 1;
GePoint3d pt = (ev.getPoint());
m_ptS = GePoint3d.create(pt.x, pt.y, pt.z);
}
else if (m_nFlag == 1)
{
m_nFlag = 0;
GePoint3d pt = (ev.getPoint());
m_ptE = GePoint3d.create(pt.x, pt.y, pt.z);
MyCube cube = new MyCube
{
Length = (int)m_ptS.distance(m_ptE)
};
GeTransform trans = __setTransform();
BPPlacement place = new BPPlacement();
place.fromTransform(trans);
cube.setPlacement(place);
//把GePoint3d的数据存二进制
HelloBIMBase2.SerializeObjectToString ser = new HelloBIMBase2.SerializeObjectToString();
GePoint3d pt0 = cube.placement.getOrigin();
List<double> points = new List<double>();
points.Add(pt0.x);
points.Add(pt0.y);
points.Add(pt0.z);
byte[] data = ser.SerializeObject(points);
status = cube.addToProject(project, model.getModelId());
}
}
return true;
}
public GeTransform __setTransform()
{
GeVec3d vecX = GeVec3d.createByStartEndNormalize(m_ptS, m_ptE);
GeVec3d vecZ = GeVec3d.create(0, 0, 1);
GeVec3d vecY = vecZ.cross(vecX);
GeTransform trans = GeTransform.createByOriginAndVectors(m_ptS, vecX, vecY, vecZ);
return trans;
}
protected override bool onRightClick(BPBaseButtonEvent ev)
{
BPModel model = ev.getViewport().getRootModel();
BPProject project = model.getBPProject();
BPEntityArray entities = BPEntityUtil.getEntitiesOfModel(out _, project, model.getModelId());
if (entities.getCount() == 0)
{
callExitTool();
return false;
}
BPEntity ent = entities.getByIndex((uint)(entities.getCount() - 1));
if (ent == null)
{
callExitTool();
return true;
}
BPObject obj = ObjectExtensionManager.getBPObject(ent);
if (obj == null)
{
callExitTool();
return true;
}
MyCube cube = obj as MyCube;
if (cube == null)
{
callExitTool();
return true;
}
//HelloBIMBase2.SerializeObjectToString trans = new HelloBIMBase2.SerializeObjectToString();
//Object sysobj = trans.DerializeObject(cube.binaryData);
//List<double> points = new List<double>();
//points = sysobj as List<double>;
//GePoint3d pt = GePoint3d.create(points[0], points[1], points[2]);
callExitTool();
return true;
}
protected override bool onMouseMove(BPBaseButtonEvent ev)
{
if (!getDynamicsStarted())
_beginDynamics();
return true;
}
protected override void onDynamicFrame(BPBaseButtonEvent ev)
{
mLayoutType = form.mLayoutType;
BPProject project = ev.getViewport().getTargetModel().getBPProject();
BPModel model = ev.getViewport().getTargetModel();
if (mLayoutType == 0)
{
MyCube cube = new MyCube();
BPGraphics graphics = cube.createPhysicalGraphics(project, model.getModelId(), true);
if (graphics == null) return;
GePoint3d pt = GePoint3d.create(ev.getPoint().x, ev.getPoint().y);
GeTransform trans = GeTransform.create(pt);
BPGraphicsUtils.transformPhysicalGraphics(graphics, trans);
graphics.finish();
BPRedrawEntitys redrawElems = new BPRedrawEntitys();
redrawElems.setDrawMode(BPDrawMode.enTempDraw);
redrawElems.setDrawPurpose(BPDrawPurpose.enDynamics);
redrawElems.setDynamicsViews(ev.getViewport());
redrawElems.doRedraw(graphics.getEntityR());
}
else
{
if (m_nFlag == 0) return;
GePoint3d pt = GePoint3d.create(ev.getPoint().x, ev.getPoint().y);
m_ptE = GePoint3d.create(ev.getPoint().x, ev.getPoint().y);
MyCube cube = new MyCube();
cube.Length = (int)m_ptS.distance(m_ptE);
BPGraphics graphics = cube.createPhysicalGraphics(project, model.getModelId(), true);
GeTransform trans = __setTransform();
BPGraphicsUtils.transformPhysicalGraphics(graphics, trans);
graphics.finish();
BPRedrawEntitys redrawElems = new BPRedrawEntitys();
redrawElems.setDrawMode(BPDrawMode.enTempDraw);
redrawElems.setDrawPurpose(BPDrawPurpose.enDynamics);
redrawElems.setDynamicsViews(ev.getViewport());
redrawElems.doRedraw(graphics.getEntityR());
}
}
}
}
FormLayoutCube.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HelloBIMBase2
{
public partial class FormLayoutCube : Form
{
public int mLayoutType { get { return mmLayoutType; } }
int mmLayoutType = 0;
public FormLayoutCube()
{
InitializeComponent();
singlePoint.Checked = true;
twoPoint.Checked = false;
}
private void SinglePoint_CheckedChanged(object sender, EventArgs e)
{
if (singlePoint.Checked)
mmLayoutType = 0;
}
private void TwoPoint_CheckedChanged(object sender, EventArgs e)
{
if (twoPoint.Checked)
mmLayoutType = 1;
}
}
}
MyCube.cs
using BIMBaseCS.Attributes;
using BIMBaseCS.UI;
using NeXus;
using NeXus.BIMBase;
using NeXus.BIMBase.Core;
using NeXus.p3d;
using NeXus.Traffic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace HelloBIMBase2
{
public class MyCube: GraphicElement<MyCube>
{
public double Length { get; set; } = 1000.0;
public double Width { get; set; } = 1000.0;
public double Height { get; set; } = 1000.0;
public string Message { get; set; } = "成功";
protected override string _getClassName()
{
return "MyCube";
}
protected override string _getSchemaName()
{
return "NewSCharpDemo_sDgkgM";
}
protected override P3DStatus _copyToData(BPData data, BPProject project)
{
P3DStatus status = 0;
status = base._copyToData(data, project);
if (status != P3DStatus.SUCCESS)
return status;
BPValue val;
val = new BPValue();
val.setDouble(this.Length);
status = data.setValue("Length", val);
if (status != P3DStatus.SUCCESS)
return status;
val = new BPValue();
val.setDouble(this.Width);
status = data.setValue("Width", val);
if (status != P3DStatus.SUCCESS)
return status;
val = new BPValue();
val.setDouble(this.Height);
status = data.setValue("Height", val);
if (status != P3DStatus.SUCCESS)
return status;
val = new BPValue();
val.setString(this.Message);
status = data.setValue("Message", val);
if (status != P3DStatus.SUCCESS)
return status;
return P3DStatus.SUCCESS;
}
protected override P3DStatus _initFromData(BPData instance)
{
P3DStatus status = 0;
status = base._initFromData(instance);
if (status != P3DStatus.SUCCESS)
return status;
BPValue val;
val = new BPValue();
status = instance.getValue(val, "Length");
if (status != P3DStatus.SUCCESS)
return status;
this.Length = val.getDouble();
val = new BPValue();
status = instance.getValue(val,"Width" );
if (status != P3DStatus.SUCCESS)
return status;
this.Width = val.getDouble();
val = new BPValue();
status = instance.getValue(val,"Height" );
if (status != P3DStatus.SUCCESS)
return status;
this.Height = val.getDouble();
val = new BPValue();
status = instance.getValue(val,"Message" );
if (status != P3DStatus.SUCCESS)
return status;
this.Message = val.getString();
return P3DStatus.SUCCESS;
}
protected override BPGraphics _createPhysicalGraphics(BPProject project, PModelId modelId, bool isDynamics)
{
BPModel model = project.loadModelById(modelId);
BPGraphics graphics = model.createPhysicalGraphics();
if (graphics == null)
{
return graphics;
}
GePoint3d Point1 = GePoint3d.create(this.Length, 0, 0);
GePoint3d Point2 = GePoint3d.create(this.Length, this.Width, 0);
GePoint3d Point3 = GePoint3d.create(0, this.Width, 0);
GePoint3d Point4 = GePoint3d.create(0, 0, 0);
GePoint3dCollection listp = new GePoint3dCollection
{
Point1,
Point2,
Point3,
Point4
};
GeCurveArray curve = GeCurveArray.createLinestringArray(listp, GeCurveArray.BoundaryType.BOUNDARY_TYPE_Outer, false);
GeExtrusionInfo info = new GeExtrusionInfo(curve, GeVec3d.create(0, 0, this.Height), true);
IGeSolidBase solid = IGeSolidBase.createGeExtrusion(info);
BPSymbology symb = BPGraphics.getDefaultSymbology();
symb.weight = 3;
//symb.color = (uint)System.Drawing.Color.Blue.ToArgb();
graphics.addGeSolidBase(solid, symb, 0);
return graphics;
}
}
}
MyCubeProperty.cs
using HelloBIMBase2;
using NeXus;
using NeXus.BIMBase;
using NeXus.BIMBase.Core;
using NeXus.BIMBase.Data;
using NeXus.p3d;
using NeXus.Traffic;
using NeXus.Traffic.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using BridgeBIMCore;
namespace HelloBIMBase2
{
public class MyCubeProperty : IToolProperty
{
enum EnCubeProperty
{
Length,
Width,
Height,
Message
}
public override void OnPropertyGet(BPEntityPArrayList refps, PBBimUIProperyList lst)
{
if (refps.Count == 0)
return;
BPEntity entity = refps[0];
if (entity == null)
return;
string clsName = entity.getClassName();
if (clsName == "MyCube")
{
OnPlaneLineGet(refps, lst);
}
}
public override TIErrorStatus OnPropertySet(BPEntityPArrayList refps, int index, PBBimUIPropertyItem item)
{
if (refps.Count == 0)
return TIErrorStatus.failed;
BPEntity entity = refps[0];
if (entity == null)
return TIErrorStatus.failed;
string clsName = entity.getClassName();
if (clsName == "MyCube")
{
return OnPlaneLineSet(refps, index, item);
}
return TIErrorStatus.failed;
}
/// <summary>
///
/// </summary>
/// <param name="refps"></param>
/// <param name="lst"></param>
void OnPlaneLineGet(BPEntityPArrayList refps, PBBimUIProperyList lst)
{
BPModel model;
int nEle = -1;
List<PBBimUIPropertyItem> properties = new List<PBBimUIPropertyItem>();
foreach (BPEntity entity in refps)
{
if (entity == null) continue;
model = entity.getBPModel();
if (model == null) continue;
if (null == entity.getBPProject()) continue;
IBPObject pBObject = ObjectExtensionManager.getBPObject(entity);
if (pBObject==null) continue;
MyCube cube = pBObject as MyCube;
if (cube == null) continue;
PBBimUIPropertyItem item1 = new PBBimUIPropertyItem("长度", cube.Length);
properties.Add(item1);
item1 = new PBBimUIPropertyItem("宽度", cube.Width);
properties.Add(item1);
item1 = new PBBimUIPropertyItem("高度", cube.Height);
properties.Add(item1);
item1 = new PBBimUIPropertyItem("消息", cube.Message);
properties.Add(item1);
//item1 = new PBBimUIPropertyItem("结构类型", cube.StructureTypeName);
//properties.Add(item1);
}
lst.AppendGroup("扩展属性");
for (uint i = 0; i < properties.LongCount(); i++)
{
lst.Append(i, properties[(int)i]);
}
}
TIErrorStatus OnPlaneLineSet(BPEntityPArrayList refps, int index, PBBimUIPropertyItem item)
{
foreach (BPEntity entity in refps)
{
if (entity == null)
continue;
BPModel model = entity.getBPModel();
if (model == null) continue;
BPProject project = entity.getBPProject();
if (project == null) continue;
IBPObject pBObject = ObjectExtensionManager.getBPObject(entity);
if (pBObject==null) continue;
MyCube cube = pBObject as MyCube;
if (cube == null) continue;
switch ((EnCubeProperty)(index))
{
case EnCubeProperty.Length:
{
double _value = 0.0;
if (!item.getValue( ref _value)) return TIErrorStatus.failed;
cube.Length = _value;
break;
}
case EnCubeProperty.Width:
{
double _value = 0.0;
if (!item.getValue(ref _value)) return TIErrorStatus.failed;
cube.Width = _value;
break;
}
case EnCubeProperty.Height:
{
double _value = 0.0;
if (!item.getValue(ref _value)) return TIErrorStatus.failed;
cube.Height = _value;
break;
}
case EnCubeProperty.Message:
{
string _value = "test";
if (!item.getValue(ref _value)) return TIErrorStatus.failed;
cube.Message = _value;
break;
}
/*case EnCubeProperty.StructuretypeName:
{
if (!item.getValue(ref _value)) return TIErrorStatus.failed;
cube.StructureTypeName = _value;
break;
}*/
default:
{
}
break;
}
}
return TIErrorStatus.succeed;
}
void CheckMultiValuelist(List<PBBimUIPropertyItem> properties, string dVal, string name, int nIndex, int nItem, bool bReadOnly = false)
{
if (properties.Count <= nItem)
{
PBBimUIPropertyItem item = new PBBimUIPropertyItem(name, dVal);
properties.Add(item);
properties[nItem].setReadOnly(bReadOnly);
}
else
{
if (!properties[nItem].multiValue()) // 比较判断是否为多值
{
double dTmp = 0;
//if (properties[nItem].getValue(ref dTmp) && Math.Abs(dTmp - dVal) > 1e-3)
properties[nItem].setMultiValue(true);
}
properties[nItem].setReadOnly(bReadOnly || properties[nItem].readOnly());
}
}
}
class MyCubePropertyFactory : IToolInterfaceFactory
{
static MyCubePropertyFactory mFactory;
public static IToolInterfaceFactory PropertyFactory
{
get
{
if (mFactory == null) { mFactory = new MyCubePropertyFactory(); }
return mFactory;
}
}
public override IToolInterface CreateInterface()
{
MyCubeProperty p = new MyCubeProperty();
p.AddRef();
return p;
}
};
}
配置.plugin文件
在这个位置添加apitest2.plugin文件

<?xml version="1.0" encoding="utf-8" ?>
<BIMBasePlugins>
<Plugin>
<Name>桥梁设计</Name>
<Description>桥梁设计</Description>
<!-- 入口DLL以及插件其他模块的列表 -->
<EntryAssembly>$(APPPath)/TBDP\Bridge\HelloBIMBase2.dll</EntryAssembly>
<AssemblyList>
<Assembly>$(APPPath)\TRAFFIC\TrafficWrapper.dl1</Assembly>
</AssemblyList>
<AssemblyPathList>
<AssemblyPath>$(APPPath)\TRAFFIC</AssemblyPath>
<AssemblyPath>$(APPPath)\TBDP</AssemblyPath>
<AssemblyPath>$(APPPath)\TBDP\Core</AssemblyPath>
<AssemblyPath>$(APPPath)\ParametricDesign</AssemblyPath>
<AssemblyPath>$(APPPath)/TBDP</AssemblyPath>
<AssemblyPath>$(APPPath)/TBDP/Bridge/</AssemblyPath>
</AssemblyPathList>
<SchemaPath>$(APPPath)/PKPMSchemas/ZJSchema</SchemaPath>
<!-- SchemaVersion的版本 -->
<SchemaVersion>1.0</SchemaVersion>
<!-- ribbon的DLL-->
<Ribbon></Ribbon>
<!-- 插件作用的专业,多个使用","隔开 <ActionChDomainKeys>电气,BIMBase</ActionChDomainKeys>-->
<ActionChDomainKeys>桥梁设计</ActionChDomainKeys>
<!-- 插件研发基于BIMBase的版本 -->
<PluginSDKVersion>1.6.1</PluginSDKVersion>
<PluginVersion>1.0.0</PluginVersion>
<PluginId>19dc23a4-4dc0-2b43-de8a-3bb1293c905a</PluginId>
<VendorId>PKPM</VendorId>
<LoadStatus>true</LoadStatus>
</Plugin>
</BIMBasePlugins>
成功




问题
修改左侧参数无法驱动…没找到原因
更多推荐



所有评论(0)