delphialtium-designerdelphiscript

How can I remove a PCB Object in Altium PCB Library using Altium scripting systems?


I am writing a Delphi Altium script to remove all tracks in TopOverLay inside the PCB Library.

When running the script though, nothing happens (the tracks are not removed).

I don't know why. Can you please help me?

Here below is my code :

procedure RemoveTrackObject;
Var 
        MyComponent     : IPCB_LibComponent;
        MyTrack         : IPCB_Track;
        Iterator        : IPCB_GroupIterator;

        DeleteList      : TInterfaceList;
        TrackTemp       : IPCB_Track;
        i               : Integer;            
begin
        MyComponent     := PCBServer.GetCurrentPCBLibrary.CurrentComponent;
        //////////////////////////////////////////////////////////////////////
        Iterator        := MyComponent.GroupIterator_Create;
        Iterator.AddFilter_ObjectSet(Mkset(eTrackObject));
        Iterator.AddFilter_LayerSet(Mkset(eTopOverLay));

        DeleteList      := TInterfaceList.Create;
        try
            MyTrack     := Iterator.FirstPCBObject;
            While MyTrack <> nil do
            begin
                    DeleteList.Add(MyTrack);
                    MyTrack := Iterator.NextPCBObject;
            end;
        
        finally
                MyComponent.GroupIterator_Destroy(Iterator);
        end;
        try
                PCBServer.PreProcess;
                for i := 0 to DeleteList.Count - 1 do
                begin
                        TrackTemp   := DeleteList.Items[i];
                        MyComponent.RemovePCBObject(TrackTemp);
                end;
        finally
                PCBServer.PostProcess;
                DeleteList.Free;
        end;
        Client.SendMessage('PCB:Zoom', 'Action=Redraw' , 255, Client.CurrentView);
end;

Solution

  • AFAIU In Altium dephiscript API InterfaceList have specific uses: holding non-PCB objects & passing to external dll functions & letting the receiving fn destroy the list. You don't really need one here.

    The PcbLib does have some strange behaviour around deleting from selected/focused footprint etc.

    I think the problem is caused by the Pcb Editor not allowing objects to be deleted from the current focused component/footprint. The history around this issue points to solutions involving moving focus away from the required component..

    You can't complete the delete process while the List still contains the object reference.

    Use a While loop, after RemovePCBObject(), remove object ref from the List (remove the List Item). Then when the While loop terminates you have zero items in List.

    Might help refresh or look & feel to use some of these fn calls:

    CurrentLib.Board.GraphicallyInvalidate;
    CurrentLib.Navigate_FirstComponent;
    CurrentLib.Board.ViewManager_FullUpdate;
    CurrentLib.Board.GraphicalView_ZoomRedraw;
    CurrentLib.RefreshView;