# API: Create and attach tool from geometry

**URL:** https://tech-community.robotics.abb.com/t/api-create-and-attach-tool-from-geometry/1601
**Category:** RobotStudio
**Created:** [January 1, 2008, 2:34pm UTC](https://tech-community.robotics.abb.com/t/api-create-and-attach-tool-from-geometry/1601 "2008-01-01T14:34:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/system/32/6_2.png) [@system](https://tech-community.robotics.abb.com/u/system)
#### Post date: [January 1, 2008, 2:34pm UTC](https://tech-community.robotics.abb.com/t/api-create-and-attach-tool-from-geometry/1601/1 "2008-01-01T14:34:43Z")

</div>

Hi,

I am trying to create a tool from a geometry.  
There is an Xml-file with the data I need for the tool.  
It stores the path to the Cad-file, its center of gravity, its position in the wrist coordinate system and so on.

I can import the geometry from a given CAD-model via

```auto
GraphicComponentLibrary gcl = GraphicComponentLibrary.Load(reader.GetAttribute("path"), false);
station.GraphicComponents.Add(gcl.RootComponent.CopyInstance ());
```

I can also create a tool with data from an Xml-reader via

```auto
RsToolData tool = new RsToolData();
tool.ModuleName = "Module1";
tool.Name = reader.GetAttribute("name");
tool.RobotHold = true;
tool.Frame.X = Convert.ToDouble(reader.GetAttribute("x"));
tool.Frame.Y = Convert.ToDouble(reader.GetAttribute("y"));
tool.Frame.Z = Convert.ToDouble(reader.GetAttribute("z"));
tool.Frame.RX = Globals.DegToRad(Convert.ToDouble(reader.GetAttribute("rx")) );
tool.Frame.RY = Globals.DegToRad(Convert.ToDouble(reader.GetAttribute("ry")) );
tool.Frame.RZ = Globals.DegToRad(Convert.ToDouble(reader.GetAttribute("rz")) );

//Create the loaddata for the tool
RsLoadData myloadData = new RsLoadData();
//Set mass
myloadData.Mass = Convert.ToDouble(reader.GetAttribute("mass"));

//Set center of gravity
myloadData.Cog = new Vector3(Convert.ToDouble(reader.GetAttribute("cogx")), Convert.ToDouble(reader.GetAttribute("cogy")), Convert.ToDouble(reader.GetAttribute("z")));

//Set the Axis of moment
myloadData.Aom = new Quaternion(Convert.ToDouble(reader.GetAttribute("aomx")), Convert.ToDouble(reader.GetAttribute("aomy")), Convert.ToDouble(reader.GetAttribute("aomz")), Convert.ToDouble(reader.GetAttribute("aomw")));

//The load can be considered a point mass, in this example, i.e. without any moment of inertia

myloadData.Inertia = new Vector3(0, 0, 0);
tool.LoadData = myloadData;

//Show the name of the tooldata in the graphics
tool.ShowName = true;

//Show the tool data in the graphics
tool.Visible = true;

//Add the tool data to the active task
station.ActiveTask.DataDeclarations.Add(tool);
```

There are some problems I could not solve:

1. How can I attach the geometry to a robot? In Rs it is just drag&drop, but with the API…?!
2. How can I connect the RsToolData with the geometry?  
Should I load the geometry as a Part?  
Part has a method called attach, but I have no clue how to use it ☹

Is this possible with the API?

Best regards and a happy new year,  
Matthias

---

<div class="post-metadata">

### Author: ![Anders\_S](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Anders\_S](https://tech-community.robotics.abb.com/u/Anders_S)
#### Post date: [January 4, 2008, 12:16pm UTC](https://tech-community.robotics.abb.com/t/api-create-and-attach-tool-from-geometry/1601/2 "2008-01-04T12:16:51Z")

</div>

Hi Matthias,

To attach a tool to a robot you can do like this:

actStn = Project.ActiveProject as Station;  
Mechanism mechRobot = actStn.GraphicComponents[“MyRobot”] as Mechanism;  
GraphicComponent grTool = actStn.GraphicComponents[“MyTool”];

IAttachableChild attachTool = grTool as IAttachableChild;  
mechRobot.GetFlanges()[0].Attach(attachTool, false);

---

<div class="post-metadata">

### Author: ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/system/32/6_2.png) [@system](https://tech-community.robotics.abb.com/u/system)
#### Post date: [January 7, 2008, 1:50pm UTC](https://tech-community.robotics.abb.com/t/api-create-and-attach-tool-from-geometry/1601/3 "2008-01-07T13:50:40Z")

</div>

Hi again!

Thanks for your help!  
Now I have

```auto
GraphicComponentLibrary gcl = GraphicComponentLibrary.Load(reader.GetAttribute("path"), false);
GraphicComponent gc = gcl.RootComponent.CopyInstance();
gc.Name = reader.GetAttribute("name");
station.GraphicComponents.Add(gc);

Mechanism mechRobot = station.GraphicComponents["IRB4400_60_196__01_2"] as Mechanism;
GraphicComponent grTool = station.GraphicComponents[reader.GetAttribute("name")];

IAttachableChild attachTool = grTool as IAttachableChild;
mechRobot.GetFlanges()[0].Attach(attachTool, true);
```

When I attach a tool to a robot manually in RS the TCP changes automatically.  
Can I do this within a Plugin?

Best Regards, Matthias

---

<div class="post-metadata">

### Author: ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/system/32/6_2.png) [@system](https://tech-community.robotics.abb.com/u/system)
#### Post date: [January 9, 2008, 10:43am UTC](https://tech-community.robotics.abb.com/t/api-create-and-attach-tool-from-geometry/1601/4 "2008-01-09T10:43:36Z")

</div>

Hi,

when I use my plugin the tool is attached to the robot, but no new TCP is created.

When I detach the tool (right click on tool in Objects-window → detach, without keeping position) and then re-attach it (right click on tool → attach to, also without keeping current position) the right TCP is created.  
How can this be? Is there any solution?

I guess I can specify the TCP by

```auto
IAttachableParent parent = mechRobot.GetFlanges()[0] as IAttachableParent;
parent.attach(attachtool, true, offset);
```

If that is the solution, how can I get the offset? The offset is loaded when I import the \*.rslib, but I don’t know how to access it.

Best regards, Matthias
