Customising Ribbon User Interface using DraftSight API

In our last blog, we talked about the DraftSight API and gave an brief insight into it and what it can do. In today’s blog, we are going to continue talking about the DraftSight API and how you can use it to customise the Ribbon User Interface.

Be warned, if you thought that the previous blog on the DraftSight API was text heavy, then you might want to take a deep breath before tackling this blog!

So, the first question you might ask is “What is the Ribbon User Interface?”.

Well, the Ribbon User Interface is a rich command presentation system that provides an alternative to the layered Menus and task panes of the Classic Windows versions.

The next question you might ask is “What do I need to customise it?”.  The answer to that is simple. If you are using Microsoft Windows, you’ll require:

If you are using OSx or Linux operating, they only support C++ and JavaScript Only.

The next step is to set up the Programming Environment and Compile the Code.

If you don’t have DraftSight Enterprise or Professional, download the free trial from the website.

Next, activate the Professional Trial from within DraftSight.

Compile the sample code in C:\Program Files\Dassault Systemes\DraftSight\APISDK\samples\C#\Simple\Ribbon using Visual Studio (as administrator) and load the solution. Compile the sample code in C:\Program Files\Dassault Systemes\DraftSight\APISDK\samples\C#\Simple\Ribbon using Visual Studio (as administrator) and load the solution.

The DraftSight API Type Libraries and Primary Interop Assemblies are as follows:

Type Libraries: 

  • install_dir\APISDK\tlb\dsAddin.tlb
  • install_dir\bin\dsAutomation.dll

Primary Interop Assemblies: 

  • install_dir\APISDK\tlb\DraftSight.Interop.dsAddin.dll
  • install_dir\APISDK\tlb\DraftSight.Interop.dsAutomation.dll

Now we are ready to use the Ribbon Sample of DraftSight API to customize the Ribbon User Interface. Using Visual Studio build the solution in Release mode. If you are using a 64-bit operating system chose the platform target as x64. If you are using a 32-bit operating system chose the platform target as x86.

So, now to Start Coding!

Open the DS Addin file in the project. The Ribbon class uses Ribbon namespace and has the methods CreateUserInterfaceAndCommands(), RemoveUserInterface() methods. These methods use the DraftSight API to create, update, fetch and delete UserInterface elements (which are objects).

Let us create a simple command that draws a line and inserts a ColorPicker into the DraftSight application. First we create a class called CommandColorPicker that extends the CommandBase class.

Override the method globalName() and return “_ColorPicker”. Global name of a command is not displayed to the user but can be used as an input in the command window.

Override the method Description() and return “This command draws a line and inserts a color picker into DraftSight“.

Override the method ItemName() and return “ColorPicker”

Override the methods SmallIcon() and LargeIcon() and return the path of the icons that you want to set for this command.

The Execute method is a delegate that gets called once the command is created successfully. The registerCommand() method already handles the logic for creation of the command and showing any error message if it fails to register. You have to implement the logic of creating the ColorPicker in the Execute method.

For inserting ColorPicker method lets write a method called InsertColorPicker() and call it from Execute() method.

void InsertColorPicker()
DraftSight.Interop.dsAutomation.Application dsApp;
Document dsDoc = default(Document);
ColorPicker dsColorPicker = default(ColorPicker);
SketchManager dsSketchMgr = default(SketchManager);
Line dsLine = default(Line);
Color dsColor = default(Color);
bool OnlyColorBoxIndex = true;
bool EnableByBlockAndLayer = true;

//Connects to DraftSight
dsApp = (DraftSight.Interop.dsAutomation.Application)Marshal.GetActiveObject(“DraftSight.Application”);

//Aborts any command currently running in DraftSight to avoid nested commands

//Gets active document

dsDoc = dsApp.GetActiveDocument();

//Gets the Sketch Manager
dsSketchMgr = dsDoc.GetModel().GetSketchManager();

//Constructs a Line
double StartX = 0, StartY = 0, StartZ = 0;
double EndX = 23.6, EndY = 12.545, EndZ = 0;
dsLine = dsSketchMgr.InsertLine(StartX, StartY, StartZ, EndX, EndY, EndZ);

//Gets Color of Line
dsColor = dsLine.Color;

//Creates an instance of the color picker
dsColorPicker = dsApp.CreateColorPicker(OnlyColorBoxIndex, EnableByBlockAndLayer);

//Sets Color to the color picker

//Opens the color picker

//Select a Color in the color picker
//Click OK

//Gets Color from color picker
dsColor = dsColorPicker.GetColor();

//Sets Color of the Line to Color selected in color picker
dsLine.Color = dsColor;

The Ribbon Sample creates a command ID for each command that you create using CommandBase class. We can get this ID by calling UserCommandID() method.

Now compile the project from Visual Studio and build it.

Loading the addin dll into DraftSight application

We have created the functionality of creating a command button. Now we have to load the dll into DraftSight application. Go to Tools -> Load Application and browse the Ribbon.dll file in the Release folder of your project. After loading the .dll file you can see a new workspace called “SampleWorkspace” created for you by Ribbon Sample project.

Test your changes

Type ColorPicker in the command window. You can see a line and a Line Picker window that has an array of colors to chose from. Chose a color of your choice and it draws a line from StartX, StartY, StartZ coordinates to EndX, EndY and EndZ that we gave in the InsertColorPicker() method.

Adding GUI to the Ribbon User Interface

Now that we have created a command successfully and tested it, let us add this command into the Ribbon UserInterface.

The DraftSight Ribbon UserInterface your workspace is made of Ribbon tabs. You can create a new Ribbon tab using the method dsWorkSpace.AddRibbonTab method. This method takes ApiUuid (UUID of the add-in or application), length (length of the ribbon tab), TabName (Name of the Ribbon tab that we are creating) and DisplayText (The name that gets displayed to the users) as parameters and creates a Ribbon tab in the workspace.

Each Ribbon tab is made of Ribbon panels. You can add a Ribbon Panel using the method ribbonTab.InsertRibbonPanel method that takes ApiUuid (UUID of the add-in or application), position (position of the ribbon panel), PanelName (Name of the Ribbon Panel that we are creating) and DisplayText (The name that gets displayed to the users) as parameters and creates a Ribbon Panel in the Ribbon tab.

Each Ribbon Panel is made of Ribbon rows. You can add a Ribbon Row using the method RibbonPanel.InsertRibbonRow method that takes ApiUuid (UUID of the add-in or application) and RowName (Name of the Ribbon Row that we are creating).

Now we can insert the command we created earlier into the Ribbon row using RibbonRow.InsertRibbonCommandButton that takes ApiUuid (UUID of the add-in or application), ButtonStyle, ItemName (Name of the Ribbon Command that we are creating) and the ID of the command button that we are inserting into the Ribbon Row. Now compile the project from Visual Studio and build it.

Test your changes

Once you load the Ribbon dll again in DraftSight, you can see the new Ribbon tab, panel and the Ribbon rows that we created. Click on the icon and DraftSight will draw a line and opens a popup window that allows you to pick color.

And that completes your first project using the DraftSight API, well done!

To purchase DraftSight Professional with its productivity-boosting features for just $99, click here.

Discover more about DraftSight Professional, a powerful 2D design solution for professionals that includes productivity tools and an API for just $99.

MJ Smyth
The first time I used CAD, it was on a DOS PC with an 8088 processor, 640K of memory and a Hercules Mono Graphics Card... That, well that was a long long time ago. I switched to DraftSight the day it was released and haven't looked back!
MJ Smyth

Latest posts by MJ Smyth (see all)