using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
using ns3DRudder;
namespace MyApplication
{
public partial class Commands
{
[CommandMethod(“3DRUDDER”, CommandFlags.Modal)]
public void Rudder()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
// Create and run our jig
var rj = new RudderJig();
var pr = ed.Drag(rj);
}
}
public class RudderJig : DrawJig
{
int _numRuddersConnected;
CSdk _rudderSdk;
Axis axis;
ModeAxis mode;
CurveArray curves;
int _selectedIndex = -1;
ns3DRudder.Status _prevStatus;
private class Item
{
public string Name;
public int Value;
public Item(string name, int value)
{
Name = name; Value = value;
}
public override string ToString()
{
// Generates the text shown in the combo box
return Name;
}
}
// To stop the running jig by sending a cancel request
[DllImport(“accore.dll”, CharSet = CharSet.Auto,
CallingConvention = CallingConvention.Cdecl,
EntryPoint = “?acedPostCommand@@YAHPEB_W@Z”
)]
extern static private int acedPostCommand(string strExpr);
public RudderJig()
{
_rudderSdk = new CSdk();
_rudderSdk.Init();
axis = new Axis();
mode = ModeAxis.NormalizedValue;
curves = new CurveArray();
}
// Flag and property for when we want to exit
protected bool _finished;
public bool Finished
{
get { return _finished; }
set { _finished = value; }
}
private void UpdateStatus()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
if (_numRuddersConnected != _rudderSdk.GetNumberOfConnectedDevice())
{
_numRuddersConnected = _rudderSdk.GetNumberOfConnectedDevice();
for (uint i = 0; i < i3DR._3DRUDDER_SDK_MAX_DEVICE; i++)
{
if (_rudderSdk.IsDeviceConnected(i))
{
ed.WriteMessage(“3DRudder #{0}\n”, i);
_selectedIndex = (int)i;
}
}
}
if (_selectedIndex != -1)
{
var selected = (uint)_selectedIndex;
if (_rudderSdk.IsDeviceConnected(selected))
{
// ushort nVersion = _rudderSdk.GetVersion(selected);
string Status = “”;
if (_rudderSdk.GetAxis(selected, mode, axis, curves) == ErrorCode.Success)
{
var stat = _rudderSdk.GetStatus(selected);
switch (stat)
{
case ns3DRudder.Status.NoFootStayStill:
Status = “Don’t put your feet!!! Stay still for 5s”;
break;
case ns3DRudder.Status.Initialisation:
Status = “Initialisation”;
break;
case ns3DRudder.Status.PutYourFeet:
Status = “Please put your feet”;
break;
case ns3DRudder.Status.PutSecondFoot:
Status = “Put your second foot”;
break;
case ns3DRudder.Status.StayStill:
Status = “Stay still”;
break;
case ns3DRudder.Status.InUse:
Status = “3DRudder in use”;
break;
case ns3DRudder.Status.ExtendedMode:
Status = “Extended functions activated”;
break;
}
if (stat != _prevStatus)
{
doc.Editor.WriteMessage(“{0}\n”, Status);
_prevStatus = stat;
}
if (stat == ns3DRudder.Status.InUse || stat == ns3DRudder.Status.ExtendedMode)
{
var XAxis = axis.GetXAxis();
var YAxis = axis.GetYAxis();
var ZAxis = axis.GetZAxis();
var ZRotation = axis.GetZRotation();
ed.WriteMessage(
“X: {0:0.00}, Y: {1:0.00}, Z: {2:0.00}, Rotation: {3:0.00} ({4})\n”,
XAxis, YAxis, ZAxis, ZRotation, Status
);
}
}
}
}
}
protected virtual SamplerStatus SamplerData()
{
UpdateStatus();
ForceMessage();
return SamplerStatus.NoChange; // Cancel;
}
protected virtual bool WorldDrawData(WorldDraw draw)
{
return false;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
var opts = new JigPromptPointOptions(“\nClick to finish: “);
opts.UserInputControls = UserInputControls.AnyBlankTerminatesInput;
opts.Cursor = CursorType.Invisible;
var ppr = prompts.AcquirePoint(opts);
if (ppr.Status == PromptStatus.OK || ppr.Status == PromptStatus.Cancel)
{
if (_finished)
{
CancelJig();
return SamplerStatus.Cancel;
}
return SamplerData();
}
return SamplerStatus.Cancel;
}
protected override bool WorldDraw(WorldDraw draw)
{
return WorldDrawData(draw);
}
// Cancel the running jig
internal static void CancelJig()
{
acedPostCommand(“CANCELCMD”);
}
public void ForceMessage()
{
System.Windows.Forms.Cursor.Position =
System.Windows.Forms.Cursor.Position;
System.Windows.Forms.Application.DoEvents();
}
}
}
Đây là một video khác về Rhino, chi tiết hơn 3dRudder cho điều hướng mô hình 3D: