XCAD.NET
XCAD.NET
Framework for .NET (C# and VB.NET) to create modern application for CAD systems (SOLIDWORKS)
Get technical support Access source code Explore code and application examples

Creating native property pages with xCAD framework


Inspired by PropertyGrid Control in .NET Framework, xCAD brings the flexibility of data model driven User Interface into SOLIDWORKS API.

Framework allows to use data model structure as a driver of the User Interface. Framework will automatically generate required interface and implement the binding of the model.

This will greatly reduce the implementation time as well as make the property pages scalable, easily maintainable and extendable.

Property pages can be defined by data model and all controls will be automatically created and bound to the data.

Property Manager Page driven by data model
Property Manager Page driven by data model

Data model

Start by defining the data model required to be filled by property manager page.

public class DataModelSimple
{
    public string Text { get; set; }
    public int Size { get; set; } = 48;
    public double Number { get; set; } = 10.5;
}

Use properties with public getters and setters

Events handler

Create handler for property manager page by inheriting the public class from Xarial.XCad.SolidWorks.UI.PropertyPage.SwPropertyManagerPageHandler class.

This class will be instantiated by the framework and will allow handling the property manager specific events from the add-in.

[ComVisible(true)]
public class MyPMPageData : SwPropertyManagerPageHandler
{
    public DataModelSimple Simple { get; set; }
    public DataModelIgnore Ignore { get; set; }
}

Class must be com visible and have public parameterless constructor.

Data model can directly inherit the handler.

Ignoring members

If it is required to exclude the members in the data model from control generation such members should be decorated with Xarial.XCad.UI.PropertyPage.Attributes.ExcludeControlAttribute

public class DataModelIgnore
{
    public string Text { get; set; }

    [ExcludeControl]
    public int CalculatedField { get; set; } //control will not be generated for this field
}

Creating instance

Create instance of the property manager page by passing the type of the handler and data model instance into the generic arguments

Data model can contain predefined (default) values. Framework will automatically use this values in the corresponding controls.

private ISwPropertyManagerPage<MyPMPageData> m_Page;
private MyPMPageData m_Data = new MyPMPageData();

private enum Commands_e
{
    ShowPmpPage
}

public override void OnConnect()
{
    m_Page = this.CreatePage<MyPMPageData>();
    this.CommandManager.AddCommandGroup<Commands_e>().CommandClick += ShowPmpPage;
}

private void ShowPmpPage(Commands_e cmd)
{
    m_Page.Closed += OnPageClosed;
    m_Page.Show(m_Data);
}

private void OnPageClosed(PageCloseReasons_e reason)
{
    Debug.Print($"Text: {m_Data.Simple.Text}");
    Debug.Print($"Size: {m_Data.Simple.Size}");
    Debug.Print($"Number: {m_Data.Simple.Number}");
}

Store instance of the data model and the property page in the class variables. This will allow to reuse the data model in the different page instances.


Powered by Docify