Trying to use csMonitor code

Post questions, comments and feedback to our 3Dconnexion Windows Development Team.

Moderator: Moderators

Post Reply
Hightree
Posts: 20
Joined: Fri May 11, 2007 1:09 am

Trying to use csMonitor code

Post by Hightree »

Hi there,
I'm trying to use the C# example inside of Unity3D.
Unfortunately when trying to hook up the callback functions, I get the following error code :

Code: Select all

NotSupportedException: non imported interfaces on imported classes is not yet implemented.
(wrapper cominterop) TDx.TDxInput._ISensorEvents_Event:add_SensorInput (TDx.TDxInput._ISensorEvents_SensorInputEventHandler)
I'm no interop or COM ninja, does anybody have any ideas on how to get round this ?

Thanks in advance, Patrick
Hit any user to continue.
jwick
Moderator
Moderator
Posts: 3340
Joined: Wed Dec 20, 2006 2:25 pm
Location: USA
Contact:

Post by jwick »

Here is a Unity3D plugin for the SpaceNavigator:

http://forum.unity3d.com/threads/130907 ... eNavigator...)
Hightree
Posts: 20
Joined: Fri May 11, 2007 1:09 am

Post by Hightree »

Thanks for the pointer, I found the plugin too this weekend.
But the programmer in me wants to write this plugin himself ;)
Hit any user to continue.
ljdp
Posts: 1
Joined: Sat Feb 23, 2013 12:03 pm

Re: Trying to use csMonitor code

Post by ljdp »

I'm getting the same error and i'd also like to know how to resolve this, that plugin for unity costs $70 which is priced way to high for me.
Hightree
Posts: 20
Joined: Fri May 11, 2007 1:09 am

Re: Trying to use csMonitor code

Post by Hightree »

In the end I got it working by using the code from that sample, but leaving out the COM event stuff.
Instead I am polling the device for its current values in my EditorWindow's Update function.

My plugin is functional by now and I am planning to open source it soon.
It just needs to be cleaned up a bit, should be done within a couple of weeks.

P.S.
I should have posted the solution when I found it, sorry for that.
Hit any user to continue.
batigol
Posts: 19
Joined: Wed May 08, 2013 8:10 pm

Re: Trying to use csMonitor code

Post by batigol »

Hi Hightree, could you give any tip for get data in Update function?
In C# sample, I could remove all event and get data when ever I want. But in Unity, I try to get data in Update function, but it always zero :(
Hightree
Posts: 20
Joined: Fri May 11, 2007 1:09 am

Re: Trying to use csMonitor code

Post by Hightree »

Strange, maybe there's something going wrong in the initialization phase.
Here's my windows implementation of the device handling:

Code: Select all

using System;
using System.Runtime.InteropServices;
using TDx.TDxInput;
using UnityEngine;

class SpaceNavigatorWindows : SpaceNavigator {	
	// Public API
	public override Vector3 GetTranslation() {
		float sensitivity = Application.isPlaying ? PlayTransSens : TransSens;
		return (SubInstance._sensor == null ?
			Vector3.zero :
			new Vector3(
				LockTranslationX || LockTranslationAll ? 0 : (float)SubInstance._sensor.Translation.X,
				LockTranslationY || LockTranslationAll ? 0 : (float)SubInstance._sensor.Translation.Y,
				LockTranslationZ || LockTranslationAll ? 0 : -(float)SubInstance._sensor.Translation.Z) *
				sensitivity * TransSensScale);
	}
	public override Quaternion GetRotation() {
		float sensitivity = Application.isPlaying ? PlayRotSens : RotSens;
		return (SubInstance._sensor == null ?
			Quaternion.identity :
			Quaternion.AngleAxis(
				(float)SubInstance._sensor.Rotation.Angle * sensitivity * RotSensScale,
				new Vector3(
					LockRotationX || LockRotationAll ? 0 : -(float)SubInstance._sensor.Rotation.X,
					LockRotationY || LockRotationAll ? 0 : -(float)SubInstance._sensor.Rotation.Y,
					LockRotationZ || LockRotationAll ? 0 : (float)SubInstance._sensor.Rotation.Z)));
	}

	// Device variables
	private readonly Sensor _sensor;
	private readonly Device _device;
	//private readonly Keyboard _keyboard;

	#region - Singleton -
	/// <summary>
	/// Private constructor, prevents a default instance of the <see cref="SpaceNavigatorWindows" /> class from being created.
	/// </summary>
	private SpaceNavigatorWindows() {
		try {
			if (_device == null) {
				_device = new DeviceClass();
				_sensor = _device.Sensor;
				//_keyboard = _device.Keyboard;
			}
			if (!_device.IsConnected)
				_device.Connect();
		}
		catch (COMException ex) {
			Debug.LogError(ex.ToString());
		}
	}

	public static SpaceNavigatorWindows SubInstance {
		get { return _subInstance ?? (_subInstance = new SpaceNavigatorWindows()); }
	}
	private static SpaceNavigatorWindows _subInstance;
	#endregion - Singleton -

	#region - IDisposable -
	public override void Dispose() {
		try {
			if (_device != null && _device.IsConnected) {
				_device.Disconnect();
				_subInstance = null;
				GC.Collect();
			}
		}
		catch (COMException ex) {
			Debug.LogError(ex.ToString());
		}
	}
	#endregion - IDisposable -
}
Hope this helps.
Hit any user to continue.
batigol
Posts: 19
Joined: Wed May 08, 2013 8:10 pm

Re: Trying to use csMonitor code

Post by batigol »

Thanks Hightree, your code help he to easy get value in Unity3D, just add close connection at onQuit :)
Here is my code, hope this helpful for someone

Code: Select all

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using TDx.TDxInput;

public class SpaceNavigator : MonoBehaviour {
	
	private Sensor sensor; 
	private Device device;
	private Keyboard keyboard;
	void Start () {
		

        device = new DeviceClass();
        sensor = device.Sensor;
        keyboard = this.device.Keyboard;
        device.Connect();
	}
	void Update () {
		var translation = sensor.Translation;
		var rotation = sensor.Rotation;
		Debug.Log("tx: "+(translation.X*1000.0).ToString()+",ty: "+translation.Y.ToString()+",tz: "+translation.Z.ToString());
		Debug.Log("rx: "+rotation.X.ToString()+",ry: "+rotation.Y.ToString()+",rz: "+rotation.Z.ToString()+",rs: "+rotation.Angle.ToString());
        System.GC.Collect();
	}
	
    void OnApplicationQuit() {
         if (device != null && device.IsConnected) {
            device.Disconnect();
            GC.Collect();
         }
	}
}
Hightree
Posts: 20
Joined: Fri May 11, 2007 1:09 am

Re: Trying to use csMonitor code

Post by Hightree »

My driver is functional and released out into the wild :)
I've published it here on the Unity forum and the source code is available on Github.
Hit any user to continue.
Post Reply