Author Topic: Clearing Entities and MOPs in c#  (Read 268 times)

Offline miketreth

  • Ewok
  • *
  • Posts: 3
    • View Profile
Clearing Entities and MOPs in c#
« on: March 25, 2020, 12:56:40 pm »
I have a VB script which works fine.  However, I want to put have a better front-end UI and have converted it to a C# plugin.  It works fine apart from two issues: clearing entities and MOPS.

The VB code I used was

             ActiveLayer.Entities.Clear()

and

             view.CADFile.Parts.Clear


I have tried to convert this to C# without success.  The code I have compiles OK, but does not clear the objects:  the code used is

            CamBamUI.MainUI.ActiveView.CADFile.ActiveLayer.Entities.Clear();

and

            CamBamUI.MainUI.ActiveView.CADFile.Parts.Clear();

Can anyone explain why this does not work and what the correct solution is please?

Thanks,

Mike



 

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 4248
    • View Profile
Re: Clearing Entities and MOPs in c#
« Reply #1 on: March 25, 2020, 14:47:41 pm »
Mike,

At the start of each plugin I use these lines;

ICADView view = CamBamUI.MainUI.ActiveView;
CADFile myfile = view.CADFile;

This allows me to refer to the main areas of CamBam like this;

myfile.ActiveLayer.Entities.Clear();
myfile.Parts.Clear();

I have not tried these in a plugin but maybe you need these lines added;

 myfile.Update();
 view.RefreshView();

If it's any use I attached a sample plugin in this thread, "sample.zip" ; http://www.cambam.co.uk/forum/index.php?topic=6668.msg53945#msg53945

It uses the Visual Studio Community edition
« Last Edit: March 25, 2020, 14:58:07 pm by EddyCurrent »
Made in England

Offline miketreth

  • Ewok
  • *
  • Posts: 3
    • View Profile
Re: Clearing Entities and MOPs in c#
« Reply #2 on: March 25, 2020, 15:40:45 pm »
Thank you for the quick response.

I tried your suggested code and it too compiles OK, but does not clear the entities, not MOPS.

I have also compiled the code for CamBam 1.0  (.NET 4) and I get the same result.

Baffled.
Mike



Offline miketreth

  • Ewok
  • *
  • Posts: 3
    • View Profile
Re: Clearing Entities and MOPs in c#
« Reply #3 on: March 25, 2020, 15:45:29 pm »
I think I will now try cycling through the list of entities and parts to see if I can delete them individually.  I am hoping this will work.

Mike

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 4248
    • View Profile
Re: Clearing Entities and MOPs in c#
« Reply #4 on: March 25, 2020, 16:44:28 pm »
Mike,

If you could attach the plugin here, that would be better.


You can use this type of loop;

foreach (CamBam.CAM.CAMPart prt in myfile.Parts)
            {
                foreach (CamBam.CAM.MachineOp mop in prt.MachineOps)
                {


Also in the forum search box at the top, search for "snippets", that will bring up many code examples.
« Last Edit: March 25, 2020, 16:51:53 pm by EddyCurrent »
Made in England

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 5866
    • View Profile
    • Cambam French Doc
Re: Clearing Entities and MOPs in c#
« Reply #5 on: March 25, 2020, 18:08:26 pm »
Hello

Try

        CamBamUI.MainUI.ActiveView.CADFile.ActiveLayer.Entities.Clear();

        CamBamUI.MainUI.ActiveView.CADFile.Parts.Clear();

       CamBamUI.MainUI.ActiveView.DrawingTree.ReloadTree();

        CamBamUI.MainUI.ActiveView.RefreshView();

------------------------

It is working with a script without the ReloadTree() because CamBamConfig.Defaults.ReloadTreeAfterScript is set to true by default for scripts.

if you write the following VB script

' New CamBam VBScript

sub main
    
   CamBamConfig.Defaults.ReloadTreeAfterScript = false
    
    CamBamUI.MainUI.ActiveView.CADFile.ActiveLayer.Entities.Clear()

    CamBamUI.MainUI.ActiveView.CADFile.Parts.Clear()
 
end sub


you get the same behavior than with plugin, the tree is not updated.

++
David
« Last Edit: March 25, 2020, 18:34:28 pm by dh42 »

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 4248
    • View Profile
Re: Clearing Entities and MOPs in c#
« Reply #6 on: March 25, 2020, 19:56:20 pm »
I can't recall using, "CamBamUI.MainUI.ActiveView.DrawingTree.ReloadTree();" in any of my plugins but I see it's used in "Box Auto" plugin and others.

David, thanks, I was wondering why it worked with the script but not a plugin.

To make finding code easy, I have all the individual plugin source projects folders in one big folder, I then use this utility;
https://tools.stefankueng.com/grepWin.html
It looks in all the files for specified text e.g. "DrawingTree"
« Last Edit: March 25, 2020, 20:03:35 pm by EddyCurrent »
Made in England

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 5866
    • View Profile
    • Cambam French Doc
Re: Clearing Entities and MOPs in c#
« Reply #7 on: March 25, 2020, 20:22:51 pm »
Thanks Eddy, I didn't know this software ...

++
David

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 5866
    • View Profile
    • Cambam French Doc
Re: Clearing Entities and MOPs in c#
« Reply #8 on: March 25, 2020, 20:47:44 pm »
Quote
David, thanks, I was wondering why it worked with the script but not a plugin.

Useful to know for VB script users.

if you write this

sub main

  CamBamUI.MainUI.ActiveView.SelectAllVisibleGeometry()

end sub


all visible drawing objects will be selected in the drawing area, but they are not selected in the tree (because the tree is reloaded)

if you write this

sub main

 CamBamConfig.Defaults.ReloadTreeAfterScript = false

 CamBamUI.MainUI.ActiveView.SelectAllVisibleGeometry()

end sub


the drawing objects stay selected in the tree.

++
David