Author Topic: Cannot set MdiActiveDocument  (Read 24778 times)

0 Members and 1 Guest are viewing this topic.

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Cannot set MdiActiveDocument
« Reply #15 on: October 10, 2013, 09:07:36 PM »
Thanks Will but I get an eNotapplicable at
if (docBlock.Editor.GetKeywords(pko).Status == PromptStatus.Cancel)

I'm using vanilla 2014 and get this using either
[CommandMethod("Library", CommandFlags.Session)]
or        [CommandMethod("Library")]

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Cannot set MdiActiveDocument
« Reply #16 on: October 10, 2013, 11:09:24 PM »
hmmm... it works fine for me on both 2013 and 2014 vanilla  :|
I've attached the .dll and project, does anybody else get the same error?

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Cannot set MdiActiveDocument
« Reply #17 on: October 11, 2013, 12:21:57 AM »
hmmm... it works fine for me on both 2013 and 2014 vanilla  :|
I've attached the .dll and project, does anybody else get the same error?
Worked for 2013 & 2014 for me

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Cannot set MdiActiveDocument
« Reply #18 on: October 11, 2013, 12:41:30 AM »
Jeff H, was your post a question or a statement?  I hesitate to assume your asking a question since its rare that I would know the answer and you wouldn't.  :)
More of a statement but, now that I think about it a little is everyone that is having a problem setting the active document include either opening or creating a new document? OPEN, NEW & TABLET are the AutoCAD commands that are Nonreentrant
Quote
Command, Nonreentrant  A command that cannot be executed in more than one document at a time. Nonreentrancy can be used for commands that should not be available for more than one document at a time, or when the demands of supporting multiple instantiation are too great to be worth the overhead.
Wonder if something under the hood blocks you from doing that?

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Cannot set MdiActiveDocument
« Reply #19 on: October 11, 2013, 12:40:40 PM »
I couldn't load you proj as you have a different version (I'm using 2010 express) but copied it into a virtually blank proj and still the same problem.
Then I tried using the references from 2014 arx and still no go.  I did notice you are not using  the  using acadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;  the core part and changing that to using acadApp = Autodesk.AutoCAD.ApplicationServices..Application; made no difference.
Thanks will but I am still lost

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Cannot set MdiActiveDocument
« Reply #20 on: October 11, 2013, 01:23:02 PM »
did the .dll have the same problem?

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Cannot set MdiActiveDocument
« Reply #21 on: October 11, 2013, 04:35:48 PM »
Yes  unfortunately.  My original code actually works fine but as I said cad crashes when you close the dwg (so not so fine)
Command: netload Cannot load assembly. Error details: System.IO.FileLoadException: Could not load file or assembly 'file:///C:\Users\bwalmsley\Documents\Visual Studio 2010\Projects\Library.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
File name: 'file:///C:\Users\bwalmsley\Documents\Visual Studio 2010\Projects\Library.dll' ---> System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at Autodesk.AutoCAD.Runtime.ExtensionLoader.Load(String fileName)
   at loadmgd()

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: Cannot set MdiActiveDocument
« Reply #22 on: October 11, 2013, 06:28:33 PM »
This is driving me crazy. I had it fixed and must have done something to cause it to stop working but can't for the life of me think of what.

I went back to basics and found that the problem still happens.
Code: [Select]
#region Namespaces
using System;
using System.Xml;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.ApplicationServices;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
#endregion

namespace AutoCADCodeSample
{
    public class Commands
    {
        [CommandMethod("Test",CommandFlags.Session)]
        public static void Test()
        {
            string fileName = string.Empty;
            System.Windows.Forms.OpenFileDialog ofd =
                new System.Windows.Forms.OpenFileDialog();
            ofd.Filter = "AutoCAD drawing files|*.dwg";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                fileName = ofd.FileName;
                MessageBox.Show("Before opening drawing active document is " +
                    acadApp.DocumentManager.MdiActiveDocument.Name);
                // Open a file
                Document doc = acadApp.DocumentManager.Open(fileName);
                MessageBox.Show("After opening " + fileName + " active document is " +
                    acadApp.DocumentManager.MdiActiveDocument.Name);
                // Set the opened document to be the active document
                acadApp.DocumentManager.MdiActiveDocument = doc;
                MessageBox.Show("After setting " + doc.Name +
                    " to be the active document the actual active document is " +
                    acadApp.DocumentManager.MdiActiveDocument.Name);
            }
        }
    }
}
VS 2012 - Acad 2013 both 32 bit
When I run this I get "Drawing1.dwg" all 3 times as the active document regardless of which file I open.

This is causing problems not only with active document but save and close functionality.
Any help would be appreciated.
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Cannot set MdiActiveDocument
« Reply #23 on: October 11, 2013, 07:27:46 PM »
I'm looking into the command sequence a little
AutoCAD menu utilities loaded.
Command:
Command: COMMANDLINE
Command: properties
Command: EXTERNALREFERENCES
Command:
Command:
Command:

this is what I see when I open vanilla 2014  w/ no code pre  loaded. (after the Customization file loaded successfully. Customization Group yadayda)

Regenerating model.
AutoCAD menu utilities loaded.
Command:
Command:
Command:
Command:

this is after opening a new dwg.     4 unexplained  command:

Is this typ?


Command: Library Regenerating model.
Regenerating model.
Regenerating model.
Regenerating model.
Command: *Cancel*
Command: *Cancel*
Command: *Cancel*
Command: *Cancel*

running MY CODE w/ 4 regens added (they show up in both dwgs  on the command line so each editor is working)
now the 4 empty commands have Cancel next to them


If I run my code then open a new dwg then swith back to the old dwg there is no crash
« Last Edit: October 11, 2013, 07:43:30 PM by Bryco »

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Cannot set MdiActiveDocument
« Reply #24 on: October 11, 2013, 08:15:45 PM »
from help
This function returns the document having current context. The associated function, mdiActiveDocument(), returns the MDI active document. curDocument() and mdiActiveDocument() can be different. You can call curDocument() to make a document "current" without actually activating it. After finish your AcDbDatabase operation under the temporary current document, call setCurDocument(acDocManager->mdiActiveDocument()) to reset the MDI active document as the current document.

setCurDocument may be the key, is it possible to get an entry point for this?

 

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Cannot set MdiActiveDocument
« Reply #25 on: October 11, 2013, 08:41:55 PM »
Yes  unfortunately.  My original code actually works fine but as I said cad crashes when you close the dwg (so not so fine)
Command: netload Cannot load assembly. Error details: System.IO.FileLoadException: Could not load file or assembly 'file:///C:\Users\bwalmsley\Documents\Visual Studio 2010\Projects\Library.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
File name: 'file:///C:\Users\bwalmsley\Documents\Visual Studio 2010\Projects\Library.dll' ---> System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at Autodesk.AutoCAD.Runtime.ExtensionLoader.Load(String fileName)
   at loadmgd()

You need to unblock the assembly before you can run it unless you change the autocad.exe.config file to allow it.  Download the zip, right click and go to properties, select 'Unblock' and pull the .dll out of there.

btw are you also running 32bit?

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Cannot set MdiActiveDocument
« Reply #26 on: October 12, 2013, 12:35:33 PM »
That worked but I still got an unhandled-eNotapplicable
I'm running windows 7 Pro

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: Cannot set MdiActiveDocument
« Reply #27 on: October 14, 2013, 05:03:28 PM »

Code: [Select]
#region Namespaces
using System;
using System.Xml;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.ApplicationServices;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
#endregion

namespace AutoCADCodeSample
{
    public class Commands
    {
        [CommandMethod("Test",CommandFlags.Session)]
        public static void Test()
        {
            string fileName = string.Empty;
            System.Windows.Forms.OpenFileDialog ofd =
                new System.Windows.Forms.OpenFileDialog();
            ofd.Filter = "AutoCAD drawing files|*.dwg";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                fileName = ofd.FileName;
                MessageBox.Show("Before opening drawing active document is " +
                    acadApp.DocumentManager.MdiActiveDocument.Name);
                // Open a file
                Document doc = acadApp.DocumentManager.Open(fileName);
                MessageBox.Show("After opening " + fileName + " active document is " +
                    acadApp.DocumentManager.MdiActiveDocument.Name);
                // Set the opened document to be the active document
                acadApp.DocumentManager.MdiActiveDocument = doc;
                MessageBox.Show("After setting " + doc.Name +
                    " to be the active document the actual active document is " +
                    acadApp.DocumentManager.MdiActiveDocument.Name);
            }
        }
    }
}
When I run this I get "Drawing1.dwg" all 3 times as the active document regardless of which file I open.

Can anyone confirm this behavior for me?
Just try the simple code posted if you will.
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Cannot set MdiActiveDocument
« Reply #28 on: October 14, 2013, 05:52:51 PM »
Trying that code snippet with 2013 and 64bit I get the new drawing name for last 2 message boxes

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Cannot set MdiActiveDocument
« Reply #29 on: October 14, 2013, 07:06:30 PM »
Do you have dual processors?   I have a quad core (NVidia quadro 40000 but not dual processors)

acad 2014 64 bit  vs2010 express   same result as mohnston