How to create a tool using the API (C#)?

Hi everyone,

it’s me again. I have another problem: i want to create a tool through the API.
With the “Create Tool” Wizard in RS it is no problem, but how can i create a tool (tooldata + graphic representation, e.g. a cylinder) with C#?

Regards, Matthias

Hi,

firstly a solution for your tooldata problem (without any geometry attached):

RsTask task = station.ActiveTask;

RsToolData tool = new RsToolData();
tool.Name = “NewTool”;
tool.Frame.Z = 0.250; // or whatever

task.DataDeclarations.Add(tool);
task.ActiveTool = tool;

This should give you a new tooldata declaration and set it to the active one.

and here a sample with geometry (mechanism of type tool):

Station station = Project.ActiveProject as Station;
RsTask task = station.ActiveTask;

// create sample geometry
Part part = new Part();
part.Bodies.Add(Body.CreateSolidCylinder(Matrix4.Identity, 0.030, 0.250));
part.Name = “MyToolPart”;

// create new tool
MechanismBuilder b = new MechanismBuilder(MechanismType.Tool);
b.Name = “MyToolName”;
b.ModelName = “MyToolModelName”;

string link = “Geometry”;
b.AddLink(link, part);
b.BaseLink = link;

Matrix4 offset = Matrix4.Identity;
offset.TranslateLocal(0.0, 0.0, 0.250);
// …

// assign gravity, offset, …
b.AddToolData(“MyToolData”, link, offset, new Vector3(0,0,1), new Vector3(0,0,1));

Mechanism mechTool = b.CompileMechanism();
mechTool.Name = “MyMechanism”;
station.GraphicComponents.Add(mechTool);

// and attach it to the flange:
// of course you should check if flange does exist and so on
task.Mechanism.GetFlanges()[0].Attach(mechTool, true);

Hope that helps.

Thank you, that was exactly what i needed. I’ll try it as soon as i am back in office!

Matthias