C# Reset Instance ID’s Within a V6 Product Structure

Print Friendly, PDF & Email

C# Reset Instance ID’s Within a V6 Product Structure

In a previous post “C# Connecting and Managing a Link to the CATIA App” we created a class that manages the connection to CATIA V6 (V5 Or Another application). Were going to use this to create the connection to V6 and to rename the instances within a product structure.

CATIA Connection

This section of code deals with creating the Connection and what to do if the connection status changes from connected to disconnected or visa-versa.

		public MainPageViewModel(ILogger logger, MainViewModel mainViewModel)
        {
            logger.Information("MainPageViewModel Constructor.");

            _logger = logger;
            MainViewModel = mainViewModel;

            _catiaV6Connection = new CATIAV6Connection(logger);
            _catiaV6Connection.OnConnectedChanged += OnConnectedChanged;
        }

		private void OnConnectedChanged(object? sender, EventArgs e)
        {
            _logger.Information("MainPageViewModel - OnConnectedChanged");

            if (_catiaV6Connection != null)
            {
                ConnectionBrush = (_catiaV6Connection.Connected == false) ? Brushes.Red : Brushes.LightGreen;
                if (MainViewModel != null)
                {
                    if (_catiaV6Connection.CATIAV6App != null)
                    {
                        MainViewModel.CATIAV6App = new CATIAV6App(_logger, _catiaV6Connection.CATIAV6App);
                        CatiaConnectionTooltip = (_catiaV6Connection.Connected == false) ? "CATIA is Not Connected." : "CATIA is Connected.";
                        IsConnected = _catiaV6Connection.Connected;
                    }
                    else
                    {
                        _logger.Information("MainPageViewModel - OnConnectedChanged - CATIAV6App was Null.");
                    }
                }
            }
        }

CATIAV6RenameInstanceIDs Class

Within a new class called CATIAV6RenameInstanceIDs we will start of by creating the constructor, that will take in the ILogger and CATIAV6Connection objects.

using Serilog;

namespace DES.CATIAV6.ClearanceContactAndClashManager.UI.Helpers.CATIAV6
{
    public class CATIAV6RenameInstanceIDs
    {
        private ILogger _logger { get; }
        private CATIAV6Connection? _catiaV6Connection { get; } = null;
        public CATIAV6RenameInstanceIDs(ILogger logger, CATIAV6Connection catiaV6Connection)
        {
            logger.Information("CATIAV6RenameInstanceIDs Constructor.");

            _logger = logger;
            _catiaV6Connection = catiaV6Connection;
        }
    }
}

Rename Instance IDs Method

Within the CATIAV6RenameInstanceIDs a new Void Method will be created called RenameInstanceIDs. To validate the uniqueness of the instance ID, a Long name will be stored within a dictionary along with the Occurrence, where the long name will be the unique dictionary key.

private Dictionary<string, VPMOccurrence> _instanceDataDict = new Dictionary<string, VPMOccurrence>() { };
public void RenameInstanceIds()
{
  if(_instanceDataDict != null && _instanceDataDict.Count ==0)
  {
    if (_catiaV6Connection?.Connected == true)
    {
      Editor? editor = _catiaV6Connection.CATIAV6App?.ActiveEditor;
      if (editor != null)
      {
        VPMRootOccurrence? vpmRootOccurrence = null;
        try
        {
          vpmRootOccurrence = (VPMRootOccurrence)editor.ActiveObject;
        }
        catch(Exception ex)
        {
          throw new Exception("The Active Document in the Session is not an Assembly.",ex);
        }
        NavigateProductStructure(vpmRootOccurrence.Occurrences);
      }
    }
  }
}

Navigate Product Structure

The Navigate Product Structure method, first defaults the instance ID to “Blank”, we have to default it to a recognizable string for when we test uniqueness.

private void NavigateProductStructure(VPMOccurrences vpmOccurrences)
        {
            for(int ioIndex = 1; ioIndex <= vpmOccurrences.Count; ioIndex++)
            {
                VPMOccurrence vpmOccurrence = vpmOccurrences.Item(ioIndex);
                vpmOccurrence.InstanceOccurrenceOf.SetAttributeValue("Name", "BLANK");
                vpmOccurrence.InstanceOccurrenceOf.SetAttributeValue("Name", GetNextId(vpmOccurrence));

                NavigateProductStructure( vpmOccurrence.Occurrences );
            }
        }

Get Long Name

Th Last Two methods, will create a long name and validate that it does not exist within the dictionary if it does it will roll to the next id until a unique id is found and return this back.

private string GetNextId(VPMOccurrence vpmOccurrence)
{
  VPMInstance vpmInstance = (VPMInstance)vpmOccurrence.PLMEntity;
  VPMReference vpmReference = vpmInstance.ReferenceInstanceOf;
  string partNumber = vpmReference.GetAttributeValue("PLM_ExternalID");
  string currentLongName = GetLongName(vpmOccurrence);
  for (int ioIndex = 1; ioIndex < 1000; ioIndex++)
  {
    string newLongName = currentLongName.Replace( "BLANK", string.Concat(partNumber, ".", ioIndex));

    if(_instanceDataDict.ContainsKey(newLongName) == false)
    {
      _instanceDataDict.Add(newLongName, vpmOccurrence);
      return newLongName;
    }
  }
  return "BLANK";
}

private string GetLongName(VPMOccurrence vpmOccurrence)
{
  string returnString = string.Empty;
  VPMOccurrence? parent = vpmOccurrence;
  try
  {
    while (parent != null)
    {
      returnString = string.Concat(parent.get_Name(), returnString);
      parent = (VPMOccurrence)parent.Parent;
      returnString = string.Concat(@"\", returnString);
    }
  }
  catch
  {
    // Do Nothing COM Exception Was Thrown By parent.Parent
  }
  finally
  {
    parent = null;
  }
  return returnString;
}

Leave a Reply

Your email address will not be published.