How to: Delete a line


Topic:

Delete a line


Class hierarchy:

IPCsApplication IPCsDocument IPCsDrawing IPCsPage IPCsLine


Description:

Create a variable of IPCsLine to store access to a line. A Line can be modified through this variable


Sourcecode:
[+] Delphi
uses DPSCAD_TLB;

var
  ADocument : IPCsDocument;
  APage : IPCsPage;
  ALines : IPCsLines;
  ALine : IPCsLine;
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 lines}
      ALines := APage.Lines;

      {get a random line of the lines in the active page}
      ALine := ALines.Item[Random(ALines.Count)];
      {delete the line}
      ALine.Delete;

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

[+] Visual Basic
Dim ADocument As Dpscad.PCsDocument
Dim APage As Dpscad.PCsPage
Dim ALines As Dpscad.PCsLines
Dim ALine As Dpscad.PCsLine

'check if we're connected to Automation
If CheckConnection() Then   'active project
  ADocument = PCsApplication.ActiveDocument
  'active page
  APage = ADocument.ActivePage
  'all lines
  ALines = APage.Lines
  
  'get a random line of the lines in the active page
  ALine = ALines(Int((ALines.Count + 1) * Rnd()))
  'delete the line
  ALine.Delete()

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

[+] C#
Dpscad.PCsDocument ADocument;
Dpscad.PCsPage APage;
Dpscad.PCsLines ALines;
Dpscad.PCsLine ALine;
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*/
  ALines = APage.Lines;

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

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

Extra comment:

When deleting an item, notice that the ALines.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 <= ALines.Items.Count-1