Author Topic: appContextOpenDocument  (Read 8288 times)

0 Members and 1 Guest are viewing this topic.

Glenn R

  • Guest
appContextOpenDocument
« on: September 06, 2006, 11:24:23 PM »
Can anybody see a .net wrapping of this or know a way to get at it from .net?

Cheers,
Glenn.

Draftek

  • Guest
Re: appContextOpenDocument
« Reply #1 on: September 07, 2006, 08:15:09 AM »
Okay, I'll bite.

What's the difference between that and this?

Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open()

Glenn R

  • Guest
Re: appContextOpenDocument
« Reply #2 on: September 07, 2006, 09:05:49 AM »
Synchronicity :D

Try using what you suggested and put a messagebox directly after the open and get it to put up the currently active document's name.

The point is, the line to open is executed and immediately follows on to your next statement WITHOUT waiting for the open to finish. You can imagine what this would do to a batch type program............

Cheers,
Glenn.

Draftek

  • Guest
Re: appContextOpenDocument
« Reply #3 on: September 07, 2006, 10:50:56 AM »
hmm. I see, that makes sense.

We have re-written our batch programs and I checked with my co-worker, who I had develop them and he had a similar problem with the plotting, and used Thread.Sleep(0) just beyond the static method that does the autocad work.

I checked the help and it doesn't seem like a viable solution but it seems to be working as we batch 10,000's of drawings at a time - xbinding, plotting, saving, etc.

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: appContextOpenDocument
« Reply #4 on: September 07, 2006, 05:53:02 PM »
Synchronicity :D
Code: [Select]
using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;

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

namespace Rivilis
{
  public class AppOpen
  {
    public enum OpenFlag
    {
      kDisallowReadOnly       = 0x01,
      kRequireReadOnly        = 0x02,
      kFileNameArgIsUnicode   = 0x04,
      kViewNameArgIsUnicode   = 0x08,
      kUseUIOnErrors          = 0x10
    };
    public enum ViewFlag
    {
      kUnknown = 0,             // not used
      kDefaultView = 1,         // open in last saved view
      kLayoutName = 2,          // specify layout by name
      kViewName = 3,            // specify view by name
      kLayoutHandle = 4,        // specify layout by acdb handle
      kViewHandle = 5,          // specify view by acdb handle
      kViewWorldCoords = 6
    };

    public struct DocOpenParams
    {
      public string filename;
      public string viewname;
      public byte   mnInitialViewType;
      public Int32  mnFlags;  
      public string mpwszPassword;
      public Int32  mpUnused;
      public Int32  mnUnused;
    };
   
    [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
       EntryPoint="?appContextOpenDocument@@YA?AW4ErrorStatus@Acad@@PBUDocOpenParams@AcApDocManager@@@Z")]
    private static extern Int32 appContextOpenDocument(ref DocOpenParams parm);

    [CommandMethod("FOPEN",CommandFlags.Session)]
    static public void Fopen()
    {
      Autodesk.AutoCAD.Windows.OpenFileDialog dlg = new Autodesk.AutoCAD.Windows.OpenFileDialog(
        "AutoCAD dwg-file:","","dwg", "Select dwg-file:",
        (Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags) 0);
      if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
        DocOpenParams parm = new DocOpenParams();
        parm.filename = dlg.Filename;
        parm.viewname = null;
        parm.mnInitialViewType = (byte) ViewFlag.kDefaultView;
        parm.mnFlags  = 0;
        parm.mpUnused = 0;
        parm.mpUnused = 0;
       
        Int32 err = appContextOpenDocument(ref parm);

        System.Windows.Forms.MessageBox.Show(
          dlg.Filename, "Opening",
          System.Windows.Forms.MessageBoxButtons.OK,
          System.Windows.Forms.MessageBoxIcon.Information);
      }
    }
  }
}
But I did not found any difference with Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open()
If I try execute appContextOpenDocument or Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open() in other Thread AutoCAD crashed. :(
« Last Edit: September 07, 2006, 07:04:41 PM by Alexander Rivilis »

Glenn R

  • Guest
Re: appContextOpenDocument
« Reply #5 on: September 07, 2006, 09:05:44 PM »
It apeears to work exactly as I expected it would Alexander - thanks very much.
I didn't realise you had to pass the DocOpenParms in - the docs don't mention it in the function call signature.

I'll do some more testing, but it's exactly what I wanted. I was using COM to open up a sequence of drawings and optionally run macros, scripts and plot them...I always wanted to reduce the amount of COM calls I needed to make and this will get rid of the COM Open calls.

Once again, thanks.

Cheers,
Glenn.

Glenn R

  • Guest
Re: appContextOpenDocument
« Reply #6 on: September 07, 2006, 09:50:55 PM »
Well I just modified my batching program to use this method of opening documents/drawings and it worked flawlessly - thanks again Alexander.
The only things that are still COM are the running of VBA macros and scripts...

Cheers,
Glenn.

TonyT

  • Guest
Re: appContextOpenDocument
« Reply #7 on: September 07, 2006, 10:03:14 PM »
But I did not found any difference with Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open()
If I try execute appContextOpenDocument or Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open() in other Thread AutoCAD crashed. :(

Hi Alex.  Very nice. 

What release of AutoCAD did you test this on?

« Last Edit: September 07, 2006, 10:14:52 PM by TonyT »

Glenn R

  • Guest
Re: appContextOpenDocument
« Reply #8 on: September 08, 2006, 12:32:28 AM »
I'm using '6 btw if you're suspecting maybe a unicode thing in '7 there Tony...?

TonyT

  • Guest
Re: appContextOpenDocument
« Reply #9 on: September 08, 2006, 01:39:11 AM »
I'm using '6 btw if you're suspecting maybe a unicode thing in '7 there Tony...?

Yes, I have a wrapper for the same API (except it only
works on '07), that's similar to Alex's except that it uses
unicode.

Alex's code should work on 07 if you make the following
changes:

1. On the DllImport() attribute, you to set the
    CharSet=CharSet.Unicode

2. On the struct, you need to add the following
    attribute:

Code: [Select]
[StructLayout( LayoutKind.Sequential, CharSet = CharSet.Unicode )]

3.  When you set the struct's fields, you need to
     set the 'flags' field to 0x4 and if you supply a
     string in the second field (view or layout name),
     you also need to include 0x8.


Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: appContextOpenDocument
« Reply #10 on: September 08, 2006, 03:36:31 AM »
Hi Alex.  Very nice. 
What release of AutoCAD did you test this on?
AutoCAD 2006.

Glenn R

  • Guest
Re: appContextOpenDocument
« Reply #11 on: September 08, 2006, 07:07:00 AM »
Hmmm...seeing as Alex has stated that he's on '06, as am I and it works fine for me, maybe it's a framework thing.
I know I have gotten different results (crashes) using 1.1 code on Autocad that has not has it's .config modified (ie always using the latest).............


Cheers,
Glenn.

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: appContextOpenDocument
« Reply #12 on: September 08, 2006, 08:19:36 AM »
Hmmm...seeing as Alex has stated that he's on '06, as am I and it works fine for me, maybe it's a framework thing.
I know I have gotten different results (crashes) using 1.1 code on Autocad that has not has it's .config modified (ie always using the latest).............
Cheers,
Glenn.
I have installed both AutoCAD 2006 and AutoCAD 2007. Config of AutoCAD 2006 is modified for using .NET 1.1
Crash was only if I start appContextOpenDocument() from other then main Thread.
« Last Edit: September 08, 2006, 10:33:21 AM by Alexander Rivilis »