How to: Delete a circle


Topic:

Delete a circle


Class hierarchy:

IPCsApplication IPCsDocument IPCsDrawing IPCsPage IPCsArc


Description:

Create a variable of IPCsArc to store access to a circle. A Circle can be modified through this variable


Sourcecode:
[+] Delphi
uses DPSCAD_TLB;

var
  ADocument : IPCsDocument;
  APage : IPCsPage;
  AArcs : IPCsArcs;
  AArc : IPCsArc;
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 circles}
      AArcs := APage.Arcs;

      {get a random circle of the circles in the active page}
      AArc := AArcs.Item[Random(AArcs.Count)];
      {delete the circle}
      AArc.Delete;

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

[+] Visual Basic
Dim ADocument As Dpscad.PCsDocument
Dim APage As Dpscad.PCsPage
Dim AArcs As Dpscad.PCsArcs
Dim AArc As Dpscad.PCsArc

'check if we're connected to Automation
If CheckConnection() Then   // (See function here)
  'active project
  ADocument = PCsApplication.ActiveDocument
  'active page
  APage = ADocument.ActivePage
  'all circles
  AArcs = APage.Arcs
  
  'get a random circle of the circles in the active page
  AArc = AArcs(Int((AArcs.Count + 1) * Rnd()))
  'delete the circle
  AArc.Delete()
  
  'update drawing in Automation through IPCsApplication
  PCsApplication.Redraw()
End If

[+] C#
Dpscad.PCsDocument ADocument;
Dpscad.PCsPage APage;
Dpscad.PCsArcs AArcs;
Dpscad.PCsArc AArc;
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 circles*/
  AArcs = APage.Arcs;

  /*get a random circle of the circles in the active page*/
  AArc = AArcs[rnd.Next(AArcs.Count)];
  /*delete the circle*/
  AArc.Delete();

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

Extra comment:

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