EBDN - Community - Question & Answers

  Wednesday, 31 March 2021
  2 Replies
  817 Visits
0
Votes
Undo
I have written a C#-Macro that sets the catalog flag of some attributes of devices in a catalog. That works fine, but if I start it in a catalog with about 1500 devices I get an out of Memory error. Is there a possibility to avoid Memory wastage or Memory clearance?

Here the Code:

using System;
using System.AddIn;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forms = System.Windows.Forms;
using Aucotec.EngineeringBase.Client.Runtime;

namespace ModifyCatalogDevices
{
/// <summary>
/// Implements Wizard Run
/// </summary>
[AddIn("Assistent", Description = "Set the 'from catalog flag' for important attributes of devices/cables with filled material number", Publisher = "HLe";)]
public class MyPlugIn : PlugInWizard
{
/// <summary>
/// Runs the wizard.
/// </summary>
/// <param name="myApplication">Application object instance</param>
///


public override void Run(Application myApplication)
{
Localize(myApplication);
Forms.MessageBoxButtons buttons = Forms.MessageBoxButtons.OKCancel;
Forms.MessageBoxIcon messageBoxIcon = Forms.MessageBoxIcon.Information;
Forms.DialogResult result;
result = Forms.MessageBox.Show(locMessage, "", buttons,messageBoxIcon);
if (result == Forms.DialogResult.Cancel) return;

WaitDialog wait = myApplication.Dialogs.CreateWaitDialog();
int i = 0; int numberModDev = 0;

//test, if is started in project or catalog
bool inProject = myApplication.Selection.FirstOrDefault().Project.TypeId != ObjectType.CatUnspecified;

foreach (var item in myApplication.Selection)
{ // collect devices and cables with existing and filled material attribute
var subDevices = item.FindObjects(ObjectKind.Device, SearchBehavior.Hierarchical);
var subCables = item.FindObjects(ObjectKind.Cable, SearchBehavior.Hierarchical);
List<ObjectItem> allDevs = new List<ObjectItem>();
allDevs.Add(item); allDevs.AddRange(subDevices); allDevs.AddRange(subCables);

List<ObjectItem> allDevswithMat = new List<ObjectItem>();
foreach (var item1 in allDevs)
{
if (item1.Attributes.TryFindById(AttributeId.Material, out AttributeItem mat) != false)
{
if (mat.Value != null) allDevswithMat.Add(item1);
}
}

if (item == myApplication.Selection.FirstOrDefault()) wait.ShowDialogAsync(allDevswithMat.Count(), locWait);
bool modDev = false;

foreach (var device in allDevswithMat)
{
i++;
wait.UpdateDialog(i,device.Parent.Parent.FullName);
foreach (var attrib in device.Attributes)
{
if (attrib.IsFromCatalog == false)
{
//all device type specific attributes
switch (attrib.Id)
{
case AttributeId.Material:
attrib.IsFromCatalog = true; modDev = true; break;
case AttributeId.ShortDescription:
attrib.IsFromCatalog = true; modDev = true; break;
case AttributeId.Description:
attrib.IsFromCatalog = true; modDev = true; break;
case AttributeId.DescriptionEnglish:
attrib.IsFromCatalog = true; modDev = true; break;
case (AttributeId)11127: //technische Spezifikation 1
attrib.IsFromCatalog = true; modDev = true; break;
case (AttributeId)11128: //technische Spezifikation 2
attrib.IsFromCatalog = true; modDev = true; break;
case AttributeId.Manufacturer:
attrib.IsFromCatalog = true; modDev = true; break;
case AttributeId.TypeDesignation:
attrib.IsFromCatalog = true; modDev = true; break;
case AttributeId.CatalogNumber:
attrib.IsFromCatalog = true; modDev = true; break;
case AttributeId.EClass:
attrib.IsFromCatalog = true; modDev = true; break;
case AttributeId.EClass6:
attrib.IsFromCatalog = true; modDev = true; break;
case AttributeId.EClass7:
attrib.IsFromCatalog = true; modDev = true; break;
case (AttributeId)11254: //ist Schaltklemme
attrib.IsFromCatalog = true; modDev = true; break;
}
//all filled attributes with following execptions
if (attrib.Value != null & attrib.ValueType != AttributeType.Boolean & !inProject)
{
switch (attrib.Id)
{
case (AttributeId)264:break; //Kennbuchstabe Gerät
case (AttributeId)40794: break; //Verwendungszweck
case (AttributeId)40795: break; //Verwendungszweck (Zusatz)
case (AttributeId)40797: break; //Einstellwert
case (AttributeId)462: break; //Verdrahtungsreihenfolge
case (AttributeId)11252: break; //Brückentyp
case (AttributeId)27878: break; //Brückenspezifikation
case (AttributeId)17424: break; //Brückenlänge
case (AttributeId)10836: break; //Mastershape Klemmenplan

default:
attrib.IsFromCatalog = true; modDev = true; break;
}
}
}
}

if (modDev)
{
device.Store();
numberModDev++;
}
modDev = false;
}

}
wait.CloseDialog();
wait.Dispose();

Forms.MessageBox.Show(i + locResult1 + numberModDev + locResult2);

}

public string locMessage;
public string locWait;
public string locResult1;
public string locResult2;

private void Localize(Application EB)
{
locMessage = "This wizard sets the catalog flag of some attributes of devices and cables in catalog";
locWait = "Please wait";
locResult1 = " devices/cables processed, ";
locResult2 = " devices/cables modified";

if (EB.CurrentCulture.LCID == 1031)
{
locMessage = "Dieser Assistent setzt das Katalog-Flag einiger Attribute von Geräten und Kabeln im Katalog";
locWait = "Bitte warten";
locResult1 = " Geräte/Kabel bearbeitet, ";
locResult2 = " Geräte/Kabel modifiziert";
}
}
}
}