How to: Delete a page


Topic:

Delete a page


Class hierarchy:

IPCsApplication IPCsDocument IPCsDrawing IPCsPage


Description:

Create a variable of IPCsPage to store access to a page. A Page can be modified through this variable


Sourcecode:
[+] Delphi
uses DPSCAD_TLB;

var
  ADocument : IPCsPage;
  RandomPage : integer;
begin
  {check if we're connected to Automation}
  if CheckConnection() then    // (See function here)
    begin
      {active project}
      ADocument := PCsApplication.ActiveDocument;
      {a random page in active document}
      RandomPage := Random(ADocument.Drawing.Pages.Count);
      {delete the page}
      ADocument.Drawing.Pages[RandomPage].Delete;
    end;
end;

[+] Visual Basic
Dim ADocument As Dpscad.PCsDocument
Dim RandomPage As Integer

'check if we're connected to Automation
If CheckConnection() Then   // (See function here)
  'active project
  ADocument = PCsApplication.ActiveDocument
  'check if we're connected to Automation
  RandomPage = Int((ADocument.Drawing.Pages.Count + 1) * Rnd())

  'delete the page
  ADocument.Drawing.Pages(RandomPage).Delete()
End If

[+] C#
Dpscad.PCsDocument ADocument;
Random rnd = new Random();
int RandomPage;

/*check if we're connected to Automation*/
if (CheckConnection())   // (See function here)
{
  /*active project*/
  ADocument = PCsApplication.ActiveDocument;
  /*check if we're connected to Automation*/
  RandomPage = rnd.Next(ADocument.Drawing.Pages.Count);
  /*delete the page*/
  ADocument.Drawing.Pages[RandomPage].Delete();
}

Extra comment:

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