In a previous post I gave a step by step tutorial on how to create a LARSA 4D plug-in using C#. Today I will talk about the challenges related to LARSA 4D Object Model.
First issue is about C# not supporting parameterized properties. Unfortunately, LARSA object model makes use of parameterized properties widely. For example, the following is legal in VB, VBA macros: Set member.joint(1) = myjoint1;
The equivalent of this in C# is:
member.set_joint(1, myjoint).
Te problem is that .NET converts all the properties of LARSA objects into method calls (producing rather ugly method list). But the problem does not end there. If you try to compile the C# equivalent code mentioned above, the compiler will complain about the arguments. Most of the time LARSA objects’ method arguments are expected to be passed as reference. For the second argument you can simply use the ref keyword, however the first argument is literal, hence cannot be send as reference. The workaround is to setup a dummy variable with a value of 1, and then pass this argument to the method as ref:
int dummy = 1;
member.set_joint(ref dummy, ref myjoint);
Not pretty at all… Microsoft resolves most of these issues in C# 4.0 by introducing dynamic types (which is currently beta). I got the chance to test it on the beta version and confirmed that ref arguments are not required; however properties are still converted to method calls.
Exploring the late binded LARSA objects using reflection in C# reveales that one can make calls to the properties of COM objects through reflection. It took me a couple of hours to get it right, but I was able to write some wrapper classes that are tightly attached to LARSA’s object model. These thin wrapper classes communicate through reflection. They utilize late binding to achieve binary-compatibility-friendly interaction with LARSA 4D. The benefits are:
- No need to add LARSA references to your C# project. Simply use the wrapper classes and they will handle the communication and referencing to LARSA 4D.
- Easier coding experience. Using this wrapper code,the example shown above would be simply:
member.IJoint = myjoint;
- Tweaked for more intuitive object model organization. Ex. joint.X instead of joint.location.x
- Simple to use… Add the provided single file to your C# project and you are ready to go.
In a previous post I gave a step by step tutorial on how to create a LARSA 4D plug-in using C#. Today I will talk about the challenges related to using LARSA 4D Object Model in C#.
First issue is about C# not supporting parameterized properties. Unfortunately, LARSA object model makes use of parameterized properties widely. For example, the following is legal in VB, VBA macros:
Set member.joint(1) = myjoint1;
The equivalent of this in C# would be:
member.set_joint(1, myjoint)
Unfortunately, .NET converts all the properties of LARSA objects into method calls producing rather ugly list of methods. But the problem does not end there. If you try to compile the C# equivalent code mentioned above, the compiler complains about the way arguments are sent. Most of the time LARSA objects’ method arguments are expected to be passed as reference. For the second argument you can simply use the ref keyword, however the first argument is literal, hence cannot be send as reference. The workaround is to setup a dummy variable with a value of 1, and then pass this argument to the method as ref:
int dummy = 1;
member.set_joint(ref dummy, ref myjoint);
Not pretty at all… Microsoft resolves most of these issues in C# 4.0 by introducing dynamic types (which is currently beta). I got the chance to test it on the beta version and confirmed that ref arguments are no longer required; however properties are still converted to method calls.
Exploring the late binded LARSA objects using reflection in C# reveales that one can make calls to the properties of COM objects through reflection. It took me a couple of hours to get it right, but I was able to write some wrapper classes that are tightly attached to LARSA’s object model. These thin wrapper classes communicate through reflection. They utilize late binding to achieve binary-compatibility-friendly interaction with LARSA 4D. The benefits are:
1- No need to add LARSA references to your C# projects. Simply use the wrapper classes and they will handle the communication and referencing to LARSA 4D.
2- Easier coding experience. Using this wrapper code,the example shown above would be simply:
member.IJoint = myjoint;
3- Tweaked for more intuitive object model organization. Ex. joint.X instead of joint.location.x
4- Simple to use… Add the provided single file to your C# project and you are ready to code.
You can download the sample C# project using this link:
http://www.larsa4d.com/download/CSharpSample.zip
“Larsa4D.cs” file contains all the wrapper classes. Program.cs contains a test module.
Recent Comments