.NET

Reply
Contributor
16 Posts
0 Kudos
Registered: ‎05-19-2011
Post 1 of 8
Accepted Solution

Purgeall

469 Views, 7 Replies
02-25-2013 03:13 AM

Hello,

 

Needed help in the following solution.

 

doc.SendStringToExecute("_-Purge _A _* _N ", True, False, False)

 

Thanks

Francisco Silva
www.lojadosdesenhadores.blogspot.com

What help do you need?

 

Maybe simplest sample:

 

using System;
using System.Reflection;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
[assembly: CommandClass(typeof(Rivilis.Purge))]
namespace Rivilis
{
public class Purge
{
[CommandMethod("MyPurgeAll")]
static public void MyPurgeAll()
{
// For AutoCAD 2013+
object adoc = Application.DocumentManager.MdiActiveDocument.GetAcadDocument();
// For AutoCAD 2012-
// object adoc = Application.DocumentManager.MdiActiveDocument.AcadDocument;
if (adoc != null) {
adoc.GetType().InvokeMember("PurgeAll",BindingFlags.InvokeMethod, null, adoc, null);
}
}
}
}

 

This code have to work in both AutoCAD 2012 (and 2011, 2010...) and AutoCAD 2013:

using System;
using System.Reflection;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
[assembly: CommandClass(typeof(Rivilis.Purge))]
namespace Rivilis
{
public class Purge
{
[CommandMethod("MyPurgeAll")]
static public void MyPurgeAll()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
object adoc = null;
if (Application.Version.Major < 19) {
// For AutoCAD 2012- (i.e. 2012, 2011, 2010, ...)
adoc = doc.GetType().InvokeMember("AcadDocument",
BindingFlags.GetProperty, null, doc, null);
} else {
// For AutoCAD 2013+ (i.e. 2013, 2014 (???), ...)
Type ext = typeof(Autodesk.AutoCAD.Windows.Menu).Assembly
.GetType("Autodesk.AutoCAD.ApplicationServices.DocumentExtension", true); MethodInfo GetAcadDocumentMethod = ext.GetMethod("GetAcadDocument", BindingFlags.Public | BindingFlags.Static); adoc = GetAcadDocumentMethod.Invoke(doc, new object[1] { doc }); } if (adoc != null) { adoc.GetType().InvokeMember("PurgeAll", BindingFlags.InvokeMethod, null, adoc, null); } } } }

 

Moderator
1,465 Posts
277 Kudos
Registered: ‎04-09-2008
Post 2 of 8

Re: Purgeall

02-25-2013 06:44 AM in reply to: franciscocsilva

What help do you need?

 

Maybe simplest sample:

 

using System;
using System.Reflection;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

[assembly: CommandClass(typeof(Rivilis.Purge))]

namespace Rivilis
{
  public class Purge
  {
    [CommandMethod("MyPurgeAll")]
    static public void MyPurgeAll()
    {
      // For AutoCAD 2013+
      object adoc = Application.DocumentManager.MdiActiveDocument.GetAcadDocument();
      // For AutoCAD 2012-
      // object adoc = Application.DocumentManager.MdiActiveDocument.AcadDocument;
      if (adoc != null) {
        adoc.GetType().InvokeMember("PurgeAll",BindingFlags.InvokeMethod, null, adoc, null);
      }
    }
  }
}

 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Contributor
16 Posts
0 Kudos
Registered: ‎05-19-2011
Post 3 of 8

Re: Purgeall

02-25-2013 10:41 AM in reply to: Alexander.Rivilis

Thanks Alexander

 

i have this message:

 

Error 1 'GetAcadDocument' is not a member of 'Autodesk.AutoCAD.ApplicationServices.Document'. 

Francisco Silva
www.lojadosdesenhadores.blogspot.com
Valued Contributor
65 Posts
1 Kudo
Registered: ‎07-23-2008
Post 4 of 8

Re: Purgeall

02-25-2013 12:59 PM in reply to: franciscocsilva

Hi Alexander,

 

the code works ok, but I have though a question: I have a drawing that was last saved in paper space (paper space being active space).

 

When I open up the drawing and try to run the code (just as the first command in the drawing), it doesn't run. Although if I enter model space through viewport or switch back and forth between model space and paper space, the code will run as supposed to.

 

I don't know what is missing.

 

Thanks,

 

e.g.

Moderator
1,465 Posts
277 Kudos
Registered: ‎04-09-2008
Post 5 of 8

Re: Purgeall

02-25-2013 01:40 PM in reply to: franciscocsilva

franciscocsilva wrote:

Thanks Alexander

 

i have this message:

 

Error 1 'GetAcadDocument' is not a member of 'Autodesk.AutoCAD.ApplicationServices.Document'. 


This is because you did not read the comments in the program. Read them carefully.

 

 


e.g. wrote:
...When I open up the drawing and try to run the code (just as the first command in the drawing), it doesn't run. Although if I enter model space through viewport or switch back and forth between model space and paper space, the code will run as supposed to...

Sorry but I can not reproduce this behavior. 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Distinguished Contributor
119 Posts
1 Kudo
Registered: ‎03-05-2011
Post 6 of 8

Re: Purgeall

02-26-2013 02:58 AM in reply to: Alexander.Rivilis

I was just telling myself, as an uneducated programming hobbyiest writing addins for myself only, that modifying geometry in CAD so far has been fairly straight forward, but there was no way I could figure out how to "purge all".... VUALA! Magic appears at Autodesk :smileyhappy:

 

Thanks Alexander (or is it Alex?)

 

One more question... I couldn't help but notice the difference in the adoc dealio... everything I've written for myself has been for 2012... are you showing me that I'll have to update every flippin acdoc in my code when I update to 2013?  sad face lol

 

Thanks!

 

 

- Brian
"Very funny, Scotty. Now beam down my clothes."
Contributor
16 Posts
0 Kudos
Registered: ‎05-19-2011
Post 7 of 8

Re: Purgeall

02-26-2013 04:07 AM in reply to: franciscocsilva

Resolved
Thanks Alexander

Francisco Silva
www.lojadosdesenhadores.blogspot.com
Moderator
1,465 Posts
277 Kudos
Registered: ‎04-09-2008
Post 8 of 8

Re: Purgeall

02-26-2013 05:05 AM in reply to: area51visitor

This code have to work in both AutoCAD 2012 (and 2011, 2010...) and AutoCAD 2013:

using System;
using System.Reflection;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

[assembly: CommandClass(typeof(Rivilis.Purge))]

namespace Rivilis
{
  public class Purge
  {
    [CommandMethod("MyPurgeAll")]
    static public void MyPurgeAll()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      object adoc = null;
      if (Application.Version.Major < 19) { 
        // For AutoCAD 2012- (i.e. 2012, 2011, 2010, ...)
        adoc = doc.GetType().InvokeMember("AcadDocument", 
          BindingFlags.GetProperty, null, doc, null);
      } else { 
        // For AutoCAD 2013+ (i.e. 2013, 2014 (???), ...)
        Type ext = typeof(Autodesk.AutoCAD.Windows.Menu).Assembly
.GetType("Autodesk.AutoCAD.ApplicationServices.DocumentExtension", true); MethodInfo GetAcadDocumentMethod = ext.GetMethod("GetAcadDocument", BindingFlags.Public | BindingFlags.Static); adoc = GetAcadDocumentMethod.Invoke(doc, new object[1] { doc }); } if (adoc != null) { adoc.GetType().InvokeMember("PurgeAll", BindingFlags.InvokeMethod, null, adoc, null); } } } }

 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!