How to: Delete a symbol


Topic:

Delete a symbol


Class hierarchy:

IPCsApplication IPCsDocument IPCsDrawing IPCsPage IPCsSymbol


Description:

Create a variable of IPCsSymbol to store access to a symbol. A Symbol can be modified through this variable


Sourcecode:
[+] Delphi
uses DPSCAD_TLB;

var
  ADocument : IPCsDocument;
  APage : IPCsPage;
  ASymbols : IPCsSymbols;
  ASymbol : IPCsSymbol;
begin
  {check if we're connected to Automation}
  if CheckConnection() then    // (See function here)
    begin
      {active project}
      ADocument := PCsApplication.ActiveDocument;
      {active page}
      APage := ADocument.ActivePage;
      {all symbols}
      ASymbols := APage.Symbols;

      {get a random symbol of the symbols in the active page}
      ASymbol := ASymbols.Item[Random(ASymbols.Count)];
      {delete the symbol}
      ASymbol.Delete;

      {update drawing in Automation through IPCsApplication}
      PCsApplication.Redraw;
    end;
end;

[+] Visual Basic
Dim ADocument As Dpscad.PCsDocument
Dim APage As Dpscad.PCsPage
Dim ASymbols As Dpscad.PCsSymbols
Dim ASymbol As Dpscad.PCsSymbol

'check if we're connected to Automation
If CheckConnection() Then   // (See function here)
  'active project
  ADocument = PCsApplication.ActiveDocument
  'active page
  APage = ADocument.ActivePage
  'all lines
  ASymbols = APage.Symbols

  'get a random line of the lines in the active page
  ASymbol = ASymbols(Int((ASymbols.Count + 1) * Rnd()))

  'delete the line
  ASymbol.Delete()

  'update drawing in Automation through IPCsApplication
  PCsApplication.Redraw()
End If

[+] C#
Dpscad.PCsDocument ADocument;
Dpscad.PCsPage APage;
Dpscad.PCsSymbols ASymbols;
Dpscad.PCsSymbol ASymbol;
Random rnd = new Random();

/*check if we're connected to Automation*/
if (CheckConnection())   // (See function here)
{
  /*active project*/
  ADocument = PCsApplication.ActiveDocument;
  /*active page*/
  APage = ADocument.ActivePage;
  /*all lines*/
  ASymbols = APage.Symbols;

  /*get a random line of the lines in the active page*/
  ASymbol = ASymbols[rnd.Next(ASymbols.Count)];
  /*delete the line*/
  ASymbol.Delete();

  /*update drawing in Automation through IPCsApplication*/
  PCsApplication.Redraw();
}

Extra comment:

When deleting an item, notice that the ASymbols.Items.Count will decrease by 1. So if the code runs through every item in a for-loop, and some of the code deletes and item, you'll get an exception. The reason is that at the end of the loop, you're code is trying to access an item thats not there anymore.
The way to access items without risking to refer to a none existing item, is to use a while loop, that before each loop, test for if ItemNoToProcess <= ASymbols.Items.Count-1