The following XML extension definition file declares a load to be created in ANSYS Mechanical that requires C# implementation:
<extension version="1" name="CSharp">
<author>ANSYS</author>
<description>This extension demonstrates how to use CSharp to write extension.</description>
<assembly src="CSharp.dll" namespace="CSharp" />
<interface context="Mechanical">
<images>images</images>
</interface>
<simdata context="Mechanical">
<load name="CSharpLoad" caption="CSharp Load" version="1" icon="tload" unit="Temperature"
color="#0000FF" class="CSharp.Load">
<property name="Geometry" control="scoping">
<attributes>
<selection_filter>face</selection_filter>
</attributes>
</property>
</load>
<result name="CSharpResult" caption="CSharp Result" version="1" unit="Temperature" icon="tload"
location="node" type="scalar" class="CSharp.Result">
<property name="Geometry" control="scoping" />
</result>
</simdata>
</extension>In the definition of the load object, the only change is the use of the attribute class. This attribute must be set to the name of the class to be used for the integration of the load.
A description of the class CSharp.Load follows.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ansys.ACT.Interfaces.Mechanical;
using Ansys.ACT.Interfaces.UserObject;
namespace CSharp
{
public class Load
{
private readonly IMechanicalExtAPI _api;
private readonly IMechanicalUserLoad _load;
public CustomLoadObjectController(IExtAPI api, IUserLoad load)
{
_api = (IMechanicalExtAPI) api;
_load = (IMechanicalUserLoad) load;
}
public virtual IEnumerable<double> getnodalvaluesfordisplay(IUserLoad load, IEnumerable<int> nodeIds)
{
var res = new List<double>();
IMeshData mesh = _load.Analysis.MeshData;
foreach (int nodeId in nodeIds)
{
INode node = mesh.NodeById(nodeId);
res.Add(Math.Sqrt(node.X * node.X + node.Y * node.Y + node.Z * node.Z));
}
return res;
}
}
}To implement a callback in C#, create a new method in your class with the name of the callback in lower case.
In the example, you implement the callback
<getnodalvaluesfordisplay> by adding the method
getnodalvaluesfordisplay to the class.