How to: Delete a text


Topic:

Delete a text


Class hierarchy:

IPCsApplication IPCsDocument IPCsDrawing IPCsPage IPCsText


Description:

Create a variable of IPCsText to store access to a line. A Text can be modified through this variable


Sourcecode:
[+] Delphi
uses DPSCAD_TLB;

var
  ADocument : IPCsDocument;
  APage : IPCsPage;
  ATexts : IPCsTexts;
  AText : IPCsText;
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 texts}
      ATexts := APage.Texts;

      {get a random text of the texts in the active page}
      AText := ATexts.Item[Random(ATexts.Count)];
      {delete the text}
      AText.Delete;

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

[+] Visual Basic
Dim ADocument As Dpscad.PCsDocument
Dim APage As Dpscad.PCsPage
Dim ATexts As Dpscad.PCsTexts
Dim AText As Dpscad.PCsText

'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
  ATexts = APage.Texts

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

  'delete the line
  AText.Delete()

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

[+] C#
Dpscad.PCsDocument ADocument;
Dpscad.PCsPage APage;
Dpscad.PCsTexts ATexts;
Dpscad.PCsText AText;
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*/
  ATexts = APage.Texts;

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

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

Extra comment:

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