.NET

Reply
Contributor
franciscocsilva
Posts: 16
Registered: ‎05-19-2011
Message 1 of 8 (466 Views)
Accepted Solution

Purgeall

466 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
Alexander.Rivilis
Posts: 1,458
Registered: ‎04-09-2008
Message 2 of 8 (445 Views)

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
franciscocsilva
Posts: 16
Registered: ‎05-19-2011
Message 3 of 8 (418 Views)

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
e.g.
Posts: 65
Registered: ‎07-23-2008
Message 4 of 8 (403 Views)

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
Alexander.Rivilis
Posts: 1,458
Registered: ‎04-09-2008
Message 5 of 8 (401 Views)

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
area51visitor
Posts: 119
Registered: ‎03-05-2011
Message 6 of 8 (374 Views)

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
franciscocsilva
Posts: 16
Registered: ‎05-19-2011
Message 7 of 8 (363 Views)

Re: Purgeall

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

Resolved
Thanks Alexander

Francisco Silva
www.lojadosdesenhadores.blogspot.com
Moderator
Alexander.Rivilis
Posts: 1,458
Registered: ‎04-09-2008
Message 8 of 8 (357 Views)

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
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.