Hide Table of Contents

Change Card Variables Add-in Example (C#)

This example shows how to create an add-in that pops up a message box when a file is approved.

//--------------------------------------------------------------------------------------
// Preconditions:
// 1. Start Microsoft Visual Studio 2010.
// 2. Click File > New > Project > Visual C# > Class Library.
// 3. Select .NET Framework 2.0 in the dropdown at the top of the dialog.
// 4. Type CardVariables_CSharp in Name.
// 5. Click Browse and navigate to the folder where to create the project.
// 6. Click OK
// 7. Right-click the project name in the Solution Explorer and click Add Reference
// 8. In the Add Reference dialog:   
//    a. Add the SOLIDWORKS Enterprise PDM type library as a reference (click COM in the
//       left-side panel, click PDMWorks Enterprise nnnn Type Library
//       and click Add).
//    b. Click Assemblies > Framework in the left-side panel, click Microsoft.VisualBasic,
//       and click Add.

//    c. Click Close.
// 9. Right-click the project name in the Solution Explorer and click Properties.
//10. In the Properties window:
//    a. Click Application > Assembly Information.
//    b. Select Make assembly COM-Visible.
//    c. Click OK.
//11. Copy the code below to Class1.cs.
//12. Click Build > Build Solution.
//
// Postconditions: 
// 1. Open the SOLIDWORKS Enterprise PDM Administration tool, expand a vault_name node, 
//    and log in as Admin.
// 2. Under vault_name, right-click Add-ins, and click New Add-in.
//    a. Navigate to the bin\Debug directory of your built project.
//    b. Click Interop.EdmLib.dll and CardVariables.dll.
//    c. Click Open.
//    d. Click OK.
// 3. Click OK after reading the SOLIDWORKS Enterprise PDM warning dialog.
// 4. In the taskbar, click the Administration tool blueberry and click Log off > vault_name.

// 5. Open Windows Explorer on a view of vault_name to:
//    a. Log in as Admin.

//    b. Create, check in, and check out a file whose data card contains Project Name and Project Number.
//    c. Click the Data Card tab.
//    d. In Project Name, type MercuryVenusEarthMarsJupiter, or Saturn.
//    e. Observe the change in Project Number.
//    f. In Project Number, type a number between 1000 and 1005.

//    g. Observe the change in Project Name.
//---------------------------------------------------------------------------------------
 

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using EdmLib;
 
public class CardVariables : IEdmAddIn5
{
 
    public void GetAddInInfo(ref EdmLib.EdmAddInInfo poInfo, EdmLib.IEdmVault5 poVault, EdmLib.IEdmCmdMgr5 poCmdMgr)
    {
        try
        {
            poInfo.mbsAddInName = "C# Card Variable Add-In";
            poInfo.mbsCompany = "Dassault Systemes";
            poInfo.mbsDescription = "Example demonstrating updating a card variable based on another card variable";
            poInfo.mlAddInVersion = 1;
            //Minimum SOLIDWORKS Enterprise PDM version
            //needed for C# Add-Ins is 6.4
            poInfo.mlRequiredVersionMajor = 6;
            poInfo.mlRequiredVersionMinor = 4;
 
            //Register to receive a notification when
            //a data card variable value changes
            poCmdMgr.AddHook(EdmCmdType.EdmCmd_CardInput);
 
            IEdmDictionary5 ProjectDictionary = default(IEdmDictionary5);
            ProjectDictionary = poVault.GetDictionary("Projects"true);
 
            bool SuccessSet = false;
            SuccessSet = ProjectDictionary.StringTestAndSetAt("1000""Mercury");
            SuccessSet = ProjectDictionary.StringTestAndSetAt("1001""Venus");
            SuccessSet = ProjectDictionary.StringTestAndSetAt("1002""Earth");
            SuccessSet = ProjectDictionary.StringTestAndSetAt("1003""Mars");
            SuccessSet = ProjectDictionary.StringTestAndSetAt("1004""Jupiter");
            SuccessSet = ProjectDictionary.StringTestAndSetAt("1005""Saturn");
 
        }
        catch (System.Runtime.InteropServices.COMException ex)
        {
            Interaction.MsgBox("HRESULT = 0x" + ex.ErrorCode.ToString("X") + Constants.vbCrLf + ex.Message);
        }
        catch (Exception ex)
        {
            Interaction.MsgBox(ex.Message);
        }
    }


    readonly Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag static_OnCmd_VariableChangeInProgress_Init = new Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag();
 
 
    //A data card variable value has changed
    bool static_OnCmd_VariableChangeInProgress;
    public void OnCmd(ref EdmLib.EdmCmd poCmd, ref System.Array ppoData)
    {
        try
        {
            switch (poCmd.meCmdType)
            {
                case EdmCmdType.EdmCmd_CardInput:

                    lock (static_OnCmd_VariableChangeInProgress_Init)
                    {
                        try
                        {
                            if (InitStaticVariableHelper(static_OnCmd_VariableChangeInProgress_Init))
                            {
                                static_OnCmd_VariableChangeInProgress = false;
                            }
                        }
                        finally
                        {
                            static_OnCmd_VariableChangeInProgress_Init.State = 1;
                        }
                    }
 
                    IEdmEnumeratorVariable5 vars = (IEdmEnumeratorVariable5)poCmd.mpoExtra;
                    IEdmStrLst5 ConfigNames = (IEdmStrLst5)((EdmCmdData)ppoData.GetValue(0)).mpoExtra;
                    string Config = null;
                    if (IsConfigInList(ConfigNames, "@"))
                    {
                        Config = "@";
                    }
                    else
                    {
                        Config = "";
                    }
 
                    //Take appropriate action based on the data card variable that has changed
                    switch (poCmd.mbsComment)
                    {
 
                        //The Project Name variable has changed
                        case "Project Name":
                            if (static_OnCmd_VariableChangeInProgress == true)
                            {
                                static_OnCmd_VariableChangeInProgress = false;
                                break
                            }
                            object ProjectName = "";
                            vars.GetVar("Project Name", Config, out ProjectName);
 
                            //Get the old Project Number
                            object ProjectNumber = "";
                            vars.GetVar("Project number", Config, out ProjectNumber);
 
                            //Get the existing Projects dictionary
                            IEdmDictionary5 ProjectDictionary = default(IEdmDictionary5);
                            ProjectDictionary = ((IEdmVault5)(poCmd.mpoVault)).GetDictionary("Projects"false);
 
                            //Look up the new project number
                            string NewProjectNumber = "";
                            //Find all values containing the substring
                            //stored in ProjectName 
                            string key = "";
                            string value = "";
                            IEdmPos5 pos = default(IEdmPos5);
                            pos = ProjectDictionary.StringFindValues((string)ProjectName);
                            while (!pos.IsNull)
                            {
                                ProjectDictionary.StringGetNextAssoc(pos, out key, out value);
                                //Traverse the values until a match
                                //is found
                                if (value == (string)ProjectName)
                                {
                                    NewProjectNumber = key;
                                    break;
                                }
                            }
 
                            //Only update the variable if it changed
                            if (!(NewProjectNumber == (string)ProjectNumber))
                            {
                                static_OnCmd_VariableChangeInProgress = true;
                                vars.SetVar("Project number", Config, NewProjectNumber);
 
                            }
                            break;
 
                        //The Project Number variable has changed
                        case "Project number":
                            if (static_OnCmd_VariableChangeInProgress == true)
                            {
                                static_OnCmd_VariableChangeInProgress = false;
                                break
                            }
 
 
                            ProjectNumber = "";
                            vars.GetVar("Project number", Config, out ProjectNumber);
 
                            //Get the old Project Name
                            ProjectName = "";
                            vars.GetVar("Project Name", Config, out ProjectName);
 
                            //Get the existing Projects dictionary
                            ProjectDictionary = ((IEdmVault5)(poCmd.mpoVault)).GetDictionary("Projects"false);
 
                            //Look up the project name
                            string NewProjectName = "";
                            ProjectDictionary.StringGetAt((string)ProjectNumber, out NewProjectName);
 
 
                            //Only update the variable if it's changed
                            if (!(NewProjectName == ((string)ProjectName)))
                            {
                                static_OnCmd_VariableChangeInProgress = true;
                                vars.SetVar("Project Name", Config, NewProjectName);
                            }
                            break;
                    }
                    break;
            }
        }
        catch (System.Runtime.InteropServices.COMException ex)
        {
            Interaction.MsgBox("HRESULT = 0x" + ex.ErrorCode.ToString("X") + Constants.vbCrLf + ex.Message);
        }
        catch (Exception ex)
        {
            Interaction.MsgBox(ex.Message);
        }
    }
 
    private bool IsConfigInList(IEdmStrLst5 ConfigNames, string ConfigName)
    {
        bool functionReturnValue = false;
        functionReturnValue = false;
        try
        {
            string CurConfig = null;
            IEdmPos5 Pos = ConfigNames.GetHeadPosition();
            while (!Pos.IsNull)
            {
                CurConfig = ConfigNames.GetNext(Pos);
                if (CurConfig == ConfigName)
                {
                    functionReturnValue = true;
                    break
                }
            }
            return functionReturnValue;
        }
        catch (System.Runtime.InteropServices.COMException ex)
        {
            Interaction.MsgBox("HRESULT = 0x" + ex.ErrorCode.ToString("X") + Constants.vbCrLf + ex.Message);
        }
        catch (Exception ex)
        {
            Interaction.MsgBox(ex.Message);
        }
        return functionReturnValue;
    }


    static bool InitStaticVariableHelper(Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag flag)
    {
        if (flag.State == 0)
        {
            flag.State = 2;
            return true;
        }
        else if (flag.State == 2)
        {
            throw new Microsoft.VisualBasic.CompilerServices.IncompleteInitialization();
        }
        else
        {
            return false;
        }
    }
 
}

Back to top



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Change Card Variables Add-in Example (C#)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:



x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2015 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.